GitHub - tsherif/simple-opengl-loader: An extensible, cross-platform, single-header C/C++ OpenGL loader library.

GitHub

An extensible, cross-platform, single-header C/C++ OpenGL loader library.

Usage

For Windows Win32 or Linux X11 applications, the simplest usage involves defining SOGL_MAJOR_VERSION, SOGL_MINOR_VERSIONand either SOGL_IMPLEMENTATION_WIN32 or SOGL_IMPLEMENTATION_X11 before including the simple-opengl-loader.h header file, and then calling sogl_loadOpenGL() after setting up an OpenGL context.

#defineSOGL_MAJOR_VERSION 4 #defineSOGL_MINOR_VERSION 5 #defineSOGL_IMPLEMENTATION_WIN32/* or SOGL_IMPLEMENTATION_X11 */#include"simple-opengl-loader.h"intmain() { /* Set up OpenGL context */sogl_loadOpenGL(); /* Use OpenGL functions */ }It is recommended that simple-opengl-loader.h be the first include to prevent other OpenGL headers from setting up their own definitions.

Platform support is included for Windows Win32 and Linux X11 applications by defining the SOGL_IMPLEMENTATION_WIN32 or SOGL_IMPLEMENTATION_X11 constants, respectively. The SOGL_IMPLEMENTATION_X11 implementation requires the application be linked against libdl. See below to implement support for other platforms.

OpenGL extensions can be loaded by defining a constant of the format SOGL_<extension name> before including the simple-opengl-loader.h header.

#defineSOGL_MAJOR_VERSION 4 #defineSOGL_MINOR_VERSION 5 #defineSOGL_OVR_multiview#defineSOGL_KHR_parallel_shader_compile#defineSOGL_IMPLEMENTATION_WIN32#include"simple-opengl-loader.h"Note that the loader makes no guarantees about OpenGL version or extension support. sogl_loadOpenGL() returns a boolean value indicating whether it was able to load all requested functions, and the function sogl_getFailures returns a null-terminated array of the names of the functions that failed to load (up to a maximum defined by SOGL_MAX_REPORTED_FAILURES).

if (!sogl_loadOpenGL()) { constchar**failures=sogl_getFailures(); inti=1; while (*failures) { fprintf(stderr, "Failed to load function %s\n", *failures); failures++; } }Platform Support

Platform-specific logic is encapsulated in two functions sogl_loadOpenGLFunction() which takes the name of an OpenGL function as a null-terminated ASCII string and returns a pointer to the appropriate function, and sogl_cleanup(), which should perform any cleanup necessary after loading is complete, e.g. freeing library handles.

void*sogl_loadOpenGLFunction(constchar*name); voidsogl_cleanup();Implementations for these functions are provided out-of-the-box for Windows Win32 and Linux X11 applications (see above). Support for other platforms simply requires implementing these two functions for the target platform and defining the constant SOGL_IMPLEMENTATION instead of either of the platform-specific implementation constants.

#defineSOGL_MAJOR_VERSION 4 #defineSOGL_MINOR_VERSION 5 #defineSOGL_IMPLEMENTATION#include"simple-opengl-loader.h"void*sogl_loadOpenGLFunction(constchar*name) { /* Custom function loader implementation */ } voidsogl_cleanup() { /* Custom cleanup implementation */ }