Fixed bug #450 SDL-1.2
authorSam Lantinga
Wed, 04 Jul 2007 15:22:07 +0000
branchSDL-1.2
changeset 3999f61a20d195f7
parent 3998 098ac044cd2f
child 4000 9776ab9063de
Fixed bug #450
src/video/x11/SDL_x11events.c
test/testwin.c
     1.1 --- a/src/video/x11/SDL_x11events.c	Wed Jul 04 07:54:06 2007 +0000
     1.2 +++ b/src/video/x11/SDL_x11events.c	Wed Jul 04 15:22:07 2007 +0000
     1.3 @@ -722,6 +722,10 @@
     1.4  			static XComposeStatus state;
     1.5  			char keybuf[32];
     1.6  
     1.7 +			keysym.scancode = keycode;
     1.8 +			keysym.sym = X11_TranslateKeycode(SDL_Display, keycode);
     1.9 +			keysym.mod = KMOD_NONE;
    1.10 +			keysym.unicode = 0;
    1.11  			if ( XLookupString(&xevent.xkey,
    1.12  			                    keybuf, sizeof(keybuf),
    1.13  			                    NULL, &state) ) {
     2.1 --- a/test/testwin.c	Wed Jul 04 07:54:06 2007 +0000
     2.2 +++ b/test/testwin.c	Wed Jul 04 15:22:07 2007 +0000
     2.3 @@ -1,377 +1,33 @@
     2.4 -
     2.5 -/* Bring up a window and play with it */
     2.6 -
     2.7 -#include <stdlib.h>
     2.8 -#include <stdio.h>
     2.9 -#include <string.h>
    2.10 -
    2.11 -#define BENCHMARK_SDL
    2.12 -
    2.13 -#define NOTICE(X)	printf("%s", X);
    2.14 -
    2.15 -#include "SDL.h"
    2.16 -
    2.17 -/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    2.18 -static void quit(int rc)
    2.19 -{
    2.20 -	SDL_Quit();
    2.21 -	exit(rc);
    2.22 -}
    2.23 +#include <windows.h>
    2.24 +#include "SDL/SDL.h"
    2.25  
    2.26 -void DrawPict(SDL_Surface *screen, char *bmpfile,
    2.27 -					int speedy, int flip, int nofade)
    2.28 -{
    2.29 -	SDL_Surface *picture;
    2.30 -	SDL_Rect dest, update;
    2.31 -	int i, centered;
    2.32 -	int ncolors;
    2.33 -	SDL_Color *colors, *cmap;
    2.34 -
    2.35 -	/* Load the image into a surface */
    2.36 -	if ( bmpfile == NULL ) {
    2.37 -		bmpfile = "sample.bmp";		/* Sample image */
    2.38 -	}
    2.39 -fprintf(stderr, "Loading picture: %s\n", bmpfile);
    2.40 -	picture = SDL_LoadBMP(bmpfile);
    2.41 -	if ( picture == NULL ) {
    2.42 -		fprintf(stderr, "Couldn't load %s: %s\n", bmpfile,
    2.43 -							SDL_GetError());
    2.44 -		return;
    2.45 -	}
    2.46 -
    2.47 -	/* Set the display colors -- on a hicolor display this is a no-op */
    2.48 -	if ( picture->format->palette ) {
    2.49 -		ncolors = picture->format->palette->ncolors;
    2.50 -		colors  = (SDL_Color *)malloc(ncolors*sizeof(SDL_Color));
    2.51 -		cmap    = (SDL_Color *)malloc(ncolors*sizeof(SDL_Color));
    2.52 -		memcpy(colors, picture->format->palette->colors,
    2.53 -						ncolors*sizeof(SDL_Color));
    2.54 -	} else {
    2.55 -		int       r, g, b;
    2.56 -
    2.57 -		/* Allocate 256 color palette */
    2.58 -		ncolors = 256;
    2.59 -		colors  = (SDL_Color *)malloc(ncolors*sizeof(SDL_Color));
    2.60 -		cmap    = (SDL_Color *)malloc(ncolors*sizeof(SDL_Color));
    2.61  
    2.62 -		/* Set a 3,3,2 color cube */
    2.63 -		for ( r=0; r<8; ++r ) {
    2.64 -			for ( g=0; g<8; ++g ) {
    2.65 -				for ( b=0; b<4; ++b ) {
    2.66 -					i = ((r<<5)|(g<<2)|b);
    2.67 -					colors[i].r = r<<5;
    2.68 -					colors[i].g = g<<5;
    2.69 -					colors[i].b = b<<6;
    2.70 -				}
    2.71 -			}
    2.72 -		}
    2.73 -	}
    2.74 -NOTICE("testwin: setting colors\n");
    2.75 -	if ( ! SDL_SetColors(screen, colors, 0, ncolors) &&
    2.76 -				(screen->format->palette != NULL) ) {
    2.77 -		fprintf(stderr,
    2.78 -"Warning: Couldn't set all of the colors, but SDL will map the image\n"
    2.79 -"         (colormap fading will suffer - try the -warp option)\n"
    2.80 -		);
    2.81 -	}
    2.82 -
    2.83 -	/* Set the screen to black (not really necessary) */
    2.84 -	if ( SDL_LockSurface(screen) == 0 ) {
    2.85 -		Uint32 black;
    2.86 -		Uint8 *pixels;
    2.87 -
    2.88 -		black = SDL_MapRGB(screen->format, 0, 0, 0);
    2.89 -		pixels = (Uint8 *)screen->pixels;
    2.90 -		for ( i=0; i<screen->h; ++i ) {
    2.91 -			memset(pixels, black,
    2.92 -				screen->w*screen->format->BytesPerPixel);
    2.93 -			pixels += screen->pitch;
    2.94 -		}
    2.95 -		SDL_UnlockSurface(screen);
    2.96 -		SDL_UpdateRect(screen, 0, 0, 0, 0);
    2.97 -	}
    2.98 -	
    2.99 -	/* Display the picture */
   2.100 -	if ( speedy ) {
   2.101 -		SDL_Surface *displayfmt;
   2.102 +#define WIDTH 480
   2.103 +#define HEIGHT 480
   2.104 +#define DEPTH 32
   2.105  
   2.106 -fprintf(stderr, "Converting picture\n");
   2.107 -		displayfmt = SDL_DisplayFormat(picture);
   2.108 -		if ( displayfmt == NULL ) {
   2.109 -			fprintf(stderr,
   2.110 -				"Couldn't convert image: %s\n", SDL_GetError());
   2.111 -			goto done;
   2.112 -		}
   2.113 -		SDL_FreeSurface(picture);
   2.114 -		picture = displayfmt;
   2.115 -	}
   2.116 -	printf("(image surface located in %s memory)\n", 
   2.117 -			(picture->flags&SDL_HWSURFACE) ? "video" : "system");
   2.118 -	centered = (screen->w - picture->w)/2;
   2.119 -	if ( centered < 0 ) {
   2.120 -		centered = 0;
   2.121 -	}
   2.122 -	dest.y = (screen->h - picture->h)/2;
   2.123 -	dest.w = picture->w;
   2.124 -	dest.h = picture->h;
   2.125 -NOTICE("testwin: moving image\n");
   2.126 -	for ( i=0; i<=centered; ++i ) {
   2.127 -		dest.x = i;
   2.128 -		update = dest;
   2.129 -		if ( SDL_BlitSurface(picture, NULL, screen, &update) < 0 ) {
   2.130 -			fprintf(stderr, "Blit failed: %s\n", SDL_GetError());
   2.131 -			break;
   2.132 -		}
   2.133 -		if ( flip ) {
   2.134 -			SDL_Flip(screen);
   2.135 -		} else {
   2.136 -			SDL_UpdateRects(screen, 1, &update);
   2.137 -		}
   2.138 -	}
   2.139 -
   2.140 -#ifdef SCREENSHOT
   2.141 -	if ( SDL_SaveBMP(screen, "screen.bmp") < 0 )
   2.142 -		printf("Couldn't save screen: %s\n", SDL_GetError());
   2.143 -#endif
   2.144 -
   2.145 -#ifndef BENCHMARK_SDL
   2.146 -	/* Let it sit there for a while */
   2.147 -	SDL_Delay(5*1000);
   2.148 -#endif
   2.149 -	/* Fade the colormap */
   2.150 -	if ( ! nofade ) {
   2.151 -		int maxstep;
   2.152 -		SDL_Color final;
   2.153 -		SDL_Color palcolors[256];
   2.154 -		struct {
   2.155 -			Sint16 r, g, b;
   2.156 -		} cdist[256];
   2.157  
   2.158 -NOTICE("testwin: fading out...\n");
   2.159 -		memcpy(cmap, colors, ncolors*sizeof(SDL_Color));
   2.160 -		maxstep = 32-1;
   2.161 -		final.r = 0xFF;
   2.162 -		final.g = 0x00;
   2.163 -		final.b = 0x00;
   2.164 -		memcpy(palcolors, colors, ncolors*sizeof(SDL_Color));
   2.165 -		for ( i=0; i<ncolors; ++i ) {
   2.166 -			cdist[i].r = final.r-palcolors[i].r;
   2.167 -			cdist[i].g = final.g-palcolors[i].g;
   2.168 -			cdist[i].b = final.b-palcolors[i].b;
   2.169 -		}
   2.170 -		for ( i=0; i<=maxstep/2; ++i ) {	/* halfway fade */
   2.171 -			int c;
   2.172 -			for ( c=0; c<ncolors; ++c ) {
   2.173 -				colors[c].r =
   2.174 -					palcolors[c].r+((cdist[c].r*i))/maxstep;
   2.175 -				colors[c].g =
   2.176 -					palcolors[c].g+((cdist[c].g*i))/maxstep;
   2.177 -				colors[c].b =
   2.178 -					palcolors[c].b+((cdist[c].b*i))/maxstep;
   2.179 -			}
   2.180 -			SDL_SetColors(screen, colors, 0, ncolors);
   2.181 -			SDL_Delay(1);
   2.182 -		}
   2.183 -		final.r = 0x00;
   2.184 -		final.g = 0x00;
   2.185 -		final.b = 0x00;
   2.186 -		memcpy(palcolors, colors, ncolors*sizeof(SDL_Color));
   2.187 -		for ( i=0; i<ncolors; ++i ) {
   2.188 -			cdist[i].r = final.r-palcolors[i].r;
   2.189 -			cdist[i].g = final.g-palcolors[i].g;
   2.190 -			cdist[i].b = final.b-palcolors[i].b;
   2.191 -		}
   2.192 -		maxstep /= 2;
   2.193 -		for ( i=0; i<=maxstep; ++i ) {		/* finish fade out */
   2.194 -			int c;
   2.195 -			for ( c=0; c<ncolors; ++c ) {
   2.196 -				colors[c].r =
   2.197 -					palcolors[c].r+((cdist[c].r*i))/maxstep;
   2.198 -				colors[c].g =
   2.199 -					palcolors[c].g+((cdist[c].g*i))/maxstep;
   2.200 -				colors[c].b =
   2.201 -					palcolors[c].b+((cdist[c].b*i))/maxstep;
   2.202 -			}
   2.203 -			SDL_SetColors(screen, colors, 0, ncolors);
   2.204 -			SDL_Delay(1);
   2.205 -		}
   2.206 -		for ( i=0; i<ncolors; ++i ) {
   2.207 -			colors[i].r = final.r;
   2.208 -			colors[i].g = final.g;
   2.209 -			colors[i].b = final.b;
   2.210 -		}
   2.211 -		SDL_SetColors(screen, colors, 0, ncolors);
   2.212 -NOTICE("testwin: fading in...\n");
   2.213 -		memcpy(palcolors, colors, ncolors*sizeof(SDL_Color));
   2.214 -		for ( i=0; i<ncolors; ++i ) {
   2.215 -			cdist[i].r = cmap[i].r-palcolors[i].r;
   2.216 -			cdist[i].g = cmap[i].g-palcolors[i].g;
   2.217 -			cdist[i].b = cmap[i].b-palcolors[i].b;
   2.218 -		}
   2.219 -		for ( i=0; i<=maxstep; ++i ) {	/* 32 step fade in */
   2.220 -			int c;
   2.221 -			for ( c=0; c<ncolors; ++c ) {
   2.222 -				colors[c].r =
   2.223 -					palcolors[c].r+((cdist[c].r*i))/maxstep;
   2.224 -				colors[c].g =
   2.225 -					palcolors[c].g+((cdist[c].g*i))/maxstep;
   2.226 -				colors[c].b =
   2.227 -					palcolors[c].b+((cdist[c].b*i))/maxstep;
   2.228 -			}
   2.229 -			SDL_SetColors(screen, colors, 0, ncolors);
   2.230 -			SDL_Delay(1);
   2.231 -		}
   2.232 -NOTICE("testwin: fading over\n");
   2.233 -	}
   2.234 -	
   2.235 -done:
   2.236 -	/* Free the picture and return */
   2.237 -	SDL_FreeSurface(picture);
   2.238 -	free(colors); free(cmap);
   2.239 -	return;
   2.240 -}
   2.241 +int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR
   2.242 +lpCmdLine, int nCmdShow)
   2.243 +{
   2.244 +        if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER) != -1)
   2.245 +        {
   2.246 +                SDL_Surface* w;
   2.247 +                Uint32* r;
   2.248 +                int x;
   2.249  
   2.250 -int main(int argc, char *argv[])
   2.251 -{
   2.252 -	SDL_Surface *screen;
   2.253 -	/* Options */
   2.254 -	int speedy, flip, nofade;
   2.255 -	int delay;
   2.256 -	int w, h;
   2.257 -	int desired_bpp;
   2.258 -	Uint32 video_flags;
   2.259 -#ifdef BENCHMARK_SDL
   2.260 -	Uint32 then, now;
   2.261 -#endif
   2.262 -	/* Set default options and check command-line */
   2.263 -	speedy = 0;
   2.264 -	flip = 0;
   2.265 -	nofade = 0;
   2.266 -	delay = 1;
   2.267 +                w = SDL_SetVideoMode(WIDTH, HEIGHT, DEPTH, SDL_SWSURFACE |
   2.268 +SDL_FULLSCREEN);
   2.269  
   2.270 -#ifdef _WIN32_WCE
   2.271 -	w = 240;
   2.272 -	h = 320;
   2.273 -	desired_bpp = 8;
   2.274 -	video_flags = SDL_FULLSCREEN;
   2.275 -#else
   2.276 -	w = 640;
   2.277 -	h = 480;
   2.278 -	desired_bpp = 0;
   2.279 -	video_flags = 0;
   2.280 -#endif
   2.281 -	if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
   2.282 -		fprintf(stderr,
   2.283 -			"Couldn't initialize SDL: %s\n", SDL_GetError());
   2.284 -		return(1);
   2.285 -	}
   2.286 +                SDL_LockSurface(w);
   2.287 +                r = w->pixels + w->pitch * HEIGHT / 2;
   2.288 +                for (x = 0; x < WIDTH; ++x) r[x] = 0xFFFFFFFF;
   2.289 +                SDL_UnlockSurface(w);
   2.290 +                SDL_UpdateRect(w, 0, 0, 0, 0);
   2.291  
   2.292 -	while ( argc > 1 ) {
   2.293 -		if ( strcmp(argv[1], "-speedy") == 0 ) {
   2.294 -			speedy = 1;
   2.295 -			argv += 1;
   2.296 -			argc -= 1;
   2.297 -		} else
   2.298 -		if ( strcmp(argv[1], "-nofade") == 0 ) {
   2.299 -			nofade = 1;
   2.300 -			argv += 1;
   2.301 -			argc -= 1;
   2.302 -		} else
   2.303 -		if ( strcmp(argv[1], "-delay") == 0 ) {
   2.304 -			if ( argv[2] ) {
   2.305 -				delay = atoi(argv[2]);
   2.306 -				argv += 2;
   2.307 -				argc -= 2;
   2.308 -			} else {
   2.309 -				fprintf(stderr,
   2.310 -				"The -delay option requires an argument\n");
   2.311 -				quit(1);
   2.312 -			}
   2.313 -		} else
   2.314 -		if ( strcmp(argv[1], "-width") == 0 ) {
   2.315 -			if ( argv[2] && ((w = atoi(argv[2])) > 0) ) {
   2.316 -				argv += 2;
   2.317 -				argc -= 2;
   2.318 -			} else {
   2.319 -				fprintf(stderr,
   2.320 -				"The -width option requires an argument\n");
   2.321 -				quit(1);
   2.322 -			}
   2.323 -		} else
   2.324 -		if ( strcmp(argv[1], "-height") == 0 ) {
   2.325 -			if ( argv[2] && ((h = atoi(argv[2])) > 0) ) {
   2.326 -				argv += 2;
   2.327 -				argc -= 2;
   2.328 -			} else {
   2.329 -				fprintf(stderr,
   2.330 -				"The -height option requires an argument\n");
   2.331 -				quit(1);
   2.332 -			}
   2.333 -		} else
   2.334 -		if ( strcmp(argv[1], "-bpp") == 0 ) {
   2.335 -			if ( argv[2] ) {
   2.336 -				desired_bpp = atoi(argv[2]);
   2.337 -				argv += 2;
   2.338 -				argc -= 2;
   2.339 -			} else {
   2.340 -				fprintf(stderr,
   2.341 -				"The -bpp option requires an argument\n");
   2.342 -				quit(1);
   2.343 -			}
   2.344 -		} else
   2.345 -		if ( strcmp(argv[1], "-warp") == 0 ) {
   2.346 -			video_flags |= SDL_HWPALETTE;
   2.347 -			argv += 1;
   2.348 -			argc -= 1;
   2.349 -		} else
   2.350 -		if ( strcmp(argv[1], "-hw") == 0 ) {
   2.351 -			video_flags |= SDL_HWSURFACE;
   2.352 -			argv += 1;
   2.353 -			argc -= 1;
   2.354 -		} else
   2.355 -		if ( strcmp(argv[1], "-flip") == 0 ) {
   2.356 -			video_flags |= SDL_DOUBLEBUF;
   2.357 -			argv += 1;
   2.358 -			argc -= 1;
   2.359 -		} else
   2.360 -		if ( strcmp(argv[1], "-fullscreen") == 0 ) {
   2.361 -			video_flags |= SDL_FULLSCREEN;
   2.362 -			argv += 1;
   2.363 -			argc -= 1;
   2.364 -		} else
   2.365 -			break;
   2.366 -	}
   2.367 +                SDL_Delay(1000);
   2.368 +                SDL_Quit();
   2.369 +        }
   2.370  
   2.371 -	/* Initialize the display */
   2.372 -	screen = SDL_SetVideoMode(w, h, desired_bpp, video_flags);
   2.373 -	if ( screen == NULL ) {
   2.374 -		fprintf(stderr, "Couldn't set %dx%dx%d video mode: %s\n",
   2.375 -					w, h, desired_bpp, SDL_GetError());
   2.376 -		quit(1);
   2.377 -	}
   2.378 -	printf("Set%s %dx%dx%d mode\n",
   2.379 -			screen->flags & SDL_FULLSCREEN ? " fullscreen" : "",
   2.380 -			screen->w, screen->h, screen->format->BitsPerPixel);
   2.381 -	printf("(video surface located in %s memory)\n",
   2.382 -			(screen->flags&SDL_HWSURFACE) ? "video" : "system");
   2.383 -	if ( screen->flags & SDL_DOUBLEBUF ) {
   2.384 -		printf("Double-buffering enabled\n");
   2.385 -		flip = 1;
   2.386 -	}
   2.387 -
   2.388 -	/* Set the window manager title bar */
   2.389 -	SDL_WM_SetCaption("SDL test window", "testwin");
   2.390 -
   2.391 -	/* Do all the drawing work */
   2.392 -#ifdef BENCHMARK_SDL
   2.393 -	then = SDL_GetTicks();
   2.394 -	DrawPict(screen, argv[1], speedy, flip, nofade);
   2.395 -	now = SDL_GetTicks();
   2.396 -	printf("Time: %d milliseconds\n", now-then);
   2.397 -#else
   2.398 -	DrawPict(screen, argv[1], speedy, flip, nofade);
   2.399 -#endif
   2.400 -	SDL_Delay(delay*1000);
   2.401 -	SDL_Quit();
   2.402 -	return(0);
   2.403 +        return 0;
   2.404  }