test/testfill.c
changeset 3576 5ea08f1c29d0
child 5535 96594ac5fd1a
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/testfill.c	Wed Dec 16 02:08:59 2009 +0000
     1.3 @@ -0,0 +1,107 @@
     1.4 +/* Simple program:  Fill the screen with colors as fast as possible */
     1.5 +
     1.6 +#include <stdlib.h>
     1.7 +#include <stdio.h>
     1.8 +#include <string.h>
     1.9 +#include <time.h>
    1.10 +
    1.11 +#include "SDL.h"
    1.12 +
    1.13 +/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    1.14 +static void
    1.15 +quit(int rc)
    1.16 +{
    1.17 +    SDL_Quit();
    1.18 +    exit(rc);
    1.19 +}
    1.20 +
    1.21 +int
    1.22 +main(int argc, char *argv[])
    1.23 +{
    1.24 +    SDL_Surface *screen;
    1.25 +    int width, height;
    1.26 +    Uint8 video_bpp;
    1.27 +    Uint32 videoflags;
    1.28 +    Uint32 colors[3];
    1.29 +    int i, done;
    1.30 +    SDL_Event event;
    1.31 +    Uint32 then, now, frames;
    1.32 +
    1.33 +    /* Initialize SDL */
    1.34 +    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
    1.35 +        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
    1.36 +        return (1);
    1.37 +    }
    1.38 +
    1.39 +    width = 640;
    1.40 +    height = 480;
    1.41 +    video_bpp = 8;
    1.42 +    videoflags = 0;
    1.43 +    while (argc > 1) {
    1.44 +        --argc;
    1.45 +        if (strcmp(argv[argc - 1], "-width") == 0) {
    1.46 +            width = atoi(argv[argc]);
    1.47 +            --argc;
    1.48 +        } else if (strcmp(argv[argc - 1], "-height") == 0) {
    1.49 +            height = atoi(argv[argc]);
    1.50 +            --argc;
    1.51 +        } else if (strcmp(argv[argc - 1], "-bpp") == 0) {
    1.52 +            video_bpp = atoi(argv[argc]);
    1.53 +            --argc;
    1.54 +        } else if (strcmp(argv[argc], "-fullscreen") == 0) {
    1.55 +            videoflags ^= SDL_FULLSCREEN;
    1.56 +        } else {
    1.57 +            fprintf(stderr,
    1.58 +                    "Usage: %s [-width N] [-height N] [-bpp N] [-fullscreen]\n",
    1.59 +                    argv[0]);
    1.60 +            quit(1);
    1.61 +        }
    1.62 +    }
    1.63 +
    1.64 +    /* Set video mode */
    1.65 +    screen = SDL_SetVideoMode(width, height, video_bpp, 0);
    1.66 +    if (!screen) {
    1.67 +        fprintf(stderr, "Couldn't set %dx%d video mode: %s\n",
    1.68 +                width, height, SDL_GetError());
    1.69 +        quit(2);
    1.70 +    }
    1.71 +
    1.72 +    /* Get the colors */
    1.73 +    colors[0] = SDL_MapRGB(screen->format, 0xFF, 0x00, 0x00);
    1.74 +    colors[1] = SDL_MapRGB(screen->format, 0x00, 0xFF, 0x00);
    1.75 +    colors[2] = SDL_MapRGB(screen->format, 0x00, 0x00, 0xFF);
    1.76 +
    1.77 +    /* Loop, filling and waiting for a keystroke */
    1.78 +    frames = 0;
    1.79 +    then = SDL_GetTicks();
    1.80 +    done = 0;
    1.81 +    while (!done) {
    1.82 +        /* Check for events */
    1.83 +        ++frames;
    1.84 +        while (SDL_PollEvent(&event)) {
    1.85 +            switch (event.type) {
    1.86 +            case SDL_MOUSEBUTTONDOWN:
    1.87 +                SDL_WarpMouse(screen->w / 2, screen->h / 2);
    1.88 +                break;
    1.89 +            case SDL_KEYDOWN:
    1.90 +                /* Any keypress quits the app... */
    1.91 +            case SDL_QUIT:
    1.92 +                done = 1;
    1.93 +                break;
    1.94 +            default:
    1.95 +                break;
    1.96 +            }
    1.97 +        }
    1.98 +        SDL_FillRect(screen, NULL, colors[frames%3]);
    1.99 +        SDL_Flip(screen);
   1.100 +    }
   1.101 +
   1.102 +    /* Print out some timing information */
   1.103 +    now = SDL_GetTicks();
   1.104 +    if (now > then) {
   1.105 +        double fps = ((double) frames * 1000) / (now - then);
   1.106 +        printf("%2.2f frames per second\n", fps);
   1.107 +    }
   1.108 +    SDL_Quit();
   1.109 +    return (0);
   1.110 +}