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