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