comparison test/testdyngl.c @ 839:7b255f0290da

Added a test program for dynamically loading OpenGL
author Sam Lantinga <slouken@libsdl.org>
date Tue, 17 Feb 2004 18:16:49 +0000
parents
children 0eec9985767d
comparison
equal deleted inserted replaced
838:f9a5fbcdf868 839:7b255f0290da
1 /*
2 * Small SDL example to demonstrate dynamically loading
3 * OpenGL lib and functions
4 *
5 * (FYI it was supposed to look like snow in the wind or something...)
6 *
7 * Compile with :
8 * gcc testdyngl.c `sdl-config --libs --cflags` -o testdyngl
9 *
10 * You can specify a different OpenGL lib on the command line, i.e. :
11 * ./testdyngl /usr/X11R6/lib/libGL.so.1.2
12 * or
13 * ./testdyngl /usr/lib/libGL.so.1.0.4496
14 *
15 */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19
20 #include "SDL.h"
21
22 #ifdef HAVE_OPENGL
23
24 #include "SDL_opengl.h"
25
26 void* get_funcaddr(const char* p)
27 {
28 void* f=SDL_GL_GetProcAddress(p);
29 if (f)
30 {
31 return f;
32 }
33 else
34 {
35 printf("Unable to get function pointer for %s\n",p);
36 exit(1);
37 }
38 }
39
40 typedef struct
41 {
42 void(*glBegin)(GLenum);
43 void(*glEnd)();
44 void(*glVertex3f)(GLfloat, GLfloat, GLfloat);
45 void(*glClearColor)(GLfloat, GLfloat, GLfloat, GLfloat);
46 void(*glClear)(GLbitfield);
47 void(*glDisable)(GLenum);
48 void(*glEnable)(GLenum);
49 void(*glColor4ub)(GLubyte,GLubyte,GLubyte,GLubyte);
50 void(*glPointSize)(GLfloat);
51 void(*glHint)(GLenum,GLenum);
52 void(*glBlendFunc)(GLenum,GLenum);
53 void(*glMatrixMode)(GLenum);
54 void(*glLoadIdentity)();
55 void(*glOrtho)(GLdouble,GLdouble,GLdouble,GLdouble,GLdouble,GLdouble);
56 void(*glRotatef)(GLfloat,GLfloat,GLfloat,GLfloat);
57 void(*glViewport)(GLint,GLint,GLsizei,GLsizei);
58 void(*glFogf)(GLenum,GLfloat);
59 }
60 glfuncs;
61
62 void init_glfuncs(glfuncs* f)
63 {
64 f->glBegin=get_funcaddr("glBegin");
65 f->glEnd=get_funcaddr("glEnd");
66 f->glVertex3f=get_funcaddr("glVertex3f");
67 f->glClearColor=get_funcaddr("glClearColor");
68 f->glClear=get_funcaddr("glClear");
69 f->glDisable=get_funcaddr("glDisable");
70 f->glEnable=get_funcaddr("glEnable");
71 f->glColor4ub=get_funcaddr("glColor4ub");
72 f->glPointSize=get_funcaddr("glPointSize");
73 f->glHint=get_funcaddr("glHint");
74 f->glBlendFunc=get_funcaddr("glBlendFunc");
75 f->glMatrixMode=get_funcaddr("glMatrixMode");
76 f->glLoadIdentity=get_funcaddr("glLoadIdentity");
77 f->glOrtho=get_funcaddr("glOrtho");
78 f->glRotatef=get_funcaddr("glRotatef");
79 f->glViewport=get_funcaddr("glViewport");
80 f->glFogf=get_funcaddr("glFogf");
81 }
82
83 #define NB_PIXELS 1000
84
85 int main(int argc,char *argv[])
86 {
87 glfuncs f;
88 int i;
89 SDL_Event event;
90 int done=0;
91 GLfloat pixels[NB_PIXELS*3];
92 char default_gl_lib[]="/usr/lib/libGL.so";
93 char* current_gl_lib=default_gl_lib;
94
95 if (argc==2)
96 {
97 current_gl_lib=argv[1];
98 }
99
100 if (SDL_Init(SDL_INIT_EVERYTHING)<0)
101 {
102 printf("Unable to init SDL : %s\n",SDL_GetError());
103 exit(1);
104 }
105
106 if (SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1)<0)
107 {
108 printf("Unable to set GL attribute : %s\n",SDL_GetError());
109 exit(1);
110 }
111
112 if (SDL_GL_LoadLibrary(current_gl_lib)<0)
113 {
114 printf("Unable to dynamically open GL lib : %s\n",SDL_GetError());
115 exit(1);
116 }
117
118 if (SDL_SetVideoMode(640,480,0,SDL_OPENGL)==NULL)
119 {
120 printf("Unable to open video mode : %s\n",SDL_GetError());
121 exit(1);
122 }
123
124 init_glfuncs(&f);
125
126 for(i=0;i<NB_PIXELS;i++)
127 {
128 pixels[3*i]=rand()%250-125;
129 pixels[3*i+1]=rand()%250-125;
130 pixels[3*i+2]=rand()%250-125;
131 }
132
133 f.glViewport(0,0,640,480);
134
135 f.glMatrixMode(GL_PROJECTION);
136 f.glLoadIdentity();
137 f.glOrtho(-100,100,-100,100,-500,500);
138
139 f.glMatrixMode(GL_MODELVIEW);
140 f.glLoadIdentity();
141
142 f.glEnable(GL_DEPTH_TEST);
143 f.glDisable(GL_TEXTURE_2D);
144 f.glEnable(GL_BLEND);
145 f.glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
146
147 f.glClearColor(0.0f,0.0f,0.0f,0.0f);
148 f.glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
149
150 f.glEnable(GL_POINT_SMOOTH);
151 f.glHint(GL_POINT_SMOOTH_HINT,GL_NICEST);
152 f.glPointSize(5.0f);
153 f.glEnable(GL_FOG);
154 f.glFogf(GL_FOG_START,-500);
155 f.glFogf(GL_FOG_END,500);
156 f.glFogf(GL_FOG_DENSITY,0.005);
157
158 do
159 {
160 f.glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
161
162 f.glRotatef(2.0,1.0,1.0,1.0);
163 f.glRotatef(1.0,0.0,1.0,1.0);
164
165 f.glColor4ub(255,255,255,255);
166 f.glBegin(GL_POINTS);
167 for(i=0;i<NB_PIXELS;i++)
168 {
169 f.glVertex3f(pixels[3*i],pixels[3*i+1],pixels[3*i+2]);
170 }
171 f.glEnd();
172 SDL_GL_SwapBuffers();
173
174 while(SDL_PollEvent(&event))
175 {
176 if(event.type & SDL_KEYDOWN)
177 done=1;
178 }
179
180 SDL_Delay(20);
181 }
182 while(!done);
183
184 SDL_Quit();
185 return 0;
186 }
187
188 #else /* HAVE_OPENGL */
189
190 int main(int argc, char *argv[])
191 {
192 printf("No OpenGL support on this system\n");
193 return 1;
194 }
195
196 #endif /* HAVE_OPENGL */