2 * Small SDL example to demonstrate dynamically loading
3 * OpenGL lib and functions
5 * (FYI it was supposed to look like snow in the wind or something...)
8 * gcc testdyngl.c `sdl-config --libs --cflags` -o testdyngl -DHAVE_OPENGL
10 * You can specify a different OpenGL lib on the command line, i.e. :
11 * ./testdyngl /usr/X11R6/lib/libGL.so.1.2
13 * ./testdyngl /usr/lib/libGL.so.1.0.4496
28 #include "SDL_opengl.h"
30 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
31 static void quit(int rc
)
37 void* get_funcaddr(const char* p
)
39 void* f
=SDL_GL_GetProcAddress(p
);
46 printf("Unable to get function pointer for %s\n",p
);
54 void(APIENTRY
*glBegin
)(GLenum
);
55 void(APIENTRY
*glEnd
)();
56 void(APIENTRY
*glVertex3f
)(GLfloat
, GLfloat
, GLfloat
);
57 void(APIENTRY
*glClearColor
)(GLfloat
, GLfloat
, GLfloat
, GLfloat
);
58 void(APIENTRY
*glClear
)(GLbitfield
);
59 void(APIENTRY
*glDisable
)(GLenum
);
60 void(APIENTRY
*glEnable
)(GLenum
);
61 void(APIENTRY
*glColor4ub
)(GLubyte
,GLubyte
,GLubyte
,GLubyte
);
62 void(APIENTRY
*glPointSize
)(GLfloat
);
63 void(APIENTRY
*glHint
)(GLenum
,GLenum
);
64 void(APIENTRY
*glBlendFunc
)(GLenum
,GLenum
);
65 void(APIENTRY
*glMatrixMode
)(GLenum
);
66 void(APIENTRY
*glLoadIdentity
)();
67 void(APIENTRY
*glOrtho
)(GLdouble
,GLdouble
,GLdouble
,GLdouble
,GLdouble
,GLdouble
);
68 void(APIENTRY
*glRotatef
)(GLfloat
,GLfloat
,GLfloat
,GLfloat
);
69 void(APIENTRY
*glViewport
)(GLint
,GLint
,GLsizei
,GLsizei
);
70 void(APIENTRY
*glFogf
)(GLenum
,GLfloat
);
74 void init_glfuncs(glfuncs
* f
)
76 f
->glBegin
=get_funcaddr("glBegin");
77 f
->glEnd
=get_funcaddr("glEnd");
78 f
->glVertex3f
=get_funcaddr("glVertex3f");
79 f
->glClearColor
=get_funcaddr("glClearColor");
80 f
->glClear
=get_funcaddr("glClear");
81 f
->glDisable
=get_funcaddr("glDisable");
82 f
->glEnable
=get_funcaddr("glEnable");
83 f
->glColor4ub
=get_funcaddr("glColor4ub");
84 f
->glPointSize
=get_funcaddr("glPointSize");
85 f
->glHint
=get_funcaddr("glHint");
86 f
->glBlendFunc
=get_funcaddr("glBlendFunc");
87 f
->glMatrixMode
=get_funcaddr("glMatrixMode");
88 f
->glLoadIdentity
=get_funcaddr("glLoadIdentity");
89 f
->glOrtho
=get_funcaddr("glOrtho");
90 f
->glRotatef
=get_funcaddr("glRotatef");
91 f
->glViewport
=get_funcaddr("glViewport");
92 f
->glFogf
=get_funcaddr("glFogf");
95 #define NB_PIXELS 1000
97 int main(int argc
,char *argv
[])
103 GLfloat pixels
[NB_PIXELS
*3];
104 const char *gl_library
= NULL
; /* Use the default GL library */
107 gl_library
= argv
[1];
110 if (SDL_Init(SDL_INIT_VIDEO
)<0)
112 printf("Unable to init SDL : %s\n",SDL_GetError());
116 if (SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER
,1)<0)
118 printf("Unable to set GL attribute : %s\n",SDL_GetError());
122 if (SDL_GL_LoadLibrary(gl_library
)<0)
124 printf("Unable to dynamically open GL lib : %s\n",SDL_GetError());
128 if (SDL_SetVideoMode(640,480,0,SDL_OPENGL
)==NULL
)
130 printf("Unable to open video mode : %s\n",SDL_GetError());
134 /* Set the window manager title bar */
135 SDL_WM_SetCaption( "SDL Dynamic OpenGL Loading Test", "testdyngl" );
139 for(i
=0;i
<NB_PIXELS
;i
++)
141 pixels
[3*i
]=rand()%250-125;
142 pixels
[3*i
+1]=rand()%250-125;
143 pixels
[3*i
+2]=rand()%250-125;
146 f
.glViewport(0,0,640,480);
148 f
.glMatrixMode(GL_PROJECTION
);
150 f
.glOrtho(-100,100,-100,100,-500,500);
152 f
.glMatrixMode(GL_MODELVIEW
);
155 f
.glEnable(GL_DEPTH_TEST
);
156 f
.glDisable(GL_TEXTURE_2D
);
157 f
.glEnable(GL_BLEND
);
158 f
.glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
160 f
.glClearColor(0.0f
,0.0f
,0.0f
,0.0f
);
161 f
.glClear(GL_COLOR_BUFFER_BIT
|GL_DEPTH_BUFFER_BIT
);
163 f
.glEnable(GL_POINT_SMOOTH
);
164 f
.glHint(GL_POINT_SMOOTH_HINT
,GL_NICEST
);
167 f
.glFogf(GL_FOG_START
,-500);
168 f
.glFogf(GL_FOG_END
,500);
169 f
.glFogf(GL_FOG_DENSITY
,0.005);
173 f
.glClear(GL_COLOR_BUFFER_BIT
|GL_DEPTH_BUFFER_BIT
);
175 f
.glRotatef(2.0,1.0,1.0,1.0);
176 f
.glRotatef(1.0,0.0,1.0,1.0);
178 f
.glColor4ub(255,255,255,255);
179 f
.glBegin(GL_POINTS
);
180 for(i
=0;i
<NB_PIXELS
;i
++)
182 f
.glVertex3f(pixels
[3*i
],pixels
[3*i
+1],pixels
[3*i
+2]);
185 SDL_GL_SwapBuffers();
187 while(SDL_PollEvent(&event
))
189 if(event
.type
& SDL_KEYDOWN
)
201 #else /* HAVE_OPENGL */
203 int main(int argc
, char *argv
[])
205 printf("No OpenGL support on this system\n");
209 #endif /* HAVE_OPENGL */