From fcd3715c706b67a65bbaff1a2b2808d81b244148 Mon Sep 17 00:00:00 2001 From: Rokas Kupstys Date: Fri, 20 Aug 2021 10:37:40 +0300 Subject: [PATCH] Move template c code from source code to template files. --- gl3w_gen.py | 267 ++++-------------------------------------------- template/gl3w.c | 188 ++++++++++++++++++++++++++++++++++ template/gl3w.h | 70 +++++++++++++ 3 files changed, 278 insertions(+), 247 deletions(-) create mode 100644 template/gl3w.c create mode 100644 template/gl3w.h diff --git a/gl3w_gen.py b/gl3w_gen.py index 8294d81..f51f108 100755 --- a/gl3w_gen.py +++ b/gl3w_gen.py @@ -39,37 +39,6 @@ except ImportError: import urllib2 -# UNLICENSE copyright header -UNLICENSE = r'''/* - * This file was generated with gl3w_gen.py, part of gl3w - * (hosted at https://github.com/skaslev/gl3w) - * - * This is free and unencumbered software released into the public domain. - * - * Anyone is free to copy, modify, publish, use, compile, sell, or - * distribute this software, either in source code form or as a compiled - * binary, for any purpose, commercial or non-commercial, and by any - * means. - * - * In jurisdictions that recognize copyright laws, the author or authors - * of this software dedicate any and all copyright interest in the - * software to the public domain. We make this dedication for the benefit - * of the public at large and to the detriment of our heirs and - * successors. We intend this dedication to be an overt act of - * relinquishment in perpetuity of all present and future rights to this - * software under copyright law. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. - * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR - * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, - * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR - * OTHER DEALINGS IN THE SOFTWARE. - */ - -''' - EXT_SUFFIX = ['ARB', 'EXT', 'KHR', 'OVR', 'NV', 'AMD', 'INTEL'] def is_ext(proc): @@ -125,227 +94,31 @@ def download(url, dst): # Generate gl3w.h print('Generating {0}...'.format(os.path.join(args.root, 'include/GL/gl3w.h'))) with open(os.path.join(args.root, 'include/GL/gl3w.h'), 'wb') as f: - write(f, UNLICENSE) - write(f, r'''#ifndef __gl3w_h_ -#define __gl3w_h_ - -#include - -#ifndef GL3W_API -#define GL3W_API -#endif - -#ifndef __gl_h_ -#define __gl_h_ -#endif - -#ifdef __cplusplus -extern "C" { -#endif - -#define GL3W_OK 0 -#define GL3W_ERROR_INIT -1 -#define GL3W_ERROR_LIBRARY_OPEN -2 -#define GL3W_ERROR_OPENGL_VERSION -3 - -typedef void (*GL3WglProc)(void); -typedef GL3WglProc (*GL3WGetProcAddressProc)(const char *proc); - -/* gl3w api */ -GL3W_API int gl3wInit(void); -GL3W_API int gl3wInit2(GL3WGetProcAddressProc proc); -GL3W_API int gl3wIsSupported(int major, int minor); -GL3W_API GL3WglProc gl3wGetProcAddress(const char *proc); - -/* gl3w internal state */ -''') - write(f, 'union GL3WProcs {\n') - write(f, '\tGL3WglProc ptr[{0}];\n'.format(len(procs))) - write(f, '\tstruct {\n') + gl3w_h = open(os.path.join(args.root, 'template/gl3w.h'), 'r', encoding='utf-8').read() + strings = [ + '/* gl3w internal state */', + 'union GL3WProcs {', + '\tGL3WglProc ptr[{0}];'.format(len(procs)), + '\tstruct {' + ] for proc in procs: - write(f, '\t\t{0: <55} {1};\n'.format('PFN{0}PROC'.format(proc.upper()), proc[2:])) - write(f, r''' } gl; -}; - -GL3W_API extern union GL3WProcs gl3wProcs; + strings.append('\t\t{0: <55} {1};'.format('PFN{0}PROC'.format(proc.upper()), proc[2:])) + strings.append('\t} gl;') # struct + strings.append('};') # union GL3WProcs + gl3w_h = gl3w_h.replace(strings[0], '\n'.join(strings)) -/* OpenGL functions */ -''') + strings = ['/* OpenGL functions */'] for proc in procs: - write(f, '#define {0: <48} gl3wProcs.gl.{1}\n'.format(proc, proc[2:])) - write(f, r''' -#ifdef __cplusplus -} -#endif - -#endif -''') + strings.append('#define {0: <48} gl3wProcs.gl.{1}'.format(proc, proc[2:])) + gl3w_h = gl3w_h.replace(strings[0], '\n'.join(strings)) + write(f, gl3w_h) # Generate gl3w.c print('Generating {0}...'.format(os.path.join(args.root, 'src/gl3w.c'))) with open(os.path.join(args.root, 'src/gl3w.c'), 'wb') as f: - write(f, UNLICENSE) - write(f, r'''#include -#include - -#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) - -#if defined(_WIN32) -#ifndef WIN32_LEAN_AND_MEAN -#define WIN32_LEAN_AND_MEAN 1 -#endif -#include - -static HMODULE libgl; -typedef PROC(__stdcall* GL3WglGetProcAddr)(LPCSTR); -static GL3WglGetProcAddr wgl_get_proc_address; - -static int open_libgl(void) -{ - libgl = LoadLibraryA("opengl32.dll"); - if (!libgl) - return GL3W_ERROR_LIBRARY_OPEN; - - wgl_get_proc_address = (GL3WglGetProcAddr)GetProcAddress(libgl, "wglGetProcAddress"); - return GL3W_OK; -} - -static void close_libgl(void) -{ - FreeLibrary(libgl); -} - -static GL3WglProc get_proc(const char *proc) -{ - GL3WglProc res; - - res = (GL3WglProc)wgl_get_proc_address(proc); - if (!res) - res = (GL3WglProc)GetProcAddress(libgl, proc); - return res; -} -#elif defined(__APPLE__) -#include - -static void *libgl; - -static int open_libgl(void) -{ - libgl = dlopen("/System/Library/Frameworks/OpenGL.framework/OpenGL", RTLD_LAZY | RTLD_LOCAL); - if (!libgl) - return GL3W_ERROR_LIBRARY_OPEN; - - return GL3W_OK; -} - -static void close_libgl(void) -{ - dlclose(libgl); -} - -static GL3WglProc get_proc(const char *proc) -{ - GL3WglProc res; - - *(void **)(&res) = dlsym(libgl, proc); - return res; -} -#else -#include - -static void *libgl; -static GL3WglProc (*glx_get_proc_address)(const GLubyte *); - -static int open_libgl(void) -{ - libgl = dlopen("libGL.so.1", RTLD_LAZY | RTLD_LOCAL); - if (!libgl) - return GL3W_ERROR_LIBRARY_OPEN; - - *(void **)(&glx_get_proc_address) = dlsym(libgl, "glXGetProcAddressARB"); - return GL3W_OK; -} - -static void close_libgl(void) -{ - dlclose(libgl); -} - -static GL3WglProc get_proc(const char *proc) -{ - GL3WglProc res; - - res = glx_get_proc_address((const GLubyte *)proc); - if (!res) - *(void **)(&res) = dlsym(libgl, proc); - return res; -} -#endif - -static struct { - int major, minor; -} version; - -static int parse_version(void) -{ - if (!glGetIntegerv) - return GL3W_ERROR_INIT; - - glGetIntegerv(GL_MAJOR_VERSION, &version.major); - glGetIntegerv(GL_MINOR_VERSION, &version.minor); - - if (version.major < 3) - return GL3W_ERROR_OPENGL_VERSION; - return GL3W_OK; -} - -static void load_procs(GL3WGetProcAddressProc proc); - -int gl3wInit(void) -{ - int res; - - res = open_libgl(); - if (res) - return res; - - atexit(close_libgl); - return gl3wInit2(get_proc); -} - -int gl3wInit2(GL3WGetProcAddressProc proc) -{ - load_procs(proc); - return parse_version(); -} - -int gl3wIsSupported(int major, int minor) -{ - if (major < 3) - return 0; - if (version.major == major) - return version.minor >= minor; - return version.major >= major; -} - -GL3WglProc gl3wGetProcAddress(const char *proc) -{ - return get_proc(proc); -} - -static const char *proc_names[] = { -''') + gl3w_c = open(os.path.join(args.root, 'template/gl3w.c'), 'r', encoding='utf-8').read() + strings = ['static const char *proc_names[] = {'] for proc in procs: - write(f, '\t"{0}",\n'.format(proc)) - write(f, r'''}; - -GL3W_API union GL3WProcs gl3wProcs; - -static void load_procs(GL3WGetProcAddressProc proc) -{ - size_t i; - - for (i = 0; i < ARRAY_SIZE(proc_names); i++) - gl3wProcs.ptr[i] = proc(proc_names[i]); -} -''') + strings.append('\t"{0}",'.format(proc)) + gl3w_c = gl3w_c.replace(strings[0], '\n'.join(strings)) + write(f, gl3w_c) diff --git a/template/gl3w.c b/template/gl3w.c new file mode 100644 index 0000000..787b678 --- /dev/null +++ b/template/gl3w.c @@ -0,0 +1,188 @@ +/* + * This file was generated with gl3w_gen.py, part of gl3w + * (hosted at https://github.com/skaslev/gl3w) + * + * This is free and unencumbered software released into the public domain. + * + * Anyone is free to copy, modify, publish, use, compile, sell, or + * distribute this software, either in source code form or as a compiled + * binary, for any purpose, commercial or non-commercial, and by any + * means. + * + * In jurisdictions that recognize copyright laws, the author or authors + * of this software dedicate any and all copyright interest in the + * software to the public domain. We make this dedication for the benefit + * of the public at large and to the detriment of our heirs and + * successors. We intend this dedication to be an overt act of + * relinquishment in perpetuity of all present and future rights to this + * software under copyright law. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR + * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + */ + +#include +#include + +#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) + +#if defined(_WIN32) +#ifndef WIN32_LEAN_AND_MEAN +#define WIN32_LEAN_AND_MEAN 1 +#endif +#include + +static HMODULE libgl; +typedef PROC(__stdcall* GL3WglGetProcAddr)(LPCSTR); +static GL3WglGetProcAddr wgl_get_proc_address; + +static int open_libgl(void) +{ + libgl = LoadLibraryA("opengl32.dll"); + if (!libgl) + return GL3W_ERROR_LIBRARY_OPEN; + + wgl_get_proc_address = (GL3WglGetProcAddr)GetProcAddress(libgl, "wglGetProcAddress"); + return GL3W_OK; +} + +static void close_libgl(void) +{ + FreeLibrary(libgl); +} + +static GL3WglProc get_proc(const char *proc) +{ + GL3WglProc res; + + res = (GL3WglProc)wgl_get_proc_address(proc); + if (!res) + res = (GL3WglProc)GetProcAddress(libgl, proc); + return res; +} +#elif defined(__APPLE__) +#include + +static void *libgl; + +static int open_libgl(void) +{ + libgl = dlopen("/System/Library/Frameworks/OpenGL.framework/OpenGL", RTLD_LAZY | RTLD_LOCAL); + if (!libgl) + return GL3W_ERROR_LIBRARY_OPEN; + + return GL3W_OK; +} + +static void close_libgl(void) +{ + dlclose(libgl); +} + +static GL3WglProc get_proc(const char *proc) +{ + GL3WglProc res; + + *(void **)(&res) = dlsym(libgl, proc); + return res; +} +#else +#include + +static void *libgl; +static GL3WglProc (*glx_get_proc_address)(const GLubyte *); + +static int open_libgl(void) +{ + libgl = dlopen("libGL.so.1", RTLD_LAZY | RTLD_LOCAL); + if (!libgl) + return GL3W_ERROR_LIBRARY_OPEN; + + *(void **)(&glx_get_proc_address) = dlsym(libgl, "glXGetProcAddressARB"); + return GL3W_OK; +} + +static void close_libgl(void) +{ + dlclose(libgl); +} + +static GL3WglProc get_proc(const char *proc) +{ + GL3WglProc res; + + res = glx_get_proc_address((const GLubyte *)proc); + if (!res) + *(void **)(&res) = dlsym(libgl, proc); + return res; +} +#endif + +static struct { + int major, minor; +} version; + +static int parse_version(void) +{ + if (!glGetIntegerv) + return GL3W_ERROR_INIT; + + glGetIntegerv(GL_MAJOR_VERSION, &version.major); + glGetIntegerv(GL_MINOR_VERSION, &version.minor); + + if (version.major < 3) + return GL3W_ERROR_OPENGL_VERSION; + return GL3W_OK; +} + +static void load_procs(GL3WGetProcAddressProc proc); + +int gl3wInit(void) +{ + int res; + + res = open_libgl(); + if (res) + return res; + + atexit(close_libgl); + return gl3wInit2(get_proc); +} + +int gl3wInit2(GL3WGetProcAddressProc proc) +{ + load_procs(proc); + return parse_version(); +} + +int gl3wIsSupported(int major, int minor) +{ + if (major < 3) + return 0; + if (version.major == major) + return version.minor >= minor; + return version.major >= major; +} + +GL3WglProc gl3wGetProcAddress(const char *proc) +{ + return get_proc(proc); +} + +static const char *proc_names[] = { +}; + +GL3W_API union GL3WProcs gl3wProcs; + +static void load_procs(GL3WGetProcAddressProc proc) +{ + size_t i; + + for (i = 0; i < ARRAY_SIZE(proc_names); i++) + gl3wProcs.ptr[i] = proc(proc_names[i]); +} diff --git a/template/gl3w.h b/template/gl3w.h new file mode 100644 index 0000000..9ff1bda --- /dev/null +++ b/template/gl3w.h @@ -0,0 +1,70 @@ +/* + * This file was generated with gl3w_gen.py, part of gl3w + * (hosted at https://github.com/skaslev/gl3w) + * + * This is free and unencumbered software released into the public domain. + * + * Anyone is free to copy, modify, publish, use, compile, sell, or + * distribute this software, either in source code form or as a compiled + * binary, for any purpose, commercial or non-commercial, and by any + * means. + * + * In jurisdictions that recognize copyright laws, the author or authors + * of this software dedicate any and all copyright interest in the + * software to the public domain. We make this dedication for the benefit + * of the public at large and to the detriment of our heirs and + * successors. We intend this dedication to be an overt act of + * relinquishment in perpetuity of all present and future rights to this + * software under copyright law. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR + * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + */ + +#ifndef __gl3w_h_ +#define __gl3w_h_ + +#include + +#ifndef GL3W_API +#define GL3W_API +#endif + +#ifndef __gl_h_ +#define __gl_h_ +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +#define GL3W_OK 0 +#define GL3W_ERROR_INIT -1 +#define GL3W_ERROR_LIBRARY_OPEN -2 +#define GL3W_ERROR_OPENGL_VERSION -3 + +typedef void (*GL3WglProc)(void); +typedef GL3WglProc (*GL3WGetProcAddressProc)(const char *proc); + +/* gl3w api */ +GL3W_API int gl3wInit(void); +GL3W_API int gl3wInit2(GL3WGetProcAddressProc proc); +GL3W_API int gl3wIsSupported(int major, int minor); +GL3W_API GL3WglProc gl3wGetProcAddress(const char *proc); + +/* gl3w internal state */ + +GL3W_API extern union GL3WProcs gl3wProcs; + +/* OpenGL functions */ + +#ifdef __cplusplus +} +#endif + +#endif