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