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