DEADSOFTWARE

Force SDL for early iOS port
[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>
26 //#include <cutils/log.h>
28 #include "nanogl.h"
29 #include "glesinterface.h"
30 #include "gl.h"
32 #define DEBUG_NANO 0
34 #ifdef __ANDROID__
35 #include <android/log.h>
36 #define LOG __android_log_print
38 #define LOGI( ... ) __android_log_print( ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__ )
39 #define LOGD( ... ) \
40 if ( DEBUG_NANO ) \
41 __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( ... ) \
47 printf( "I: "__VA_ARGS__ ); \
48 printf( "\n" )
49 #define LOGD( ... ) \
50 if ( DEBUG_NANO ) \
51 { \
52 printf( "D: "__VA_ARGS__ ); \
53 printf( "\n" ); \
54 }
55 #define LOGE( ... ) \
56 printf( "E: "__VA_ARGS__ ); \
57 printf( "\n" )
58 #define LOGW( ... ) \
59 printf( "W: "__VA_ARGS__ ); \
60 printf( "\n" )
61 #else
62 #define LOGI printf
63 #define LOGD printf
64 #define LOGE printf
65 #define LOGW printf
67 #endif
68 #endif
70 #ifdef _WIN32
71 #include <windows.h>
72 #define dlopen( x, y ) LoadLibraryA( x )
73 #define dlsym( x, y ) ( void * ) GetProcAddress( (HINSTANCE)x, y )
74 #define dlclose( x ) FreeLibrary( (HINSTANCE)x )
75 #else
76 #include <dlfcn.h>
77 #endif
79 //#define GL_ENTRY(_r, _api, ...) #_api,
81 static char const *const gl_names[] = {
82 #include "funcnames.h"
83 NULL};
85 //const char * driver;
87 static void *glesLib = NULL;
89 extern "C++" GlESInterface *glEsImpl;
90 GlESInterface *glEsImpl = NULL;
92 extern void InitGLStructs( );
94 void APIENTRY gl_unimplemented( GLenum none )
95 {
96 #ifndef USE_CORE_PROFILE
97 LOGE( "Called unimplemented OpenGL ES API\n" );
98 #endif
99 }
101 #ifdef XASH_SDL
102 #include "SDL.h"
103 #endif
105 void *nanoGL_GetProcAddress( const char *name )
107 void *addr = NULL;
108 #ifdef XASH_SDL
109 addr = SDL_GL_GetProcAddress( name );
110 if ( !addr )
111 #endif
112 addr = dlsym( glesLib, name );
113 return addr;
116 static int CreateGlEsInterface( const char *name, void *lib, void *lib1, void *default_func )
118 // alloc space
119 if ( !glEsImpl )
120 glEsImpl = (GlESInterface *)malloc( sizeof( GlESInterface ) );
122 if ( !glEsImpl )
124 return 0;
127 // load GL API calls
128 char const *const *api;
129 api = gl_names;
131 // nanoGL interface pointer
132 void **ptr = (void **)( glEsImpl );
134 while ( *api )
136 void *f;
138 f = SDL_GL_GetProcAddress(*api);//dlsym( lib, *api ); // try libGLESxx_CM.so
140 #ifdef USE_CORE_PROFILE
141 // Hack: try ARB and EXT suffix
142 if ( f == NULL )
144 char namearb[256];
145 snprintf( namearb, 256, "%sARB", *api );
146 f = dlsym( lib, namearb );
148 if ( f == NULL )
150 char namearb[256];
151 snprintf( namearb, 256, "%sEXT", *api );
152 f = dlsym( lib, namearb );
154 #endif
155 /*if ( f == NULL )
157 LOGW( "<%s> not found in %s. Trying libEGL.so.", *api, name ); //driver);
159 // try lib1
160 if ( lib1 )
162 f = dlsym( lib1, *api ); // libEGL.so
164 if ( f == NULL )
166 LOGE( "<%s> not found in libEGL.so", *api );
167 if ( glEsImpl->eglGetProcAddress && ( (void *)glEsImpl->eglGetProcAddress != (void *)gl_unimplemented ) )
168 f = (void *)glEsImpl->eglGetProcAddress( *api );
169 if ( f == NULL )
170 f = (void *)default_func; //(void*)gl_unimplemented;
172 else
174 LOGD( "<%s> @ 0x%p\n", *api, f );
177 else
179 LOGE( "libEGL.so not loaded!" );
180 if ( glEsImpl->eglGetProcAddress && ( (void *)glEsImpl->eglGetProcAddress != (void *)gl_unimplemented ) )
181 f = (void *)glEsImpl->eglGetProcAddress( *api );
182 if ( !f )
183 f = (void *)default_func;
186 else*/
188 LOGD( "<%s> @ 0x%p\n", *api, f );
191 *ptr++ = f;
192 api++;
195 return 1;
198 // Load using the dynamic loader
199 static int loadDriver( const char *name )
201 glesLib = dlopen( name, RTLD_NOW | RTLD_LOCAL );
202 int rc = ( glesLib ) ? 1 : 0;
203 return rc;
206 /**
207 * Init
208 */
209 #ifdef _WIN32
210 int nanoGL_Init( )
212 const char *lib1 = "opengl32.dll"; // Has both gl* & egl* funcs SDK < 1.5
213 const char *lib2 = "opengl32.dll"; // Only gl* funcs SDK >= 1.5
214 const char *lib3 = "opengl32.dll"; // Only egl* funcs SDK >= 1.5
215 const char *driver;
217 // load lib
218 LOGI( "nanoGL: Init loading driver %s\n", lib1 );
219 //LOG (ANDROID_LOG_DEBUG, LOG_TAG, "nanoGL: Init loading driver %s\n", lib1);
221 if ( !loadDriver( lib1 ) )
223 LOGE( "Failed to load driver %s. Trying %s\n", lib1, lib2 );
225 if ( !loadDriver( lib2 ) )
227 LOGE( "Failed to load %s.\n", lib2 );
228 return 0;
230 else
231 driver = lib2;
233 else
234 driver = lib1;
236 void *eglLib;
238 //if ( strcmp(driver, lib2) == 0 ) {
239 LOGD( "**** Will Load EGL subs from %s ****", lib3 );
241 eglLib = dlopen( lib3, RTLD_NOW | RTLD_LOCAL );
243 if ( !eglLib )
245 LOGE( "Failed to load %s", lib3 );
247 //}
249 // Load API gl* for 1.5+ else egl* gl*
250 //if (CreateGlEsInterface(driver, glesLib, eglLib, NULL) == -1)
251 if ( !CreateGlEsInterface( driver, glesLib, eglLib, (void *)gl_unimplemented ) == -1 )
253 // release lib
254 LOGE( "CreateGlEsInterface failed." );
256 dlclose( glesLib );
257 return 0;
260 // Init nanoGL
261 InitGLStructs( );
262 return 1;
264 #else
265 int nanoGL_Init( )
267 const char *lib1 = "libGLESv1_CM.so"; // Has both gl* & egl* funcs SDK < 1.5
268 const char *lib2 = "libGLESv2.so"; // Only gl* funcs SDK >= 1.5
269 const char *lib3 = "libEGL.so"; // Only egl* funcs SDK >= 1.5
270 const char *driver = NULL;
272 // load lib
273 LOGI( "nanoGL: Init loading driver %s\n", lib1 );
274 //LOG (ANDROID_LOG_DEBUG, LOG_TAG, "nanoGL: Init loading driver %s\n", lib1);
276 /*if ( !loadDriver( lib1 ) )
278 LOGE( "Failed to load driver %s. Trying %s\n", lib1, lib2 );
280 if ( !loadDriver( lib2 ) )
282 LOGE( "Failed to load %s.\n", lib2 );
283 return 0;
285 else
286 driver = lib2;
288 else
289 driver = lib1;*/
291 void *eglLib;
293 //if ( strcmp(driver, lib2) == 0 ) {
294 LOGD( "**** Will Load EGL subs from %s ****", lib3 );
296 eglLib = dlopen( lib3, RTLD_NOW | RTLD_LOCAL );
298 if ( !eglLib )
300 LOGE( "Failed to load %s", lib3 );
302 //}
304 // Load API gl* for 1.5+ else egl* gl*
305 //if (CreateGlEsInterface(driver, glesLib, eglLib, NULL) == -1)
306 if ( !CreateGlEsInterface( driver, 0, 0, (void *)gl_unimplemented ) == -1 )
308 // release lib
309 LOGE( "CreateGlEsInterface failed." );
311 dlclose( glesLib );
312 return 0;
315 #ifdef __ANDROID__
316 // somewhy it does not initialize correctly
317 *( (void **)&glEsImpl->glGenFramebuffers ) = (void *)glEsImpl->eglGetProcAddress( "glGenFramebuffersOES" );
318 *( (void **)&glEsImpl->glGenRenderbuffers ) = (void *)glEsImpl->eglGetProcAddress( "glGenRenderbuffersOES" );
319 *( (void **)&glEsImpl->glRenderbufferStorage ) = (void *)glEsImpl->eglGetProcAddress( "glRenderbufferStorageOES" );
320 *( (void **)&glEsImpl->glBindFramebuffer ) = (void *)glEsImpl->eglGetProcAddress( "glBindFramebufferOES" );
321 *( (void **)&glEsImpl->glBindRenderbuffer ) = (void *)glEsImpl->eglGetProcAddress( "glBindRenderbufferOES" );
322 *( (void **)&glEsImpl->glFramebufferTexture2D ) = (void *)glEsImpl->eglGetProcAddress( "glFramebufferTexture2DOES" );
323 *( (void **)&glEsImpl->glDeleteRenderbuffers ) = (void *)glEsImpl->eglGetProcAddress( "glDeleteRenderbuffersOES" );
324 *( (void **)&glEsImpl->glDeleteFramebuffers ) = (void *)glEsImpl->eglGetProcAddress( "glDeleteFramebuffersOES" );
325 *( (void **)&glEsImpl->glFramebufferRenderbuffer ) = (void *)glEsImpl->eglGetProcAddress( "glFramebufferRenderbufferOES" );
326 #endif
328 // Init nanoGL
329 InitGLStructs( );
330 return 1;
332 #endif
333 void nanoGL_Destroy( )
335 LOGD( "nanoGL_Destroy" );
337 if ( glEsImpl )
339 free( glEsImpl );
340 glEsImpl = NULL;
343 // release lib
344 dlclose( glesLib );