Skip to content

Instantly share code, notes, and snippets.

@pmttavara
Created January 2, 2020 08:37
Show Gist options
  • Save pmttavara/029036c588e79676f4fe41e6a948a188 to your computer and use it in GitHub Desktop.
Save pmttavara/029036c588e79676f4fe41e6a948a188 to your computer and use it in GitHub Desktop.
OpenGL 1.0 loader in Win32
static const char *const GL_funcnames[] = {
"wglDeleteContext",
"wglUseFontBitmapsA",
"wglSwapIntervalEXT",
"glGetError",
"glBlendFunc",
"glEnable",
"glDisable",
"glViewport",
"glVertex3f",
"glClearColor",
"glClear",
"glBegin",
"glColor4f",
"glEnd",
"glTexCoord2f",
"glTexImage2D",
"glGenTextures",
"glBindTexture",
"glTexParameteri",
"glEnableClientState",
"glDisableClientState",
"glDrawArrays",
"glVertexPointer",
};
struct {
void *(*wglGetProcAddress)(const char *name);
void *(*wglCreateContext)(void *hdc);
int (*wglMakeCurrent)(void *hdc, void *hglrc);
union {
struct {
int (*wglDeleteContext)(void *hglrc);
int (*wglUseFontBitmapsA)(void *hdc, unsigned int, unsigned int, unsigned int);
int (*wglSwapIntervalEXT)(int interval);
unsigned int (*GetError)(void);
void (*BlendFunc)(unsigned int sfactor, unsigned int dfactor);
void (*Enable)(unsigned int cap);
void (*Disable)(unsigned int cap);
void (*Viewport)(int x, int y, int w, int h);
void (*Vertex3f)(float x, float y, float z);
void (*ClearColor)(float r, float g, float b, float a);
void (*Clear)(unsigned int mask);
void (*Begin)(unsigned int mode);
void (*Color4f)(float r, float g, float b, float a);
void (*End)(void);
void (*TexCoord2f)(float s, float t);
void (*TexImage2D)(unsigned int target, int level, int internalformat, int width, int height, int border, unsigned int format, unsigned int type, const void *pixels);
void (*GenTextures)(int n, unsigned int *textures);
void (*BindTexture)(unsigned int target, unsigned int texture);
void (*TexParameteri)(unsigned int target, unsigned int pname, int param);
void (*EnableClientState)(unsigned int cap);
void (*DisableClientState)(unsigned int cap);
void (*DrawArrays)(unsigned int mode, int first, int count);
void (*VertexPointer)(int size, unsigned int type, int stride, const void *pointer);
};
enum { GL_NUM_FUNCTIONS = sizeof GL_funcnames / sizeof *GL_funcnames } dummy_enum;
void *addresses[GL_NUM_FUNCTIONS];
};
} GL;
static void *GL_load_proc(void *opengl32_dll, const char *name) {
void *result = GL.wglGetProcAddress(name);
if (!result) result = GetProcAddress(opengl32_dll, name);
return result;
}
static int GL_load_procedures(void *opengl32_dll) {
for (int i = 0; i < GL_NUM_FUNCTIONS; i += 1) {
GL.addresses[i] = GL_load_proc(opengl32_dll, GL_funcnames[i]);
}
return 0;
}
// .. somewhere in startup:
void *opengl32_dll = LoadLibraryA("opengl32.dll");
*(void **)&GL.wglGetProcAddress = GetProcAddress(opengl32_dll, "wglGetProcAddress");
*(void **)&GL.wglCreateContext = GetProcAddress(opengl32_dll, "wglCreateContext");
*(void **)&GL.wglMakeCurrent = GetProcAddress(opengl32_dll, "wglMakeCurrent");
void *hglrc = GL.wglCreateContext(hdc);
GL.wglMakeCurrent(hdc, hglrc);
GL_load_procedures(opengl32_dll);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment