aboutsummaryrefslogtreecommitdiff
path: root/src/gl.c
blob: 4ac022a6d8dde5f2c044d06b7aadc1a1ca0b62cf (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include "../include/mgl/gl.h"
#include <dlfcn.h>
#include <stdio.h>

typedef struct {
    void **func;
    const char *name;
} dlsym_assign;

static void* dlsym_print_fail(void *handle, const char *name, int required) {
    dlerror();
    void *sym = dlsym(handle, name);
    char *err_str = dlerror();

    if(!sym)
        fprintf(stderr, "mgl %s: dlsym(handle, \"%s\") failed, error: %s\n", required ? "error" : "warning", name, err_str ? err_str : "(null)");

    return sym;
}

int mgl_gl_load(mgl_gl *self) {
    const char *glx_path = "libGL.so.1";
    self->handle = dlopen(glx_path, RTLD_LAZY);
    if(!self->handle) {
        fprintf(stderr, "mgl error:dlopen(\"%s\", RTLD_LAZY) failed\n", glx_path);
        return -1;
    }

    dlsym_assign required_dlsym[] = {
        { (void**)&self->glXCreateNewContext, "glXCreateNewContext" },
        { (void**)&self->glXMakeContextCurrent, "glXMakeContextCurrent" },
        { (void**)&self->glXDestroyContext, "glXDestroyContext" },
        { (void**)&self->glXSwapBuffers, "glXSwapBuffers" },
        { (void**)&self->glXChooseFBConfig, "glXChooseFBConfig" },
        { (void**)&self->glXGetVisualFromFBConfig, "glXGetVisualFromFBConfig" },

        { (void**)&self->glViewport, "glViewport" },
        { (void**)&self->glScissor, "glScissor" },
        { (void**)&self->glClearColor, "glClearColor" },
        { (void**)&self->glClear, "glClear" },
        { (void**)&self->glEnable, "glEnable" },
        { (void**)&self->glBlendFunc, "glBlendFunc" },
        { (void**)&self->glGenTextures, "glGenTextures" },
        { (void**)&self->glDeleteTextures, "glDeleteTextures" },
        { (void**)&self->glTexImage2D, "glTexImage2D" },
        { (void**)&self->glTexSubImage2D, "glTexSubImage2D" },
        { (void**)&self->glBindTexture, "glBindTexture" },
        { (void**)&self->glTexParameteri, "glTexParameteri" },
        { (void**)&self->glBegin, "glBegin" },
        { (void**)&self->glEnd, "glEnd" },
        { (void**)&self->glVertex3f, "glVertex3f" },
        { (void**)&self->glColor4ub, "glColor4ub" },
        { (void**)&self->glTexCoord2f, "glTexCoord2f" },
        { (void**)&self->glOrtho, "glOrtho" },
        { (void**)&self->glMatrixMode, "glMatrixMode" },
        { (void**)&self->glPushMatrix, "glPushMatrix" },
        { (void**)&self->glPopMatrix, "glPopMatrix" },
        { (void**)&self->glLoadIdentity, "glLoadIdentity" },
        { (void**)&self->glLoadMatrixf, "glLoadMatrixf" },
        { (void**)&self->glTranslatef, "glTranslatef" },
        { (void**)&self->glRotatef, "glRotatef" },
        { (void**)&self->glGenBuffers, "glGenBuffers" },
        { (void**)&self->glBindBuffer, "glBindBuffer" },
        { (void**)&self->glDeleteBuffers, "glDeleteBuffers" },
        { (void**)&self->glBufferData, "glBufferData" },
        { (void**)&self->glBufferSubData, "glBufferSubData" },
        { (void**)&self->glDrawArrays, "glDrawArrays" },
        { (void**)&self->glEnableClientState, "glEnableClientState" },
        { (void**)&self->glVertexPointer, "glVertexPointer" },
        { (void**)&self->glColorPointer, "glColorPointer" },
        { (void**)&self->glTexCoordPointer, "glTexCoordPointer" },
        { (void**)&self->glCompileShader, "glCompileShader" },
        { (void**)&self->glCreateProgram, "glCreateProgram" },
        { (void**)&self->glCreateShader, "glCreateShader" },
        { (void**)&self->glDeleteProgram, "glDeleteProgram" },
        { (void**)&self->glDeleteShader, "glDeleteShader" },
        { (void**)&self->glGetShaderiv, "glGetShaderiv" },
        { (void**)&self->glGetShaderInfoLog, "glGetShaderInfoLog" },
        { (void**)&self->glGetProgramiv, "glGetProgramiv" },
        { (void**)&self->glGetProgramInfoLog, "glGetProgramInfoLog" },
        { (void**)&self->glLinkProgram, "glLinkProgram" },
        { (void**)&self->glShaderSource, "glShaderSource" },
        { (void**)&self->glUseProgram, "glUseProgram" },
        { (void**)&self->glAttachShader, "glAttachShader" },
        { (void**)&self->glGetUniformLocation, "glGetUniformLocation" },
        { (void**)&self->glUniform1f, "glUniform1f" },
        { (void**)&self->glUniform2f, "glUniform2f" },
        { (void**)&self->glUniform3f, "glUniform3f" },
        { (void**)&self->glUniform4f, "glUniform4f" },
        { (void**)&self->glGetError, "glGetError" },
        { (void**)&self->glGetString, "glGetString" },
        { (void**)&self->glGetIntegerv, "glGetIntegerv" },
        { (void**)&self->glPixelStorei, "glPixelStorei" },
        { (void**)&self->glFlush, "glFlush" },
        { (void**)&self->glFinish, "glFinish" },

        { NULL, NULL }
    };

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

    const dlsym_assign optional_dlsym[] = {
        { (void**)&self->glXSwapIntervalEXT, "glXSwapIntervalEXT" },
        { (void**)&self->glXSwapIntervalMESA, "glXGetSwapIntervalMESA" },
        { (void**)&self->glXSwapIntervalSGI, "glXSwapIntervalSGI" },
        { (void**)&self->glGenerateMipmap, "glGenerateMipmap" },

        { NULL, NULL }
    };

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

    return 0;
}

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