aboutsummaryrefslogtreecommitdiff
path: root/src/gl.c
blob: a06f75a13b2e696fad558908604402a97df2f2f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#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 *glx_path = "/usr/lib/libGL.so.1";
    self->handle = dlopen(glx_path, RTLD_LAZY);
    if(!self->handle) {
        fprintf(stderr, "dlopen(\"%s\", RTLD_LAZY) failed\n", glx_path);
        return -1;
    }

    const dlsym_assign required_dlsym[] = {
        { &self->glXChooseVisual, "glXChooseVisual" },
        { &self->glXCreateContext, "glXCreateContext" },
        { &self->glXDestroyContext, "glXDestroyContext" },
        { &self->glXMakeCurrent, "glXMakeCurrent" },
        { &self->glXSwapBuffers, "glXSwapBuffers" },

        { &self->glViewport, "glViewport" },
        { &self->glClearColor, "glClearColor" },
        { &self->glClear, "glClear" },
        { &self->glEnable, "glEnable" },
        { &self->glBlendFunc, "glBlendFunc" },
        { &self->glGenTextures, "glGenTextures" },
        { &self->glDeleteTextures, "glDeleteTextures" },
        { &self->glTexImage2D, "glTexImage2D" },
        { &self->glCompressedTexImage2D, "glCompressedTexImage2D" },
        { &self->glBindTexture, "glBindTexture" },
        { &self->glTexParameteri, "glTexParameteri" },
        { &self->glHint, "glHint" },
        { &self->glBegin, "glBegin" },
        { &self->glEnd, "glEnd" },
        { &self->glColor3f, "glColor3f" },
        { &self->glVertex3f, "glVertex3f" },
        { &self->glColor4f, "glColor4f" },
        { &self->glTexCoord2f, "glTexCoord2f" },
        { &self->glOrtho, "glOrtho" },
        { &self->glMatrixMode, "glMatrixMode" },
        { &self->glLoadIdentity, "glLoadIdentity" },

        { NULL, NULL }
    };

    for(int i = 0; required_dlsym[i].func; ++i) {
        *required_dlsym[i].func = dlsym_print_fail(self->handle, required_dlsym[i].name);
        if(!required_dlsym[i].func) {
            mgl_gl_unload(self);
            return -1;
        }
    }

    const dlsym_assign optional_dlsym[] = {
        { &self->glXSwapIntervalEXT, "glXSwapIntervalEXT" },
        { &self->glXSwapIntervalMESA, "glXGetSwapIntervalMESA" },
        { &self->glXSwapIntervalSGI, "glXSwapIntervalSGI" },
        { NULL, NULL }
    };

    for(int i = 0; optional_dlsym[i].func; ++i) {
        *optional_dlsym[i].func = dlsym_print_fail(self->handle, optional_dlsym[i].name);
    }

    return 0;
}

void mgl_gl_unload(mgl_gl *self) {
    if(self->handle) {
        dlclose(self->handle);
        self->handle = NULL;
    }
}