Minimal example of a custom OpenGL function loader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdbool.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <SDL2/SDL.h> | |
#include <SDL2/SDL_opengl.h> | |
#include "opengl.h" | |
#define OPENGL_DEFINE(func_name, func_type) \ | |
func_type func_name = NULL; | |
#define OPENGL_FUNCTION OPENGL_DEFINE | |
OPENGL_FUNCTIONS | |
#undef OPENGL_FUNCTION | |
union bridge { | |
void* object_ptr; | |
void (*function_ptr)(void); | |
}; | |
#define OPENGL_LOAD(func_name, func_type) \ | |
func_name = (func_type)(union bridge){ \ | |
.object_ptr = SDL_GL_GetProcAddress(#func_name) \ | |
}.function_ptr; | |
#define OPENGL_VALIDATE(func_name, func_type) \ | |
if (func_name == NULL) { \ | |
fprintf(stderr, "failed to load func: %s\n", #func_name); \ | |
return false; \ | |
} | |
bool opengl_load_functions(void) { | |
#define OPENGL_FUNCTION OPENGL_LOAD | |
OPENGL_FUNCTIONS | |
#undef OPENGL_FUNCTION | |
#define OPENGL_FUNCTION OPENGL_VALIDATE | |
OPENGL_FUNCTIONS | |
#undef OPENGL_FUNCTION | |
return true; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef DEMO_OPENGL_H_INCLUDED | |
#define DEMO_OPENGL_H_INCLUDED | |
#include <stdbool.h> | |
#include <SDL2/SDL_opengl.h> | |
#define OPENGL_FUNCTIONS \ | |
OPENGL_FUNCTION(glCreateShader, PFNGLCREATESHADERPROC) \ | |
OPENGL_FUNCTION(glDeleteShader, PFNGLDELETESHADERPROC) | |
#define OPENGL_DECLARE(func_name, func_type) \ | |
extern func_type func_name; | |
#define OPENGL_FUNCTION OPENGL_DECLARE | |
OPENGL_FUNCTIONS | |
#undef OPENGL_FUNCTION | |
bool opengl_load_functions(void); | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment