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