comparison test/testdyngl.c @ 1662:782fd950bd46 SDL-1.3

Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API. WARNING: None of the video drivers have been updated for the new API yet! The API is still under design and very fluid. The code is now run through a consistent indent format: indent -i4 -nut -nsc -br -ce The headers are being converted to automatically generate doxygen documentation.
author Sam Lantinga <slouken@libsdl.org>
date Sun, 28 May 2006 13:04:16 +0000
parents 14717b52abc0
children 4da1ee79c9af
comparison
equal deleted inserted replaced
1661:281d3f4870e5 1662:782fd950bd46
26 #ifdef HAVE_OPENGL 26 #ifdef HAVE_OPENGL
27 27
28 #include "SDL_opengl.h" 28 #include "SDL_opengl.h"
29 29
30 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */ 30 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
31 static void quit(int rc) 31 static void
32 { 32 quit (int rc)
33 SDL_Quit(); 33 {
34 exit(rc); 34 SDL_Quit ();
35 } 35 exit (rc);
36 36 }
37 void* get_funcaddr(const char* p) 37
38 { 38 void *
39 void* f=SDL_GL_GetProcAddress(p); 39 get_funcaddr (const char *p)
40 if (f) 40 {
41 { 41 void *f = SDL_GL_GetProcAddress (p);
42 return f; 42 if (f) {
43 } 43 return f;
44 else 44 } else {
45 { 45 printf ("Unable to get function pointer for %s\n", p);
46 printf("Unable to get function pointer for %s\n",p); 46 quit (1);
47 quit(1); 47 }
48 } 48 return NULL;
49 return NULL;
50 } 49 }
51 50
52 typedef struct 51 typedef struct
53 { 52 {
54 void(APIENTRY*glBegin)(GLenum); 53 void (APIENTRY * glBegin) (GLenum);
55 void(APIENTRY*glEnd)(); 54 void (APIENTRY * glEnd) ();
56 void(APIENTRY*glVertex3f)(GLfloat, GLfloat, GLfloat); 55 void (APIENTRY * glVertex3f) (GLfloat, GLfloat, GLfloat);
57 void(APIENTRY*glClearColor)(GLfloat, GLfloat, GLfloat, GLfloat); 56 void (APIENTRY * glClearColor) (GLfloat, GLfloat, GLfloat, GLfloat);
58 void(APIENTRY*glClear)(GLbitfield); 57 void (APIENTRY * glClear) (GLbitfield);
59 void(APIENTRY*glDisable)(GLenum); 58 void (APIENTRY * glDisable) (GLenum);
60 void(APIENTRY*glEnable)(GLenum); 59 void (APIENTRY * glEnable) (GLenum);
61 void(APIENTRY*glColor4ub)(GLubyte,GLubyte,GLubyte,GLubyte); 60 void (APIENTRY * glColor4ub) (GLubyte, GLubyte, GLubyte, GLubyte);
62 void(APIENTRY*glPointSize)(GLfloat); 61 void (APIENTRY * glPointSize) (GLfloat);
63 void(APIENTRY*glHint)(GLenum,GLenum); 62 void (APIENTRY * glHint) (GLenum, GLenum);
64 void(APIENTRY*glBlendFunc)(GLenum,GLenum); 63 void (APIENTRY * glBlendFunc) (GLenum, GLenum);
65 void(APIENTRY*glMatrixMode)(GLenum); 64 void (APIENTRY * glMatrixMode) (GLenum);
66 void(APIENTRY*glLoadIdentity)(); 65 void (APIENTRY * glLoadIdentity) ();
67 void(APIENTRY*glOrtho)(GLdouble,GLdouble,GLdouble,GLdouble,GLdouble,GLdouble); 66 void (APIENTRY * glOrtho) (GLdouble, GLdouble, GLdouble, GLdouble,
68 void(APIENTRY*glRotatef)(GLfloat,GLfloat,GLfloat,GLfloat); 67 GLdouble, GLdouble);
69 void(APIENTRY*glViewport)(GLint,GLint,GLsizei,GLsizei); 68 void (APIENTRY * glRotatef) (GLfloat, GLfloat, GLfloat, GLfloat);
70 void(APIENTRY*glFogf)(GLenum,GLfloat); 69 void (APIENTRY * glViewport) (GLint, GLint, GLsizei, GLsizei);
70 void (APIENTRY * glFogf) (GLenum, GLfloat);
71 } 71 }
72 glfuncs; 72 glfuncs;
73 73
74 void init_glfuncs(glfuncs* f) 74 void
75 { 75 init_glfuncs (glfuncs * f)
76 f->glBegin=get_funcaddr("glBegin"); 76 {
77 f->glEnd=get_funcaddr("glEnd"); 77 f->glBegin = get_funcaddr ("glBegin");
78 f->glVertex3f=get_funcaddr("glVertex3f"); 78 f->glEnd = get_funcaddr ("glEnd");
79 f->glClearColor=get_funcaddr("glClearColor"); 79 f->glVertex3f = get_funcaddr ("glVertex3f");
80 f->glClear=get_funcaddr("glClear"); 80 f->glClearColor = get_funcaddr ("glClearColor");
81 f->glDisable=get_funcaddr("glDisable"); 81 f->glClear = get_funcaddr ("glClear");
82 f->glEnable=get_funcaddr("glEnable"); 82 f->glDisable = get_funcaddr ("glDisable");
83 f->glColor4ub=get_funcaddr("glColor4ub"); 83 f->glEnable = get_funcaddr ("glEnable");
84 f->glPointSize=get_funcaddr("glPointSize"); 84 f->glColor4ub = get_funcaddr ("glColor4ub");
85 f->glHint=get_funcaddr("glHint"); 85 f->glPointSize = get_funcaddr ("glPointSize");
86 f->glBlendFunc=get_funcaddr("glBlendFunc"); 86 f->glHint = get_funcaddr ("glHint");
87 f->glMatrixMode=get_funcaddr("glMatrixMode"); 87 f->glBlendFunc = get_funcaddr ("glBlendFunc");
88 f->glLoadIdentity=get_funcaddr("glLoadIdentity"); 88 f->glMatrixMode = get_funcaddr ("glMatrixMode");
89 f->glOrtho=get_funcaddr("glOrtho"); 89 f->glLoadIdentity = get_funcaddr ("glLoadIdentity");
90 f->glRotatef=get_funcaddr("glRotatef"); 90 f->glOrtho = get_funcaddr ("glOrtho");
91 f->glViewport=get_funcaddr("glViewport"); 91 f->glRotatef = get_funcaddr ("glRotatef");
92 f->glFogf=get_funcaddr("glFogf"); 92 f->glViewport = get_funcaddr ("glViewport");
93 f->glFogf = get_funcaddr ("glFogf");
93 } 94 }
94 95
95 #define NB_PIXELS 1000 96 #define NB_PIXELS 1000
96 97
97 int main(int argc,char *argv[]) 98 int
98 { 99 main (int argc, char *argv[])
99 glfuncs f; 100 {
100 int i; 101 glfuncs f;
101 SDL_Event event; 102 int i;
102 int done=0; 103 SDL_Event event;
103 GLfloat pixels[NB_PIXELS*3]; 104 int done = 0;
104 const char *gl_library = NULL; /* Use the default GL library */ 105 GLfloat pixels[NB_PIXELS * 3];
105 106 const char *gl_library = NULL; /* Use the default GL library */
106 if (argv[1]) { 107
107 gl_library = argv[1]; 108 if (argv[1]) {
108 } 109 gl_library = argv[1];
109 110 }
110 if (SDL_Init(SDL_INIT_VIDEO)<0) 111
111 { 112 if (SDL_Init (SDL_INIT_VIDEO) < 0) {
112 printf("Unable to init SDL : %s\n",SDL_GetError()); 113 printf ("Unable to init SDL : %s\n", SDL_GetError ());
113 return(1); 114 return (1);
114 } 115 }
115 116
116 if (SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1)<0) 117 if (SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1) < 0) {
117 { 118 printf ("Unable to set GL attribute : %s\n", SDL_GetError ());
118 printf("Unable to set GL attribute : %s\n",SDL_GetError()); 119 quit (1);
119 quit(1); 120 }
120 } 121
121 122 if (SDL_GL_LoadLibrary (gl_library) < 0) {
122 if (SDL_GL_LoadLibrary(gl_library)<0) 123 printf ("Unable to dynamically open GL lib : %s\n", SDL_GetError ());
123 { 124 quit (1);
124 printf("Unable to dynamically open GL lib : %s\n",SDL_GetError()); 125 }
125 quit(1); 126
126 } 127 if (SDL_SetVideoMode (640, 480, 0, SDL_OPENGL) == NULL) {
127 128 printf ("Unable to open video mode : %s\n", SDL_GetError ());
128 if (SDL_SetVideoMode(640,480,0,SDL_OPENGL)==NULL) 129 quit (1);
129 { 130 }
130 printf("Unable to open video mode : %s\n",SDL_GetError()); 131
131 quit(1); 132 /* Set the window manager title bar */
132 } 133 SDL_WM_SetCaption ("SDL Dynamic OpenGL Loading Test", "testdyngl");
133 134
134 /* Set the window manager title bar */ 135 init_glfuncs (&f);
135 SDL_WM_SetCaption( "SDL Dynamic OpenGL Loading Test", "testdyngl" ); 136
136 137 for (i = 0; i < NB_PIXELS; i++) {
137 init_glfuncs(&f); 138 pixels[3 * i] = rand () % 250 - 125;
138 139 pixels[3 * i + 1] = rand () % 250 - 125;
139 for(i=0;i<NB_PIXELS;i++) 140 pixels[3 * i + 2] = rand () % 250 - 125;
140 { 141 }
141 pixels[3*i]=rand()%250-125; 142
142 pixels[3*i+1]=rand()%250-125; 143 f.glViewport (0, 0, 640, 480);
143 pixels[3*i+2]=rand()%250-125; 144
144 } 145 f.glMatrixMode (GL_PROJECTION);
145 146 f.glLoadIdentity ();
146 f.glViewport(0,0,640,480); 147 f.glOrtho (-100, 100, -100, 100, -500, 500);
147 148
148 f.glMatrixMode(GL_PROJECTION); 149 f.glMatrixMode (GL_MODELVIEW);
149 f.glLoadIdentity(); 150 f.glLoadIdentity ();
150 f.glOrtho(-100,100,-100,100,-500,500); 151
151 152 f.glEnable (GL_DEPTH_TEST);
152 f.glMatrixMode(GL_MODELVIEW); 153 f.glDisable (GL_TEXTURE_2D);
153 f.glLoadIdentity(); 154 f.glEnable (GL_BLEND);
154 155 f.glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
155 f.glEnable(GL_DEPTH_TEST); 156
156 f.glDisable(GL_TEXTURE_2D); 157 f.glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
157 f.glEnable(GL_BLEND); 158 f.glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
158 f.glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 159
159 160 f.glEnable (GL_POINT_SMOOTH);
160 f.glClearColor(0.0f,0.0f,0.0f,0.0f); 161 f.glHint (GL_POINT_SMOOTH_HINT, GL_NICEST);
161 f.glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); 162 f.glPointSize (5.0f);
162 163 f.glEnable (GL_FOG);
163 f.glEnable(GL_POINT_SMOOTH); 164 f.glFogf (GL_FOG_START, -500);
164 f.glHint(GL_POINT_SMOOTH_HINT,GL_NICEST); 165 f.glFogf (GL_FOG_END, 500);
165 f.glPointSize(5.0f); 166 f.glFogf (GL_FOG_DENSITY, 0.005);
166 f.glEnable(GL_FOG); 167
167 f.glFogf(GL_FOG_START,-500); 168 do {
168 f.glFogf(GL_FOG_END,500); 169 f.glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
169 f.glFogf(GL_FOG_DENSITY,0.005); 170
170 171 f.glRotatef (2.0, 1.0, 1.0, 1.0);
171 do 172 f.glRotatef (1.0, 0.0, 1.0, 1.0);
172 { 173
173 f.glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); 174 f.glColor4ub (255, 255, 255, 255);
174 175 f.glBegin (GL_POINTS);
175 f.glRotatef(2.0,1.0,1.0,1.0); 176 for (i = 0; i < NB_PIXELS; i++) {
176 f.glRotatef(1.0,0.0,1.0,1.0); 177 f.glVertex3f (pixels[3 * i], pixels[3 * i + 1],
177 178 pixels[3 * i + 2]);
178 f.glColor4ub(255,255,255,255); 179 }
179 f.glBegin(GL_POINTS); 180 f.glEnd ();
180 for(i=0;i<NB_PIXELS;i++) 181 SDL_GL_SwapBuffers ();
181 { 182
182 f.glVertex3f(pixels[3*i],pixels[3*i+1],pixels[3*i+2]); 183 while (SDL_PollEvent (&event)) {
183 } 184 if (event.type & SDL_KEYDOWN)
184 f.glEnd(); 185 done = 1;
185 SDL_GL_SwapBuffers(); 186 }
186 187
187 while(SDL_PollEvent(&event)) 188 SDL_Delay (20);
188 { 189 }
189 if(event.type & SDL_KEYDOWN) 190 while (!done);
190 done=1; 191
191 } 192 SDL_Quit ();
192 193 return 0;
193 SDL_Delay(20);
194 }
195 while(!done);
196
197 SDL_Quit();
198 return 0;
199 } 194 }
200 195
201 #else /* HAVE_OPENGL */ 196 #else /* HAVE_OPENGL */
202 197
203 int main(int argc, char *argv[]) 198 int
204 { 199 main (int argc, char *argv[])
205 printf("No OpenGL support on this system\n"); 200 {
206 return 1; 201 printf ("No OpenGL support on this system\n");
202 return 1;
207 } 203 }
208 204
209 #endif /* HAVE_OPENGL */ 205 #endif /* HAVE_OPENGL */