From 9614b9b22f458ab4449b3e9584cf8c73bab2884e Mon Sep 17 00:00:00 2001 From: Camilla Berglund Date: Mon, 23 Apr 2012 13:00:49 +0200 Subject: [PATCH] Moved OpenGL init and terminate to opengl module. --- src/x11_init.c | 58 +++------------------------------------ src/x11_opengl.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++ src/x11_platform.h | 2 ++ 3 files changed, 74 insertions(+), 54 deletions(-) diff --git a/src/x11_init.c b/src/x11_init.c index d59fbcc6..6b2f473e 100644 --- a/src/x11_init.c +++ b/src/x11_init.c @@ -36,33 +36,6 @@ #include -//======================================================================== -// Dynamically load libraries -//======================================================================== - -static void initLibraries(void) -{ -#ifdef _GLFW_DLOPEN_LIBGL - int i; - char* libGL_names[ ] = - { - "libGL.so", - "libGL.so.1", - "/usr/lib/libGL.so", - "/usr/lib/libGL.so.1", - NULL - }; - - for (i = 0; libGL_names[i] != NULL; i++) - { - _glfwLibrary.GLX.libGL = dlopen(libGL_names[i], RTLD_LAZY | RTLD_GLOBAL); - if (_glfwLibrary.GLX.libGL) - break; - } -#endif -} - - //======================================================================== // Translate an X11 key code to a GLFW key code. //======================================================================== @@ -561,22 +534,6 @@ static GLboolean initDisplay(void) _glfwLibrary.X11.RandR.available = GL_FALSE; #endif /*_GLFW_HAS_XRANDR*/ - // Check if GLX is supported on this display - if (!glXQueryExtension(_glfwLibrary.X11.display, NULL, NULL)) - { - _glfwSetError(GLFW_OPENGL_UNAVAILABLE, "X11/GLX: GLX supported not found"); - return GL_FALSE; - } - - if (!glXQueryVersion(_glfwLibrary.X11.display, - &_glfwLibrary.GLX.majorVersion, - &_glfwLibrary.GLX.minorVersion)) - { - _glfwSetError(GLFW_OPENGL_UNAVAILABLE, - "X11/GLX: Failed to query GLX version"); - return GL_FALSE; - } - // Check if Xkb is supported on this display #if defined(_GLFW_HAS_XKB) _glfwLibrary.X11.Xkb.majorVersion = 1; @@ -739,15 +696,15 @@ int _glfwPlatformInit(void) if (!initDisplay()) return GL_FALSE; + if (!_glfwInitOpenGL()) + return GL_FALSE; + initGammaRamp(); initEWMH(); _glfwLibrary.X11.cursor = createNULLCursor(); - // Try to load libGL.so if necessary - initLibraries(); - _glfwInitJoysticks(); // Start the timer @@ -773,14 +730,7 @@ int _glfwPlatformTerminate(void) _glfwTerminateJoysticks(); - // Unload libGL.so if necessary -#ifdef _GLFW_DLOPEN_LIBGL - if (_glfwLibrary.GLX.libGL != NULL) - { - dlclose(_glfwLibrary.GLX.libGL); - _glfwLibrary.GLX.libGL = NULL; - } -#endif + _glfwTerminateOpenGL(); // Free clipboard memory if (_glfwLibrary.X11.selection.string) diff --git a/src/x11_opengl.c b/src/x11_opengl.c index 825cacc2..a09f00fd 100644 --- a/src/x11_opengl.c +++ b/src/x11_opengl.c @@ -529,6 +529,74 @@ static int createContext(_GLFWwindow* window, ////// GLFW internal API ////// ////////////////////////////////////////////////////////////////////////// +//======================================================================== +// Initialize GLX +//======================================================================== + +int _glfwInitOpenGL(void) +{ +#ifdef _GLFW_DLOPEN_LIBGL + int i; + char* libGL_names[ ] = + { + "libGL.so", + "libGL.so.1", + "/usr/lib/libGL.so", + "/usr/lib/libGL.so.1", + NULL + }; + + for (i = 0; libGL_names[i] != NULL; i++) + { + _glfwLibrary.GLX.libGL = dlopen(libGL_names[i], RTLD_LAZY | RTLD_GLOBAL); + if (_glfwLibrary.GLX.libGL) + break; + } + + if (!_glfwLibrary.GLX.libGL) + { + _glfwSetError(GLFW_PLATFORM_ERROR, "X11/GLX: Failed to find libGL"); + return GL_FALSE; + } +#endif + + // Check if GLX is supported on this display + if (!glXQueryExtension(_glfwLibrary.X11.display, NULL, NULL)) + { + _glfwSetError(GLFW_OPENGL_UNAVAILABLE, "X11/GLX: GLX supported not found"); + return GL_FALSE; + } + + if (!glXQueryVersion(_glfwLibrary.X11.display, + &_glfwLibrary.GLX.majorVersion, + &_glfwLibrary.GLX.minorVersion)) + { + _glfwSetError(GLFW_OPENGL_UNAVAILABLE, + "X11/GLX: Failed to query GLX version"); + return GL_FALSE; + } + + return GL_TRUE; +} + + +//======================================================================== +// Terminate GLX +//======================================================================== + +void _glfwTerminateOpenGL(void) +{ + // Unload libGL.so if necessary +#ifdef _GLFW_DLOPEN_LIBGL + if (_glfwLibrary.GLX.libGL != NULL) + { + dlclose(_glfwLibrary.GLX.libGL); + _glfwLibrary.GLX.libGL = NULL; + } +#endif +} + + //======================================================================== // Prepare for creation of the OpenGL context //======================================================================== diff --git a/src/x11_platform.h b/src/x11_platform.h index 96fb64fa..bf959f85 100644 --- a/src/x11_platform.h +++ b/src/x11_platform.h @@ -283,6 +283,8 @@ GLFWGLOBAL struct { void _glfwInitTimer(void); // OpenGL support +int _glfwInitOpenGL(void); +void _glfwTerminateOpenGL(void); int _glfwCreateContext(_GLFWwindow* window, const _GLFWwndconfig* wndconfig, const _GLFWfbconfig* fbconfig);