| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | #include <dlfcn.h> |
|---|
| 6 | #include <stdio.h> |
|---|
| 7 | #include "opengl.h" |
|---|
| 8 | |
|---|
| 9 | static void* gl_module; |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | PFNGLXCHOOSEVISUAL pfnglXChooseVisual; |
|---|
| 15 | PFNGLXCREATECONTEXT pfnglXCreateContext; |
|---|
| 16 | PFNGLXDESTROYCONTEXT pfnglXDestroyContext; |
|---|
| 17 | PFNGLXMAKECURRENT pfnglXMakeCurrent; |
|---|
| 18 | PFNGLXCOPYCONTEXT pfnglXCopyContext; |
|---|
| 19 | PFNGLXSWAPBUFFERS pfnglXSwapBuffers; |
|---|
| 20 | PFNGLXCREATEGLXPIXMAP pfnglXCreateGLXPixmap; |
|---|
| 21 | PFNGLXDESTROYGLXPIXMAP pfnglXDestroyGLXPixmap; |
|---|
| 22 | PFNGLXQUERYEXTENSION pfnglXQueryExtension; |
|---|
| 23 | PFNGLXQUERYVERSION pfnglXQueryVersion; |
|---|
| 24 | PFNGLXISDIRECT pfnglXIsDirect; |
|---|
| 25 | PFNGLXGETCONFIG pfnglXGetConfig; |
|---|
| 26 | PFNGLXGETCURRENTCONTEXT pfnglXGetCurrentContext; |
|---|
| 27 | PFNGLXGETCURRENTDRAWABLE pfnglXGetCurrentDrawable; |
|---|
| 28 | PFNGLXWAITGL pfnglXWaitGL; |
|---|
| 29 | PFNGLXWAITX pfnglXWaitX; |
|---|
| 30 | PFNGLXUSEXFONT pfnglXUseXFont; |
|---|
| 31 | PFNGLXQUERYEXTENSIONSSTRING pfnglXQueryExtensionsString; |
|---|
| 32 | PFNGLXQUERYSERVERSTRING pfnglXQueryServerString; |
|---|
| 33 | PFNGLXGETCLIENTSTRING pfnglXGetClientString; |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | PFNGLXGETPROCADDRESSARB pfnglXGetProcAddressARB; |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | void* Sys_GLGetProc (const char *symbol) |
|---|
| 46 | { |
|---|
| 47 | void* func = dlsym (gl_module, symbol); |
|---|
| 48 | const char* error = dlerror (); |
|---|
| 49 | if (error) |
|---|
| 50 | printf ("Error loading OpenGL library.\n%s\n", error); |
|---|
| 51 | return func; |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | void* Sys_GLGetExtension (const char *symbol) |
|---|
| 55 | { |
|---|
| 56 | if (pfnglXGetProcAddressARB == NULL) |
|---|
| 57 | return NULL; |
|---|
| 58 | else |
|---|
| 59 | return pfnglXGetProcAddressARB ((GLubyte*)symbol); |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | bool Sys_GLOpenLibrary (const char* libname) |
|---|
| 63 | { |
|---|
| 64 | const char *error; |
|---|
| 65 | |
|---|
| 66 | if (libname) |
|---|
| 67 | { |
|---|
| 68 | gl_module = dlopen (libname, RTLD_LAZY|RTLD_GLOBAL); |
|---|
| 69 | error = dlerror (); |
|---|
| 70 | if (error) |
|---|
| 71 | printf ("Error loading OpenGL library.\n%s\n", error); |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | if (gl_module == NULL) |
|---|
| 75 | { |
|---|
| 76 | gl_module = dlopen ("libGL.so.1", RTLD_LAZY|RTLD_GLOBAL); |
|---|
| 77 | error = dlerror (); |
|---|
| 78 | if (error) |
|---|
| 79 | printf ("Error loading OpenGL library.\n%s\n", error); |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | if (gl_module == NULL) |
|---|
| 83 | { |
|---|
| 84 | gl_module = dlopen ("libMesaGL.so.1", RTLD_LAZY|RTLD_GLOBAL); |
|---|
| 85 | error = dlerror (); |
|---|
| 86 | if (error) |
|---|
| 87 | printf ("Error loading OpenGL library.\n%s\n", error); |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | if (gl_module == NULL) |
|---|
| 91 | return false; |
|---|
| 92 | |
|---|
| 93 | pfnglXChooseVisual = (PFNGLXCHOOSEVISUAL) Sys_GLGetProc ("glXChooseVisual"); |
|---|
| 94 | pfnglXCreateContext = (PFNGLXCREATECONTEXT) Sys_GLGetProc ("glXCreateContext"); |
|---|
| 95 | pfnglXDestroyContext = (PFNGLXDESTROYCONTEXT) Sys_GLGetProc ("glXDestroyContext"); |
|---|
| 96 | pfnglXMakeCurrent = (PFNGLXMAKECURRENT) Sys_GLGetProc ("glXMakeCurrent"); |
|---|
| 97 | pfnglXCopyContext = (PFNGLXCOPYCONTEXT) Sys_GLGetProc ("glXCopyContext"); |
|---|
| 98 | pfnglXSwapBuffers = (PFNGLXSWAPBUFFERS) Sys_GLGetProc ("glXSwapBuffers"); |
|---|
| 99 | pfnglXCreateGLXPixmap = (PFNGLXCREATEGLXPIXMAP) Sys_GLGetProc ("glXCreateGLXPixmap"); |
|---|
| 100 | pfnglXDestroyGLXPixmap = (PFNGLXDESTROYGLXPIXMAP) Sys_GLGetProc ("glXDestroyGLXPixmap"); |
|---|
| 101 | pfnglXQueryExtension = (PFNGLXQUERYEXTENSION) Sys_GLGetProc ("glXQueryExtension"); |
|---|
| 102 | pfnglXQueryVersion = (PFNGLXQUERYVERSION) Sys_GLGetProc ("glXQueryVersion"); |
|---|
| 103 | pfnglXIsDirect = (PFNGLXISDIRECT) Sys_GLGetProc ("glXIsDirect"); |
|---|
| 104 | pfnglXGetConfig = (PFNGLXGETCONFIG) Sys_GLGetProc ("glXGetConfig"); |
|---|
| 105 | pfnglXGetCurrentContext = (PFNGLXGETCURRENTCONTEXT) Sys_GLGetProc ("glXGetCurrentContext"); |
|---|
| 106 | pfnglXGetCurrentDrawable = (PFNGLXGETCURRENTDRAWABLE) Sys_GLGetProc ("glXGetCurrentDrawable"); |
|---|
| 107 | pfnglXWaitGL = (PFNGLXWAITGL) Sys_GLGetProc ("glXWaitGL"); |
|---|
| 108 | pfnglXWaitX = (PFNGLXWAITX) Sys_GLGetProc ("glXWaitX"); |
|---|
| 109 | pfnglXUseXFont = (PFNGLXUSEXFONT) Sys_GLGetProc ("glXUseXFont"); |
|---|
| 110 | pfnglXQueryExtensionsString = (PFNGLXQUERYEXTENSIONSSTRING) Sys_GLGetProc ("glXQueryExtensionsString"); |
|---|
| 111 | pfnglXQueryServerString = (PFNGLXQUERYSERVERSTRING) Sys_GLGetProc ("glXQueryServerString"); |
|---|
| 112 | pfnglXGetClientString = (PFNGLXGETCLIENTSTRING) Sys_GLGetProc ("glXGetClientString"); |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | pfnglXGetProcAddressARB = (PFNGLXGETPROCADDRESSARB) Sys_GLGetProc ("glXGetProcAddressARB"); |
|---|
| 120 | |
|---|
| 121 | return true; |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | void Sys_GLCloseLibrary () |
|---|
| 125 | { |
|---|
| 126 | if (gl_module) |
|---|
| 127 | { |
|---|
| 128 | dlclose (gl_module); |
|---|
| 129 | gl_module = NULL; |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | pfnglXChooseVisual = NULL; |
|---|
| 133 | pfnglXCreateContext = NULL; |
|---|
| 134 | pfnglXDestroyContext = NULL; |
|---|
| 135 | pfnglXMakeCurrent = NULL; |
|---|
| 136 | pfnglXCopyContext = NULL; |
|---|
| 137 | pfnglXSwapBuffers = NULL; |
|---|
| 138 | pfnglXCreateGLXPixmap = NULL; |
|---|
| 139 | pfnglXDestroyGLXPixmap = NULL; |
|---|
| 140 | pfnglXQueryExtension = NULL; |
|---|
| 141 | pfnglXQueryVersion = NULL; |
|---|
| 142 | pfnglXIsDirect = NULL; |
|---|
| 143 | pfnglXGetConfig = NULL; |
|---|
| 144 | pfnglXGetCurrentContext = NULL; |
|---|
| 145 | pfnglXGetCurrentDrawable = NULL; |
|---|
| 146 | pfnglXWaitGL = NULL; |
|---|
| 147 | pfnglXWaitX = NULL; |
|---|
| 148 | pfnglXUseXFont = NULL; |
|---|
| 149 | pfnglXQueryExtensionsString = NULL; |
|---|
| 150 | pfnglXQueryServerString = NULL; |
|---|
| 151 | pfnglXGetClientString = NULL; |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 158 | pfnglXGetProcAddressARB = NULL; |
|---|
| 159 | } |
|---|