Advertisement
Grant12311

Untitled

Apr 14th, 2020
467
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.73 KB | None | 0 0
  1. #include "engine.h"
  2.  
  3. #include <iostream>
  4.  
  5. #include <X11/Xlib.h>
  6. #include <X11/Xutil.h>
  7.  
  8. #include <EGL/egl.h>
  9.  
  10. Atom wmDelete;
  11.  
  12. EngineWindow engineCreateWindow(const char* title, unsigned int height, unsigned int width)
  13. {
  14.     EngineWindow toReturn;
  15.     toReturn.title = title;
  16.     toReturn.height = height;
  17.     toReturn.width = width;
  18.  
  19.     toReturn.xDisplay = XOpenDisplay(NULL);
  20.  
  21.     if (toReturn.xDisplay == NULL)
  22.     {
  23.         std::cerr << "Cannot open display" << std::endl;
  24.         return toReturn;
  25.     }
  26.  
  27.     toReturn.xScreen = XDefaultScreen(toReturn.xDisplay);
  28.     unsigned long black = BlackPixel(toReturn.xDisplay, toReturn.xScreen);
  29.     unsigned long white = WhitePixel(toReturn.xDisplay, toReturn.xScreen);
  30.     toReturn.xWindow = XCreateSimpleWindow(toReturn.xDisplay, XRootWindow(toReturn.xDisplay, toReturn.xScreen), 10, 10, toReturn.width, toReturn.height, 1, black, white);
  31.     XSetStandardProperties(toReturn.xDisplay, toReturn.xWindow, toReturn.title, toReturn.title, None, NULL, 0, NULL);
  32.     XSelectInput(toReturn.xDisplay, toReturn.xWindow, StructureNotifyMask | KeyPressMask);
  33.  
  34.     wmDelete = XInternAtom(toReturn.xDisplay, "WM_DELETE_WINDOW", True);
  35.     XSetWMProtocols(toReturn.xDisplay, toReturn.xWindow, &wmDelete, 1);
  36.  
  37.     XMapWindow(toReturn.xDisplay, toReturn.xWindow);
  38.  
  39.     XGetGeometry(toReturn.xDisplay, toReturn.xWindow, &toReturn.xRootWindow, &toReturn.posX, &toReturn.posY, &toReturn.width, &toReturn.height, &toReturn.borderWidth, &toReturn.depth);
  40.  
  41.     toReturn.creationStatus = true;
  42.     return toReturn;
  43. }
  44.  
  45. bool engineCreateContext(EngineWindow window)
  46. {
  47.     EGLint attr[] = { // some attributes to set up our egl-interface
  48.     EGL_BUFFER_SIZE, 16,
  49.     EGL_RENDERABLE_TYPE,
  50.     EGL_OPENGL_ES2_BIT,
  51.     EGL_NONE
  52.     };
  53.     EGLint ctxattr[] = {
  54.         EGL_CONTEXT_CLIENT_VERSION,
  55.         2,
  56.         EGL_NONE
  57.     };
  58.  
  59.     window.eglDisplay = eglGetDisplay((EGLNativeDisplayType) window.xDisplay);
  60.     if (window.eglDisplay == EGL_NO_DISPLAY)
  61.     {
  62.         std::cerr << "Got no EGL display." << std::endl;
  63.         return false;
  64.     }
  65.     if (!eglInitialize(window.eglDisplay, NULL, NULL))
  66.     {
  67.         std::cerr << "Unable to initialize EGL" << std::endl;
  68.         return false;
  69.     }
  70.  
  71.     EGLConfig ecfg;
  72.     EGLint num_config;
  73.     if (!eglChooseConfig(window.eglDisplay, attr, &ecfg, 1, &num_config))
  74.     {
  75.         std::cerr << "Failed to choose config (eglError: " << eglGetError() << ")" << std::endl;
  76.         return false;
  77.     }
  78.     if (num_config != 1)
  79.     {
  80.         std::cerr << "Didn't get exactly one config, but " << num_config << std::endl;
  81.         return false;
  82.     }
  83.  
  84.     window.eglSurface = eglCreateWindowSurface(window.eglDisplay, ecfg, window.xWindow, NULL);
  85.     if (window.eglSurface == EGL_NO_SURFACE)
  86.     {
  87.         std::cerr << "Unable to create EGL surface (eglError: " << eglGetError() << ")" << std::endl;
  88.         return false;
  89.     }
  90.  
  91.     window.eglContext = eglCreateContext(window.eglDisplay, ecfg, EGL_NO_CONTEXT, ctxattr);
  92.     if (window.eglContext == EGL_NO_CONTEXT)
  93.     {
  94.         std::cerr << "Unable to create EGL context (eglError: " << eglGetError() << ")" << std::endl;
  95.         return false;
  96.     }
  97.  
  98.     eglMakeCurrent(window.eglDisplay, window.eglSurface, window.eglSurface, window.eglContext);
  99.     eglSwapInterval(window.eglDisplay, 1);
  100.  
  101.     return true;
  102. }
  103.  
  104. void engineCloseWindow(EngineWindow window)
  105. {
  106.     eglMakeCurrent(window.eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
  107.     eglDestroySurface(window.eglDisplay, window.eglSurface);
  108.     eglDestroyContext(window.eglDisplay, window.eglContext);
  109.     eglTerminate(window.eglDisplay);
  110.  
  111.     XDestroyWindow(window.xDisplay, window.xWindow);
  112.     XCloseDisplay(window.xDisplay);
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement