test/testwin.c
branchSDL-1.2
changeset 4000 9776ab9063de
parent 3999 f61a20d195f7
     1.1 --- a/test/testwin.c	Wed Jul 04 15:22:07 2007 +0000
     1.2 +++ b/test/testwin.c	Wed Jul 04 15:22:47 2007 +0000
     1.3 @@ -1,33 +1,377 @@
     1.4 -#include <windows.h>
     1.5 -#include "SDL/SDL.h"
     1.6  
     1.7 +/* Bring up a window and play with it */
     1.8  
     1.9 -#define WIDTH 480
    1.10 -#define HEIGHT 480
    1.11 -#define DEPTH 32
    1.12 +#include <stdlib.h>
    1.13 +#include <stdio.h>
    1.14 +#include <string.h>
    1.15  
    1.16 +#define BENCHMARK_SDL
    1.17  
    1.18 -int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR
    1.19 -lpCmdLine, int nCmdShow)
    1.20 +#define NOTICE(X)	printf("%s", X);
    1.21 +
    1.22 +#include "SDL.h"
    1.23 +
    1.24 +/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    1.25 +static void quit(int rc)
    1.26  {
    1.27 -        if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER) != -1)
    1.28 -        {
    1.29 -                SDL_Surface* w;
    1.30 -                Uint32* r;
    1.31 -                int x;
    1.32 +	SDL_Quit();
    1.33 +	exit(rc);
    1.34 +}
    1.35  
    1.36 -                w = SDL_SetVideoMode(WIDTH, HEIGHT, DEPTH, SDL_SWSURFACE |
    1.37 -SDL_FULLSCREEN);
    1.38 +void DrawPict(SDL_Surface *screen, char *bmpfile,
    1.39 +					int speedy, int flip, int nofade)
    1.40 +{
    1.41 +	SDL_Surface *picture;
    1.42 +	SDL_Rect dest, update;
    1.43 +	int i, centered;
    1.44 +	int ncolors;
    1.45 +	SDL_Color *colors, *cmap;
    1.46  
    1.47 -                SDL_LockSurface(w);
    1.48 -                r = w->pixels + w->pitch * HEIGHT / 2;
    1.49 -                for (x = 0; x < WIDTH; ++x) r[x] = 0xFFFFFFFF;
    1.50 -                SDL_UnlockSurface(w);
    1.51 -                SDL_UpdateRect(w, 0, 0, 0, 0);
    1.52 +	/* Load the image into a surface */
    1.53 +	if ( bmpfile == NULL ) {
    1.54 +		bmpfile = "sample.bmp";		/* Sample image */
    1.55 +	}
    1.56 +fprintf(stderr, "Loading picture: %s\n", bmpfile);
    1.57 +	picture = SDL_LoadBMP(bmpfile);
    1.58 +	if ( picture == NULL ) {
    1.59 +		fprintf(stderr, "Couldn't load %s: %s\n", bmpfile,
    1.60 +							SDL_GetError());
    1.61 +		return;
    1.62 +	}
    1.63  
    1.64 -                SDL_Delay(1000);
    1.65 -                SDL_Quit();
    1.66 -        }
    1.67 +	/* Set the display colors -- on a hicolor display this is a no-op */
    1.68 +	if ( picture->format->palette ) {
    1.69 +		ncolors = picture->format->palette->ncolors;
    1.70 +		colors  = (SDL_Color *)malloc(ncolors*sizeof(SDL_Color));
    1.71 +		cmap    = (SDL_Color *)malloc(ncolors*sizeof(SDL_Color));
    1.72 +		memcpy(colors, picture->format->palette->colors,
    1.73 +						ncolors*sizeof(SDL_Color));
    1.74 +	} else {
    1.75 +		int       r, g, b;
    1.76  
    1.77 -        return 0;
    1.78 +		/* Allocate 256 color palette */
    1.79 +		ncolors = 256;
    1.80 +		colors  = (SDL_Color *)malloc(ncolors*sizeof(SDL_Color));
    1.81 +		cmap    = (SDL_Color *)malloc(ncolors*sizeof(SDL_Color));
    1.82 +
    1.83 +		/* Set a 3,3,2 color cube */
    1.84 +		for ( r=0; r<8; ++r ) {
    1.85 +			for ( g=0; g<8; ++g ) {
    1.86 +				for ( b=0; b<4; ++b ) {
    1.87 +					i = ((r<<5)|(g<<2)|b);
    1.88 +					colors[i].r = r<<5;
    1.89 +					colors[i].g = g<<5;
    1.90 +					colors[i].b = b<<6;
    1.91 +				}
    1.92 +			}
    1.93 +		}
    1.94 +	}
    1.95 +NOTICE("testwin: setting colors\n");
    1.96 +	if ( ! SDL_SetColors(screen, colors, 0, ncolors) &&
    1.97 +				(screen->format->palette != NULL) ) {
    1.98 +		fprintf(stderr,
    1.99 +"Warning: Couldn't set all of the colors, but SDL will map the image\n"
   1.100 +"         (colormap fading will suffer - try the -warp option)\n"
   1.101 +		);
   1.102 +	}
   1.103 +
   1.104 +	/* Set the screen to black (not really necessary) */
   1.105 +	if ( SDL_LockSurface(screen) == 0 ) {
   1.106 +		Uint32 black;
   1.107 +		Uint8 *pixels;
   1.108 +
   1.109 +		black = SDL_MapRGB(screen->format, 0, 0, 0);
   1.110 +		pixels = (Uint8 *)screen->pixels;
   1.111 +		for ( i=0; i<screen->h; ++i ) {
   1.112 +			memset(pixels, black,
   1.113 +				screen->w*screen->format->BytesPerPixel);
   1.114 +			pixels += screen->pitch;
   1.115 +		}
   1.116 +		SDL_UnlockSurface(screen);
   1.117 +		SDL_UpdateRect(screen, 0, 0, 0, 0);
   1.118 +	}
   1.119 +	
   1.120 +	/* Display the picture */
   1.121 +	if ( speedy ) {
   1.122 +		SDL_Surface *displayfmt;
   1.123 +
   1.124 +fprintf(stderr, "Converting picture\n");
   1.125 +		displayfmt = SDL_DisplayFormat(picture);
   1.126 +		if ( displayfmt == NULL ) {
   1.127 +			fprintf(stderr,
   1.128 +				"Couldn't convert image: %s\n", SDL_GetError());
   1.129 +			goto done;
   1.130 +		}
   1.131 +		SDL_FreeSurface(picture);
   1.132 +		picture = displayfmt;
   1.133 +	}
   1.134 +	printf("(image surface located in %s memory)\n", 
   1.135 +			(picture->flags&SDL_HWSURFACE) ? "video" : "system");
   1.136 +	centered = (screen->w - picture->w)/2;
   1.137 +	if ( centered < 0 ) {
   1.138 +		centered = 0;
   1.139 +	}
   1.140 +	dest.y = (screen->h - picture->h)/2;
   1.141 +	dest.w = picture->w;
   1.142 +	dest.h = picture->h;
   1.143 +NOTICE("testwin: moving image\n");
   1.144 +	for ( i=0; i<=centered; ++i ) {
   1.145 +		dest.x = i;
   1.146 +		update = dest;
   1.147 +		if ( SDL_BlitSurface(picture, NULL, screen, &update) < 0 ) {
   1.148 +			fprintf(stderr, "Blit failed: %s\n", SDL_GetError());
   1.149 +			break;
   1.150 +		}
   1.151 +		if ( flip ) {
   1.152 +			SDL_Flip(screen);
   1.153 +		} else {
   1.154 +			SDL_UpdateRects(screen, 1, &update);
   1.155 +		}
   1.156 +	}
   1.157 +
   1.158 +#ifdef SCREENSHOT
   1.159 +	if ( SDL_SaveBMP(screen, "screen.bmp") < 0 )
   1.160 +		printf("Couldn't save screen: %s\n", SDL_GetError());
   1.161 +#endif
   1.162 +
   1.163 +#ifndef BENCHMARK_SDL
   1.164 +	/* Let it sit there for a while */
   1.165 +	SDL_Delay(5*1000);
   1.166 +#endif
   1.167 +	/* Fade the colormap */
   1.168 +	if ( ! nofade ) {
   1.169 +		int maxstep;
   1.170 +		SDL_Color final;
   1.171 +		SDL_Color palcolors[256];
   1.172 +		struct {
   1.173 +			Sint16 r, g, b;
   1.174 +		} cdist[256];
   1.175 +
   1.176 +NOTICE("testwin: fading out...\n");
   1.177 +		memcpy(cmap, colors, ncolors*sizeof(SDL_Color));
   1.178 +		maxstep = 32-1;
   1.179 +		final.r = 0xFF;
   1.180 +		final.g = 0x00;
   1.181 +		final.b = 0x00;
   1.182 +		memcpy(palcolors, colors, ncolors*sizeof(SDL_Color));
   1.183 +		for ( i=0; i<ncolors; ++i ) {
   1.184 +			cdist[i].r = final.r-palcolors[i].r;
   1.185 +			cdist[i].g = final.g-palcolors[i].g;
   1.186 +			cdist[i].b = final.b-palcolors[i].b;
   1.187 +		}
   1.188 +		for ( i=0; i<=maxstep/2; ++i ) {	/* halfway fade */
   1.189 +			int c;
   1.190 +			for ( c=0; c<ncolors; ++c ) {
   1.191 +				colors[c].r =
   1.192 +					palcolors[c].r+((cdist[c].r*i))/maxstep;
   1.193 +				colors[c].g =
   1.194 +					palcolors[c].g+((cdist[c].g*i))/maxstep;
   1.195 +				colors[c].b =
   1.196 +					palcolors[c].b+((cdist[c].b*i))/maxstep;
   1.197 +			}
   1.198 +			SDL_SetColors(screen, colors, 0, ncolors);
   1.199 +			SDL_Delay(1);
   1.200 +		}
   1.201 +		final.r = 0x00;
   1.202 +		final.g = 0x00;
   1.203 +		final.b = 0x00;
   1.204 +		memcpy(palcolors, colors, ncolors*sizeof(SDL_Color));
   1.205 +		for ( i=0; i<ncolors; ++i ) {
   1.206 +			cdist[i].r = final.r-palcolors[i].r;
   1.207 +			cdist[i].g = final.g-palcolors[i].g;
   1.208 +			cdist[i].b = final.b-palcolors[i].b;
   1.209 +		}
   1.210 +		maxstep /= 2;
   1.211 +		for ( i=0; i<=maxstep; ++i ) {		/* finish fade out */
   1.212 +			int c;
   1.213 +			for ( c=0; c<ncolors; ++c ) {
   1.214 +				colors[c].r =
   1.215 +					palcolors[c].r+((cdist[c].r*i))/maxstep;
   1.216 +				colors[c].g =
   1.217 +					palcolors[c].g+((cdist[c].g*i))/maxstep;
   1.218 +				colors[c].b =
   1.219 +					palcolors[c].b+((cdist[c].b*i))/maxstep;
   1.220 +			}
   1.221 +			SDL_SetColors(screen, colors, 0, ncolors);
   1.222 +			SDL_Delay(1);
   1.223 +		}
   1.224 +		for ( i=0; i<ncolors; ++i ) {
   1.225 +			colors[i].r = final.r;
   1.226 +			colors[i].g = final.g;
   1.227 +			colors[i].b = final.b;
   1.228 +		}
   1.229 +		SDL_SetColors(screen, colors, 0, ncolors);
   1.230 +NOTICE("testwin: fading in...\n");
   1.231 +		memcpy(palcolors, colors, ncolors*sizeof(SDL_Color));
   1.232 +		for ( i=0; i<ncolors; ++i ) {
   1.233 +			cdist[i].r = cmap[i].r-palcolors[i].r;
   1.234 +			cdist[i].g = cmap[i].g-palcolors[i].g;
   1.235 +			cdist[i].b = cmap[i].b-palcolors[i].b;
   1.236 +		}
   1.237 +		for ( i=0; i<=maxstep; ++i ) {	/* 32 step fade in */
   1.238 +			int c;
   1.239 +			for ( c=0; c<ncolors; ++c ) {
   1.240 +				colors[c].r =
   1.241 +					palcolors[c].r+((cdist[c].r*i))/maxstep;
   1.242 +				colors[c].g =
   1.243 +					palcolors[c].g+((cdist[c].g*i))/maxstep;
   1.244 +				colors[c].b =
   1.245 +					palcolors[c].b+((cdist[c].b*i))/maxstep;
   1.246 +			}
   1.247 +			SDL_SetColors(screen, colors, 0, ncolors);
   1.248 +			SDL_Delay(1);
   1.249 +		}
   1.250 +NOTICE("testwin: fading over\n");
   1.251 +	}
   1.252 +	
   1.253 +done:
   1.254 +	/* Free the picture and return */
   1.255 +	SDL_FreeSurface(picture);
   1.256 +	free(colors); free(cmap);
   1.257 +	return;
   1.258  }
   1.259 +
   1.260 +int main(int argc, char *argv[])
   1.261 +{
   1.262 +	SDL_Surface *screen;
   1.263 +	/* Options */
   1.264 +	int speedy, flip, nofade;
   1.265 +	int delay;
   1.266 +	int w, h;
   1.267 +	int desired_bpp;
   1.268 +	Uint32 video_flags;
   1.269 +#ifdef BENCHMARK_SDL
   1.270 +	Uint32 then, now;
   1.271 +#endif
   1.272 +	/* Set default options and check command-line */
   1.273 +	speedy = 0;
   1.274 +	flip = 0;
   1.275 +	nofade = 0;
   1.276 +	delay = 1;
   1.277 +
   1.278 +#ifdef _WIN32_WCE
   1.279 +	w = 240;
   1.280 +	h = 320;
   1.281 +	desired_bpp = 8;
   1.282 +	video_flags = SDL_FULLSCREEN;
   1.283 +#else
   1.284 +	w = 640;
   1.285 +	h = 480;
   1.286 +	desired_bpp = 0;
   1.287 +	video_flags = 0;
   1.288 +#endif
   1.289 +	if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
   1.290 +		fprintf(stderr,
   1.291 +			"Couldn't initialize SDL: %s\n", SDL_GetError());
   1.292 +		return(1);
   1.293 +	}
   1.294 +
   1.295 +	while ( argc > 1 ) {
   1.296 +		if ( strcmp(argv[1], "-speedy") == 0 ) {
   1.297 +			speedy = 1;
   1.298 +			argv += 1;
   1.299 +			argc -= 1;
   1.300 +		} else
   1.301 +		if ( strcmp(argv[1], "-nofade") == 0 ) {
   1.302 +			nofade = 1;
   1.303 +			argv += 1;
   1.304 +			argc -= 1;
   1.305 +		} else
   1.306 +		if ( strcmp(argv[1], "-delay") == 0 ) {
   1.307 +			if ( argv[2] ) {
   1.308 +				delay = atoi(argv[2]);
   1.309 +				argv += 2;
   1.310 +				argc -= 2;
   1.311 +			} else {
   1.312 +				fprintf(stderr,
   1.313 +				"The -delay option requires an argument\n");
   1.314 +				quit(1);
   1.315 +			}
   1.316 +		} else
   1.317 +		if ( strcmp(argv[1], "-width") == 0 ) {
   1.318 +			if ( argv[2] && ((w = atoi(argv[2])) > 0) ) {
   1.319 +				argv += 2;
   1.320 +				argc -= 2;
   1.321 +			} else {
   1.322 +				fprintf(stderr,
   1.323 +				"The -width option requires an argument\n");
   1.324 +				quit(1);
   1.325 +			}
   1.326 +		} else
   1.327 +		if ( strcmp(argv[1], "-height") == 0 ) {
   1.328 +			if ( argv[2] && ((h = atoi(argv[2])) > 0) ) {
   1.329 +				argv += 2;
   1.330 +				argc -= 2;
   1.331 +			} else {
   1.332 +				fprintf(stderr,
   1.333 +				"The -height option requires an argument\n");
   1.334 +				quit(1);
   1.335 +			}
   1.336 +		} else
   1.337 +		if ( strcmp(argv[1], "-bpp") == 0 ) {
   1.338 +			if ( argv[2] ) {
   1.339 +				desired_bpp = atoi(argv[2]);
   1.340 +				argv += 2;
   1.341 +				argc -= 2;
   1.342 +			} else {
   1.343 +				fprintf(stderr,
   1.344 +				"The -bpp option requires an argument\n");
   1.345 +				quit(1);
   1.346 +			}
   1.347 +		} else
   1.348 +		if ( strcmp(argv[1], "-warp") == 0 ) {
   1.349 +			video_flags |= SDL_HWPALETTE;
   1.350 +			argv += 1;
   1.351 +			argc -= 1;
   1.352 +		} else
   1.353 +		if ( strcmp(argv[1], "-hw") == 0 ) {
   1.354 +			video_flags |= SDL_HWSURFACE;
   1.355 +			argv += 1;
   1.356 +			argc -= 1;
   1.357 +		} else
   1.358 +		if ( strcmp(argv[1], "-flip") == 0 ) {
   1.359 +			video_flags |= SDL_DOUBLEBUF;
   1.360 +			argv += 1;
   1.361 +			argc -= 1;
   1.362 +		} else
   1.363 +		if ( strcmp(argv[1], "-fullscreen") == 0 ) {
   1.364 +			video_flags |= SDL_FULLSCREEN;
   1.365 +			argv += 1;
   1.366 +			argc -= 1;
   1.367 +		} else
   1.368 +			break;
   1.369 +	}
   1.370 +
   1.371 +	/* Initialize the display */
   1.372 +	screen = SDL_SetVideoMode(w, h, desired_bpp, video_flags);
   1.373 +	if ( screen == NULL ) {
   1.374 +		fprintf(stderr, "Couldn't set %dx%dx%d video mode: %s\n",
   1.375 +					w, h, desired_bpp, SDL_GetError());
   1.376 +		quit(1);
   1.377 +	}
   1.378 +	printf("Set%s %dx%dx%d mode\n",
   1.379 +			screen->flags & SDL_FULLSCREEN ? " fullscreen" : "",
   1.380 +			screen->w, screen->h, screen->format->BitsPerPixel);
   1.381 +	printf("(video surface located in %s memory)\n",
   1.382 +			(screen->flags&SDL_HWSURFACE) ? "video" : "system");
   1.383 +	if ( screen->flags & SDL_DOUBLEBUF ) {
   1.384 +		printf("Double-buffering enabled\n");
   1.385 +		flip = 1;
   1.386 +	}
   1.387 +
   1.388 +	/* Set the window manager title bar */
   1.389 +	SDL_WM_SetCaption("SDL test window", "testwin");
   1.390 +
   1.391 +	/* Do all the drawing work */
   1.392 +#ifdef BENCHMARK_SDL
   1.393 +	then = SDL_GetTicks();
   1.394 +	DrawPict(screen, argv[1], speedy, flip, nofade);
   1.395 +	now = SDL_GetTicks();
   1.396 +	printf("Time: %d milliseconds\n", now-then);
   1.397 +#else
   1.398 +	DrawPict(screen, argv[1], speedy, flip, nofade);
   1.399 +#endif
   1.400 +	SDL_Delay(delay*1000);
   1.401 +	SDL_Quit();
   1.402 +	return(0);
   1.403 +}