diff options
Diffstat (limited to 'src/gl.c')
-rw-r--r-- | src/gl.c | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/src/gl.c b/src/gl.c new file mode 100644 index 0000000..c83ab49 --- /dev/null +++ b/src/gl.c @@ -0,0 +1,53 @@ +#include "../include/mgl/gl.h" +#include <dlfcn.h> +/*#include <GL/gl.h>*/ +#include <stdio.h> + +typedef struct { + void **func; + const char *name; +} dlsym_assign; + +static void* dlsym_print_fail(void *handle, const char *name) { + dlerror(); + void *sym = dlsym(handle, name); + char *err_str = dlerror(); + + if(!sym) + fprintf(stderr, "dlsym(handle, \"%s\") failed, error: %s\n", name, err_str ? err_str : "(null)"); + + return sym; +} + +int mgl_gl_load(mgl_gl *self) { + const char *gl_path = "/usr/lib/libOpenGL.so.0"; + self->handle = dlopen(gl_path, RTLD_LAZY); + if(!self->handle) { + fprintf(stderr, "dlopen(\"%s\", RTLD_LAZY) failed\n", gl_path); + return -1; + } + + const dlsym_assign assign[] = { + { &self->glViewport, "glViewport" }, + { &self->glClearColor, "glClearColor" }, + { &self->glClear, "glClear" }, + { NULL, NULL } + }; + + for(int i = 0; assign[i].func; ++i) { + *assign[i].func = dlsym_print_fail(self->handle, assign[i].name); + if(!assign[i].func) { + mgl_gl_unload(self); + return -1; + } + } + + return 0; +} + +void mgl_gl_unload(mgl_gl *self) { + if(self->handle) { + dlclose(self->handle); + self->handle = NULL; + } +} |