test/testfill.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 16 Dec 2009 02:08:59 +0000
changeset 3576 5ea08f1c29d0
child 5535 96594ac5fd1a
permissions -rw-r--r--
Added testfill to test raw fill performance
slouken@3576
     1
/* Simple program:  Fill the screen with colors as fast as possible */
slouken@3576
     2
slouken@3576
     3
#include <stdlib.h>
slouken@3576
     4
#include <stdio.h>
slouken@3576
     5
#include <string.h>
slouken@3576
     6
#include <time.h>
slouken@3576
     7
slouken@3576
     8
#include "SDL.h"
slouken@3576
     9
slouken@3576
    10
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
slouken@3576
    11
static void
slouken@3576
    12
quit(int rc)
slouken@3576
    13
{
slouken@3576
    14
    SDL_Quit();
slouken@3576
    15
    exit(rc);
slouken@3576
    16
}
slouken@3576
    17
slouken@3576
    18
int
slouken@3576
    19
main(int argc, char *argv[])
slouken@3576
    20
{
slouken@3576
    21
    SDL_Surface *screen;
slouken@3576
    22
    int width, height;
slouken@3576
    23
    Uint8 video_bpp;
slouken@3576
    24
    Uint32 videoflags;
slouken@3576
    25
    Uint32 colors[3];
slouken@3576
    26
    int i, done;
slouken@3576
    27
    SDL_Event event;
slouken@3576
    28
    Uint32 then, now, frames;
slouken@3576
    29
slouken@3576
    30
    /* Initialize SDL */
slouken@3576
    31
    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
slouken@3576
    32
        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
slouken@3576
    33
        return (1);
slouken@3576
    34
    }
slouken@3576
    35
slouken@3576
    36
    width = 640;
slouken@3576
    37
    height = 480;
slouken@3576
    38
    video_bpp = 8;
slouken@3576
    39
    videoflags = 0;
slouken@3576
    40
    while (argc > 1) {
slouken@3576
    41
        --argc;
slouken@3576
    42
        if (strcmp(argv[argc - 1], "-width") == 0) {
slouken@3576
    43
            width = atoi(argv[argc]);
slouken@3576
    44
            --argc;
slouken@3576
    45
        } else if (strcmp(argv[argc - 1], "-height") == 0) {
slouken@3576
    46
            height = atoi(argv[argc]);
slouken@3576
    47
            --argc;
slouken@3576
    48
        } else if (strcmp(argv[argc - 1], "-bpp") == 0) {
slouken@3576
    49
            video_bpp = atoi(argv[argc]);
slouken@3576
    50
            --argc;
slouken@3576
    51
        } else if (strcmp(argv[argc], "-fullscreen") == 0) {
slouken@3576
    52
            videoflags ^= SDL_FULLSCREEN;
slouken@3576
    53
        } else {
slouken@3576
    54
            fprintf(stderr,
slouken@3576
    55
                    "Usage: %s [-width N] [-height N] [-bpp N] [-fullscreen]\n",
slouken@3576
    56
                    argv[0]);
slouken@3576
    57
            quit(1);
slouken@3576
    58
        }
slouken@3576
    59
    }
slouken@3576
    60
slouken@3576
    61
    /* Set video mode */
slouken@3576
    62
    screen = SDL_SetVideoMode(width, height, video_bpp, 0);
slouken@3576
    63
    if (!screen) {
slouken@3576
    64
        fprintf(stderr, "Couldn't set %dx%d video mode: %s\n",
slouken@3576
    65
                width, height, SDL_GetError());
slouken@3576
    66
        quit(2);
slouken@3576
    67
    }
slouken@3576
    68
slouken@3576
    69
    /* Get the colors */
slouken@3576
    70
    colors[0] = SDL_MapRGB(screen->format, 0xFF, 0x00, 0x00);
slouken@3576
    71
    colors[1] = SDL_MapRGB(screen->format, 0x00, 0xFF, 0x00);
slouken@3576
    72
    colors[2] = SDL_MapRGB(screen->format, 0x00, 0x00, 0xFF);
slouken@3576
    73
slouken@3576
    74
    /* Loop, filling and waiting for a keystroke */
slouken@3576
    75
    frames = 0;
slouken@3576
    76
    then = SDL_GetTicks();
slouken@3576
    77
    done = 0;
slouken@3576
    78
    while (!done) {
slouken@3576
    79
        /* Check for events */
slouken@3576
    80
        ++frames;
slouken@3576
    81
        while (SDL_PollEvent(&event)) {
slouken@3576
    82
            switch (event.type) {
slouken@3576
    83
            case SDL_MOUSEBUTTONDOWN:
slouken@3576
    84
                SDL_WarpMouse(screen->w / 2, screen->h / 2);
slouken@3576
    85
                break;
slouken@3576
    86
            case SDL_KEYDOWN:
slouken@3576
    87
                /* Any keypress quits the app... */
slouken@3576
    88
            case SDL_QUIT:
slouken@3576
    89
                done = 1;
slouken@3576
    90
                break;
slouken@3576
    91
            default:
slouken@3576
    92
                break;
slouken@3576
    93
            }
slouken@3576
    94
        }
slouken@3576
    95
        SDL_FillRect(screen, NULL, colors[frames%3]);
slouken@3576
    96
        SDL_Flip(screen);
slouken@3576
    97
    }
slouken@3576
    98
slouken@3576
    99
    /* Print out some timing information */
slouken@3576
   100
    now = SDL_GetTicks();
slouken@3576
   101
    if (now > then) {
slouken@3576
   102
        double fps = ((double) frames * 1000) / (now - then);
slouken@3576
   103
        printf("%2.2f frames per second\n", fps);
slouken@3576
   104
    }
slouken@3576
   105
    SDL_Quit();
slouken@3576
   106
    return (0);
slouken@3576
   107
}