wgl.py (3324B)
1 from glad.lang.common.loader import BaseLoader 2 from glad.lang.c.loader import LOAD_OPENGL_DLL, LOAD_OPENGL_DLL_H, LOAD_OPENGL_GLAPI_H 3 4 _WGL_LOADER = \ 5 LOAD_OPENGL_DLL % {'pre':'static', 'init':'open_gl', 6 'proc':'get_proc', 'terminate':'close_gl'} + ''' 7 int gladLoadWGL(HDC hdc) { 8 int status = 0; 9 10 if(open_gl()) { 11 status = gladLoadWGLLoader((GLADloadproc)get_proc, hdc); 12 close_gl(); 13 } 14 15 return status; 16 } 17 ''' 18 19 _WGL_HEADER_START = ''' 20 #ifndef WINAPI 21 # ifndef WIN32_LEAN_AND_MEAN 22 # define WIN32_LEAN_AND_MEAN 1 23 # endif 24 # include <windows.h> 25 #endif 26 27 ''' 28 29 #include <glad/glad.h> 30 31 _WGL_HEADER_MID = ''' 32 #ifndef __glad_wglext_h_ 33 34 #ifdef __wglext_h_ 35 #error WGL header already included, remove this include, glad already provides it 36 #endif 37 38 #define __glad_wglext_h_ 39 #define __wglext_h_ 40 41 #ifndef APIENTRY 42 #define APIENTRY 43 #endif 44 #ifndef APIENTRYP 45 #define APIENTRYP APIENTRY * 46 #endif 47 48 #ifdef __cplusplus 49 extern "C" { 50 #endif 51 52 typedef void* (* GLADloadproc)(const char *name); 53 ''' + LOAD_OPENGL_GLAPI_H 54 55 _WGL_HEADER_LOADER = ''' 56 GLAPI int gladLoadWGL(HDC hdc); 57 ''' + LOAD_OPENGL_DLL_H 58 59 _WGL_HEADER_END = ''' 60 #ifdef __cplusplus 61 } 62 #endif 63 64 #endif 65 ''' 66 67 _WGL_HAS_EXT = ''' 68 static HDC GLADWGLhdc = (HDC)INVALID_HANDLE_VALUE; 69 70 static int get_exts(void) { 71 return 1; 72 } 73 74 static void free_exts(void) { 75 return; 76 } 77 78 static int has_ext(const char *ext) { 79 const char *terminator; 80 const char *loc; 81 const char *extensions; 82 83 if(wglGetExtensionsStringEXT == NULL && wglGetExtensionsStringARB == NULL) 84 return 0; 85 86 if(wglGetExtensionsStringARB == NULL || GLADWGLhdc == INVALID_HANDLE_VALUE) 87 extensions = wglGetExtensionsStringEXT(); 88 else 89 extensions = wglGetExtensionsStringARB(GLADWGLhdc); 90 91 if(extensions == NULL || ext == NULL) 92 return 0; 93 94 while(1) { 95 loc = strstr(extensions, ext); 96 if(loc == NULL) 97 break; 98 99 terminator = loc + strlen(ext); 100 if((loc == extensions || *(loc - 1) == ' ') && 101 (*terminator == ' ' || *terminator == '\\0')) 102 { 103 return 1; 104 } 105 extensions = terminator; 106 } 107 108 return 0; 109 } 110 ''' 111 112 class WGLCLoader(BaseLoader): 113 def write(self, fobj): 114 if not self.disabled: 115 fobj.write(_WGL_LOADER) 116 117 def write_begin_load(self, fobj): 118 fobj.write('\twglGetExtensionsStringARB = (PFNWGLGETEXTENSIONSSTRINGARBPROC)load("wglGetExtensionsStringARB");\n') 119 fobj.write('\twglGetExtensionsStringEXT = (PFNWGLGETEXTENSIONSSTRINGEXTPROC)load("wglGetExtensionsStringEXT");\n') 120 fobj.write('\tif(wglGetExtensionsStringARB == NULL && wglGetExtensionsStringEXT == NULL) return 0;\n') 121 122 def write_end_load(self, fobj): 123 fobj.write('\treturn 1;\n') 124 125 def write_find_core(self, fobj): 126 fobj.write('\tGLADWGLhdc = hdc;\n') 127 128 def write_has_ext(self, fobj): 129 fobj.write(_WGL_HAS_EXT) 130 131 def write_header(self, fobj): 132 fobj.write(_WGL_HEADER_START) 133 if self.local_files: 134 fobj.write('#include "glad.h"\n') 135 else: 136 fobj.write('#include <glad/glad.h>\n') 137 fobj.write(_WGL_HEADER_MID) 138 if not self.disabled: 139 fobj.write(_WGL_HEADER_LOADER) 140 141 def write_header_end(self, fobj): 142 fobj.write(_WGL_HEADER_END)