DEADSOFTWARE

Use SDL for GetProcAddress
[nanogl.git] / nanogl.cpp
1 /*
2 Copyright (C) 2007-2009 Olli Hinkka
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
21 #define LOG_TAG "nanoGL"
23 #include <stdio.h>
24 #include <stdlib.h>
27 //#include <cutils/log.h>
29 #include "nanogl.h"
30 #include "glesinterface.h"
31 #include "gl.h"
34 #define DEBUG_NANO 0
36 #ifdef __ANDROID__
37 #include <android/log.h>
38 #define LOG __android_log_print
40 #define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
41 #define LOGD(...) if (DEBUG_NANO) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)
42 #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG,__VA_ARGS__)
43 #define LOGW(...) __android_log_print(ANDROID_LOG_WARN, LOG_TAG,__VA_ARGS__)
44 #else
45 #ifndef _MSC_VER
46 #define LOGI(...) printf("I: "__VA_ARGS__);printf("\n")
47 #define LOGD(...) if(DEBUG_NANO) {printf("D: "__VA_ARGS__);printf("\n");}
48 #define LOGE(...) printf("E: "__VA_ARGS__);printf("\n")
49 #define LOGW(...) printf("W: "__VA_ARGS__);printf("\n")
50 #else
51 #define LOGI printf
52 #define LOGD printf
53 #define LOGE printf
54 #define LOGW printf
56 #endif
57 #endif
59 #ifdef _WIN32
60 #include <windows.h>
61 #define dlopen(x,y) LoadLibraryA(x)
62 #define dlsym(x,y) (void*)GetProcAddress((HINSTANCE)x,y)
63 #define dlclose(x) FreeLibrary((HINSTANCE)x)
64 #else
65 #include <dlfcn.h>
66 #endif
69 //#define GL_ENTRY(_r, _api, ...) #_api,
71 static char const * const gl_names[] = {
72 #include "funcnames.h"
73 NULL
74 };
76 //const char * driver;
78 static void* glesLib = NULL;
80 GlESInterface* glEsImpl = NULL;
82 extern void InitGLStructs();
84 void APIENTRY gl_unimplemented(GLenum none) {
85 #ifndef USE_CORE_PROFILE
86 LOGE ("Called unimplemented OpenGL ES API\n");
87 #endif
88 }
90 void *nanoGL_GetProcAddress(const char *name)
91 {
92 void *addr = NULL;
93 #ifdef XASH_SDL
94 addr = SDL_GL_GetProcAddress( name );
95 if( !addr )
96 #endif
97 addr = dlsym(glesLib, name);
98 return addr;
99 }
101 static int CreateGlEsInterface( const char * name, void * lib, void * lib1, void * default_func )
103 // alloc space
104 if ( !glEsImpl )
105 glEsImpl = (GlESInterface *) malloc(sizeof(GlESInterface));
107 if (!glEsImpl) {
108 return 0;
111 // load GL API calls
112 char const * const * api;
113 api = gl_names;
115 // nanoGL interface pointer
116 void ** ptr = (void **)(glEsImpl);
118 while (*api)
120 void * f;
122 f = dlsym(lib, *api); // try libGLESxx_CM.so
124 #ifdef USE_CORE_PROFILE
125 // Hack: try ARB and EXT suffix
126 if (f == NULL) {
127 char namearb[256];
128 snprintf( namearb, 256, "%sARB", *api );
129 f = dlsym( lib, namearb );
131 if (f == NULL) {
132 char namearb[256];
133 snprintf( namearb, 256, "%sEXT", *api );
134 f = dlsym( lib, namearb );
136 #endif
137 if (f == NULL) {
138 LOGW( "<%s> not found in %s. Trying libEGL.so.", *api, name); //driver);
140 // try lib1
141 if ( lib1 ) {
142 f = dlsym(lib1, *api); // libEGL.so
144 if ( f == NULL ) {
145 LOGE ( "<%s> not found in libEGL.so", *api);
146 f = default_func; //(void*)gl_unimplemented;
148 else {
149 LOGD ("<%s> @ 0x%p\n", *api, f);
152 else
154 LOGE ( "libEGL.so not loaded!");
155 f = default_func;
158 else {
159 LOGD ("<%s> @ 0x%p\n", *api, f);
162 *ptr++ = f;
163 api++;
164 }
166 return 1;
169 // Load using the dynamic loader
170 static int loadDriver(const char * name) {
171 glesLib = dlopen(name, RTLD_NOW | RTLD_LOCAL);
172 int rc = (glesLib) ? 1 : 0;
173 return rc;
176 /**
177 * Init
178 */
179 #ifdef _WIN32
180 int nanoGL_Init()
182 const char * lib1 = "opengl32.dll"; // Has both gl* & egl* funcs SDK < 1.5
183 const char * lib2 = "opengl32.dll"; // Only gl* funcs SDK >= 1.5
184 const char * lib3 = "opengl32.dll"; // Only egl* funcs SDK >= 1.5
185 const char * driver;
187 // load lib
188 LOGI("nanoGL: Init loading driver %s\n", lib1);
189 //LOG (ANDROID_LOG_DEBUG, LOG_TAG, "nanoGL: Init loading driver %s\n", lib1);
191 if ( ! loadDriver(lib1) )
193 LOGE("Failed to load driver %s. Trying %s\n", lib1, lib2);
195 if ( ! loadDriver(lib2) ) {
196 LOGE ("Failed to load %s.\n", lib2);
197 return 0;
199 else
200 driver = lib2;
202 else
203 driver = lib1;
205 void * eglLib;
207 //if ( strcmp(driver, lib2) == 0 ) {
208 LOGD ("**** Will Load EGL subs from %s ****", lib3);
210 eglLib = dlopen(lib3, RTLD_NOW | RTLD_LOCAL);
212 if ( ! eglLib ) {
213 LOGE ( "Failed to load %s", lib3);
215 //}
217 // Load API gl* for 1.5+ else egl* gl*
218 //if (CreateGlEsInterface(driver, glesLib, eglLib, NULL) == -1)
219 if ( !CreateGlEsInterface(driver, glesLib, eglLib, (void *) gl_unimplemented) == -1)
221 // release lib
222 LOGE ( "CreateGlEsInterface failed.");
224 dlclose(glesLib);
225 return 0;
228 // Init nanoGL
229 InitGLStructs();
230 return 1;
232 #else
233 int nanoGL_Init()
235 const char * lib1 = "libGLESv1_CM.so"; // Has both gl* & egl* funcs SDK < 1.5
236 const char * lib2 = "libGLESv2.so"; // Only gl* funcs SDK >= 1.5
237 const char * lib3 = "libEGL.so"; // Only egl* funcs SDK >= 1.5
238 const char * driver;
240 // load lib
241 LOGI("nanoGL: Init loading driver %s\n", lib1);
242 //LOG (ANDROID_LOG_DEBUG, LOG_TAG, "nanoGL: Init loading driver %s\n", lib1);
244 if ( ! loadDriver(lib1) )
246 LOGE("Failed to load driver %s. Trying %s\n", lib1, lib2);
248 if ( ! loadDriver(lib2) ) {
249 LOGE ("Failed to load %s.\n", lib2);
250 return 0;
252 else
253 driver = lib2;
255 else
256 driver = lib1;
258 void * eglLib;
260 //if ( strcmp(driver, lib2) == 0 ) {
261 LOGD ("**** Will Load EGL subs from %s ****", lib3);
263 eglLib = dlopen(lib3, RTLD_NOW | RTLD_LOCAL);
265 if ( ! eglLib ) {
266 LOGE ( "Failed to load %s", lib3);
268 //}
270 // Load API gl* for 1.5+ else egl* gl*
271 //if (CreateGlEsInterface(driver, glesLib, eglLib, NULL) == -1)
272 if ( !CreateGlEsInterface(driver, glesLib, eglLib, (void *) gl_unimplemented) == -1)
274 // release lib
275 LOGE ( "CreateGlEsInterface failed.");
277 dlclose(glesLib);
278 return 0;
281 // Init nanoGL
282 InitGLStructs();
283 return 1;
285 #endif
286 void nanoGL_Destroy()
288 LOGD ("nanoGL_Destroy");
290 if (glEsImpl) {
291 free( glEsImpl);
292 glEsImpl = NULL;
295 // release lib
296 dlclose(glesLib);