test/graywin.c
branchSDL-1.3
changeset 1662 782fd950bd46
parent 945 d33645c36072
child 1668 4da1ee79c9af
     1.1 --- a/test/graywin.c	Sun May 21 17:27:13 2006 +0000
     1.2 +++ b/test/graywin.c	Sun May 28 13:04:16 2006 +0000
     1.3 @@ -8,250 +8,238 @@
     1.4  
     1.5  #include "SDL.h"
     1.6  
     1.7 -#ifdef TEST_VGA16 /* Define this if you want to test VGA 16-color video modes */
     1.8 +#ifdef TEST_VGA16               /* Define this if you want to test VGA 16-color video modes */
     1.9  #define NUM_COLORS	16
    1.10  #else
    1.11  #define NUM_COLORS	256
    1.12  #endif
    1.13  
    1.14  /* Draw a randomly sized and colored box centered about (X,Y) */
    1.15 -void DrawBox(SDL_Surface *screen, int X, int Y, int width, int height)
    1.16 +void
    1.17 +DrawBox (SDL_Surface * screen, int X, int Y, int width, int height)
    1.18  {
    1.19 -	static unsigned int seeded = 0;
    1.20 -	SDL_Rect area;
    1.21 -	Uint32 color;
    1.22 -        Uint32 randc;
    1.23 +    static unsigned int seeded = 0;
    1.24 +    SDL_Rect area;
    1.25 +    Uint32 color;
    1.26 +    Uint32 randc;
    1.27  
    1.28 -	/* Seed the random number generator */
    1.29 -	if ( seeded == 0 ) {
    1.30 -		srand(time(NULL));
    1.31 -		seeded = 1;
    1.32 -	}
    1.33 +    /* Seed the random number generator */
    1.34 +    if (seeded == 0) {
    1.35 +        srand (time (NULL));
    1.36 +        seeded = 1;
    1.37 +    }
    1.38  
    1.39 -	/* Get the bounds of the rectangle */
    1.40 -	area.w = (rand()%width);
    1.41 -	area.h = (rand()%height);
    1.42 -	area.x = X-(area.w/2);
    1.43 -	area.y = Y-(area.h/2);
    1.44 -        randc = (rand()%NUM_COLORS);
    1.45 +    /* Get the bounds of the rectangle */
    1.46 +    area.w = (rand () % width);
    1.47 +    area.h = (rand () % height);
    1.48 +    area.x = X - (area.w / 2);
    1.49 +    area.y = Y - (area.h / 2);
    1.50 +    randc = (rand () % NUM_COLORS);
    1.51  
    1.52 -        if (screen->format->BytesPerPixel==1)
    1.53 -        {
    1.54 -            color = randc;
    1.55 +    if (screen->format->BytesPerPixel == 1) {
    1.56 +        color = randc;
    1.57 +    } else {
    1.58 +        color = SDL_MapRGB (screen->format, randc, randc, randc);
    1.59 +    }
    1.60 +
    1.61 +    /* Do it! */
    1.62 +    SDL_FillRect (screen, &area, color);
    1.63 +    if (screen->flags & SDL_DOUBLEBUF) {
    1.64 +        SDL_Flip (screen);
    1.65 +    } else {
    1.66 +        SDL_UpdateRects (screen, 1, &area);
    1.67 +    }
    1.68 +}
    1.69 +
    1.70 +void
    1.71 +DrawBackground (SDL_Surface * screen)
    1.72 +{
    1.73 +    int i, j, k;
    1.74 +    Uint8 *buffer;
    1.75 +    Uint16 *buffer16;
    1.76 +    Uint16 color;
    1.77 +    Uint8 gradient;
    1.78 +
    1.79 +    /* Set the surface pixels and refresh! */
    1.80 +    /* Use two loops in case the surface is double-buffered (both sides) */
    1.81 +
    1.82 +    for (j = 0; j < 2; ++j) {
    1.83 +        if (SDL_LockSurface (screen) < 0) {
    1.84 +            fprintf (stderr, "Couldn't lock display surface: %s\n",
    1.85 +                     SDL_GetError ());
    1.86 +            return;
    1.87          }
    1.88 -        else
    1.89 -        {
    1.90 -            color = SDL_MapRGB(screen->format, randc, randc, randc);
    1.91 +        buffer = (Uint8 *) screen->pixels;
    1.92 +
    1.93 +        if (screen->format->BytesPerPixel != 2) {
    1.94 +            for (i = 0; i < screen->h; ++i) {
    1.95 +                memset (buffer, (i * (NUM_COLORS - 1)) / screen->h,
    1.96 +                        screen->w * screen->format->BytesPerPixel);
    1.97 +                buffer += screen->pitch;
    1.98 +            }
    1.99 +        } else {
   1.100 +            for (i = 0; i < screen->h; ++i) {
   1.101 +                gradient = ((i * (NUM_COLORS - 1)) / screen->h);
   1.102 +                color =
   1.103 +                    SDL_MapRGB (screen->format, gradient, gradient, gradient);
   1.104 +                buffer16 = (Uint16 *) buffer;
   1.105 +                for (k = 0; k < screen->w; k++) {
   1.106 +                    *(buffer16 + k) = color;
   1.107 +                }
   1.108 +                buffer += screen->pitch;
   1.109 +            }
   1.110          }
   1.111  
   1.112 -	/* Do it! */
   1.113 -	SDL_FillRect(screen, &area, color);
   1.114 -	if ( screen->flags & SDL_DOUBLEBUF ) {
   1.115 -		SDL_Flip(screen);
   1.116 -	} else {
   1.117 -		SDL_UpdateRects(screen, 1, &area);
   1.118 -	}
   1.119 +        SDL_UnlockSurface (screen);
   1.120 +        if (screen->flags & SDL_DOUBLEBUF) {
   1.121 +            SDL_Flip (screen);
   1.122 +        } else {
   1.123 +            SDL_UpdateRect (screen, 0, 0, 0, 0);
   1.124 +            break;
   1.125 +        }
   1.126 +    }
   1.127  }
   1.128  
   1.129 -void DrawBackground(SDL_Surface *screen)
   1.130 +SDL_Surface *
   1.131 +CreateScreen (Uint16 w, Uint16 h, Uint8 bpp, Uint32 flags)
   1.132  {
   1.133 -	int i, j, k;
   1.134 -	Uint8  *buffer;
   1.135 -	Uint16 *buffer16;
   1.136 -        Uint16 color;
   1.137 -        Uint8  gradient;
   1.138 +    SDL_Surface *screen;
   1.139 +    int i;
   1.140 +    SDL_Color palette[NUM_COLORS];
   1.141  
   1.142 -	/* Set the surface pixels and refresh! */
   1.143 -	/* Use two loops in case the surface is double-buffered (both sides) */
   1.144 +    /* Set the video mode */
   1.145 +    screen = SDL_SetVideoMode (w, h, bpp, flags);
   1.146 +    if (screen == NULL) {
   1.147 +        fprintf (stderr, "Couldn't set display mode: %s\n", SDL_GetError ());
   1.148 +        return (NULL);
   1.149 +    }
   1.150 +    fprintf (stderr, "Screen is in %s mode\n",
   1.151 +             (screen->flags & SDL_FULLSCREEN) ? "fullscreen" : "windowed");
   1.152  
   1.153 -	for ( j=0; j<2; ++j ) {
   1.154 -		if ( SDL_LockSurface(screen) < 0 ) {
   1.155 -			fprintf(stderr, "Couldn't lock display surface: %s\n",
   1.156 -								SDL_GetError());
   1.157 -			return;
   1.158 -		}
   1.159 -		buffer = (Uint8 *)screen->pixels;
   1.160 +    if (bpp == 8) {
   1.161 +        /* Set a gray colormap, reverse order from white to black */
   1.162 +        for (i = 0; i < NUM_COLORS; ++i) {
   1.163 +            palette[i].r = (NUM_COLORS - 1) - i * (256 / NUM_COLORS);
   1.164 +            palette[i].g = (NUM_COLORS - 1) - i * (256 / NUM_COLORS);
   1.165 +            palette[i].b = (NUM_COLORS - 1) - i * (256 / NUM_COLORS);
   1.166 +        }
   1.167 +        SDL_SetColors (screen, palette, 0, NUM_COLORS);
   1.168 +    }
   1.169  
   1.170 -		if (screen->format->BytesPerPixel!=2) {
   1.171 -			for ( i=0; i<screen->h; ++i ) {
   1.172 -				memset(buffer,(i*(NUM_COLORS-1))/screen->h, screen->w * screen->format->BytesPerPixel);
   1.173 -				buffer += screen->pitch;
   1.174 -			}
   1.175 -		}
   1.176 -                else
   1.177 -                {
   1.178 -			for ( i=0; i<screen->h; ++i ) {
   1.179 -				gradient=((i*(NUM_COLORS-1))/screen->h);
   1.180 -                                color = SDL_MapRGB(screen->format, gradient, gradient, gradient);
   1.181 -                                buffer16=(Uint16*)buffer;
   1.182 -                                for (k=0; k<screen->w; k++)
   1.183 -                                {
   1.184 -                                   *(buffer16+k)=color;
   1.185 -                                }
   1.186 -				buffer += screen->pitch;
   1.187 -			}
   1.188 -                }
   1.189 -
   1.190 -		SDL_UnlockSurface(screen);
   1.191 -		if ( screen->flags & SDL_DOUBLEBUF ) {
   1.192 -			SDL_Flip(screen);
   1.193 -		} else {
   1.194 -			SDL_UpdateRect(screen, 0, 0, 0, 0);
   1.195 -                        break;
   1.196 -		}
   1.197 -	}
   1.198 +    return (screen);
   1.199  }
   1.200  
   1.201 -SDL_Surface *CreateScreen(Uint16 w, Uint16 h, Uint8 bpp, Uint32 flags)
   1.202 +int
   1.203 +main (int argc, char *argv[])
   1.204  {
   1.205 -	SDL_Surface *screen;
   1.206 -	int i;
   1.207 -	SDL_Color palette[NUM_COLORS];
   1.208 +    SDL_Surface *screen;
   1.209 +    Uint32 videoflags;
   1.210 +    int done;
   1.211 +    SDL_Event event;
   1.212 +    int width, height, bpp;
   1.213  
   1.214 -	/* Set the video mode */
   1.215 -	screen = SDL_SetVideoMode(w, h, bpp, flags);
   1.216 -	if ( screen == NULL ) {
   1.217 -		fprintf(stderr, "Couldn't set display mode: %s\n",
   1.218 -							SDL_GetError());
   1.219 -		return(NULL);
   1.220 -	}
   1.221 -	fprintf(stderr, "Screen is in %s mode\n",
   1.222 -		(screen->flags & SDL_FULLSCREEN) ? "fullscreen" : "windowed");
   1.223 +    /* Initialize SDL */
   1.224 +    if (SDL_Init (SDL_INIT_VIDEO) < 0) {
   1.225 +        fprintf (stderr, "Couldn't initialize SDL: %s\n", SDL_GetError ());
   1.226 +        exit (1);
   1.227 +    }
   1.228  
   1.229 -	if (bpp==8) {
   1.230 -		/* Set a gray colormap, reverse order from white to black */
   1.231 -		for ( i=0; i<NUM_COLORS; ++i ) {
   1.232 -			palette[i].r = (NUM_COLORS-1)-i * (256 / NUM_COLORS);
   1.233 -			palette[i].g = (NUM_COLORS-1)-i * (256 / NUM_COLORS);
   1.234 -			palette[i].b = (NUM_COLORS-1)-i * (256 / NUM_COLORS);
   1.235 -		}
   1.236 -		SDL_SetColors(screen, palette, 0, NUM_COLORS);
   1.237 -	}
   1.238 +    /* See if we try to get a hardware colormap */
   1.239 +    width = 640;
   1.240 +    height = 480;
   1.241 +    bpp = 8;
   1.242 +    videoflags = SDL_SWSURFACE;
   1.243 +    while (argc > 1) {
   1.244 +        --argc;
   1.245 +        if (argv[argc - 1] && (strcmp (argv[argc - 1], "-width") == 0)) {
   1.246 +            width = atoi (argv[argc]);
   1.247 +            --argc;
   1.248 +        } else if (argv[argc - 1]
   1.249 +                   && (strcmp (argv[argc - 1], "-height") == 0)) {
   1.250 +            height = atoi (argv[argc]);
   1.251 +            --argc;
   1.252 +        } else if (argv[argc - 1] && (strcmp (argv[argc - 1], "-bpp") == 0)) {
   1.253 +            bpp = atoi (argv[argc]);
   1.254 +            --argc;
   1.255 +        } else if (argv[argc] && (strcmp (argv[argc], "-hw") == 0)) {
   1.256 +            videoflags |= SDL_HWSURFACE;
   1.257 +        } else if (argv[argc] && (strcmp (argv[argc], "-hwpalette") == 0)) {
   1.258 +            videoflags |= SDL_HWPALETTE;
   1.259 +        } else if (argv[argc] && (strcmp (argv[argc], "-flip") == 0)) {
   1.260 +            videoflags |= SDL_DOUBLEBUF;
   1.261 +        } else if (argv[argc] && (strcmp (argv[argc], "-noframe") == 0)) {
   1.262 +            videoflags |= SDL_NOFRAME;
   1.263 +        } else if (argv[argc] && (strcmp (argv[argc], "-resize") == 0)) {
   1.264 +            videoflags |= SDL_RESIZABLE;
   1.265 +        } else if (argv[argc] && (strcmp (argv[argc], "-fullscreen") == 0)) {
   1.266 +            videoflags |= SDL_FULLSCREEN;
   1.267 +        } else {
   1.268 +            fprintf (stderr,
   1.269 +                     "Usage: %s [-width] [-height] [-bpp] [-hw] [-hwpalette] [-flip] [-noframe] [-fullscreen] [-resize]\n",
   1.270 +                     argv[0]);
   1.271 +            exit (1);
   1.272 +        }
   1.273 +    }
   1.274  
   1.275 -	return(screen);
   1.276 +    /* Set a video mode */
   1.277 +    screen = CreateScreen (width, height, bpp, videoflags);
   1.278 +    if (screen == NULL) {
   1.279 +        exit (2);
   1.280 +    }
   1.281 +
   1.282 +    DrawBackground (screen);
   1.283 +
   1.284 +    /* Wait for a keystroke */
   1.285 +    done = 0;
   1.286 +    while (!done && SDL_WaitEvent (&event)) {
   1.287 +        switch (event.type) {
   1.288 +        case SDL_MOUSEBUTTONDOWN:
   1.289 +            DrawBox (screen, event.button.x, event.button.y, width, height);
   1.290 +            break;
   1.291 +        case SDL_KEYDOWN:
   1.292 +            /* Ignore ALT-TAB for windows */
   1.293 +            if ((event.key.keysym.sym == SDLK_LALT) ||
   1.294 +                (event.key.keysym.sym == SDLK_TAB)) {
   1.295 +                break;
   1.296 +            }
   1.297 +            /* Center the mouse on <SPACE> */
   1.298 +            if (event.key.keysym.sym == SDLK_SPACE) {
   1.299 +                SDL_WarpMouse (width / 2, height / 2);
   1.300 +                break;
   1.301 +            }
   1.302 +            /* Toggle fullscreen mode on <RETURN> */
   1.303 +            if (event.key.keysym.sym == SDLK_RETURN) {
   1.304 +                videoflags ^= SDL_FULLSCREEN;
   1.305 +                screen = CreateScreen (screen->w, screen->h,
   1.306 +                                       screen->format->BitsPerPixel,
   1.307 +                                       videoflags);
   1.308 +                if (screen == NULL) {
   1.309 +                    fprintf (stderr, "Couldn't toggle fullscreen mode\n");
   1.310 +                    done = 1;
   1.311 +                }
   1.312 +                DrawBackground (screen);
   1.313 +                break;
   1.314 +            }
   1.315 +            /* Any other key quits the application... */
   1.316 +        case SDL_QUIT:
   1.317 +            done = 1;
   1.318 +            break;
   1.319 +        case SDL_VIDEOEXPOSE:
   1.320 +            DrawBackground (screen);
   1.321 +            break;
   1.322 +        case SDL_VIDEORESIZE:
   1.323 +            screen = CreateScreen (event.resize.w, event.resize.h,
   1.324 +                                   screen->format->BitsPerPixel, videoflags);
   1.325 +            if (screen == NULL) {
   1.326 +                fprintf (stderr, "Couldn't resize video mode\n");
   1.327 +                done = 1;
   1.328 +            }
   1.329 +            DrawBackground (screen);
   1.330 +            break;
   1.331 +        default:
   1.332 +            break;
   1.333 +        }
   1.334 +    }
   1.335 +    SDL_Quit ();
   1.336 +    return (0);
   1.337  }
   1.338 -
   1.339 -int main(int argc, char *argv[])
   1.340 -{
   1.341 -	SDL_Surface *screen;
   1.342 -	Uint32 videoflags;
   1.343 -	int    done;
   1.344 -	SDL_Event event;
   1.345 -	int width, height, bpp;
   1.346 -
   1.347 -	/* Initialize SDL */
   1.348 -	if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
   1.349 -		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
   1.350 -		exit(1);
   1.351 -	}
   1.352 -
   1.353 -	/* See if we try to get a hardware colormap */
   1.354 -	width = 640;
   1.355 -	height = 480;
   1.356 -	bpp = 8;
   1.357 -	videoflags = SDL_SWSURFACE;
   1.358 -	while ( argc > 1 ) {
   1.359 -		--argc;
   1.360 -		if ( argv[argc-1] && (strcmp(argv[argc-1], "-width") == 0) ) {
   1.361 -			width = atoi(argv[argc]);
   1.362 -			--argc;
   1.363 -		} else
   1.364 -		if ( argv[argc-1] && (strcmp(argv[argc-1], "-height") == 0) ) {
   1.365 -			height = atoi(argv[argc]);
   1.366 -			--argc;
   1.367 -		} else
   1.368 -		if ( argv[argc-1] && (strcmp(argv[argc-1], "-bpp") == 0) ) {
   1.369 -			bpp = atoi(argv[argc]);
   1.370 -			--argc;
   1.371 -		} else
   1.372 -		if ( argv[argc] && (strcmp(argv[argc], "-hw") == 0) ) {
   1.373 -			videoflags |= SDL_HWSURFACE;
   1.374 -		} else
   1.375 -		if ( argv[argc] && (strcmp(argv[argc], "-hwpalette") == 0) ) {
   1.376 -			videoflags |= SDL_HWPALETTE;
   1.377 -		} else
   1.378 -		if ( argv[argc] && (strcmp(argv[argc], "-flip") == 0) ) {
   1.379 -			videoflags |= SDL_DOUBLEBUF;
   1.380 -		} else
   1.381 -		if ( argv[argc] && (strcmp(argv[argc], "-noframe") == 0) ) {
   1.382 -			videoflags |= SDL_NOFRAME;
   1.383 -		} else
   1.384 -		if ( argv[argc] && (strcmp(argv[argc], "-resize") == 0) ) {
   1.385 -			videoflags |= SDL_RESIZABLE;
   1.386 -		} else
   1.387 -		if ( argv[argc] && (strcmp(argv[argc], "-fullscreen") == 0) ) {
   1.388 -			videoflags |= SDL_FULLSCREEN;
   1.389 -		} else {
   1.390 -			fprintf(stderr, "Usage: %s [-width] [-height] [-bpp] [-hw] [-hwpalette] [-flip] [-noframe] [-fullscreen] [-resize]\n",
   1.391 -								argv[0]);
   1.392 -			exit(1);
   1.393 -		}
   1.394 -	}
   1.395 -
   1.396 -	/* Set a video mode */
   1.397 -	screen = CreateScreen(width, height, bpp, videoflags);
   1.398 -	if ( screen == NULL ) {
   1.399 -		exit(2);
   1.400 -	}
   1.401 -        
   1.402 -        DrawBackground(screen);
   1.403 -		
   1.404 -	/* Wait for a keystroke */
   1.405 -	done = 0;
   1.406 -	while ( !done && SDL_WaitEvent(&event) ) {
   1.407 -		switch (event.type) {
   1.408 -			case SDL_MOUSEBUTTONDOWN:
   1.409 -				DrawBox(screen, event.button.x, event.button.y, width, height);
   1.410 -				break;
   1.411 -			case SDL_KEYDOWN:
   1.412 -				/* Ignore ALT-TAB for windows */
   1.413 -				if ( (event.key.keysym.sym == SDLK_LALT) ||
   1.414 -				     (event.key.keysym.sym == SDLK_TAB) ) {
   1.415 -					break;
   1.416 -				}
   1.417 -				/* Center the mouse on <SPACE> */
   1.418 -				if ( event.key.keysym.sym == SDLK_SPACE ) {
   1.419 -					SDL_WarpMouse(width/2, height/2);
   1.420 -					break;
   1.421 -				}
   1.422 -				/* Toggle fullscreen mode on <RETURN> */
   1.423 -				if ( event.key.keysym.sym == SDLK_RETURN ) {
   1.424 -					videoflags ^= SDL_FULLSCREEN;
   1.425 -					screen = CreateScreen(
   1.426 -						screen->w, screen->h,
   1.427 -						screen->format->BitsPerPixel,
   1.428 -								videoflags);
   1.429 -					if ( screen == NULL ) {
   1.430 -						fprintf(stderr,
   1.431 -					"Couldn't toggle fullscreen mode\n");
   1.432 -						done = 1;
   1.433 -					}
   1.434 -                                        DrawBackground(screen);
   1.435 -					break;
   1.436 -				}
   1.437 -				/* Any other key quits the application... */
   1.438 -			case SDL_QUIT:
   1.439 -				done = 1;
   1.440 -				break;
   1.441 -			case SDL_VIDEOEXPOSE:
   1.442 -				DrawBackground(screen);
   1.443 -				break;
   1.444 -			case SDL_VIDEORESIZE:
   1.445 -					screen = CreateScreen(
   1.446 -						event.resize.w, event.resize.h,
   1.447 -						screen->format->BitsPerPixel,
   1.448 -								videoflags);
   1.449 -					if ( screen == NULL ) {
   1.450 -						fprintf(stderr,
   1.451 -					"Couldn't resize video mode\n");
   1.452 -						done = 1;
   1.453 -					}
   1.454 -					DrawBackground(screen);
   1.455 -				break;
   1.456 -			default:
   1.457 -				break;
   1.458 -		}
   1.459 -	}
   1.460 -	SDL_Quit();
   1.461 -	return(0);
   1.462 -}