a-game

2D platformer written from scratch.
git clone git://git.amin.space/a-game.git
Log | Files | Refs | README | LICENSE

am_gl.c (1748B)


      1 #if defined(PLATFORM_LINUX)
      2 #include <dlfcn.h>
      3 int am_gl_init()
      4 {
      5     char *libgl_name = "libGL.so";
      6     void *libgl = dlopen(libgl_name, RTLD_NOW | RTLD_GLOBAL);
      7     if (libgl != NULL)
      8     {
      9 #define AM_GLE(ret, name, ...) \
     10         dlerror(); \
     11         name = (name##Proc *) dlsym(libgl, #name); \
     12         if (!name) { \
     13             printf("Function " #name " couldn't be loaded.\n"); \
     14             printf("dlerror(): %s\n", dlerror()); \
     15             return 0; \
     16         }
     17         GL_LOAD_FUNCTION_LIST
     18 #undef AM_GLE
     19         dlclose(libgl);
     20         return 1;
     21     }
     22     else
     23     {
     24         printf("Failed to load libgl\n");
     25         return 0;
     26     }
     27 }
     28 #elif defined(PLATFORM_WINDOWS)
     29 int am_gl_init()
     30 {
     31     char *libgl_name = "opengl32.dll";
     32     HINSTANCE libgl = LoadLibraryA(libgl_name);
     33     if(libgl)
     34     {
     35         typedef PROC WINAPI wglGetProcAddressProc(LPCSTR lpszProc);
     36         wglGetProcAddressProc *wglGetProcAddress = (wglGetProcAddressProc *)GetProcAddress(libgl, "wglGetProcAddress");
     37         if (wglGetProcAddress)
     38         {
     39 #define AM_GLE(ret, name, ...) \
     40             name = (name##Proc *) GetProcAddress(libgl, #name); \
     41             if (!name) { \
     42                 name = (name##Proc *) wglGetProcAddress(#name); \
     43             } \
     44             if (!name) { \
     45                 printf("Function " #name " couldn't be loaded.\n"); \
     46                 return 0; \
     47             }
     48             GL_LOAD_FUNCTION_LIST
     49 #undef AM_GLE
     50         }
     51         else
     52         {
     53             printf("Failed to load wglGetProcAddress\n");
     54             FreeLibrary((HMODULE) libgl);
     55             return 0;
     56         }
     57         FreeLibrary((HMODULE) libgl);
     58         return 1;
     59     }
     60     else
     61     {
     62         printf("Failed to load libgl\n");
     63         return 0;
     64     }
     65 }
     66 #endif