]> git.jsancho.org Git - lugaru.git/commitdiff
Dynamically load the GL.
authorRyan C. Gordon <icculus@icculus.org>
Sat, 6 Aug 2005 23:43:44 +0000 (23:43 +0000)
committerRyan C. Gordon <icculus@icculus.org>
Sat, 6 Aug 2005 23:43:44 +0000 (23:43 +0000)
Source/OpenGL_Windows.cpp
Source/glstubs.h [new file with mode: 0644]
makefile

index 77f0c305162f129d027c723b9238c3f55c2d5cd3..15073ce19546940855d9e54b1a598907c24bcdaf 100644 (file)
@@ -134,6 +134,43 @@ typedef struct tagPOINT {
 #endif
 
 #if USE_SDL
+#define GL_FUNC(ret,fn,params,call,rt) \
+    extern "C" { \
+        static ret GLAPIENTRY (*p##fn) params = NULL; \
+        ret GLAPIENTRY fn params { rt p##fn call; } \
+    }
+#include "glstubs.h"
+#undef GL_FUNC
+
+static bool lookup_glsym(const char *funcname, void **func, const char *libname)
+{
+    *func = SDL_GL_GetProcAddress(funcname);
+    if (*func == NULL)
+    {
+        fprintf(stderr, "Failed to find OpenGL symbol \"%s\" in \"%s\"\n",
+                 funcname, libname);
+        return false;
+    }
+    return true;
+}
+
+static bool lookup_all_glsyms(const char *libname)
+{
+    bool retval = true;
+    #define GL_FUNC(ret,fn,params,call,rt) \
+        if (!lookup_glsym(#fn, (void **) &p##fn, libname)) retval = false;
+    #include "glstubs.h"
+    #undef GL_FUNC
+    return retval;
+}
+
+static void GLAPIENTRY glDeleteTextures_doNothing(GLsizei n, const GLuint *textures)
+{
+    // no-op.
+}
+
+
+
 void sdlGetCursorPos(POINT *pt)
 {
     int x, y;
@@ -798,9 +835,20 @@ Boolean SetUp (Game & game)
             fprintf(stderr, "SDL_Init() failed: %s\n", SDL_GetError());
             return false;
         }
+
+        const char *libname = "libGL.so.1";  // !!! FIXME: Linux specific!
+        if (SDL_GL_LoadLibrary(libname) == -1)
+        {
+            fprintf(stderr, "SDL_GL_LoadLibrary(\"%s\") failed: %s\n",
+                    libname, SDL_GetError());
+            return false;
+        }
+
+        if (!lookup_all_glsyms(libname))
+            return false;
     }
 
-    Uint32 sdlflags = SDL_OPENGL;
+    Uint32 sdlflags = SDL_OPENGL;  // !!! FIXME: SDL_FULLSCREEN?
     SDL_WM_SetCaption("Lugaru", "lugaru");
     SDL_ShowCursor(0);
     if (SDL_SetVideoMode(kContextWidth, kContextHeight, 0, sdlflags) == NULL)
@@ -809,6 +857,8 @@ Boolean SetUp (Game & game)
         return false;
     }
 
+
+
 #elif (defined WIN32)
        //------------------------------------------------------------------
        // create window
@@ -1183,6 +1233,12 @@ void CleanUp (void)
 
 #if USE_SDL
     SDL_Quit();
+    #define GL_FUNC(ret,fn,params,call,rt) p##fn = NULL;
+    #include "glstubs.h"
+    #undef GL_FUNC
+    // cheat here...static destructors are calling glDeleteTexture() after
+    //  the context is destroyed and libGL unloaded by SDL_Quit().
+    pglDeleteTextures = glDeleteTextures_doNothing;
 
 #elif (defined WIN32)
        if (hRC)
@@ -1254,7 +1310,8 @@ int main (void)
                        //ofstream os("log.txt");
                        //os.close();
 
-                       SetUp (game);
+                       if (!SetUp (game))
+                return 42;
 
                        while (!gDone&&!game.quit&&(!game.tryquit||!game.registered))
                        {
diff --git a/Source/glstubs.h b/Source/glstubs.h
new file mode 100644 (file)
index 0000000..7512a6f
--- /dev/null
@@ -0,0 +1,80 @@
+GL_FUNC(void,glAlphaFunc,(GLenum f,GLclampf x),(f,x),)
+GL_FUNC(void,glBegin,(GLenum e),(e),)
+GL_FUNC(void,glBindTexture,(GLenum target,GLuint name),(target,name),)
+GL_FUNC(void,glBlendFunc,(GLenum f,GLenum x),(f,x),)
+GL_FUNC(void,glCallLists,(GLsizei a,GLenum b,const GLvoid* c),(a,b,c),)
+GL_FUNC(void,glClear,(GLbitfield a),(a),)
+GL_FUNC(void,glClearColor,(GLclampf r,GLclampf g,GLclampf b,GLclampf a),(r,g,b,a),)
+GL_FUNC(void,glClearDepth,(GLclampd x),(x),)
+GL_FUNC(void,glColor3f,(GLfloat r,GLfloat g,GLfloat b),(r,g,b),)
+GL_FUNC(void,glColor4f,(GLfloat r,GLfloat g,GLfloat b,GLfloat a),(r,g,b,a),)
+GL_FUNC(void,glColorMask,(GLboolean red, GLboolean green, GLboolean blue, GLboolean alpha),(red,green,blue,alpha),)
+GL_FUNC(void,glColorPointer,(GLint size, GLenum type, GLsizei stride, const GLvoid *pointer),(size, type, stride, pointer),)
+GL_FUNC(void,glCopyTexImage2D,(GLenum target, GLint level, GLenum internalFormat, GLint x, GLint y, GLsizei width, GLsizei height, GLint border),(target, level, internalFormat, x, y, width, height, border),)
+GL_FUNC(void,glCopyTexSubImage2D,(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height),(target, level, xoffset, yoffset, x, y, width, height),)
+GL_FUNC(void,glCullFace,(GLenum mode),(mode),)
+GL_FUNC(void,glDeleteLists,(GLuint list, GLsizei range),(list,range),)
+GL_FUNC(void,glDeleteTextures,(GLsizei n, const GLuint *textures),(n,textures),)
+GL_FUNC(void,glDepthFunc,(GLenum func),(func),)
+GL_FUNC(void,glDepthMask,(GLboolean flag),(flag),)
+GL_FUNC(void,glDisable,(GLenum cap),(cap),)
+GL_FUNC(void,glDisableClientState,(GLenum array),(array),)
+GL_FUNC(void,glDrawArrays,(GLenum mode, GLint first, GLsizei count),(mode,first,count),)
+GL_FUNC(void,glDrawBuffer,(GLenum mode),(mode),)
+GL_FUNC(void,glEnable,(GLenum cap),(cap),)
+GL_FUNC(void,glEnableClientState,(GLenum array),(array),)
+GL_FUNC(void,glEnd,(void),(),)
+GL_FUNC(void,glEndList,(void),(),)
+GL_FUNC(GLuint,glGenLists,(GLsizei range),(range),return)
+GL_FUNC(void,glGenTextures,(GLsizei n, GLuint *textures),(n,textures),)
+GL_FUNC(GLenum,glGetError,(void),(),return)
+GL_FUNC(void,glGetFloatv,(GLenum pname, GLfloat *params),(pname,params),)
+GL_FUNC(void,glHint,(GLenum target, GLenum mode),(target,mode),)
+GL_FUNC(void,glInterleavedArrays,(GLenum format, GLsizei stride, const GLvoid *pointer),(format,stride,pointer),)
+GL_FUNC(GLboolean,glIsTexture,(GLuint texture),(texture),return)
+GL_FUNC(void,glLightfv,(GLenum light, GLenum pname, const GLfloat *params),(light,pname,params),)
+GL_FUNC(void,glListBase,(GLuint base),(base),)
+GL_FUNC(void,glLoadIdentity,(void),(),)
+GL_FUNC(void,glLoadMatrixf,(const GLfloat *m),(m),)
+GL_FUNC(void,glMatrixMode,(GLenum mode),(mode),)
+GL_FUNC(void,glNewList,(GLuint list, GLenum mode),(list,mode),)
+GL_FUNC(void,glNormal3f,(GLfloat nx, GLfloat ny, GLfloat nz),(nx,ny,nz),)
+GL_FUNC(void,glOrtho,(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble zNear, GLdouble zFar),(left,right,bottom,top,zNear,zFar),)
+GL_FUNC(void,glPixelStorei,(GLenum pname, GLint param),(pname,param),)
+GL_FUNC(void,glPixelTransferi,(GLenum pname, GLint param),(pname,param),)
+GL_FUNC(void,glPointSize,(GLfloat size),(size),)
+GL_FUNC(void,glPopMatrix,(void),(),)
+GL_FUNC(void,glPushMatrix,(void),(),)
+GL_FUNC(void,glReadBuffer,(GLenum mode),(mode),)
+GL_FUNC(void,glReadPixels,(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLvoid *pixels),(x,y,width,height,format,type,pixels),)
+GL_FUNC(void,glRotatef,(GLfloat angle, GLfloat x, GLfloat y, GLfloat z),(angle,x,y,z),)
+GL_FUNC(void,glScalef,(GLfloat x, GLfloat y, GLfloat z),(x,y,z),)
+GL_FUNC(void,glShadeModel,(GLenum mode),(mode),)
+GL_FUNC(void,glTexCoord2f,(GLfloat s, GLfloat t),(s,t),)
+GL_FUNC(void,glTexCoordPointer,(GLint size, GLenum type, GLsizei stride, const GLvoid *pointer),(size,type,stride,pointer),)
+GL_FUNC(void,glTexEnvf,(GLenum target, GLenum pname, GLfloat param),(target,pname,param),)
+GL_FUNC(void,glTexEnvfv,(GLenum target, GLenum pname, const GLfloat *params),(target,pname,params),)
+GL_FUNC(void,glTexEnvi,(GLenum target, GLenum pname, GLint param),(target,pname,param),)
+GL_FUNC(void,glTexParameterf,(GLenum target, GLenum pname, GLfloat param),(target,pname,param),)
+GL_FUNC(void,glTexParameteri,(GLenum target, GLenum pname, GLint param),(target,pname,param),)
+GL_FUNC(void,glTexSubImage2D,(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, const GLvoid *pixels),(target,level,xoffset,yoffset,width,height,format,type,pixels),)
+GL_FUNC(void,glTranslated,(GLdouble x, GLdouble y, GLdouble z),(x,y,z),)
+GL_FUNC(void,glTranslatef,(GLfloat x, GLfloat y, GLfloat z),(x,y,z),)
+GL_FUNC(void,glVertex2i,(GLint x, GLint y),(x,y),)
+GL_FUNC(void,glVertex3f,(GLfloat x, GLfloat y, GLfloat z),(x,y,z),)
+GL_FUNC(void,glVertexPointer,(GLint size, GLenum type, GLsizei stride, const GLvoid *pointer),(size,type,stride,pointer),)
+GL_FUNC(void,glViewport,(GLint x, GLint y, GLsizei width, GLsizei height),(x,y,width,height),)
+GL_FUNC(void,glLockArraysEXT,(GLint first, GLsizei count),(first,count),)
+GL_FUNC(void,glUnlockArraysEXT,(void),(),)
+
+// stuff GLU needs...
+GL_FUNC(void,glGetIntegerv,(GLenum pname, GLint *params),(pname,params),)
+GL_FUNC(const GLubyte *,glGetString,(GLenum name),(name),return)
+GL_FUNC(void,glGetTexLevelParameteriv,(GLenum target, GLint level, GLenum pname, GLint *params),(target,level,pname,params),)
+GL_FUNC(void,glMultMatrixd,(const GLdouble *m),(m),)
+GL_FUNC(void,glMultMatrixf,(const GLfloat *m),(m),)
+GL_FUNC(void,glGetTexImage,(GLenum target, GLint level, GLenum format, GLenum type, GLvoid *pixels),(target,level,format,type,pixels),)
+GL_FUNC(void,glTexImage1D,(GLenum target, GLint level, GLint internalFormat, GLsizei width, GLint border, GLenum format, GLenum type, const GLvoid *pixels),(target,level,internalFormat,width,border,format,type,pixels),)
+GL_FUNC(void,glTexImage2D,(GLenum target, GLint level, GLint internalFormat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid *pixels),(target,level,internalFormat,width,height,border,format,type,pixels),)
+GL_FUNC(void,glTexImage3D,(GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLenum format, GLenum type, const GLvoid *pixels),(target,level,internalformat,width,height,depth,border,format,type,pixels),)
+
index 8672d2e2431c6de718b73e442dbc7d24d2a39238..7a167171ccb12a7037b24c4f6f757f26c0d4ad8e 100644 (file)
--- a/makefile
+++ b/makefile
@@ -60,8 +60,8 @@ ifeq ($(strip $(macosx)),true)
   APPLDFLAGS := $(SDLDIR)/lib/libSDL-1.2.0.dylib $(SDLDIR)/lib/libSDLmain-osx.a
 else
   CFLAGS += -DPLATFORM_LINUX=1
-  #CFLAGS += -msse -mmmx
-  LDFLAGS := ./libSDL-1.2.so.0 -lGL -lGLU ./libfmod.so
+  LDFLAGS := ./libSDL-1.2.so.0 ./libfmod.so
+  POSTLDFLAGS := /usr/lib/libGLU.a
 
   ifeq ($(strip $(use_devil)),true)
     LDFLAGS += ./libIL.so.1 ./libILU.so.1 ./libILUT.so.1
@@ -228,7 +228,7 @@ ifeq ($(strip $(macosx)),true)
        ranlib $(SDLDIR)/lib/libSDLmain-osx.a
        ranlib $(FREETYPEDIR)/lib/libfreetype-osx.a
 endif
-       $(LD) -o $@ $(APPLDFLAGS) $(LDFLAGS) $(OBJS) $(APPOBJS)
+       $(LD) -o $@ $(APPLDFLAGS) $(LDFLAGS) $(OBJS) $(APPOBJS) $(POSTLDFLAGS)
 
 clean:
        rm -f $(BINDIR)/*.o