test/testwin.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 29 Feb 2008 13:58:37 +0000
branchSDL-1.2
changeset 4141 8b10a6efad41
parent 4000 9776ab9063de
permissions -rw-r--r--
Added patch note for Sylvain's patch
slouken@0
     1
slouken@4000
     2
/* Bring up a window and play with it */
slouken@0
     3
slouken@4000
     4
#include <stdlib.h>
slouken@4000
     5
#include <stdio.h>
slouken@4000
     6
#include <string.h>
slouken@0
     7
slouken@4000
     8
#define BENCHMARK_SDL
slouken@0
     9
slouken@4000
    10
#define NOTICE(X)	printf("%s", X);
slouken@4000
    11
slouken@4000
    12
#include "SDL.h"
slouken@4000
    13
slouken@4000
    14
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
slouken@4000
    15
static void quit(int rc)
slouken@3999
    16
{
slouken@4000
    17
	SDL_Quit();
slouken@4000
    18
	exit(rc);
slouken@4000
    19
}
slouken@0
    20
slouken@4000
    21
void DrawPict(SDL_Surface *screen, char *bmpfile,
slouken@4000
    22
					int speedy, int flip, int nofade)
slouken@4000
    23
{
slouken@4000
    24
	SDL_Surface *picture;
slouken@4000
    25
	SDL_Rect dest, update;
slouken@4000
    26
	int i, centered;
slouken@4000
    27
	int ncolors;
slouken@4000
    28
	SDL_Color *colors, *cmap;
slouken@0
    29
slouken@4000
    30
	/* Load the image into a surface */
slouken@4000
    31
	if ( bmpfile == NULL ) {
slouken@4000
    32
		bmpfile = "sample.bmp";		/* Sample image */
slouken@4000
    33
	}
slouken@4000
    34
fprintf(stderr, "Loading picture: %s\n", bmpfile);
slouken@4000
    35
	picture = SDL_LoadBMP(bmpfile);
slouken@4000
    36
	if ( picture == NULL ) {
slouken@4000
    37
		fprintf(stderr, "Couldn't load %s: %s\n", bmpfile,
slouken@4000
    38
							SDL_GetError());
slouken@4000
    39
		return;
slouken@4000
    40
	}
slouken@3999
    41
slouken@4000
    42
	/* Set the display colors -- on a hicolor display this is a no-op */
slouken@4000
    43
	if ( picture->format->palette ) {
slouken@4000
    44
		ncolors = picture->format->palette->ncolors;
slouken@4000
    45
		colors  = (SDL_Color *)malloc(ncolors*sizeof(SDL_Color));
slouken@4000
    46
		cmap    = (SDL_Color *)malloc(ncolors*sizeof(SDL_Color));
slouken@4000
    47
		memcpy(colors, picture->format->palette->colors,
slouken@4000
    48
						ncolors*sizeof(SDL_Color));
slouken@4000
    49
	} else {
slouken@4000
    50
		int       r, g, b;
slouken@3999
    51
slouken@4000
    52
		/* Allocate 256 color palette */
slouken@4000
    53
		ncolors = 256;
slouken@4000
    54
		colors  = (SDL_Color *)malloc(ncolors*sizeof(SDL_Color));
slouken@4000
    55
		cmap    = (SDL_Color *)malloc(ncolors*sizeof(SDL_Color));
slouken@4000
    56
slouken@4000
    57
		/* Set a 3,3,2 color cube */
slouken@4000
    58
		for ( r=0; r<8; ++r ) {
slouken@4000
    59
			for ( g=0; g<8; ++g ) {
slouken@4000
    60
				for ( b=0; b<4; ++b ) {
slouken@4000
    61
					i = ((r<<5)|(g<<2)|b);
slouken@4000
    62
					colors[i].r = r<<5;
slouken@4000
    63
					colors[i].g = g<<5;
slouken@4000
    64
					colors[i].b = b<<6;
slouken@4000
    65
				}
slouken@4000
    66
			}
slouken@4000
    67
		}
slouken@4000
    68
	}
slouken@4000
    69
NOTICE("testwin: setting colors\n");
slouken@4000
    70
	if ( ! SDL_SetColors(screen, colors, 0, ncolors) &&
slouken@4000
    71
				(screen->format->palette != NULL) ) {
slouken@4000
    72
		fprintf(stderr,
slouken@4000
    73
"Warning: Couldn't set all of the colors, but SDL will map the image\n"
slouken@4000
    74
"         (colormap fading will suffer - try the -warp option)\n"
slouken@4000
    75
		);
slouken@4000
    76
	}
slouken@4000
    77
slouken@4000
    78
	/* Set the screen to black (not really necessary) */
slouken@4000
    79
	if ( SDL_LockSurface(screen) == 0 ) {
slouken@4000
    80
		Uint32 black;
slouken@4000
    81
		Uint8 *pixels;
slouken@4000
    82
slouken@4000
    83
		black = SDL_MapRGB(screen->format, 0, 0, 0);
slouken@4000
    84
		pixels = (Uint8 *)screen->pixels;
slouken@4000
    85
		for ( i=0; i<screen->h; ++i ) {
slouken@4000
    86
			memset(pixels, black,
slouken@4000
    87
				screen->w*screen->format->BytesPerPixel);
slouken@4000
    88
			pixels += screen->pitch;
slouken@4000
    89
		}
slouken@4000
    90
		SDL_UnlockSurface(screen);
slouken@4000
    91
		SDL_UpdateRect(screen, 0, 0, 0, 0);
slouken@4000
    92
	}
slouken@4000
    93
	
slouken@4000
    94
	/* Display the picture */
slouken@4000
    95
	if ( speedy ) {
slouken@4000
    96
		SDL_Surface *displayfmt;
slouken@4000
    97
slouken@4000
    98
fprintf(stderr, "Converting picture\n");
slouken@4000
    99
		displayfmt = SDL_DisplayFormat(picture);
slouken@4000
   100
		if ( displayfmt == NULL ) {
slouken@4000
   101
			fprintf(stderr,
slouken@4000
   102
				"Couldn't convert image: %s\n", SDL_GetError());
slouken@4000
   103
			goto done;
slouken@4000
   104
		}
slouken@4000
   105
		SDL_FreeSurface(picture);
slouken@4000
   106
		picture = displayfmt;
slouken@4000
   107
	}
slouken@4000
   108
	printf("(image surface located in %s memory)\n", 
slouken@4000
   109
			(picture->flags&SDL_HWSURFACE) ? "video" : "system");
slouken@4000
   110
	centered = (screen->w - picture->w)/2;
slouken@4000
   111
	if ( centered < 0 ) {
slouken@4000
   112
		centered = 0;
slouken@4000
   113
	}
slouken@4000
   114
	dest.y = (screen->h - picture->h)/2;
slouken@4000
   115
	dest.w = picture->w;
slouken@4000
   116
	dest.h = picture->h;
slouken@4000
   117
NOTICE("testwin: moving image\n");
slouken@4000
   118
	for ( i=0; i<=centered; ++i ) {
slouken@4000
   119
		dest.x = i;
slouken@4000
   120
		update = dest;
slouken@4000
   121
		if ( SDL_BlitSurface(picture, NULL, screen, &update) < 0 ) {
slouken@4000
   122
			fprintf(stderr, "Blit failed: %s\n", SDL_GetError());
slouken@4000
   123
			break;
slouken@4000
   124
		}
slouken@4000
   125
		if ( flip ) {
slouken@4000
   126
			SDL_Flip(screen);
slouken@4000
   127
		} else {
slouken@4000
   128
			SDL_UpdateRects(screen, 1, &update);
slouken@4000
   129
		}
slouken@4000
   130
	}
slouken@4000
   131
slouken@4000
   132
#ifdef SCREENSHOT
slouken@4000
   133
	if ( SDL_SaveBMP(screen, "screen.bmp") < 0 )
slouken@4000
   134
		printf("Couldn't save screen: %s\n", SDL_GetError());
slouken@4000
   135
#endif
slouken@4000
   136
slouken@4000
   137
#ifndef BENCHMARK_SDL
slouken@4000
   138
	/* Let it sit there for a while */
slouken@4000
   139
	SDL_Delay(5*1000);
slouken@4000
   140
#endif
slouken@4000
   141
	/* Fade the colormap */
slouken@4000
   142
	if ( ! nofade ) {
slouken@4000
   143
		int maxstep;
slouken@4000
   144
		SDL_Color final;
slouken@4000
   145
		SDL_Color palcolors[256];
slouken@4000
   146
		struct {
slouken@4000
   147
			Sint16 r, g, b;
slouken@4000
   148
		} cdist[256];
slouken@4000
   149
slouken@4000
   150
NOTICE("testwin: fading out...\n");
slouken@4000
   151
		memcpy(cmap, colors, ncolors*sizeof(SDL_Color));
slouken@4000
   152
		maxstep = 32-1;
slouken@4000
   153
		final.r = 0xFF;
slouken@4000
   154
		final.g = 0x00;
slouken@4000
   155
		final.b = 0x00;
slouken@4000
   156
		memcpy(palcolors, colors, ncolors*sizeof(SDL_Color));
slouken@4000
   157
		for ( i=0; i<ncolors; ++i ) {
slouken@4000
   158
			cdist[i].r = final.r-palcolors[i].r;
slouken@4000
   159
			cdist[i].g = final.g-palcolors[i].g;
slouken@4000
   160
			cdist[i].b = final.b-palcolors[i].b;
slouken@4000
   161
		}
slouken@4000
   162
		for ( i=0; i<=maxstep/2; ++i ) {	/* halfway fade */
slouken@4000
   163
			int c;
slouken@4000
   164
			for ( c=0; c<ncolors; ++c ) {
slouken@4000
   165
				colors[c].r =
slouken@4000
   166
					palcolors[c].r+((cdist[c].r*i))/maxstep;
slouken@4000
   167
				colors[c].g =
slouken@4000
   168
					palcolors[c].g+((cdist[c].g*i))/maxstep;
slouken@4000
   169
				colors[c].b =
slouken@4000
   170
					palcolors[c].b+((cdist[c].b*i))/maxstep;
slouken@4000
   171
			}
slouken@4000
   172
			SDL_SetColors(screen, colors, 0, ncolors);
slouken@4000
   173
			SDL_Delay(1);
slouken@4000
   174
		}
slouken@4000
   175
		final.r = 0x00;
slouken@4000
   176
		final.g = 0x00;
slouken@4000
   177
		final.b = 0x00;
slouken@4000
   178
		memcpy(palcolors, colors, ncolors*sizeof(SDL_Color));
slouken@4000
   179
		for ( i=0; i<ncolors; ++i ) {
slouken@4000
   180
			cdist[i].r = final.r-palcolors[i].r;
slouken@4000
   181
			cdist[i].g = final.g-palcolors[i].g;
slouken@4000
   182
			cdist[i].b = final.b-palcolors[i].b;
slouken@4000
   183
		}
slouken@4000
   184
		maxstep /= 2;
slouken@4000
   185
		for ( i=0; i<=maxstep; ++i ) {		/* finish fade out */
slouken@4000
   186
			int c;
slouken@4000
   187
			for ( c=0; c<ncolors; ++c ) {
slouken@4000
   188
				colors[c].r =
slouken@4000
   189
					palcolors[c].r+((cdist[c].r*i))/maxstep;
slouken@4000
   190
				colors[c].g =
slouken@4000
   191
					palcolors[c].g+((cdist[c].g*i))/maxstep;
slouken@4000
   192
				colors[c].b =
slouken@4000
   193
					palcolors[c].b+((cdist[c].b*i))/maxstep;
slouken@4000
   194
			}
slouken@4000
   195
			SDL_SetColors(screen, colors, 0, ncolors);
slouken@4000
   196
			SDL_Delay(1);
slouken@4000
   197
		}
slouken@4000
   198
		for ( i=0; i<ncolors; ++i ) {
slouken@4000
   199
			colors[i].r = final.r;
slouken@4000
   200
			colors[i].g = final.g;
slouken@4000
   201
			colors[i].b = final.b;
slouken@4000
   202
		}
slouken@4000
   203
		SDL_SetColors(screen, colors, 0, ncolors);
slouken@4000
   204
NOTICE("testwin: fading in...\n");
slouken@4000
   205
		memcpy(palcolors, colors, ncolors*sizeof(SDL_Color));
slouken@4000
   206
		for ( i=0; i<ncolors; ++i ) {
slouken@4000
   207
			cdist[i].r = cmap[i].r-palcolors[i].r;
slouken@4000
   208
			cdist[i].g = cmap[i].g-palcolors[i].g;
slouken@4000
   209
			cdist[i].b = cmap[i].b-palcolors[i].b;
slouken@4000
   210
		}
slouken@4000
   211
		for ( i=0; i<=maxstep; ++i ) {	/* 32 step fade in */
slouken@4000
   212
			int c;
slouken@4000
   213
			for ( c=0; c<ncolors; ++c ) {
slouken@4000
   214
				colors[c].r =
slouken@4000
   215
					palcolors[c].r+((cdist[c].r*i))/maxstep;
slouken@4000
   216
				colors[c].g =
slouken@4000
   217
					palcolors[c].g+((cdist[c].g*i))/maxstep;
slouken@4000
   218
				colors[c].b =
slouken@4000
   219
					palcolors[c].b+((cdist[c].b*i))/maxstep;
slouken@4000
   220
			}
slouken@4000
   221
			SDL_SetColors(screen, colors, 0, ncolors);
slouken@4000
   222
			SDL_Delay(1);
slouken@4000
   223
		}
slouken@4000
   224
NOTICE("testwin: fading over\n");
slouken@4000
   225
	}
slouken@4000
   226
	
slouken@4000
   227
done:
slouken@4000
   228
	/* Free the picture and return */
slouken@4000
   229
	SDL_FreeSurface(picture);
slouken@4000
   230
	free(colors); free(cmap);
slouken@4000
   231
	return;
icculus@1151
   232
}
slouken@4000
   233
slouken@4000
   234
int main(int argc, char *argv[])
slouken@4000
   235
{
slouken@4000
   236
	SDL_Surface *screen;
slouken@4000
   237
	/* Options */
slouken@4000
   238
	int speedy, flip, nofade;
slouken@4000
   239
	int delay;
slouken@4000
   240
	int w, h;
slouken@4000
   241
	int desired_bpp;
slouken@4000
   242
	Uint32 video_flags;
slouken@4000
   243
#ifdef BENCHMARK_SDL
slouken@4000
   244
	Uint32 then, now;
slouken@4000
   245
#endif
slouken@4000
   246
	/* Set default options and check command-line */
slouken@4000
   247
	speedy = 0;
slouken@4000
   248
	flip = 0;
slouken@4000
   249
	nofade = 0;
slouken@4000
   250
	delay = 1;
slouken@4000
   251
slouken@4000
   252
#ifdef _WIN32_WCE
slouken@4000
   253
	w = 240;
slouken@4000
   254
	h = 320;
slouken@4000
   255
	desired_bpp = 8;
slouken@4000
   256
	video_flags = SDL_FULLSCREEN;
slouken@4000
   257
#else
slouken@4000
   258
	w = 640;
slouken@4000
   259
	h = 480;
slouken@4000
   260
	desired_bpp = 0;
slouken@4000
   261
	video_flags = 0;
slouken@4000
   262
#endif
slouken@4000
   263
	if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
slouken@4000
   264
		fprintf(stderr,
slouken@4000
   265
			"Couldn't initialize SDL: %s\n", SDL_GetError());
slouken@4000
   266
		return(1);
slouken@4000
   267
	}
slouken@4000
   268
slouken@4000
   269
	while ( argc > 1 ) {
slouken@4000
   270
		if ( strcmp(argv[1], "-speedy") == 0 ) {
slouken@4000
   271
			speedy = 1;
slouken@4000
   272
			argv += 1;
slouken@4000
   273
			argc -= 1;
slouken@4000
   274
		} else
slouken@4000
   275
		if ( strcmp(argv[1], "-nofade") == 0 ) {
slouken@4000
   276
			nofade = 1;
slouken@4000
   277
			argv += 1;
slouken@4000
   278
			argc -= 1;
slouken@4000
   279
		} else
slouken@4000
   280
		if ( strcmp(argv[1], "-delay") == 0 ) {
slouken@4000
   281
			if ( argv[2] ) {
slouken@4000
   282
				delay = atoi(argv[2]);
slouken@4000
   283
				argv += 2;
slouken@4000
   284
				argc -= 2;
slouken@4000
   285
			} else {
slouken@4000
   286
				fprintf(stderr,
slouken@4000
   287
				"The -delay option requires an argument\n");
slouken@4000
   288
				quit(1);
slouken@4000
   289
			}
slouken@4000
   290
		} else
slouken@4000
   291
		if ( strcmp(argv[1], "-width") == 0 ) {
slouken@4000
   292
			if ( argv[2] && ((w = atoi(argv[2])) > 0) ) {
slouken@4000
   293
				argv += 2;
slouken@4000
   294
				argc -= 2;
slouken@4000
   295
			} else {
slouken@4000
   296
				fprintf(stderr,
slouken@4000
   297
				"The -width option requires an argument\n");
slouken@4000
   298
				quit(1);
slouken@4000
   299
			}
slouken@4000
   300
		} else
slouken@4000
   301
		if ( strcmp(argv[1], "-height") == 0 ) {
slouken@4000
   302
			if ( argv[2] && ((h = atoi(argv[2])) > 0) ) {
slouken@4000
   303
				argv += 2;
slouken@4000
   304
				argc -= 2;
slouken@4000
   305
			} else {
slouken@4000
   306
				fprintf(stderr,
slouken@4000
   307
				"The -height option requires an argument\n");
slouken@4000
   308
				quit(1);
slouken@4000
   309
			}
slouken@4000
   310
		} else
slouken@4000
   311
		if ( strcmp(argv[1], "-bpp") == 0 ) {
slouken@4000
   312
			if ( argv[2] ) {
slouken@4000
   313
				desired_bpp = atoi(argv[2]);
slouken@4000
   314
				argv += 2;
slouken@4000
   315
				argc -= 2;
slouken@4000
   316
			} else {
slouken@4000
   317
				fprintf(stderr,
slouken@4000
   318
				"The -bpp option requires an argument\n");
slouken@4000
   319
				quit(1);
slouken@4000
   320
			}
slouken@4000
   321
		} else
slouken@4000
   322
		if ( strcmp(argv[1], "-warp") == 0 ) {
slouken@4000
   323
			video_flags |= SDL_HWPALETTE;
slouken@4000
   324
			argv += 1;
slouken@4000
   325
			argc -= 1;
slouken@4000
   326
		} else
slouken@4000
   327
		if ( strcmp(argv[1], "-hw") == 0 ) {
slouken@4000
   328
			video_flags |= SDL_HWSURFACE;
slouken@4000
   329
			argv += 1;
slouken@4000
   330
			argc -= 1;
slouken@4000
   331
		} else
slouken@4000
   332
		if ( strcmp(argv[1], "-flip") == 0 ) {
slouken@4000
   333
			video_flags |= SDL_DOUBLEBUF;
slouken@4000
   334
			argv += 1;
slouken@4000
   335
			argc -= 1;
slouken@4000
   336
		} else
slouken@4000
   337
		if ( strcmp(argv[1], "-fullscreen") == 0 ) {
slouken@4000
   338
			video_flags |= SDL_FULLSCREEN;
slouken@4000
   339
			argv += 1;
slouken@4000
   340
			argc -= 1;
slouken@4000
   341
		} else
slouken@4000
   342
			break;
slouken@4000
   343
	}
slouken@4000
   344
slouken@4000
   345
	/* Initialize the display */
slouken@4000
   346
	screen = SDL_SetVideoMode(w, h, desired_bpp, video_flags);
slouken@4000
   347
	if ( screen == NULL ) {
slouken@4000
   348
		fprintf(stderr, "Couldn't set %dx%dx%d video mode: %s\n",
slouken@4000
   349
					w, h, desired_bpp, SDL_GetError());
slouken@4000
   350
		quit(1);
slouken@4000
   351
	}
slouken@4000
   352
	printf("Set%s %dx%dx%d mode\n",
slouken@4000
   353
			screen->flags & SDL_FULLSCREEN ? " fullscreen" : "",
slouken@4000
   354
			screen->w, screen->h, screen->format->BitsPerPixel);
slouken@4000
   355
	printf("(video surface located in %s memory)\n",
slouken@4000
   356
			(screen->flags&SDL_HWSURFACE) ? "video" : "system");
slouken@4000
   357
	if ( screen->flags & SDL_DOUBLEBUF ) {
slouken@4000
   358
		printf("Double-buffering enabled\n");
slouken@4000
   359
		flip = 1;
slouken@4000
   360
	}
slouken@4000
   361
slouken@4000
   362
	/* Set the window manager title bar */
slouken@4000
   363
	SDL_WM_SetCaption("SDL test window", "testwin");
slouken@4000
   364
slouken@4000
   365
	/* Do all the drawing work */
slouken@4000
   366
#ifdef BENCHMARK_SDL
slouken@4000
   367
	then = SDL_GetTicks();
slouken@4000
   368
	DrawPict(screen, argv[1], speedy, flip, nofade);
slouken@4000
   369
	now = SDL_GetTicks();
slouken@4000
   370
	printf("Time: %d milliseconds\n", now-then);
slouken@4000
   371
#else
slouken@4000
   372
	DrawPict(screen, argv[1], speedy, flip, nofade);
slouken@4000
   373
#endif
slouken@4000
   374
	SDL_Delay(delay*1000);
slouken@4000
   375
	SDL_Quit();
slouken@4000
   376
	return(0);
slouken@4000
   377
}