test/testfill.c
author Ryan C. Gordon <icculus@icculus.org>
Fri, 03 Jun 2011 16:03:10 -0400
changeset 5547 4ccecd0901e2
parent 5535 96594ac5fd1a
permissions -rw-r--r--
Assert code's stdio interface was reading from the wrong variable.

Thanks to Frank Zago for the catch.
slouken@5535
     1
/*
slouken@5535
     2
  Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
slouken@5535
     3
slouken@5535
     4
  This software is provided 'as-is', without any express or implied
slouken@5535
     5
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     6
  arising from the use of this software.
slouken@5535
     7
slouken@5535
     8
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
     9
  including commercial applications, and to alter it and redistribute it
slouken@5535
    10
  freely.
slouken@5535
    11
*/
slouken@3576
    12
/* Simple program:  Fill the screen with colors as fast as possible */
slouken@3576
    13
slouken@3576
    14
#include <stdlib.h>
slouken@3576
    15
#include <stdio.h>
slouken@3576
    16
#include <string.h>
slouken@3576
    17
#include <time.h>
slouken@3576
    18
slouken@3576
    19
#include "SDL.h"
slouken@3576
    20
slouken@3576
    21
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
slouken@3576
    22
static void
slouken@3576
    23
quit(int rc)
slouken@3576
    24
{
slouken@3576
    25
    SDL_Quit();
slouken@3576
    26
    exit(rc);
slouken@3576
    27
}
slouken@3576
    28
slouken@3576
    29
int
slouken@3576
    30
main(int argc, char *argv[])
slouken@3576
    31
{
slouken@3576
    32
    SDL_Surface *screen;
slouken@3576
    33
    int width, height;
slouken@3576
    34
    Uint8 video_bpp;
slouken@3576
    35
    Uint32 videoflags;
slouken@3576
    36
    Uint32 colors[3];
slouken@3576
    37
    int i, done;
slouken@3576
    38
    SDL_Event event;
slouken@3576
    39
    Uint32 then, now, frames;
slouken@3576
    40
slouken@3576
    41
    /* Initialize SDL */
slouken@3576
    42
    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
slouken@3576
    43
        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
slouken@3576
    44
        return (1);
slouken@3576
    45
    }
slouken@3576
    46
slouken@3576
    47
    width = 640;
slouken@3576
    48
    height = 480;
slouken@3576
    49
    video_bpp = 8;
slouken@3576
    50
    videoflags = 0;
slouken@3576
    51
    while (argc > 1) {
slouken@3576
    52
        --argc;
slouken@3576
    53
        if (strcmp(argv[argc - 1], "-width") == 0) {
slouken@3576
    54
            width = atoi(argv[argc]);
slouken@3576
    55
            --argc;
slouken@3576
    56
        } else if (strcmp(argv[argc - 1], "-height") == 0) {
slouken@3576
    57
            height = atoi(argv[argc]);
slouken@3576
    58
            --argc;
slouken@3576
    59
        } else if (strcmp(argv[argc - 1], "-bpp") == 0) {
slouken@3576
    60
            video_bpp = atoi(argv[argc]);
slouken@3576
    61
            --argc;
slouken@3576
    62
        } else if (strcmp(argv[argc], "-fullscreen") == 0) {
slouken@3576
    63
            videoflags ^= SDL_FULLSCREEN;
slouken@3576
    64
        } else {
slouken@3576
    65
            fprintf(stderr,
slouken@3576
    66
                    "Usage: %s [-width N] [-height N] [-bpp N] [-fullscreen]\n",
slouken@3576
    67
                    argv[0]);
slouken@3576
    68
            quit(1);
slouken@3576
    69
        }
slouken@3576
    70
    }
slouken@3576
    71
slouken@3576
    72
    /* Set video mode */
slouken@3576
    73
    screen = SDL_SetVideoMode(width, height, video_bpp, 0);
slouken@3576
    74
    if (!screen) {
slouken@3576
    75
        fprintf(stderr, "Couldn't set %dx%d video mode: %s\n",
slouken@3576
    76
                width, height, SDL_GetError());
slouken@3576
    77
        quit(2);
slouken@3576
    78
    }
slouken@3576
    79
slouken@3576
    80
    /* Get the colors */
slouken@3576
    81
    colors[0] = SDL_MapRGB(screen->format, 0xFF, 0x00, 0x00);
slouken@3576
    82
    colors[1] = SDL_MapRGB(screen->format, 0x00, 0xFF, 0x00);
slouken@3576
    83
    colors[2] = SDL_MapRGB(screen->format, 0x00, 0x00, 0xFF);
slouken@3576
    84
slouken@3576
    85
    /* Loop, filling and waiting for a keystroke */
slouken@3576
    86
    frames = 0;
slouken@3576
    87
    then = SDL_GetTicks();
slouken@3576
    88
    done = 0;
slouken@3576
    89
    while (!done) {
slouken@3576
    90
        /* Check for events */
slouken@3576
    91
        ++frames;
slouken@3576
    92
        while (SDL_PollEvent(&event)) {
slouken@3576
    93
            switch (event.type) {
slouken@3576
    94
            case SDL_MOUSEBUTTONDOWN:
slouken@3576
    95
                SDL_WarpMouse(screen->w / 2, screen->h / 2);
slouken@3576
    96
                break;
slouken@3576
    97
            case SDL_KEYDOWN:
slouken@3576
    98
                /* Any keypress quits the app... */
slouken@3576
    99
            case SDL_QUIT:
slouken@3576
   100
                done = 1;
slouken@3576
   101
                break;
slouken@3576
   102
            default:
slouken@3576
   103
                break;
slouken@3576
   104
            }
slouken@3576
   105
        }
slouken@3576
   106
        SDL_FillRect(screen, NULL, colors[frames%3]);
slouken@3576
   107
        SDL_Flip(screen);
slouken@3576
   108
    }
slouken@3576
   109
slouken@3576
   110
    /* Print out some timing information */
slouken@3576
   111
    now = SDL_GetTicks();
slouken@3576
   112
    if (now > then) {
slouken@3576
   113
        double fps = ((double) frames * 1000) / (now - then);
slouken@3576
   114
        printf("%2.2f frames per second\n", fps);
slouken@3576
   115
    }
slouken@3576
   116
    SDL_Quit();
slouken@3576
   117
    return (0);
slouken@3576
   118
}