├── README.md └── gl3w_gen.py /README.md: -------------------------------------------------------------------------------- 1 | # gl3w Single File Verions 2 | Original gl3w.h - https://github.com/skaslev/gl3w 3 | This version is modified so that the generated files are in the same directory as the `gl3w_gen.py`, merges the `gl3w.h` and `gl3w.c` files into one, and adds better namespacing for global variables and procedures. 4 | 5 | # API Reference 6 | 7 | #define GL3W_IMPLEMENTATION 8 | 9 | in EXACLY _one_ C or C++ file that includes this header, BEFORE the include, 10 | like this: 11 | 12 | #define GL3W_IMPLEMENTATION 13 | #include "gl3w.h" 14 | 15 | All other files should just `#include "gl3w.h"` without the `#define`. 16 | 17 | The API consists of three procedures: 18 | 19 | int gl3w_init(void); 20 | > Initalizes the library. Should be called once after an OpenGL context has been created. Returns `0` when gl3w was initialized successfully, `-1` is there was an error. 21 | 22 | int gl3w_is_supported(int major, int minor); 23 | 24 | > Return `1` when OpenGL core profile version _major.minor_ is available and `0` otherwise. 25 | 26 | GL3WglProc gl3w_get_proc_address(char const *proc); 27 | > Returns the address of an OpenGL extensions procedure. 28 | 29 | ## License 30 | [gl3w](https://github.com/skaslev/gl3w/blob/master/UNLICENSE) is in the public domain. 31 | -------------------------------------------------------------------------------- /gl3w_gen.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | # This file is part of gl3w, hosted at https://github.com/skaslev/gl3w 4 | # 5 | # This is free and unencumbered software released into the public domain. 6 | # 7 | # Anyone is free to copy, modify, publish, use, compile, sell, or 8 | # distribute this software, either in source code form or as a compiled 9 | # binary, for any purpose, commercial or non-commercial, and by any 10 | # means. 11 | # 12 | # In jurisdictions that recognize copyright laws, the author or authors 13 | # of this software dedicate any and all copyright interest in the 14 | # software to the public domain. We make this dedication for the benefit 15 | # of the public at large and to the detriment of our heirs and 16 | # successors. We intend this dedication to be an overt act of 17 | # relinquishment in perpetuity of all present and future rights to this 18 | # software under copyright law. 19 | # 20 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 21 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 22 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 23 | # IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 24 | # OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 25 | # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 26 | # OTHER DEALINGS IN THE SOFTWARE. 27 | 28 | # Allow Python 2.6+ to use the print() function 29 | from __future__ import print_function 30 | 31 | import re 32 | import os 33 | 34 | # Try to import Python 3 library urllib.request 35 | # and if it fails, fall back to Python 2 urllib2 36 | try: 37 | import urllib.request as urllib2 38 | except ImportError: 39 | import urllib2 40 | 41 | # UNLICENSE copyright header 42 | UNLICENSE = br'''/* 43 | 44 | This file is a modified version of gl3w_gen.py, part of gl3w 45 | (hosted at https://github.com/skaslev/gl3w) 46 | 47 | This is free and unencumbered software released into the public domain. 48 | 49 | Anyone is free to copy, modify, publish, use, compile, sell, or 50 | distribute this software, either in source code form or as a compiled 51 | binary, for any purpose, commercial or non-commercial, and by any 52 | means. 53 | 54 | In jurisdictions that recognize copyright laws, the author or authors 55 | of this software dedicate any and all copyright interest in the 56 | software to the public domain. We make this dedication for the benefit 57 | of the public at large and to the detriment of our heirs and 58 | successors. We intend this dedication to be an overt act of 59 | relinquishment in perpetuity of all present and future rights to this 60 | software under copyright law. 61 | 62 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 63 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 64 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 65 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 66 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 67 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 68 | OTHER DEALINGS IN THE SOFTWARE. 69 | 70 | ============================================================================ 71 | You MUST 72 | 73 | #define GL3W_IMPLEMENTATION 74 | 75 | in EXACLY _one_ C or C++ file that includes this header, BEFORE the include, 76 | like this: 77 | 78 | #define GL3W_IMPLEMENTATION 79 | #include "gl3w.h" 80 | 81 | All other files should just #include "gl3w.h" without the #define. 82 | 83 | */ 84 | 85 | ''' 86 | 87 | 88 | # Download glcorearb.h 89 | if not os.path.exists('glcorearb.h'): 90 | print('Downloading glcorearb.h...') 91 | web = urllib2.urlopen('https://www.opengl.org/registry/api/GL/glcorearb.h') 92 | with open('glcorearb.h', 'wb') as f: 93 | f.writelines(web.readlines()) 94 | else: 95 | print('Reusing glcorearb.h...') 96 | 97 | # Parse function names from glcorearb.h 98 | print('Parsing glcorearb.h header...') 99 | procs = [] 100 | p = re.compile(r'GLAPI.*APIENTRY\s+(\w+)') 101 | with open('glcorearb.h', 'r') as f: 102 | for line in f: 103 | m = p.match(line) 104 | if m: 105 | procs.append(m.group(1)) 106 | procs.sort() 107 | 108 | def proc_t(proc): 109 | return { 'p': proc, 110 | 'p_s': 'gl3w' + proc[2:], 111 | 'p_t': 'PFN' + proc.upper() + 'PROC' } 112 | 113 | # Generate gl3w.h 114 | print('Generating gl3w.h...') 115 | with open('gl3w.h', 'wb') as f: 116 | f.write(UNLICENSE) 117 | f.write(br'''#ifndef __gl3w_h_ 118 | #define __gl3w_h_ 119 | 120 | #include "glcorearb.h" 121 | 122 | #ifndef __gl_h_ 123 | #define __gl_h_ 124 | #endif 125 | 126 | #ifdef __cplusplus 127 | extern "C" { 128 | #endif 129 | 130 | typedef void (*GL3WglProc)(void); 131 | 132 | /* gl3w api */ 133 | int gl3w_init(void); 134 | int gl3w_is_supported(int major, int minor); 135 | GL3WglProc gl3w_get_proc_address(char const *proc); 136 | 137 | /* OpenGL functions */ 138 | ''') 139 | for proc in procs: 140 | f.write('extern {0[p_t]: <52} {0[p_s]};\n'.format(proc_t(proc)).encode("utf-8")) 141 | f.write(b'\n') 142 | for proc in procs: 143 | f.write('#define {0[p]: <45} {0[p_s]}\n'.format(proc_t(proc)).encode("utf-8")) 144 | f.write(br''' 145 | #ifdef __cplusplus 146 | } 147 | #endif 148 | 149 | #endif 150 | 151 | #if defined(GL3W_IMPLEMENTATION) && !defined(GL3W_IMPLEMENTATION_DONE) 152 | #define GL3W_IMPLEMENTATION_DONE 153 | 154 | #ifdef _WIN32 155 | 156 | #define WIN32_LEAN_AND_MEAN 1 157 | #define WIN32_MEAN_AND_LEAN 1 158 | #include 159 | 160 | static HMODULE gl3w__libgl; 161 | 162 | static void gl3w__open_libgl (void) { gl3w__libgl = LoadLibraryA("opengl32.dll"); } 163 | static void gl3w__close_libgl(void) { FreeLibrary(gl3w__libgl); } 164 | 165 | static GL3WglProc gl3w__get_proc(char const *proc) 166 | { 167 | GL3WglProc res; 168 | 169 | res = (GL3WglProc) wglGetProcAddress(proc); 170 | if (!res) 171 | res = (GL3WglProc) GetProcAddress(gl3w__libgl, proc); 172 | return res; 173 | } 174 | 175 | #elif defined(__APPLE__) || defined(__APPLE_CC__) 176 | 177 | #include 178 | 179 | CFBundleRef gl3w__bundle; 180 | CFURLRef gl3w__bundleURL; 181 | 182 | static void gl3w__open_libgl(void) 183 | { 184 | gl3w__bundleURL = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, 185 | CFSTR("/System/Library/Frameworks/OpenGL.framework"), 186 | kCFURLPOSIXPathStyle, true); 187 | 188 | gl3w__bundle = CFBundleCreate(kCFAllocatorDefault, gl3w__bundleURL); 189 | assert(gl3w__bundle != NULL); 190 | } 191 | 192 | static void gl3w__close_libgl(void) 193 | { 194 | CFRelease(gl3w__bundle); 195 | CFRelease(gl3w__bundleURL); 196 | } 197 | 198 | static GL3WglProc gl3w__get_proc(char const *proc) 199 | { 200 | GL3WglProc res; 201 | 202 | CFStringRef procname = CFStringCreateWithCString(kCFAllocatorDefault, proc, 203 | kCFStringEncodingASCII); 204 | res = (GL3WglProc) CFBundleGetFunctionPointerForName(gl3w__bundle, procname); 205 | CFRelease(procname); 206 | return res; 207 | } 208 | 209 | #else 210 | 211 | #include 212 | #include 213 | 214 | static void *gl3w__libgl; 215 | static PFNGLXGETPROCADDRESSPROC gl3w__glx_get_proc_address; 216 | 217 | static void gl3w__open_libgl(void) 218 | { 219 | gl3w__libgl = dlopen("libGL.so.1", RTLD_LAZY | RTLD_GLOBAL); 220 | gl3w__glx_get_proc_address = (PFNGLXGETPROCADDRESSPROC) dlsym(gl3w__libgl, "glXGetProcAddressARB"); 221 | } 222 | 223 | static void gl3w__close_libgl(void) { dlclose(gl3w__libgl); } 224 | 225 | static GL3WglProc gl3w__get_proc(char const *proc) 226 | { 227 | GL3WglProc res; 228 | 229 | res = (GL3WglProc) gl3w__glx_get_proc_address((const GLubyte *) proc); 230 | if (!res) 231 | res = (GL3WglProc) dlsym(gl3w__libgl, proc); 232 | return res; 233 | } 234 | 235 | #endif 236 | 237 | static struct { 238 | int major, minor; 239 | } gl3w__version; 240 | 241 | static int gl3w__parse_version(void) 242 | { 243 | if (!glGetIntegerv) 244 | return -1; 245 | 246 | glGetIntegerv(GL_MAJOR_VERSION, &gl3w__version.major); 247 | glGetIntegerv(GL_MINOR_VERSION, &gl3w__version.minor); 248 | 249 | if (gl3w__version.major < 3) 250 | return -1; 251 | return 0; 252 | } 253 | 254 | static void gl3w__load_procs(void); 255 | 256 | int gl3w_init(void) 257 | { 258 | gl3w__open_libgl(); 259 | gl3w__load_procs(); 260 | gl3w__close_libgl(); 261 | return gl3w__parse_version(); 262 | } 263 | 264 | int gl3w_is_supported(int major, int minor) 265 | { 266 | if (major < 3) 267 | return 0; 268 | if (gl3w__version.major == major) 269 | return gl3w__version.minor >= minor; 270 | return gl3w__version.major >= major; 271 | } 272 | 273 | GL3WglProc gl3w_get_proc_address(char const *proc) 274 | { 275 | return gl3w__get_proc(proc); 276 | } 277 | 278 | ''') 279 | for proc in procs: 280 | f.write('{0[p_t]: <52} {0[p_s]};\n'.format(proc_t(proc)).encode("utf-8")) 281 | f.write(br''' 282 | static void gl3w__load_procs(void) 283 | { 284 | ''') 285 | for proc in procs: 286 | f.write('\t{0[p_s]} = ({0[p_t]}) gl3w__get_proc("{0[p]}");\n'.format(proc_t(proc)).encode("utf-8")) 287 | f.write(b'''} 288 | 289 | 290 | #endif /* GL3W_IMPLEMENTATION */ 291 | ''') 292 | --------------------------------------------------------------------------------