test/testgl.c
author Ryan C. Gordon <icculus@icculus.org>
Fri, 15 Jun 2007 07:19:05 +0000
branchSDL-1.2
changeset 3965 19d708301fab
parent 1880 9d940db55a91
child 1895 c121d94672cb
permissions -rw-r--r--
Make sure __inline__ isn't defined before #defining it, since some compilers
(like Sun Studio) have this as a predefined macro.

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