Mercurial > sdl-ios-xcode
annotate test/testgl.c @ 221:50620ec9c86a
*** empty log message ***
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Fri, 02 Nov 2001 18:12:52 +0000 |
parents | 0e5d6dd77bda |
children | 5b42a7f5fab3 |
rev | line source |
---|---|
0 | 1 #include <stdlib.h> |
2 #include <stdio.h> | |
3 #include <string.h> | |
4 #include <math.h> | |
5 | |
6 #include "SDL.h" | |
7 | |
8 #ifdef HAVE_OPENGL | |
214
0e5d6dd77bda
Added platform independent OpenGL header - SDL_opengl.h
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
9 #include "SDL_opengl.h" |
0 | 10 |
11 #define SHADED_CUBE | |
12 | |
13 | |
14 void HotKey_ToggleFullScreen(void) | |
15 { | |
16 SDL_Surface *screen; | |
17 | |
18 screen = SDL_GetVideoSurface(); | |
19 if ( SDL_WM_ToggleFullScreen(screen) ) { | |
20 printf("Toggled fullscreen mode - now %s\n", | |
21 (screen->flags&SDL_FULLSCREEN) ? "fullscreen" : "windowed"); | |
22 } else { | |
23 printf("Unable to toggle fullscreen mode\n"); | |
24 } | |
25 } | |
26 | |
27 void HotKey_ToggleGrab(void) | |
28 { | |
29 SDL_GrabMode mode; | |
30 | |
31 printf("Ctrl-G: toggling input grab!\n"); | |
32 mode = SDL_WM_GrabInput(SDL_GRAB_QUERY); | |
33 if ( mode == SDL_GRAB_ON ) { | |
34 printf("Grab was on\n"); | |
35 } else { | |
36 printf("Grab was off\n"); | |
37 } | |
38 mode = SDL_WM_GrabInput(!mode); | |
39 if ( mode == SDL_GRAB_ON ) { | |
40 printf("Grab is now on\n"); | |
41 } else { | |
42 printf("Grab is now off\n"); | |
43 } | |
44 } | |
45 | |
46 void HotKey_Iconify(void) | |
47 { | |
48 printf("Ctrl-Z: iconifying window!\n"); | |
49 SDL_WM_IconifyWindow(); | |
50 } | |
51 | |
52 int HandleEvent(SDL_Event *event) | |
53 { | |
54 int done; | |
55 | |
56 done = 0; | |
57 switch( event->type ) { | |
58 case SDL_ACTIVEEVENT: | |
59 /* See what happened */ | |
60 printf( "app %s ", event->active.gain ? "gained" : "lost" ); | |
61 if ( event->active.state & SDL_APPACTIVE ) { | |
62 printf( "active " ); | |
63 } else if ( event->active.state & SDL_APPMOUSEFOCUS ) { | |
64 printf( "mouse " ); | |
65 } else if ( event->active.state & SDL_APPINPUTFOCUS ) { | |
66 printf( "input " ); | |
67 } | |
68 printf( "focus\n" ); | |
69 break; | |
70 | |
71 | |
72 case SDL_KEYDOWN: | |
73 if ( event->key.keysym.sym == SDLK_ESCAPE ) { | |
74 done = 1; | |
75 } | |
76 if ( (event->key.keysym.sym == SDLK_g) && | |
77 (event->key.keysym.mod & KMOD_CTRL) ) { | |
78 HotKey_ToggleGrab(); | |
79 } | |
80 if ( (event->key.keysym.sym == SDLK_z) && | |
81 (event->key.keysym.mod & KMOD_CTRL) ) { | |
82 HotKey_Iconify(); | |
83 } | |
84 if ( (event->key.keysym.sym == SDLK_RETURN) && | |
85 (event->key.keysym.mod & KMOD_ALT) ) { | |
86 HotKey_ToggleFullScreen(); | |
87 } | |
88 printf("key '%s' pressed\n", | |
89 SDL_GetKeyName(event->key.keysym.sym)); | |
90 break; | |
91 case SDL_QUIT: | |
92 done = 1; | |
93 break; | |
94 } | |
95 return(done); | |
96 } | |
97 | |
98 void DrawSDLLogo(void) | |
99 { | |
100 static SDL_Surface *image = NULL; | |
101 static int x = 0; | |
102 static int y = 0; | |
103 static int delta_x = 1; | |
104 static int delta_y = 1; | |
105 static Uint32 last_moved = 0; | |
106 | |
107 SDL_Rect dst; | |
108 SDL_Surface *screen; | |
109 | |
110 if ( image == NULL ) { | |
111 SDL_Surface *temp; | |
112 | |
113 temp = SDL_LoadBMP("icon.bmp"); | |
114 if ( temp == NULL ) { | |
115 return; | |
116 } | |
117 image = SDL_CreateRGBSurface( | |
118 SDL_SWSURFACE, | |
119 temp->w, temp->h, | |
120 32, | |
121 #if SDL_BYTEORDER == SDL_LIL_ENDIAN | |
122 0x000000FF, | |
123 0x0000FF00, | |
124 0x00FF0000, | |
125 0xFF000000 | |
126 #else | |
127 0xFF000000, | |
128 0x00FF0000, | |
129 0x0000FF00, | |
130 0x000000FF | |
131 #endif | |
132 ); | |
133 if ( image != NULL ) { | |
134 SDL_BlitSurface(temp, NULL, image, NULL); | |
135 } | |
136 SDL_FreeSurface(temp); | |
137 if ( image == NULL ) { | |
138 return; | |
139 } | |
140 } | |
141 | |
142 screen = SDL_GetVideoSurface(); | |
143 | |
144 /* Show the image on the screen */ | |
145 dst.x = x; | |
146 dst.y = y; | |
147 dst.w = image->w; | |
148 dst.h = image->h; | |
149 | |
150 /* Move it around | |
151 Note that we do not clear the old position. This is because we | |
152 perform a glClear() which clears the framebuffer and then only | |
153 update the new area. | |
154 Note that you can also achieve interesting effects by modifying | |
155 the screen surface alpha channel. It's set to 255 by default.. | |
156 */ | |
157 if ( (SDL_GetTicks() - last_moved) > 100 ) { | |
158 x += delta_x; | |
159 if ( x < 0 ) { | |
160 x = 0; | |
161 delta_x = -delta_x; | |
162 } else | |
163 if ( (x+image->w) > screen->w ) { | |
164 x = screen->w-image->w; | |
165 delta_x = -delta_x; | |
166 } | |
167 y += delta_y; | |
168 if ( y < 0 ) { | |
169 y = 0; | |
170 delta_y = -delta_y; | |
171 } else | |
172 if ( (y+image->h) > screen->h ) { | |
173 y = screen->h-image->h; | |
174 delta_y = -delta_y; | |
175 } | |
176 SDL_BlitSurface(image, NULL, screen, &dst); | |
177 } | |
178 SDL_UpdateRects(screen, 1, &dst); | |
179 } | |
180 | |
181 int RunGLTest( int argc, char* argv[], | |
182 int logo, int slowly, int bpp, float gamma ) | |
183 { | |
184 int i; | |
185 int rgb_size[3]; | |
186 int w = 640; | |
187 int h = 480; | |
188 int done = 0; | |
189 int frames; | |
190 Uint32 start_time, this_time; | |
191 float color[8][3]= {{ 1.0, 1.0, 0.0}, | |
192 { 1.0, 0.0, 0.0}, | |
193 { 0.0, 0.0, 0.0}, | |
194 { 0.0, 1.0, 0.0}, | |
195 { 0.0, 1.0, 1.0}, | |
196 { 1.0, 1.0, 1.0}, | |
197 { 1.0, 0.0, 1.0}, | |
198 { 0.0, 0.0, 1.0}}; | |
199 float cube[8][3]= {{ 0.5, 0.5, -0.5}, | |
200 { 0.5, -0.5, -0.5}, | |
201 {-0.5, -0.5, -0.5}, | |
202 {-0.5, 0.5, -0.5}, | |
203 {-0.5, 0.5, 0.5}, | |
204 { 0.5, 0.5, 0.5}, | |
205 { 0.5, -0.5, 0.5}, | |
206 {-0.5, -0.5, 0.5}}; | |
207 Uint32 video_flags; | |
208 int value; | |
209 | |
210 if( SDL_Init( SDL_INIT_VIDEO ) < 0 ) { | |
211 fprintf(stderr,"Couldn't initialize SDL: %s\n",SDL_GetError()); | |
212 exit( 1 ); | |
213 } | |
214 | |
215 /* See if we should detect the display depth */ | |
216 if ( bpp == 0 ) { | |
217 if ( SDL_GetVideoInfo()->vfmt->BitsPerPixel <= 8 ) { | |
218 bpp = 8; | |
219 } else { | |
220 bpp = 16; /* More doesn't seem to work */ | |
221 } | |
222 } | |
223 | |
224 /* Set the flags we want to use for setting the video mode */ | |
225 if ( logo ) { | |
226 video_flags = SDL_OPENGLBLIT; | |
227 } else { | |
228 video_flags = SDL_OPENGL; | |
229 } | |
230 for ( i=1; argv[i]; ++i ) { | |
231 if ( strcmp(argv[1], "-fullscreen") == 0 ) { | |
232 video_flags |= SDL_FULLSCREEN; | |
233 } | |
234 } | |
235 | |
236 /* Initialize the display */ | |
237 switch (bpp) { | |
238 case 8: | |
239 rgb_size[0] = 2; | |
240 rgb_size[1] = 3; | |
241 rgb_size[2] = 3; | |
242 break; | |
243 case 15: | |
244 case 16: | |
245 rgb_size[0] = 5; | |
246 rgb_size[1] = 5; | |
247 rgb_size[2] = 5; | |
248 break; | |
249 default: | |
250 rgb_size[0] = 8; | |
251 rgb_size[1] = 8; | |
252 rgb_size[2] = 8; | |
253 break; | |
254 } | |
255 SDL_GL_SetAttribute( SDL_GL_RED_SIZE, rgb_size[0] ); | |
256 SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, rgb_size[1] ); | |
257 SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, rgb_size[2] ); | |
258 SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 ); | |
259 SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); | |
260 if ( SDL_SetVideoMode( w, h, bpp, video_flags ) == NULL ) { | |
261 fprintf(stderr, "Couldn't set GL mode: %s\n", SDL_GetError()); | |
262 SDL_Quit(); | |
263 exit(1); | |
264 } | |
265 | |
266 printf("Screen BPP: %d\n", SDL_GetVideoSurface()->format->BitsPerPixel); | |
267 printf("\n"); | |
268 printf( "Vendor : %s\n", glGetString( GL_VENDOR ) ); | |
269 printf( "Renderer : %s\n", glGetString( GL_RENDERER ) ); | |
270 printf( "Version : %s\n", glGetString( GL_VERSION ) ); | |
271 printf( "Extensions : %s\n", glGetString( GL_EXTENSIONS ) ); | |
272 printf("\n"); | |
273 | |
274 SDL_GL_GetAttribute( SDL_GL_RED_SIZE, &value ); | |
275 printf( "SDL_GL_RED_SIZE: requested %d, got %d\n", rgb_size[0],value); | |
276 SDL_GL_GetAttribute( SDL_GL_GREEN_SIZE, &value ); | |
277 printf( "SDL_GL_GREEN_SIZE: requested %d, got %d\n", rgb_size[1],value); | |
278 SDL_GL_GetAttribute( SDL_GL_BLUE_SIZE, &value ); | |
279 printf( "SDL_GL_BLUE_SIZE: requested %d, got %d\n", rgb_size[2],value); | |
280 SDL_GL_GetAttribute( SDL_GL_DEPTH_SIZE, &value ); | |
281 printf( "SDL_GL_DEPTH_SIZE: requested %d, got %d\n", bpp, value ); | |
282 SDL_GL_GetAttribute( SDL_GL_DOUBLEBUFFER, &value ); | |
283 printf( "SDL_GL_DOUBLEBUFFER: requested 1, got %d\n", value ); | |
284 | |
285 /* Set the window manager title bar */ | |
286 SDL_WM_SetCaption( "SDL GL test", "testgl" ); | |
287 | |
288 /* Set the gamma for the window */ | |
289 if ( gamma != 0.0 ) { | |
290 SDL_SetGamma(gamma, gamma, gamma); | |
291 } | |
292 | |
293 glViewport( 0, 0, w, h ); | |
294 glMatrixMode( GL_PROJECTION ); | |
295 glLoadIdentity( ); | |
296 | |
297 glOrtho( -2.0, 2.0, -2.0, 2.0, -20.0, 20.0 ); | |
298 | |
299 glMatrixMode( GL_MODELVIEW ); | |
300 glLoadIdentity( ); | |
301 | |
302 glEnable(GL_DEPTH_TEST); | |
303 | |
304 glDepthFunc(GL_LESS); | |
305 | |
306 glShadeModel(GL_SMOOTH); | |
307 | |
308 /* Loop until done. */ | |
309 start_time = SDL_GetTicks(); | |
310 frames = 0; | |
311 while( !done ) { | |
312 GLenum gl_error; | |
313 char* sdl_error; | |
314 SDL_Event event; | |
315 | |
316 /* Do our drawing, too. */ | |
317 glClearColor( 0.0, 0.0, 0.0, 1.0 ); | |
318 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
319 | |
320 glBegin( GL_QUADS ); | |
321 | |
322 #ifdef SHADED_CUBE | |
323 glColor3fv(color[0]); | |
324 glVertex3fv(cube[0]); | |
325 glColor3fv(color[1]); | |
326 glVertex3fv(cube[1]); | |
327 glColor3fv(color[2]); | |
328 glVertex3fv(cube[2]); | |
329 glColor3fv(color[3]); | |
330 glVertex3fv(cube[3]); | |
331 | |
332 glColor3fv(color[3]); | |
333 glVertex3fv(cube[3]); | |
334 glColor3fv(color[4]); | |
335 glVertex3fv(cube[4]); | |
336 glColor3fv(color[7]); | |
337 glVertex3fv(cube[7]); | |
338 glColor3fv(color[2]); | |
339 glVertex3fv(cube[2]); | |
340 | |
341 glColor3fv(color[0]); | |
342 glVertex3fv(cube[0]); | |
343 glColor3fv(color[5]); | |
344 glVertex3fv(cube[5]); | |
345 glColor3fv(color[6]); | |
346 glVertex3fv(cube[6]); | |
347 glColor3fv(color[1]); | |
348 glVertex3fv(cube[1]); | |
349 | |
350 glColor3fv(color[5]); | |
351 glVertex3fv(cube[5]); | |
352 glColor3fv(color[4]); | |
353 glVertex3fv(cube[4]); | |
354 glColor3fv(color[7]); | |
355 glVertex3fv(cube[7]); | |
356 glColor3fv(color[6]); | |
357 glVertex3fv(cube[6]); | |
358 | |
359 glColor3fv(color[5]); | |
360 glVertex3fv(cube[5]); | |
361 glColor3fv(color[0]); | |
362 glVertex3fv(cube[0]); | |
363 glColor3fv(color[3]); | |
364 glVertex3fv(cube[3]); | |
365 glColor3fv(color[4]); | |
366 glVertex3fv(cube[4]); | |
367 | |
368 glColor3fv(color[6]); | |
369 glVertex3fv(cube[6]); | |
370 glColor3fv(color[1]); | |
371 glVertex3fv(cube[1]); | |
372 glColor3fv(color[2]); | |
373 glVertex3fv(cube[2]); | |
374 glColor3fv(color[7]); | |
375 glVertex3fv(cube[7]); | |
376 #else // flat cube | |
377 glColor3f(1.0, 0.0, 0.0); | |
378 glVertex3fv(cube[0]); | |
379 glVertex3fv(cube[1]); | |
380 glVertex3fv(cube[2]); | |
381 glVertex3fv(cube[3]); | |
382 | |
383 glColor3f(0.0, 1.0, 0.0); | |
384 glVertex3fv(cube[3]); | |
385 glVertex3fv(cube[4]); | |
386 glVertex3fv(cube[7]); | |
387 glVertex3fv(cube[2]); | |
388 | |
389 glColor3f(0.0, 0.0, 1.0); | |
390 glVertex3fv(cube[0]); | |
391 glVertex3fv(cube[5]); | |
392 glVertex3fv(cube[6]); | |
393 glVertex3fv(cube[1]); | |
394 | |
395 glColor3f(0.0, 1.0, 1.0); | |
396 glVertex3fv(cube[5]); | |
397 glVertex3fv(cube[4]); | |
398 glVertex3fv(cube[7]); | |
399 glVertex3fv(cube[6]); | |
400 | |
401 glColor3f(1.0, 1.0, 0.0); | |
402 glVertex3fv(cube[5]); | |
403 glVertex3fv(cube[0]); | |
404 glVertex3fv(cube[3]); | |
405 glVertex3fv(cube[4]); | |
406 | |
407 glColor3f(1.0, 0.0, 1.0); | |
408 glVertex3fv(cube[6]); | |
409 glVertex3fv(cube[1]); | |
410 glVertex3fv(cube[2]); | |
411 glVertex3fv(cube[7]); | |
412 #endif /* SHADED_CUBE */ | |
413 | |
414 glEnd( ); | |
415 | |
416 glMatrixMode(GL_MODELVIEW); | |
417 glRotatef(5.0, 1.0, 1.0, 1.0); | |
418 | |
419 /* Draw 2D logo onto the 3D display */ | |
420 if ( logo ) { | |
421 DrawSDLLogo(); | |
422 } | |
423 | |
424 SDL_GL_SwapBuffers( ); | |
425 | |
426 /* Check for error conditions. */ | |
427 gl_error = glGetError( ); | |
428 | |
429 if( gl_error != GL_NO_ERROR ) { | |
430 fprintf( stderr, "testgl: OpenGL error: %d\n", gl_error ); | |
431 } | |
432 | |
433 sdl_error = SDL_GetError( ); | |
434 | |
435 if( sdl_error[0] != '\0' ) { | |
436 fprintf(stderr, "testgl: SDL error '%s'\n", sdl_error); | |
437 SDL_ClearError(); | |
438 } | |
439 | |
440 /* Allow the user to see what's happening */ | |
441 if ( slowly ) { | |
442 SDL_Delay( 20 ); | |
443 } | |
444 | |
445 /* Check if there's a pending event. */ | |
446 while( SDL_PollEvent( &event ) ) { | |
447 done = HandleEvent(&event); | |
448 } | |
449 ++frames; | |
450 } | |
451 | |
452 /* Print out the frames per second */ | |
453 this_time = SDL_GetTicks(); | |
454 if ( this_time != start_time ) { | |
455 printf("%2.2f FPS\n", | |
456 ((float)frames/(this_time-start_time))*1000.0); | |
457 } | |
458 | |
459 /* Destroy our GL context, etc. */ | |
460 SDL_Quit( ); | |
461 return(0); | |
462 } | |
463 | |
464 int main(int argc, char *argv[]) | |
465 { | |
466 int i, logo; | |
467 int numtests; | |
468 int bpp = 0; | |
469 int slowly; | |
470 float gamma = 0.0; | |
471 | |
472 logo = 0; | |
473 slowly = 0; | |
474 numtests = 1; | |
475 for ( i=1; argv[i]; ++i ) { | |
476 if ( strcmp(argv[i], "-twice") == 0 ) { | |
477 ++numtests; | |
478 } | |
479 if ( strcmp(argv[i], "-logo") == 0 ) { | |
480 logo = 1; | |
481 } | |
482 if ( strcmp(argv[i], "-slow") == 0 ) { | |
483 slowly = 1; | |
484 } | |
485 if ( strcmp(argv[i], "-bpp") == 0 ) { | |
486 bpp = atoi(argv[++i]); | |
487 } | |
488 if ( strcmp(argv[i], "-gamma") == 0 ) { | |
489 gamma = (float)atof(argv[++i]); | |
490 } | |
491 if ( strncmp(argv[i], "-h", 2) == 0 ) { | |
492 printf( | |
493 "Usage: %s [-twice] [-logo] [-slow] [-bpp n] [-gamma n]\n", | |
494 argv[0]); | |
495 exit(0); | |
496 } | |
497 } | |
498 for ( i=0; i<numtests; ++i ) { | |
499 RunGLTest(argc, argv, logo, slowly, bpp, gamma); | |
500 } | |
501 return 0; | |
502 } | |
503 | |
504 #else /* HAVE_OPENGL */ | |
505 | |
506 int main(int argc, char *argv[]) | |
507 { | |
508 printf("No OpenGL support on this system\n"); | |
509 return 1; | |
510 } | |
511 | |
512 #endif /* HAVE_OPENGL */ |