Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //////////////////////////////////// .cpp
- #ifdef GLES_2
- #include <EGL/egl.h>
- #include <GLES2/gl2.h>
- #include <GLES2/gl2ext.h>
- # define GLContext EGLContext
- bool TestEGLError(HWND hWnd, char* pszLocation)
- {
- EGLint iErr = eglGetError();
- if (iErr != EGL_SUCCESS)
- {
- #ifndef NO_GDI
- TCHAR pszStr[256];
- _stprintf(pszStr, _T("%s failed (%d).\n"), pszLocation, iErr);
- MessageBox(hWnd, pszStr, _T("Error"), MB_OK|MB_ICONEXCLAMATION);
- #endif
- return false;
- }
- return true;
- }
- GLContext fltk::create_gl_context(const Window* window, const GlChoice* g, int layer)
- {
- EGLDisplay eglDisplay = 0;
- EGLConfig eglConfig = 0;
- EGLSurface eglSurface = 0;
- EGLContext eglContext = 0;
- EGLNativeWindowType eglWindow = 0;
- CreatedWindow* i = CreatedWindow::find(window);
- eglWindow = i->xid;
- eglDisplay = eglGetDisplay(i->dc);
- if(eglDisplay == EGL_NO_DISPLAY)
- eglDisplay = eglGetDisplay((EGLNativeDisplayType) EGL_DEFAULT_DISPLAY);
- EGLint iMajorVersion, iMinorVersion;
- if (!eglInitialize(eglDisplay, &iMajorVersion, &iMinorVersion))
- {
- #ifndef NO_GDI
- MessageBox(0, _T("eglInitialize() failed."), _T("Error"), MB_OK|MB_ICONEXCLAMATION);
- #endif
- return NULL;
- }
- /*
- Make OpenGL ES current.
- EGL provides ways to set up OpenGL ES and OpenVG contexts
- (and possibly other graphics APIs in the future), so we need
- to specify the "current API".
- */
- eglBindAPI(EGL_OPENGL_ES_API);
- if (!TestEGLError(i->xid, "eglBindAPI"))
- {
- return NULL;
- }
- const EGLint pi32ConfigAttribs[] =
- {
- EGL_LEVEL, 0,
- EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
- EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
- EGL_NATIVE_RENDERABLE, EGL_FALSE,
- EGL_DEPTH_SIZE, EGL_DONT_CARE,
- EGL_NONE
- };
- int iConfigs;
- if (!eglChooseConfig(eglDisplay, pi32ConfigAttribs, &eglConfig, 1, &iConfigs) || (iConfigs != 1))
- {
- #ifndef NO_GDI
- MessageBox(0, _T("eglChooseConfig() failed."), _T("Error"), MB_OK|MB_ICONEXCLAMATION);
- #endif
- return NULL;
- }
- eglSurface = eglCreateWindowSurface(eglDisplay, eglConfig, eglWindow, NULL);
- if (!TestEGLError(i->xid, "eglCreateWindowSurface"))
- {
- return NULL;
- }
- if(eglSurface == EGL_NO_SURFACE)
- {
- eglGetError(); // Clear error
- eglSurface = eglCreateWindowSurface(eglDisplay, eglConfig, NULL, NULL);
- }
- if (!TestEGLError(i->xid, "eglCreateWindowSurface"))
- {
- return NULL;
- }
- EGLint ai32ContextAttribs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };
- eglContext = eglCreateContext(eglDisplay, eglConfig, NULL, ai32ContextAttribs);
- if (!TestEGLError(i->xid, "eglCreateContext"))
- {
- return NULL;
- }
- const_cast<EGLDisplay>(g->eglDisplay) = eglDisplay;
- const_cast<EGLConfig>(g->eglConfig) = eglConfig;
- const_cast<EGLSurface>(g->eglSurface) = eglSurface;
- const_cast<EGLContext>(g->eglContext) = eglContext;
- const_cast<EGLNativeWindowType>(g->eglWindow) = eglWindow;
- return eglContext;
- }
- #endif
- GLContext fl_current_glcontext;
- static const Window* cached_window;
- void fltk::set_gl_context(const Window* window, GLContext context)
- {
- if (context != fl_current_glcontext || window != cached_window)
- {
- fl_current_glcontext = context;
- cached_window = window;
- #ifdef GLES_2
- assert(first);
- if(first)
- {
- eglMakeCurrent(first->eglDisplay, first->eglSurface, first->eglSurface, first->eglContext);
- }
- #else
- wglMakeCurrent(CreatedWindow::find(window)->dc, context);
- #endif
- }
- }
- void fltk::no_gl_context()
- {
- #ifdef GLES_2
- eglMakeCurrent(first->eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
- #endif
- fl_current_glcontext = 0;
- cached_window = 0;
- }
- void fltk::delete_gl_context(GLContext context)
- {
- #ifdef GLES_2
- eglDestroyContext(first->eglDisplay, first->eglContext);
- eglDestroySurface(first->eglDisplay, first->eglSurface);
- eglTerminate(first->eglDisplay);
- #endif // GLES_2
- }
- #endif
- ////////////////////////////////////////////// misc stuff
- #ifdef GLES_2
- // egl swap function:
- eglSwapBuffers(gl_choice->eglDisplay, gl_choice->eglSurface);
- #endif
Advertisement
Add Comment
Please, Sign In to add comment