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