DEADSOFTWARE

Implement some wrappers for xashxt
[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 if( glEsImpl->eglGetProcAddress && ( (void*)glEsImpl->eglGetProcAddress != (void*)gl_unimplemented ) )
147 f = glEsImpl->eglGetProcAddress( *api );
148 if(f == NULL)
149 f = default_func; //(void*)gl_unimplemented;
152 else {
153 LOGD ("<%s> @ 0x%p\n", *api, f);
156 else
158 LOGE ( "libEGL.so not loaded!");
159 if( glEsImpl->eglGetProcAddress && ( (void*)glEsImpl->eglGetProcAddress != (void*)gl_unimplemented ) )
160 f = glEsImpl->eglGetProcAddress( *api );
161 if( !f )
162 f = default_func;
165 else {
166 LOGD ("<%s> @ 0x%p\n", *api, f);
169 *ptr++ = f;
170 api++;
171 }
173 return 1;
176 // Load using the dynamic loader
177 static int loadDriver(const char * name) {
178 glesLib = dlopen(name, RTLD_NOW | RTLD_LOCAL);
179 int rc = (glesLib) ? 1 : 0;
180 return rc;
183 /**
184 * Init
185 */
186 #ifdef _WIN32
187 int nanoGL_Init()
189 const char * lib1 = "opengl32.dll"; // Has both gl* & egl* funcs SDK < 1.5
190 const char * lib2 = "opengl32.dll"; // Only gl* funcs SDK >= 1.5
191 const char * lib3 = "opengl32.dll"; // Only egl* funcs SDK >= 1.5
192 const char * driver;
194 // load lib
195 LOGI("nanoGL: Init loading driver %s\n", lib1);
196 //LOG (ANDROID_LOG_DEBUG, LOG_TAG, "nanoGL: Init loading driver %s\n", lib1);
198 if ( ! loadDriver(lib1) )
200 LOGE("Failed to load driver %s. Trying %s\n", lib1, lib2);
202 if ( ! loadDriver(lib2) ) {
203 LOGE ("Failed to load %s.\n", lib2);
204 return 0;
206 else
207 driver = lib2;
209 else
210 driver = lib1;
212 void * eglLib;
214 //if ( strcmp(driver, lib2) == 0 ) {
215 LOGD ("**** Will Load EGL subs from %s ****", lib3);
217 eglLib = dlopen(lib3, RTLD_NOW | RTLD_LOCAL);
219 if ( ! eglLib ) {
220 LOGE ( "Failed to load %s", lib3);
222 //}
224 // Load API gl* for 1.5+ else egl* gl*
225 //if (CreateGlEsInterface(driver, glesLib, eglLib, NULL) == -1)
226 if ( !CreateGlEsInterface(driver, glesLib, eglLib, (void *) gl_unimplemented) == -1)
228 // release lib
229 LOGE ( "CreateGlEsInterface failed.");
231 dlclose(glesLib);
232 return 0;
235 // Init nanoGL
236 InitGLStructs();
237 return 1;
239 #else
240 int nanoGL_Init()
242 const char * lib1 = "libGLESv1_CM.so"; // Has both gl* & egl* funcs SDK < 1.5
243 const char * lib2 = "libGLESv2.so"; // Only gl* funcs SDK >= 1.5
244 const char * lib3 = "libEGL.so"; // Only egl* funcs SDK >= 1.5
245 const char * driver;
247 // load lib
248 LOGI("nanoGL: Init loading driver %s\n", lib1);
249 //LOG (ANDROID_LOG_DEBUG, LOG_TAG, "nanoGL: Init loading driver %s\n", lib1);
251 if ( ! loadDriver(lib1) )
253 LOGE("Failed to load driver %s. Trying %s\n", lib1, lib2);
255 if ( ! loadDriver(lib2) ) {
256 LOGE ("Failed to load %s.\n", lib2);
257 return 0;
259 else
260 driver = lib2;
262 else
263 driver = lib1;
265 void * eglLib;
267 //if ( strcmp(driver, lib2) == 0 ) {
268 LOGD ("**** Will Load EGL subs from %s ****", lib3);
270 eglLib = dlopen(lib3, RTLD_NOW | RTLD_LOCAL);
272 if ( ! eglLib ) {
273 LOGE ( "Failed to load %s", lib3);
275 //}
277 // Load API gl* for 1.5+ else egl* gl*
278 //if (CreateGlEsInterface(driver, glesLib, eglLib, NULL) == -1)
279 if ( !CreateGlEsInterface(driver, glesLib, eglLib, (void *) gl_unimplemented) == -1)
281 // release lib
282 LOGE ( "CreateGlEsInterface failed.");
284 dlclose(glesLib);
285 return 0;
288 // Init nanoGL
289 InitGLStructs();
290 return 1;
292 #endif
293 void nanoGL_Destroy()
295 LOGD ("nanoGL_Destroy");
297 if (glEsImpl) {
298 free( glEsImpl);
299 glEsImpl = NULL;
302 // release lib
303 dlclose(glesLib);