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