test/testgl.c
changeset 0 74212992fb08
child 214 0e5d6dd77bda
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/testgl.c	Thu Apr 26 16:45:43 2001 +0000
     1.3 @@ -0,0 +1,519 @@
     1.4 +#include <stdlib.h>
     1.5 +#include <stdio.h>
     1.6 +#include <string.h>
     1.7 +#include <math.h>
     1.8 +
     1.9 +#include "SDL.h"
    1.10 +
    1.11 +#ifdef HAVE_OPENGL
    1.12 +#ifdef WIN32
    1.13 +#include <windows.h>
    1.14 +#endif
    1.15 +#if defined(__APPLE__) && defined(__MACH__)
    1.16 +#include <OpenGL/gl.h>
    1.17 +#else
    1.18 +#include <GL/gl.h>
    1.19 +#endif
    1.20 +
    1.21 +#define SHADED_CUBE
    1.22 +
    1.23 +
    1.24 +void HotKey_ToggleFullScreen(void)
    1.25 +{
    1.26 +	SDL_Surface *screen;
    1.27 +
    1.28 +	screen = SDL_GetVideoSurface();
    1.29 +	if ( SDL_WM_ToggleFullScreen(screen) ) {
    1.30 +		printf("Toggled fullscreen mode - now %s\n",
    1.31 +		    (screen->flags&SDL_FULLSCREEN) ? "fullscreen" : "windowed");
    1.32 +	} else {
    1.33 +		printf("Unable to toggle fullscreen mode\n");
    1.34 +	}
    1.35 +}
    1.36 +
    1.37 +void HotKey_ToggleGrab(void)
    1.38 +{
    1.39 +	SDL_GrabMode mode;
    1.40 +
    1.41 +	printf("Ctrl-G: toggling input grab!\n");
    1.42 +	mode = SDL_WM_GrabInput(SDL_GRAB_QUERY);
    1.43 +	if ( mode == SDL_GRAB_ON ) {
    1.44 +		printf("Grab was on\n");
    1.45 +	} else {
    1.46 +		printf("Grab was off\n");
    1.47 +	}
    1.48 +	mode = SDL_WM_GrabInput(!mode);
    1.49 +	if ( mode == SDL_GRAB_ON ) {
    1.50 +		printf("Grab is now on\n");
    1.51 +	} else {
    1.52 +		printf("Grab is now off\n");
    1.53 +	}
    1.54 +}
    1.55 +
    1.56 +void HotKey_Iconify(void)
    1.57 +{
    1.58 +	printf("Ctrl-Z: iconifying window!\n");
    1.59 +	SDL_WM_IconifyWindow();
    1.60 +}
    1.61 +
    1.62 +int HandleEvent(SDL_Event *event)
    1.63 +{
    1.64 +	int done;
    1.65 +
    1.66 +	done = 0;
    1.67 +	switch( event->type ) {
    1.68 +	    case SDL_ACTIVEEVENT:
    1.69 +		/* See what happened */
    1.70 +		printf( "app %s ", event->active.gain ? "gained" : "lost" );
    1.71 +		if ( event->active.state & SDL_APPACTIVE ) {
    1.72 +			printf( "active " );
    1.73 +		} else if ( event->active.state & SDL_APPMOUSEFOCUS ) {
    1.74 +			printf( "mouse " );
    1.75 +		} else if ( event->active.state & SDL_APPINPUTFOCUS ) {
    1.76 +			printf( "input " );
    1.77 +		}
    1.78 +		printf( "focus\n" );
    1.79 +		break;
    1.80 +		
    1.81 +
    1.82 +	    case SDL_KEYDOWN:
    1.83 +		if ( event->key.keysym.sym == SDLK_ESCAPE ) {
    1.84 +			done = 1;
    1.85 +		}
    1.86 +		if ( (event->key.keysym.sym == SDLK_g) &&
    1.87 +		     (event->key.keysym.mod & KMOD_CTRL) ) {
    1.88 +			HotKey_ToggleGrab();
    1.89 +		}
    1.90 +		if ( (event->key.keysym.sym == SDLK_z) &&
    1.91 +		     (event->key.keysym.mod & KMOD_CTRL) ) {
    1.92 +			HotKey_Iconify();
    1.93 +		}
    1.94 +		if ( (event->key.keysym.sym == SDLK_RETURN) &&
    1.95 +		     (event->key.keysym.mod & KMOD_ALT) ) {
    1.96 +			HotKey_ToggleFullScreen();
    1.97 +		}
    1.98 +		printf("key '%s' pressed\n", 
    1.99 +			SDL_GetKeyName(event->key.keysym.sym));
   1.100 +		break;
   1.101 +	    case SDL_QUIT:
   1.102 +		done = 1;
   1.103 +		break;
   1.104 +	}
   1.105 +	return(done);
   1.106 +}
   1.107 +
   1.108 +void DrawSDLLogo(void)
   1.109 +{
   1.110 +	static SDL_Surface *image = NULL;
   1.111 +	static int x = 0;
   1.112 +	static int y = 0;
   1.113 +	static int delta_x = 1;
   1.114 +	static int delta_y = 1;
   1.115 +	static Uint32 last_moved = 0;
   1.116 +
   1.117 +	SDL_Rect dst;
   1.118 +	SDL_Surface *screen;
   1.119 +
   1.120 +	if ( image == NULL ) {
   1.121 +		SDL_Surface *temp;
   1.122 +
   1.123 +		temp = SDL_LoadBMP("icon.bmp");
   1.124 +		if ( temp == NULL ) {
   1.125 +			return;
   1.126 +		}
   1.127 +		image = SDL_CreateRGBSurface(
   1.128 +				SDL_SWSURFACE,
   1.129 +				temp->w, temp->h,
   1.130 +				32,
   1.131 +#if SDL_BYTEORDER == SDL_LIL_ENDIAN
   1.132 +				0x000000FF, 
   1.133 +				0x0000FF00, 
   1.134 +				0x00FF0000, 
   1.135 + 			       0xFF000000
   1.136 +#else
   1.137 + 			       0xFF000000,
   1.138 + 			       0x00FF0000, 
   1.139 + 			       0x0000FF00, 
   1.140 + 			       0x000000FF
   1.141 +#endif
   1.142 + 			       );
   1.143 +		if ( image != NULL ) {
   1.144 +			SDL_BlitSurface(temp, NULL, image, NULL);
   1.145 +		}
   1.146 +		SDL_FreeSurface(temp);
   1.147 +		if ( image == NULL ) {
   1.148 +			return;
   1.149 +		}
   1.150 +	}
   1.151 +
   1.152 +	screen = SDL_GetVideoSurface();
   1.153 +
   1.154 +	/* Show the image on the screen */
   1.155 +	dst.x = x;
   1.156 +	dst.y = y;
   1.157 +	dst.w = image->w;
   1.158 +	dst.h = image->h;
   1.159 +
   1.160 +	/* Move it around
   1.161 +           Note that we do not clear the old position.  This is because we
   1.162 +           perform a glClear() which clears the framebuffer and then only
   1.163 +           update the new area.
   1.164 +           Note that you can also achieve interesting effects by modifying
   1.165 +           the screen surface alpha channel.  It's set to 255 by default..
   1.166 +         */
   1.167 +	if ( (SDL_GetTicks() - last_moved) > 100 ) {
   1.168 +		x += delta_x;
   1.169 +		if ( x < 0 ) {
   1.170 +			x = 0;
   1.171 +			delta_x = -delta_x;
   1.172 +		} else
   1.173 +		if ( (x+image->w) > screen->w ) {
   1.174 +			x = screen->w-image->w;
   1.175 +			delta_x = -delta_x;
   1.176 +		}
   1.177 +		y += delta_y;
   1.178 +		if ( y < 0 ) {
   1.179 +			y = 0;
   1.180 +			delta_y = -delta_y;
   1.181 +		} else
   1.182 +		if ( (y+image->h) > screen->h ) {
   1.183 +			y = screen->h-image->h;
   1.184 +			delta_y = -delta_y;
   1.185 +		}
   1.186 +		SDL_BlitSurface(image, NULL, screen, &dst);
   1.187 +	}
   1.188 +	SDL_UpdateRects(screen, 1, &dst);
   1.189 +}
   1.190 +
   1.191 +int RunGLTest( int argc, char* argv[],
   1.192 +               int logo, int slowly, int bpp, float gamma )
   1.193 +{
   1.194 +	int i;
   1.195 +	int rgb_size[3];
   1.196 +	int w = 640;
   1.197 +	int h = 480;
   1.198 +	int done = 0;
   1.199 +	int frames;
   1.200 +	Uint32 start_time, this_time;
   1.201 +        float color[8][3]= {{ 1.0,  1.0,  0.0}, 
   1.202 +			    { 1.0,  0.0,  0.0},
   1.203 +			    { 0.0,  0.0,  0.0},
   1.204 +			    { 0.0,  1.0,  0.0},
   1.205 +			    { 0.0,  1.0,  1.0},
   1.206 +			    { 1.0,  1.0,  1.0},
   1.207 +			    { 1.0,  0.0,  1.0},
   1.208 +			    { 0.0,  0.0,  1.0}};
   1.209 +	float cube[8][3]= {{ 0.5,  0.5, -0.5}, 
   1.210 +			   { 0.5, -0.5, -0.5},
   1.211 +			   {-0.5, -0.5, -0.5},
   1.212 +			   {-0.5,  0.5, -0.5},
   1.213 +			   {-0.5,  0.5,  0.5},
   1.214 +			   { 0.5,  0.5,  0.5},
   1.215 +			   { 0.5, -0.5,  0.5},
   1.216 +			   {-0.5, -0.5,  0.5}};
   1.217 +	Uint32 video_flags;
   1.218 +	int value;
   1.219 +
   1.220 +	if( SDL_Init( SDL_INIT_VIDEO ) < 0 ) {
   1.221 +		fprintf(stderr,"Couldn't initialize SDL: %s\n",SDL_GetError());
   1.222 +		exit( 1 );
   1.223 +	}
   1.224 +
   1.225 +	/* See if we should detect the display depth */
   1.226 +	if ( bpp == 0 ) {
   1.227 +		if ( SDL_GetVideoInfo()->vfmt->BitsPerPixel <= 8 ) {
   1.228 +			bpp = 8;
   1.229 +		} else {
   1.230 +			bpp = 16;  /* More doesn't seem to work */
   1.231 +		}
   1.232 +	}
   1.233 +
   1.234 +	/* Set the flags we want to use for setting the video mode */
   1.235 +	if ( logo ) {
   1.236 +		video_flags = SDL_OPENGLBLIT;
   1.237 +	} else {
   1.238 +		video_flags = SDL_OPENGL;
   1.239 +	}
   1.240 +	for ( i=1; argv[i]; ++i ) {
   1.241 +		if ( strcmp(argv[1], "-fullscreen") == 0 ) {
   1.242 +			video_flags |= SDL_FULLSCREEN;
   1.243 +		}
   1.244 +	}
   1.245 +
   1.246 +	/* Initialize the display */
   1.247 +	switch (bpp) {
   1.248 +	    case 8:
   1.249 +		rgb_size[0] = 2;
   1.250 +		rgb_size[1] = 3;
   1.251 +		rgb_size[2] = 3;
   1.252 +		break;
   1.253 +	    case 15:
   1.254 +	    case 16:
   1.255 +		rgb_size[0] = 5;
   1.256 +		rgb_size[1] = 5;
   1.257 +		rgb_size[2] = 5;
   1.258 +		break;
   1.259 +            default:
   1.260 +		rgb_size[0] = 8;
   1.261 +		rgb_size[1] = 8;
   1.262 +		rgb_size[2] = 8;
   1.263 +		break;
   1.264 +	}
   1.265 +	SDL_GL_SetAttribute( SDL_GL_RED_SIZE, rgb_size[0] );
   1.266 +	SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, rgb_size[1] );
   1.267 +	SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, rgb_size[2] );
   1.268 +	SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
   1.269 +	SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
   1.270 +	if ( SDL_SetVideoMode( w, h, bpp, video_flags ) == NULL ) {
   1.271 +		fprintf(stderr, "Couldn't set GL mode: %s\n", SDL_GetError());
   1.272 +		SDL_Quit();
   1.273 +		exit(1);
   1.274 +	}
   1.275 +
   1.276 +	printf("Screen BPP: %d\n", SDL_GetVideoSurface()->format->BitsPerPixel);
   1.277 +	printf("\n");
   1.278 +	printf( "Vendor     : %s\n", glGetString( GL_VENDOR ) );
   1.279 +	printf( "Renderer   : %s\n", glGetString( GL_RENDERER ) );
   1.280 +	printf( "Version    : %s\n", glGetString( GL_VERSION ) );
   1.281 +	printf( "Extensions : %s\n", glGetString( GL_EXTENSIONS ) );
   1.282 +	printf("\n");
   1.283 +
   1.284 +	SDL_GL_GetAttribute( SDL_GL_RED_SIZE, &value );
   1.285 +	printf( "SDL_GL_RED_SIZE: requested %d, got %d\n", rgb_size[0],value);
   1.286 +	SDL_GL_GetAttribute( SDL_GL_GREEN_SIZE, &value );
   1.287 +	printf( "SDL_GL_GREEN_SIZE: requested %d, got %d\n", rgb_size[1],value);
   1.288 +	SDL_GL_GetAttribute( SDL_GL_BLUE_SIZE, &value );
   1.289 +	printf( "SDL_GL_BLUE_SIZE: requested %d, got %d\n", rgb_size[2],value);
   1.290 +	SDL_GL_GetAttribute( SDL_GL_DEPTH_SIZE, &value );
   1.291 +	printf( "SDL_GL_DEPTH_SIZE: requested %d, got %d\n", bpp, value );
   1.292 +	SDL_GL_GetAttribute( SDL_GL_DOUBLEBUFFER, &value );
   1.293 +	printf( "SDL_GL_DOUBLEBUFFER: requested 1, got %d\n", value );
   1.294 +
   1.295 +	/* Set the window manager title bar */
   1.296 +	SDL_WM_SetCaption( "SDL GL test", "testgl" );
   1.297 +
   1.298 +	/* Set the gamma for the window */
   1.299 +	if ( gamma != 0.0 ) {
   1.300 +		SDL_SetGamma(gamma, gamma, gamma);
   1.301 +	}
   1.302 +
   1.303 +	glViewport( 0, 0, w, h );
   1.304 +	glMatrixMode( GL_PROJECTION );
   1.305 +	glLoadIdentity( );
   1.306 +
   1.307 +	glOrtho( -2.0, 2.0, -2.0, 2.0, -20.0, 20.0 );
   1.308 +
   1.309 +	glMatrixMode( GL_MODELVIEW );
   1.310 +	glLoadIdentity( );
   1.311 +
   1.312 +	glEnable(GL_DEPTH_TEST);
   1.313 +
   1.314 +	glDepthFunc(GL_LESS);
   1.315 +
   1.316 +	glShadeModel(GL_SMOOTH);
   1.317 +
   1.318 +	/* Loop until done. */
   1.319 +	start_time = SDL_GetTicks();
   1.320 +	frames = 0;
   1.321 +	while( !done ) {
   1.322 +		GLenum gl_error;
   1.323 +		char* sdl_error;
   1.324 +		SDL_Event event;
   1.325 +
   1.326 +		/* Do our drawing, too. */
   1.327 +		glClearColor( 0.0, 0.0, 0.0, 1.0 );
   1.328 +		glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   1.329 +
   1.330 +		glBegin( GL_QUADS );
   1.331 +
   1.332 +#ifdef SHADED_CUBE
   1.333 +			glColor3fv(color[0]);
   1.334 +			glVertex3fv(cube[0]);
   1.335 +			glColor3fv(color[1]);
   1.336 +			glVertex3fv(cube[1]);
   1.337 +			glColor3fv(color[2]);
   1.338 +			glVertex3fv(cube[2]);
   1.339 +			glColor3fv(color[3]);
   1.340 +			glVertex3fv(cube[3]);
   1.341 +			
   1.342 +			glColor3fv(color[3]);
   1.343 +			glVertex3fv(cube[3]);
   1.344 +			glColor3fv(color[4]);
   1.345 +			glVertex3fv(cube[4]);
   1.346 +			glColor3fv(color[7]);
   1.347 +			glVertex3fv(cube[7]);
   1.348 +			glColor3fv(color[2]);
   1.349 +			glVertex3fv(cube[2]);
   1.350 +			
   1.351 +			glColor3fv(color[0]);
   1.352 +			glVertex3fv(cube[0]);
   1.353 +			glColor3fv(color[5]);
   1.354 +			glVertex3fv(cube[5]);
   1.355 +			glColor3fv(color[6]);
   1.356 +			glVertex3fv(cube[6]);
   1.357 +			glColor3fv(color[1]);
   1.358 +			glVertex3fv(cube[1]);
   1.359 +			
   1.360 +			glColor3fv(color[5]);
   1.361 +			glVertex3fv(cube[5]);
   1.362 +			glColor3fv(color[4]);
   1.363 +			glVertex3fv(cube[4]);
   1.364 +			glColor3fv(color[7]);
   1.365 +			glVertex3fv(cube[7]);
   1.366 +			glColor3fv(color[6]);
   1.367 +			glVertex3fv(cube[6]);
   1.368 +
   1.369 +			glColor3fv(color[5]);
   1.370 +			glVertex3fv(cube[5]);
   1.371 +			glColor3fv(color[0]);
   1.372 +			glVertex3fv(cube[0]);
   1.373 +			glColor3fv(color[3]);
   1.374 +			glVertex3fv(cube[3]);
   1.375 +			glColor3fv(color[4]);
   1.376 +			glVertex3fv(cube[4]);
   1.377 +
   1.378 +			glColor3fv(color[6]);
   1.379 +			glVertex3fv(cube[6]);
   1.380 +			glColor3fv(color[1]);
   1.381 +			glVertex3fv(cube[1]);
   1.382 +			glColor3fv(color[2]);
   1.383 +			glVertex3fv(cube[2]);
   1.384 +			glColor3fv(color[7]);
   1.385 +			glVertex3fv(cube[7]);
   1.386 +#else // flat cube
   1.387 +			glColor3f(1.0, 0.0, 0.0);
   1.388 +			glVertex3fv(cube[0]);
   1.389 +			glVertex3fv(cube[1]);
   1.390 +			glVertex3fv(cube[2]);
   1.391 +			glVertex3fv(cube[3]);
   1.392 +			
   1.393 +			glColor3f(0.0, 1.0, 0.0);
   1.394 +			glVertex3fv(cube[3]);
   1.395 +			glVertex3fv(cube[4]);
   1.396 +			glVertex3fv(cube[7]);
   1.397 +			glVertex3fv(cube[2]);
   1.398 +			
   1.399 +			glColor3f(0.0, 0.0, 1.0);
   1.400 +			glVertex3fv(cube[0]);
   1.401 +			glVertex3fv(cube[5]);
   1.402 +			glVertex3fv(cube[6]);
   1.403 +			glVertex3fv(cube[1]);
   1.404 +			
   1.405 +			glColor3f(0.0, 1.0, 1.0);
   1.406 +			glVertex3fv(cube[5]);
   1.407 +			glVertex3fv(cube[4]);
   1.408 +			glVertex3fv(cube[7]);
   1.409 +			glVertex3fv(cube[6]);
   1.410 +
   1.411 +			glColor3f(1.0, 1.0, 0.0);
   1.412 +			glVertex3fv(cube[5]);
   1.413 +			glVertex3fv(cube[0]);
   1.414 +			glVertex3fv(cube[3]);
   1.415 +			glVertex3fv(cube[4]);
   1.416 +
   1.417 +			glColor3f(1.0, 0.0, 1.0);
   1.418 +			glVertex3fv(cube[6]);
   1.419 +			glVertex3fv(cube[1]);
   1.420 +			glVertex3fv(cube[2]);
   1.421 +			glVertex3fv(cube[7]);
   1.422 +#endif /* SHADED_CUBE */
   1.423 +
   1.424 +		glEnd( );
   1.425 +		
   1.426 +		glMatrixMode(GL_MODELVIEW);
   1.427 +		glRotatef(5.0, 1.0, 1.0, 1.0);
   1.428 +
   1.429 +		/* Draw 2D logo onto the 3D display */
   1.430 +		if ( logo ) {
   1.431 +			DrawSDLLogo();
   1.432 +		}
   1.433 +
   1.434 +		SDL_GL_SwapBuffers( );
   1.435 +
   1.436 +		/* Check for error conditions. */
   1.437 +		gl_error = glGetError( );
   1.438 +
   1.439 +		if( gl_error != GL_NO_ERROR ) {
   1.440 +			fprintf( stderr, "testgl: OpenGL error: %d\n", gl_error );
   1.441 +		}
   1.442 +
   1.443 +		sdl_error = SDL_GetError( );
   1.444 +
   1.445 +		if( sdl_error[0] != '\0' ) {
   1.446 +			fprintf(stderr, "testgl: SDL error '%s'\n", sdl_error);
   1.447 +			SDL_ClearError();
   1.448 +		}
   1.449 +
   1.450 +		/* Allow the user to see what's happening */
   1.451 +		if ( slowly ) {
   1.452 +			SDL_Delay( 20 );
   1.453 +		}
   1.454 +
   1.455 +		/* Check if there's a pending event. */
   1.456 +		while( SDL_PollEvent( &event ) ) {
   1.457 +			done = HandleEvent(&event);
   1.458 +		}
   1.459 +		++frames;
   1.460 +	}
   1.461 +
   1.462 +	/* Print out the frames per second */
   1.463 +	this_time = SDL_GetTicks();
   1.464 +	if ( this_time != start_time ) {
   1.465 +		printf("%2.2f FPS\n",
   1.466 +			((float)frames/(this_time-start_time))*1000.0);
   1.467 +	}
   1.468 +
   1.469 +	/* Destroy our GL context, etc. */
   1.470 +	SDL_Quit( );
   1.471 +	return(0);
   1.472 +}
   1.473 +
   1.474 +int main(int argc, char *argv[])
   1.475 +{
   1.476 +	int i, logo;
   1.477 +	int numtests;
   1.478 +	int bpp = 0;
   1.479 +	int slowly;
   1.480 +	float gamma = 0.0;
   1.481 +
   1.482 +	logo = 0;
   1.483 +	slowly = 0;
   1.484 +	numtests = 1;
   1.485 +	for ( i=1; argv[i]; ++i ) {
   1.486 +		if ( strcmp(argv[i], "-twice") == 0 ) {
   1.487 +			++numtests;
   1.488 +		}
   1.489 +		if ( strcmp(argv[i], "-logo") == 0 ) {
   1.490 +			logo = 1;
   1.491 +		}
   1.492 +		if ( strcmp(argv[i], "-slow") == 0 ) {
   1.493 +			slowly = 1;
   1.494 +		}
   1.495 +		if ( strcmp(argv[i], "-bpp") == 0 ) {
   1.496 + 		       bpp = atoi(argv[++i]);
   1.497 +		}
   1.498 +		if ( strcmp(argv[i], "-gamma") == 0 ) {
   1.499 + 		       gamma = (float)atof(argv[++i]);
   1.500 +		}
   1.501 +		if ( strncmp(argv[i], "-h", 2) == 0 ) {
   1.502 + 		       printf(
   1.503 +"Usage: %s [-twice] [-logo] [-slow] [-bpp n] [-gamma n]\n",
   1.504 + 			      argv[0]);
   1.505 +			exit(0);
   1.506 +		}
   1.507 +	}
   1.508 +	for ( i=0; i<numtests; ++i ) {
   1.509 + 	       RunGLTest(argc, argv, logo, slowly, bpp, gamma);
   1.510 +	}
   1.511 +	return 0;
   1.512 +}
   1.513 +
   1.514 +#else /* HAVE_OPENGL */
   1.515 +
   1.516 +int main(int argc, char *argv[])
   1.517 +{
   1.518 +	printf("No OpenGL support on this system\n");
   1.519 +	return 1;
   1.520 +}
   1.521 +
   1.522 +#endif /* HAVE_OPENGL */