Guest User

gles2 init code for fltk2 (win32)

a guest
Dec 8th, 2012
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.06 KB | None | 0 0
  1. //////////////////////////////////// .cpp
  2.  
  3. #ifdef GLES_2
  4.  
  5.  
  6. #include <EGL/egl.h>
  7. #include <GLES2/gl2.h>
  8. #include <GLES2/gl2ext.h>
  9.  
  10. # define GLContext EGLContext
  11.  
  12. bool TestEGLError(HWND hWnd, char* pszLocation)
  13. {
  14.  
  15.     EGLint iErr = eglGetError();
  16.     if (iErr != EGL_SUCCESS)
  17.     {
  18. #ifndef NO_GDI
  19.         TCHAR pszStr[256];
  20.         _stprintf(pszStr, _T("%s failed (%d).\n"), pszLocation, iErr);
  21.         MessageBox(hWnd, pszStr, _T("Error"), MB_OK|MB_ICONEXCLAMATION);
  22. #endif
  23.         return false;
  24.     }
  25.  
  26.     return true;
  27. }
  28.  
  29.  
  30. GLContext fltk::create_gl_context(const Window* window, const GlChoice* g, int layer)
  31. {
  32.     EGLDisplay eglDisplay       = 0;
  33.     EGLConfig eglConfig     = 0;
  34.     EGLSurface eglSurface       = 0;
  35.     EGLContext eglContext       = 0;
  36.     EGLNativeWindowType eglWindow   = 0;
  37.  
  38.     CreatedWindow* i = CreatedWindow::find(window);
  39.  
  40.     eglWindow = i->xid;
  41.  
  42.     eglDisplay = eglGetDisplay(i->dc);
  43.  
  44.     if(eglDisplay == EGL_NO_DISPLAY)
  45.         eglDisplay = eglGetDisplay((EGLNativeDisplayType) EGL_DEFAULT_DISPLAY);
  46.  
  47.     EGLint iMajorVersion, iMinorVersion;
  48.     if (!eglInitialize(eglDisplay, &iMajorVersion, &iMinorVersion))
  49.     {
  50. #ifndef NO_GDI
  51.         MessageBox(0, _T("eglInitialize() failed."), _T("Error"), MB_OK|MB_ICONEXCLAMATION);
  52. #endif
  53.         return NULL;
  54.     }
  55.  
  56.     /*
  57.         Make OpenGL ES current.
  58.         EGL provides ways to set up OpenGL ES and OpenVG contexts
  59.         (and possibly other graphics APIs in the future), so we need
  60.         to specify the "current API".
  61.     */
  62.     eglBindAPI(EGL_OPENGL_ES_API);
  63.     if (!TestEGLError(i->xid, "eglBindAPI"))
  64.     {
  65.         return NULL;
  66.     }
  67.  
  68.     const EGLint pi32ConfigAttribs[] =
  69.     {
  70.         EGL_LEVEL,              0,
  71.         EGL_SURFACE_TYPE,       EGL_WINDOW_BIT,
  72.         EGL_RENDERABLE_TYPE,    EGL_OPENGL_ES2_BIT,
  73.         EGL_NATIVE_RENDERABLE,  EGL_FALSE,
  74.         EGL_DEPTH_SIZE,         EGL_DONT_CARE,
  75.         EGL_NONE
  76.     };
  77.  
  78.     int iConfigs;
  79.     if (!eglChooseConfig(eglDisplay, pi32ConfigAttribs, &eglConfig, 1, &iConfigs) || (iConfigs != 1))
  80.     {
  81. #ifndef NO_GDI
  82.         MessageBox(0, _T("eglChooseConfig() failed."), _T("Error"), MB_OK|MB_ICONEXCLAMATION);
  83. #endif
  84.         return NULL;
  85.     }
  86.  
  87.     eglSurface = eglCreateWindowSurface(eglDisplay, eglConfig, eglWindow, NULL);
  88.     if (!TestEGLError(i->xid, "eglCreateWindowSurface"))
  89.     {
  90.         return NULL;
  91.     }
  92.  
  93.     if(eglSurface == EGL_NO_SURFACE)
  94.     {
  95.         eglGetError(); // Clear error
  96.         eglSurface = eglCreateWindowSurface(eglDisplay, eglConfig, NULL, NULL);
  97.     }
  98.  
  99.     if (!TestEGLError(i->xid, "eglCreateWindowSurface"))
  100.     {
  101.         return NULL;
  102.     }
  103.  
  104.     EGLint ai32ContextAttribs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };
  105.     eglContext = eglCreateContext(eglDisplay, eglConfig, NULL, ai32ContextAttribs);
  106.     if (!TestEGLError(i->xid, "eglCreateContext"))
  107.     {
  108.         return NULL;
  109.     }
  110.  
  111.     const_cast<EGLDisplay>(g->eglDisplay)           = eglDisplay;
  112.     const_cast<EGLConfig>(g->eglConfig)         = eglConfig;
  113.     const_cast<EGLSurface>(g->eglSurface)           = eglSurface;
  114.     const_cast<EGLContext>(g->eglContext)           = eglContext;
  115.     const_cast<EGLNativeWindowType>(g->eglWindow)       = eglWindow;
  116.  
  117.  
  118.     return eglContext;
  119. }
  120.  
  121. #endif
  122.  
  123. GLContext fl_current_glcontext;
  124. static const Window* cached_window;
  125.  
  126. void fltk::set_gl_context(const Window* window, GLContext context)
  127. {
  128.   if (context != fl_current_glcontext || window != cached_window)
  129.   {
  130.     fl_current_glcontext = context;
  131.     cached_window = window;
  132.  
  133. #ifdef GLES_2
  134.     assert(first);
  135.     if(first)
  136.     {
  137.        eglMakeCurrent(first->eglDisplay, first->eglSurface, first->eglSurface, first->eglContext);
  138.     }
  139. #else
  140.     wglMakeCurrent(CreatedWindow::find(window)->dc, context);
  141. #endif
  142.  
  143.   }
  144. }
  145.  
  146. void fltk::no_gl_context()
  147. {
  148. #ifdef GLES_2
  149.     eglMakeCurrent(first->eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
  150. #endif
  151.  
  152.   fl_current_glcontext = 0;
  153.   cached_window = 0;
  154. }
  155.  
  156. void fltk::delete_gl_context(GLContext context)
  157. {
  158. #ifdef GLES_2
  159.       eglDestroyContext(first->eglDisplay, first->eglContext);
  160.       eglDestroySurface(first->eglDisplay, first->eglSurface);
  161.       eglTerminate(first->eglDisplay);
  162. #endif // GLES_2  
  163. }
  164.  
  165. #endif
  166.  
  167.  
  168. ////////////////////////////////////////////// misc stuff
  169.  
  170. #ifdef GLES_2
  171.  
  172.  
  173. // egl swap function:
  174.  
  175. eglSwapBuffers(gl_choice->eglDisplay, gl_choice->eglSurface);
  176.  
  177. #endif
Advertisement
Add Comment
Please, Sign In to add comment