Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "engine.h"
- #include <iostream>
- #include <X11/Xlib.h>
- #include <X11/Xutil.h>
- #include <EGL/egl.h>
- Atom wmDelete;
- EngineWindow engineCreateWindow(const char* title, unsigned int height, unsigned int width)
- {
- EngineWindow toReturn;
- toReturn.title = title;
- toReturn.height = height;
- toReturn.width = width;
- toReturn.xDisplay = XOpenDisplay(NULL);
- if (toReturn.xDisplay == NULL)
- {
- std::cerr << "Cannot open display" << std::endl;
- return toReturn;
- }
- toReturn.xScreen = XDefaultScreen(toReturn.xDisplay);
- unsigned long black = BlackPixel(toReturn.xDisplay, toReturn.xScreen);
- unsigned long white = WhitePixel(toReturn.xDisplay, toReturn.xScreen);
- toReturn.xWindow = XCreateSimpleWindow(toReturn.xDisplay, XRootWindow(toReturn.xDisplay, toReturn.xScreen), 10, 10, toReturn.width, toReturn.height, 1, black, white);
- XSetStandardProperties(toReturn.xDisplay, toReturn.xWindow, toReturn.title, toReturn.title, None, NULL, 0, NULL);
- XSelectInput(toReturn.xDisplay, toReturn.xWindow, StructureNotifyMask | KeyPressMask);
- wmDelete = XInternAtom(toReturn.xDisplay, "WM_DELETE_WINDOW", True);
- XSetWMProtocols(toReturn.xDisplay, toReturn.xWindow, &wmDelete, 1);
- XMapWindow(toReturn.xDisplay, toReturn.xWindow);
- XGetGeometry(toReturn.xDisplay, toReturn.xWindow, &toReturn.xRootWindow, &toReturn.posX, &toReturn.posY, &toReturn.width, &toReturn.height, &toReturn.borderWidth, &toReturn.depth);
- toReturn.creationStatus = true;
- return toReturn;
- }
- bool engineCreateContext(EngineWindow window)
- {
- EGLint attr[] = { // some attributes to set up our egl-interface
- EGL_BUFFER_SIZE, 16,
- EGL_RENDERABLE_TYPE,
- EGL_OPENGL_ES2_BIT,
- EGL_NONE
- };
- EGLint ctxattr[] = {
- EGL_CONTEXT_CLIENT_VERSION,
- 2,
- EGL_NONE
- };
- window.eglDisplay = eglGetDisplay((EGLNativeDisplayType) window.xDisplay);
- if (window.eglDisplay == EGL_NO_DISPLAY)
- {
- std::cerr << "Got no EGL display." << std::endl;
- return false;
- }
- if (!eglInitialize(window.eglDisplay, NULL, NULL))
- {
- std::cerr << "Unable to initialize EGL" << std::endl;
- return false;
- }
- EGLConfig ecfg;
- EGLint num_config;
- if (!eglChooseConfig(window.eglDisplay, attr, &ecfg, 1, &num_config))
- {
- std::cerr << "Failed to choose config (eglError: " << eglGetError() << ")" << std::endl;
- return false;
- }
- if (num_config != 1)
- {
- std::cerr << "Didn't get exactly one config, but " << num_config << std::endl;
- return false;
- }
- window.eglSurface = eglCreateWindowSurface(window.eglDisplay, ecfg, window.xWindow, NULL);
- if (window.eglSurface == EGL_NO_SURFACE)
- {
- std::cerr << "Unable to create EGL surface (eglError: " << eglGetError() << ")" << std::endl;
- return false;
- }
- window.eglContext = eglCreateContext(window.eglDisplay, ecfg, EGL_NO_CONTEXT, ctxattr);
- if (window.eglContext == EGL_NO_CONTEXT)
- {
- std::cerr << "Unable to create EGL context (eglError: " << eglGetError() << ")" << std::endl;
- return false;
- }
- eglMakeCurrent(window.eglDisplay, window.eglSurface, window.eglSurface, window.eglContext);
- eglSwapInterval(window.eglDisplay, 1);
- return true;
- }
- void engineCloseWindow(EngineWindow window)
- {
- eglMakeCurrent(window.eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
- eglDestroySurface(window.eglDisplay, window.eglSurface);
- eglDestroyContext(window.eglDisplay, window.eglContext);
- eglTerminate(window.eglDisplay);
- XDestroyWindow(window.xDisplay, window.xWindow);
- XCloseDisplay(window.xDisplay);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement