test/testgl.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 17 May 2006 08:18:28 +0000
branchSDL-1.3
changeset 1659 14717b52abc0
parent 1656 96c2f89cc7e1
child 1662 782fd950bd46
permissions -rw-r--r--
Merge trunk-1.3-3
     1 #include <stdlib.h>
     2 #include <stdio.h>
     3 #include <string.h>
     4 #include <math.h>
     5 
     6 #include "SDL.h"
     7 
     8 #ifdef __MACOS__
     9 #define HAVE_OPENGL
    10 #endif
    11 
    12 #ifdef HAVE_OPENGL
    13 
    14 #include "SDL_opengl.h"
    15 
    16 /* Undefine this if you want a flat cube instead of a rainbow cube */
    17 #define SHADED_CUBE
    18 
    19 /* Define this to be the name of the logo image to use with -logo */
    20 #define LOGO_FILE	"icon.bmp"
    21 
    22 static SDL_Surface *global_image = NULL;
    23 static GLuint global_texture = 0;
    24 static GLuint cursor_texture = 0;
    25 
    26 /**********************************************************************/
    27 
    28 void HotKey_ToggleFullScreen(void)
    29 {
    30 	SDL_Surface *screen;
    31 
    32 	screen = SDL_GetVideoSurface();
    33 	if ( SDL_WM_ToggleFullScreen(screen) ) {
    34 		printf("Toggled fullscreen mode - now %s\n",
    35 		    (screen->flags&SDL_FULLSCREEN) ? "fullscreen" : "windowed");
    36 	} else {
    37 		printf("Unable to toggle fullscreen mode\n");
    38 	}
    39 }
    40 
    41 void HotKey_ToggleGrab(void)
    42 {
    43 	SDL_GrabMode mode;
    44 
    45 	printf("Ctrl-G: toggling input grab!\n");
    46 	mode = SDL_WM_GrabInput(SDL_GRAB_QUERY);
    47 	if ( mode == SDL_GRAB_ON ) {
    48 		printf("Grab was on\n");
    49 	} else {
    50 		printf("Grab was off\n");
    51 	}
    52 	mode = SDL_WM_GrabInput(!mode);
    53 	if ( mode == SDL_GRAB_ON ) {
    54 		printf("Grab is now on\n");
    55 	} else {
    56 		printf("Grab is now off\n");
    57 	}
    58 }
    59 
    60 void HotKey_Iconify(void)
    61 {
    62 	printf("Ctrl-Z: iconifying window!\n");
    63 	SDL_WM_IconifyWindow();
    64 }
    65 
    66 int HandleEvent(SDL_Event *event)
    67 {
    68 	int done;
    69 
    70 	done = 0;
    71 	switch( event->type ) {
    72 	    case SDL_ACTIVEEVENT:
    73 		/* See what happened */
    74 		printf( "app %s ", event->active.gain ? "gained" : "lost" );
    75 		if ( event->active.state & SDL_APPACTIVE ) {
    76 			printf( "active " );
    77 		} else if ( event->active.state & SDL_APPMOUSEFOCUS ) {
    78 			printf( "mouse " );
    79 		} else if ( event->active.state & SDL_APPINPUTFOCUS ) {
    80 			printf( "input " );
    81 		}
    82 		printf( "focus\n" );
    83 		break;
    84 		
    85 
    86 	    case SDL_KEYDOWN:
    87 		if ( event->key.keysym.sym == SDLK_ESCAPE ) {
    88 			done = 1;
    89 		}
    90 		if ( (event->key.keysym.sym == SDLK_g) &&
    91 		     (event->key.keysym.mod & KMOD_CTRL) ) {
    92 			HotKey_ToggleGrab();
    93 		}
    94 		if ( (event->key.keysym.sym == SDLK_z) &&
    95 		     (event->key.keysym.mod & KMOD_CTRL) ) {
    96 			HotKey_Iconify();
    97 		}
    98 		if ( (event->key.keysym.sym == SDLK_RETURN) &&
    99 		     (event->key.keysym.mod & KMOD_ALT) ) {
   100 			HotKey_ToggleFullScreen();
   101 		}
   102 		printf("key '%s' pressed\n", 
   103 			SDL_GetKeyName(event->key.keysym.sym));
   104 		break;
   105 	    case SDL_QUIT:
   106 		done = 1;
   107 		break;
   108 	}
   109 	return(done);
   110 }
   111 
   112 void SDL_GL_Enter2DMode()
   113 {
   114 	SDL_Surface *screen = SDL_GetVideoSurface();
   115 
   116 	/* Note, there may be other things you need to change,
   117 	   depending on how you have your OpenGL state set up.
   118 	*/
   119 	glPushAttrib(GL_ENABLE_BIT);
   120 	glDisable(GL_DEPTH_TEST);
   121 	glDisable(GL_CULL_FACE);
   122 	glEnable(GL_TEXTURE_2D);
   123 
   124 	/* This allows alpha blending of 2D textures with the scene */
   125 	glEnable(GL_BLEND);
   126 	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
   127 
   128 	glViewport(0, 0, screen->w, screen->h);
   129 
   130 	glMatrixMode(GL_PROJECTION);
   131 	glPushMatrix();
   132 	glLoadIdentity();
   133 
   134 	glOrtho(0.0, (GLdouble)screen->w, (GLdouble)screen->h, 0.0, 0.0, 1.0);
   135 
   136 	glMatrixMode(GL_MODELVIEW);
   137 	glPushMatrix();
   138 	glLoadIdentity();
   139 
   140 	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
   141 }
   142 
   143 void SDL_GL_Leave2DMode()
   144 {
   145 	glMatrixMode(GL_MODELVIEW);
   146 	glPopMatrix();
   147 
   148 	glMatrixMode(GL_PROJECTION);
   149 	glPopMatrix();
   150 
   151 	glPopAttrib();
   152 }
   153 
   154 /* Quick utility function for texture creation */
   155 static int power_of_two(int input)
   156 {
   157 	int value = 1;
   158 
   159 	while ( value < input ) {
   160 		value <<= 1;
   161 	}
   162 	return value;
   163 }
   164 
   165 GLuint SDL_GL_LoadTexture(SDL_Surface *surface, GLfloat *texcoord)
   166 {
   167 	GLuint texture;
   168 	int w, h;
   169 	SDL_Surface *image;
   170 	SDL_Rect area;
   171 	Uint32 saved_flags;
   172 	Uint8  saved_alpha;
   173 
   174 	/* Use the surface width and height expanded to powers of 2 */
   175 	w = power_of_two(surface->w);
   176 	h = power_of_two(surface->h);
   177 	texcoord[0] = 0.0f;			/* Min X */
   178 	texcoord[1] = 0.0f;			/* Min Y */
   179 	texcoord[2] = (GLfloat)surface->w / w;	/* Max X */
   180 	texcoord[3] = (GLfloat)surface->h / h;	/* Max Y */
   181 
   182 	image = SDL_CreateRGBSurface(
   183 			SDL_SWSURFACE,
   184 			w, h,
   185 			32,
   186 #if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
   187 			0x000000FF, 
   188 			0x0000FF00, 
   189 			0x00FF0000, 
   190 			0xFF000000
   191 #else
   192 			0xFF000000,
   193 			0x00FF0000, 
   194 			0x0000FF00, 
   195 			0x000000FF
   196 #endif
   197 		       );
   198 	if ( image == NULL ) {
   199 		return 0;
   200 	}
   201 
   202 	/* Save the alpha blending attributes */
   203 	saved_flags = surface->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
   204 	saved_alpha = surface->format->alpha;
   205 	if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) {
   206 		SDL_SetAlpha(surface, 0, 0);
   207 	}
   208 
   209 	/* Copy the surface into the GL texture image */
   210 	area.x = 0;
   211 	area.y = 0;
   212 	area.w = surface->w;
   213 	area.h = surface->h;
   214 	SDL_BlitSurface(surface, &area, image, &area);
   215 
   216 	/* Restore the alpha blending attributes */
   217 	if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) {
   218 		SDL_SetAlpha(surface, saved_flags, saved_alpha);
   219 	}
   220 
   221 	/* Create an OpenGL texture for the image */
   222 	glGenTextures(1, &texture);
   223 	glBindTexture(GL_TEXTURE_2D, texture);
   224 	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
   225 	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
   226 	glTexImage2D(GL_TEXTURE_2D,
   227 		     0,
   228 		     GL_RGBA,
   229 		     w, h,
   230 		     0,
   231 		     GL_RGBA,
   232 		     GL_UNSIGNED_BYTE,
   233 		     image->pixels);
   234 	SDL_FreeSurface(image); /* No longer needed */
   235 
   236 	return texture;
   237 }
   238 
   239 void DrawLogoCursor(void)
   240 {
   241 	static GLfloat texMinX, texMinY;
   242 	static GLfloat texMaxX, texMaxY;
   243 	static int w, h;
   244 	int x, y;
   245 
   246 	if ( ! cursor_texture ) {
   247 		SDL_Surface *image;
   248 		GLfloat texcoord[4];
   249 
   250 		/* Load the image (could use SDL_image library here) */
   251 		image = SDL_LoadBMP(LOGO_FILE);
   252 		if ( image == NULL ) {
   253 			return;
   254 		}
   255 		w = image->w;
   256 		h = image->h;
   257 
   258 		/* Convert the image into an OpenGL texture */
   259 		cursor_texture = SDL_GL_LoadTexture(image, texcoord);
   260 
   261 		/* Make texture coordinates easy to understand */
   262 		texMinX = texcoord[0];
   263 		texMinY = texcoord[1];
   264 		texMaxX = texcoord[2];
   265 		texMaxY = texcoord[3];
   266 
   267 		/* We don't need the original image anymore */
   268 		SDL_FreeSurface(image);
   269 
   270 		/* Make sure that the texture conversion is okay */
   271 		if ( ! cursor_texture ) {
   272 			return;
   273 		}
   274 	}
   275 
   276 	/* Move the image around */
   277 	SDL_GetMouseState(&x, &y);
   278 	x -= w/2;
   279 	y -= h/2;
   280 
   281 	/* Show the image on the screen */
   282 	SDL_GL_Enter2DMode();
   283 	glBindTexture(GL_TEXTURE_2D, cursor_texture);
   284 	glBegin(GL_TRIANGLE_STRIP);
   285 	glTexCoord2f(texMinX, texMinY); glVertex2i(x,   y  );
   286 	glTexCoord2f(texMaxX, texMinY); glVertex2i(x+w, y  );
   287 	glTexCoord2f(texMinX, texMaxY); glVertex2i(x,   y+h);
   288 	glTexCoord2f(texMaxX, texMaxY); glVertex2i(x+w, y+h);
   289 	glEnd();
   290 	SDL_GL_Leave2DMode();
   291 }
   292 
   293 void DrawLogoTexture(void)
   294 {
   295 	static GLfloat texMinX, texMinY;
   296 	static GLfloat texMaxX, texMaxY;
   297 	static int x = 0;
   298 	static int y = 0;
   299 	static int w, h;
   300 	static int delta_x = 1;
   301 	static int delta_y = 1;
   302 
   303 	SDL_Surface *screen = SDL_GetVideoSurface();
   304 
   305 	if ( ! global_texture ) {
   306 		SDL_Surface *image;
   307 		GLfloat texcoord[4];
   308 
   309 		/* Load the image (could use SDL_image library here) */
   310 		image = SDL_LoadBMP(LOGO_FILE);
   311 		if ( image == NULL ) {
   312 			return;
   313 		}
   314 		w = image->w;
   315 		h = image->h;
   316 
   317 		/* Convert the image into an OpenGL texture */
   318 		global_texture = SDL_GL_LoadTexture(image, texcoord);
   319 
   320 		/* Make texture coordinates easy to understand */
   321 		texMinX = texcoord[0];
   322 		texMinY = texcoord[1];
   323 		texMaxX = texcoord[2];
   324 		texMaxY = texcoord[3];
   325 
   326 		/* We don't need the original image anymore */
   327 		SDL_FreeSurface(image);
   328 
   329 		/* Make sure that the texture conversion is okay */
   330 		if ( ! global_texture ) {
   331 			return;
   332 		}
   333 	}
   334 
   335 	/* Move the image around */
   336 	x += delta_x;
   337 	if ( x < 0 ) {
   338 		x = 0;
   339 		delta_x = -delta_x;
   340 	} else
   341 	if ( (x+w) > screen->w ) {
   342 		x = screen->w-w;
   343 		delta_x = -delta_x;
   344 	}
   345 	y += delta_y;
   346 	if ( y < 0 ) {
   347 		y = 0;
   348 		delta_y = -delta_y;
   349 	} else
   350 	if ( (y+h) > screen->h ) {
   351 		y = screen->h-h;
   352 		delta_y = -delta_y;
   353 	}
   354 
   355 	/* Show the image on the screen */
   356 	SDL_GL_Enter2DMode();
   357 	glBindTexture(GL_TEXTURE_2D, global_texture);
   358 	glBegin(GL_TRIANGLE_STRIP);
   359 	glTexCoord2f(texMinX, texMinY); glVertex2i(x,   y  );
   360 	glTexCoord2f(texMaxX, texMinY); glVertex2i(x+w, y  );
   361 	glTexCoord2f(texMinX, texMaxY); glVertex2i(x,   y+h);
   362 	glTexCoord2f(texMaxX, texMaxY); glVertex2i(x+w, y+h);
   363 	glEnd();
   364 	SDL_GL_Leave2DMode();
   365 }
   366 
   367 int RunGLTest( int argc, char* argv[],
   368                int logo, int logocursor, int slowly, int bpp, float gamma, int noframe, int fsaa, int sync, int accel )
   369 {
   370 	int i;
   371 	int rgb_size[3];
   372 	int w = 640;
   373 	int h = 480;
   374 	int done = 0;
   375 	int frames;
   376 	Uint32 start_time, this_time;
   377         float color[8][3]= {{ 1.0,  1.0,  0.0}, 
   378 			    { 1.0,  0.0,  0.0},
   379 			    { 0.0,  0.0,  0.0},
   380 			    { 0.0,  1.0,  0.0},
   381 			    { 0.0,  1.0,  1.0},
   382 			    { 1.0,  1.0,  1.0},
   383 			    { 1.0,  0.0,  1.0},
   384 			    { 0.0,  0.0,  1.0}};
   385 	float cube[8][3]= {{ 0.5,  0.5, -0.5}, 
   386 			   { 0.5, -0.5, -0.5},
   387 			   {-0.5, -0.5, -0.5},
   388 			   {-0.5,  0.5, -0.5},
   389 			   {-0.5,  0.5,  0.5},
   390 			   { 0.5,  0.5,  0.5},
   391 			   { 0.5, -0.5,  0.5},
   392 			   {-0.5, -0.5,  0.5}};
   393 	Uint32 video_flags;
   394 	int value;
   395 
   396 	if( SDL_Init( SDL_INIT_VIDEO ) < 0 ) {
   397 		fprintf(stderr,"Couldn't initialize SDL: %s\n",SDL_GetError());
   398 		exit( 1 );
   399 	}
   400 
   401 	/* See if we should detect the display depth */
   402 	if ( bpp == 0 ) {
   403 		if ( SDL_GetVideoInfo()->vfmt->BitsPerPixel <= 8 ) {
   404 			bpp = 8;
   405 		} else {
   406 			bpp = 16;  /* More doesn't seem to work */
   407 		}
   408 	}
   409 
   410 	/* Set the flags we want to use for setting the video mode */
   411 	video_flags = SDL_OPENGL;
   412 	for ( i=1; argv[i]; ++i ) {
   413 		if ( strcmp(argv[i], "-fullscreen") == 0 ) {
   414 			video_flags |= SDL_FULLSCREEN;
   415 		}
   416 	}
   417 
   418         if (noframe) {
   419            video_flags |= SDL_NOFRAME;
   420         }
   421 
   422 	/* Initialize the display */
   423 	switch (bpp) {
   424 	    case 8:
   425 		rgb_size[0] = 3;
   426 		rgb_size[1] = 3;
   427 		rgb_size[2] = 2;
   428 		break;
   429 	    case 15:
   430 	    case 16:
   431 		rgb_size[0] = 5;
   432 		rgb_size[1] = 5;
   433 		rgb_size[2] = 5;
   434 		break;
   435             default:
   436 		rgb_size[0] = 8;
   437 		rgb_size[1] = 8;
   438 		rgb_size[2] = 8;
   439 		break;
   440 	}
   441 	SDL_GL_SetAttribute( SDL_GL_RED_SIZE, rgb_size[0] );
   442 	SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, rgb_size[1] );
   443 	SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, rgb_size[2] );
   444 	SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
   445 	SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
   446 	if ( fsaa ) {
   447 		SDL_GL_SetAttribute( SDL_GL_MULTISAMPLEBUFFERS, 1 );
   448 		SDL_GL_SetAttribute( SDL_GL_MULTISAMPLESAMPLES, fsaa );
   449 	}
   450 	if ( accel ) {
   451 		SDL_GL_SetAttribute( SDL_GL_ACCELERATED_VISUAL, 1 );
   452 	}
   453 	if ( sync ) {
   454 		SDL_GL_SetAttribute( SDL_GL_SWAP_CONTROL, 1 );
   455 	} else {
   456 		SDL_GL_SetAttribute( SDL_GL_SWAP_CONTROL, 0 );
   457 	}
   458 	if ( SDL_SetVideoMode( w, h, bpp, video_flags ) == NULL ) {
   459 		fprintf(stderr, "Couldn't set GL mode: %s\n", SDL_GetError());
   460 		SDL_Quit();
   461 		exit(1);
   462 	}
   463 
   464 	printf("Screen BPP: %d\n", SDL_GetVideoSurface()->format->BitsPerPixel);
   465 	printf("\n");
   466 	printf( "Vendor     : %s\n", glGetString( GL_VENDOR ) );
   467 	printf( "Renderer   : %s\n", glGetString( GL_RENDERER ) );
   468 	printf( "Version    : %s\n", glGetString( GL_VERSION ) );
   469 	printf( "Extensions : %s\n", glGetString( GL_EXTENSIONS ) );
   470 	printf("\n");
   471 
   472 	SDL_GL_GetAttribute( SDL_GL_RED_SIZE, &value );
   473 	printf( "SDL_GL_RED_SIZE: requested %d, got %d\n", rgb_size[0],value);
   474 	SDL_GL_GetAttribute( SDL_GL_GREEN_SIZE, &value );
   475 	printf( "SDL_GL_GREEN_SIZE: requested %d, got %d\n", rgb_size[1],value);
   476 	SDL_GL_GetAttribute( SDL_GL_BLUE_SIZE, &value );
   477 	printf( "SDL_GL_BLUE_SIZE: requested %d, got %d\n", rgb_size[2],value);
   478 	SDL_GL_GetAttribute( SDL_GL_DEPTH_SIZE, &value );
   479 	printf( "SDL_GL_DEPTH_SIZE: requested %d, got %d\n", bpp, value );
   480 	SDL_GL_GetAttribute( SDL_GL_DOUBLEBUFFER, &value );
   481 	printf( "SDL_GL_DOUBLEBUFFER: requested 1, got %d\n", value );
   482 	if ( fsaa ) {
   483 		SDL_GL_GetAttribute( SDL_GL_MULTISAMPLEBUFFERS, &value );
   484 		printf("SDL_GL_MULTISAMPLEBUFFERS: requested 1, got %d\n", value );
   485 		SDL_GL_GetAttribute( SDL_GL_MULTISAMPLESAMPLES, &value );
   486 		printf("SDL_GL_MULTISAMPLESAMPLES: requested %d, got %d\n", fsaa, value );
   487 	}
   488 	if ( accel ) {
   489 		SDL_GL_GetAttribute( SDL_GL_ACCELERATED_VISUAL, &value );
   490 		printf( "SDL_GL_ACCELERATED_VISUAL: requested 1, got %d\n", value );
   491 	}
   492 	if ( sync ) {
   493 		SDL_GL_GetAttribute( SDL_GL_SWAP_CONTROL, &value );
   494 		printf( "SDL_GL_SWAP_CONTROL: requested 1, got %d\n", value );
   495 	}
   496 
   497 	/* Set the window manager title bar */
   498 	SDL_WM_SetCaption( "SDL GL test", "testgl" );
   499 
   500 	/* Set the gamma for the window */
   501 	if ( gamma != 0.0 ) {
   502 		SDL_SetGamma(gamma, gamma, gamma);
   503 	}
   504 
   505 	glViewport( 0, 0, w, h );
   506 	glMatrixMode( GL_PROJECTION );
   507 	glLoadIdentity( );
   508 
   509 	glOrtho( -2.0, 2.0, -2.0, 2.0, -20.0, 20.0 );
   510 
   511 	glMatrixMode( GL_MODELVIEW );
   512 	glLoadIdentity( );
   513 
   514 	glEnable(GL_DEPTH_TEST);
   515 
   516 	glDepthFunc(GL_LESS);
   517 
   518 	glShadeModel(GL_SMOOTH);
   519 
   520 	/* Loop until done. */
   521 	start_time = SDL_GetTicks();
   522 	frames = 0;
   523 	while( !done ) {
   524 		GLenum gl_error;
   525 		char* sdl_error;
   526 		SDL_Event event;
   527 
   528 		/* Do our drawing, too. */
   529 		glClearColor( 0.0, 0.0, 0.0, 1.0 );
   530 		glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   531 
   532 		glBegin( GL_QUADS );
   533 
   534 #ifdef SHADED_CUBE
   535 			glColor3fv(color[0]);
   536 			glVertex3fv(cube[0]);
   537 			glColor3fv(color[1]);
   538 			glVertex3fv(cube[1]);
   539 			glColor3fv(color[2]);
   540 			glVertex3fv(cube[2]);
   541 			glColor3fv(color[3]);
   542 			glVertex3fv(cube[3]);
   543 			
   544 			glColor3fv(color[3]);
   545 			glVertex3fv(cube[3]);
   546 			glColor3fv(color[4]);
   547 			glVertex3fv(cube[4]);
   548 			glColor3fv(color[7]);
   549 			glVertex3fv(cube[7]);
   550 			glColor3fv(color[2]);
   551 			glVertex3fv(cube[2]);
   552 			
   553 			glColor3fv(color[0]);
   554 			glVertex3fv(cube[0]);
   555 			glColor3fv(color[5]);
   556 			glVertex3fv(cube[5]);
   557 			glColor3fv(color[6]);
   558 			glVertex3fv(cube[6]);
   559 			glColor3fv(color[1]);
   560 			glVertex3fv(cube[1]);
   561 			
   562 			glColor3fv(color[5]);
   563 			glVertex3fv(cube[5]);
   564 			glColor3fv(color[4]);
   565 			glVertex3fv(cube[4]);
   566 			glColor3fv(color[7]);
   567 			glVertex3fv(cube[7]);
   568 			glColor3fv(color[6]);
   569 			glVertex3fv(cube[6]);
   570 
   571 			glColor3fv(color[5]);
   572 			glVertex3fv(cube[5]);
   573 			glColor3fv(color[0]);
   574 			glVertex3fv(cube[0]);
   575 			glColor3fv(color[3]);
   576 			glVertex3fv(cube[3]);
   577 			glColor3fv(color[4]);
   578 			glVertex3fv(cube[4]);
   579 
   580 			glColor3fv(color[6]);
   581 			glVertex3fv(cube[6]);
   582 			glColor3fv(color[1]);
   583 			glVertex3fv(cube[1]);
   584 			glColor3fv(color[2]);
   585 			glVertex3fv(cube[2]);
   586 			glColor3fv(color[7]);
   587 			glVertex3fv(cube[7]);
   588 #else /* flat cube */
   589 			glColor3f(1.0, 0.0, 0.0);
   590 			glVertex3fv(cube[0]);
   591 			glVertex3fv(cube[1]);
   592 			glVertex3fv(cube[2]);
   593 			glVertex3fv(cube[3]);
   594 			
   595 			glColor3f(0.0, 1.0, 0.0);
   596 			glVertex3fv(cube[3]);
   597 			glVertex3fv(cube[4]);
   598 			glVertex3fv(cube[7]);
   599 			glVertex3fv(cube[2]);
   600 			
   601 			glColor3f(0.0, 0.0, 1.0);
   602 			glVertex3fv(cube[0]);
   603 			glVertex3fv(cube[5]);
   604 			glVertex3fv(cube[6]);
   605 			glVertex3fv(cube[1]);
   606 			
   607 			glColor3f(0.0, 1.0, 1.0);
   608 			glVertex3fv(cube[5]);
   609 			glVertex3fv(cube[4]);
   610 			glVertex3fv(cube[7]);
   611 			glVertex3fv(cube[6]);
   612 
   613 			glColor3f(1.0, 1.0, 0.0);
   614 			glVertex3fv(cube[5]);
   615 			glVertex3fv(cube[0]);
   616 			glVertex3fv(cube[3]);
   617 			glVertex3fv(cube[4]);
   618 
   619 			glColor3f(1.0, 0.0, 1.0);
   620 			glVertex3fv(cube[6]);
   621 			glVertex3fv(cube[1]);
   622 			glVertex3fv(cube[2]);
   623 			glVertex3fv(cube[7]);
   624 #endif /* SHADED_CUBE */
   625 
   626 		glEnd( );
   627 		
   628 		glMatrixMode(GL_MODELVIEW);
   629 		glRotatef(5.0, 1.0, 1.0, 1.0);
   630 
   631 		/* Draw 2D logo onto the 3D display */
   632 		if ( logo ) {
   633 			DrawLogoTexture();
   634 		}
   635 		if ( logocursor ) {
   636 			DrawLogoCursor();
   637 		}
   638 
   639 		SDL_GL_SwapBuffers( );
   640 
   641 		/* Check for error conditions. */
   642 		gl_error = glGetError( );
   643 
   644 		if( gl_error != GL_NO_ERROR ) {
   645 			fprintf( stderr, "testgl: OpenGL error: %d\n", gl_error );
   646 		}
   647 
   648 		sdl_error = SDL_GetError( );
   649 
   650 		if( sdl_error[0] != '\0' ) {
   651 			fprintf(stderr, "testgl: SDL error '%s'\n", sdl_error);
   652 			SDL_ClearError();
   653 		}
   654 
   655 		/* Allow the user to see what's happening */
   656 		if ( slowly ) {
   657 			SDL_Delay( 20 );
   658 		}
   659 
   660 		/* Check if there's a pending event. */
   661 		while( SDL_PollEvent( &event ) ) {
   662 			done = HandleEvent(&event);
   663 		}
   664 		++frames;
   665 	}
   666 
   667 	/* Print out the frames per second */
   668 	this_time = SDL_GetTicks();
   669 	if ( this_time != start_time ) {
   670 		printf("%2.2f FPS\n",
   671 			((float)frames/(this_time-start_time))*1000.0);
   672 	}
   673 
   674 	if ( global_image ) {
   675 	   	SDL_FreeSurface(global_image);
   676 		global_image = NULL;
   677 	}
   678 	if ( global_texture ) {
   679 		glDeleteTextures( 1, &global_texture );
   680 		global_texture = 0;
   681 	}
   682 	if ( cursor_texture ) {
   683 		glDeleteTextures( 1, &cursor_texture );
   684 		cursor_texture = 0;
   685 	}
   686 
   687 	/* Destroy our GL context, etc. */
   688 	SDL_Quit( );
   689 	return(0);
   690 }
   691 
   692 int main(int argc, char *argv[])
   693 {
   694 	int i, logo, logocursor = 0;
   695 	int numtests;
   696 	int bpp = 0;
   697 	int slowly;
   698 	float gamma = 0.0;
   699 	int noframe = 0;
   700 	int fsaa = 0;
   701 	int accel = 0;
   702 	int sync = 0;
   703 
   704 	logo = 0;
   705 	slowly = 0;
   706 	numtests = 1;
   707 	for ( i=1; argv[i]; ++i ) {
   708 		if ( strcmp(argv[i], "-twice") == 0 ) {
   709 			++numtests;
   710 		}
   711 		if ( strcmp(argv[i], "-logo") == 0 ) {
   712 			logo = 1;
   713 		}
   714 		if ( strcmp(argv[i], "-logocursor") == 0 ) {
   715 			logocursor = 1;
   716 		}
   717 		if ( strcmp(argv[i], "-slow") == 0 ) {
   718 			slowly = 1;
   719 		}
   720 		if ( strcmp(argv[i], "-bpp") == 0 ) {
   721  		       bpp = atoi(argv[++i]);
   722 		}
   723 		if ( strcmp(argv[i], "-gamma") == 0 ) {
   724  		       gamma = (float)atof(argv[++i]);
   725 		}
   726 		if ( strcmp(argv[i], "-noframe") == 0 ) {
   727  		       noframe = 1;
   728 		}
   729 		if ( strcmp(argv[i], "-fsaa") == 0 ) {
   730  		       ++fsaa;
   731 		}
   732 		if ( strcmp(argv[i], "-accel") == 0 ) {
   733  		       ++accel;
   734 		}
   735 		if ( strcmp(argv[i], "-sync") == 0 ) {
   736  		       ++sync;
   737 		}
   738 		if ( strncmp(argv[i], "-h", 2) == 0 ) {
   739  		       printf(
   740 "Usage: %s [-twice] [-logo] [-logocursor] [-slow] [-bpp n] [-gamma n] [-noframe] [-fsaa] [-fullscreen]\n",
   741  			      argv[0]);
   742 			exit(0);
   743 		}
   744 	}
   745 	for ( i=0; i<numtests; ++i ) {
   746  		RunGLTest(argc, argv, logo, logocursor, slowly, bpp, gamma, noframe, fsaa, sync, accel);
   747 	}
   748 	return 0;
   749 }
   750 
   751 #else /* HAVE_OPENGL */
   752 
   753 int main(int argc, char *argv[])
   754 {
   755 	printf("No OpenGL support on this system\n");
   756 	return 1;
   757 }
   758 
   759 #endif /* HAVE_OPENGL */