test/graywin.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 
    13 /* Simple program:  Fill a colormap with gray and stripe it down the screen */
    14 
    15 #include <stdio.h>
    16 #include <stdlib.h>
    17 #include <string.h>
    18 #include <time.h>
    19 
    20 #include "SDL.h"
    21 
    22 #ifdef TEST_VGA16               /* Define this if you want to test VGA 16-color video modes */
    23 #define NUM_COLORS	16
    24 #else
    25 #define NUM_COLORS	256
    26 #endif
    27 
    28 /* Draw a randomly sized and colored box centered about (X,Y) */
    29 void
    30 DrawBox(SDL_Surface * screen, int X, int Y, int width, int height)
    31 {
    32     static unsigned int seeded = 0;
    33     SDL_Rect area;
    34     Uint32 color;
    35     Uint32 randc;
    36 
    37     /* Seed the random number generator */
    38     if (seeded == 0) {
    39         srand((unsigned int)time(NULL));
    40         seeded = 1;
    41     }
    42 
    43     /* Get the bounds of the rectangle */
    44     area.w = (rand() % width);
    45     area.h = (rand() % height);
    46     area.x = X - (area.w / 2);
    47     area.y = Y - (area.h / 2);
    48     randc = (rand() % NUM_COLORS);
    49 
    50     if (screen->format->BytesPerPixel == 1) {
    51         color = randc;
    52     } else {
    53         color = SDL_MapRGB(screen->format, randc, randc, randc);
    54     }
    55 
    56     /* Do it! */
    57     SDL_FillRect(screen, &area, color);
    58     if (screen->flags & SDL_DOUBLEBUF) {
    59         SDL_Flip(screen);
    60     } else {
    61         SDL_UpdateRects(screen, 1, &area);
    62     }
    63 }
    64 
    65 void
    66 DrawBackground(SDL_Surface * screen)
    67 {
    68     int i, j, k;
    69     Uint8 *buffer;
    70     Uint16 *buffer16;
    71     Uint16 color;
    72     Uint8 gradient;
    73 
    74     /* Set the surface pixels and refresh! */
    75     /* Use two loops in case the surface is double-buffered (both sides) */
    76 
    77     for (j = 0; j < 2; ++j) {
    78         if (SDL_LockSurface(screen) < 0) {
    79             fprintf(stderr, "Couldn't lock display surface: %s\n",
    80                     SDL_GetError());
    81             return;
    82         }
    83         buffer = (Uint8 *) screen->pixels;
    84 
    85         if (screen->format->BytesPerPixel != 2) {
    86             for (i = 0; i < screen->h; ++i) {
    87                 memset(buffer, (i * (NUM_COLORS - 1)) / screen->h,
    88                        screen->w * screen->format->BytesPerPixel);
    89                 buffer += screen->pitch;
    90             }
    91         } else {
    92             for (i = 0; i < screen->h; ++i) {
    93                 gradient = ((i * (NUM_COLORS - 1)) / screen->h);
    94                 color =
    95                     SDL_MapRGB(screen->format, gradient, gradient, gradient);
    96                 buffer16 = (Uint16 *) buffer;
    97                 for (k = 0; k < screen->w; k++) {
    98                     *(buffer16 + k) = color;
    99                 }
   100                 buffer += screen->pitch;
   101             }
   102         }
   103 
   104         SDL_UnlockSurface(screen);
   105         if (screen->flags & SDL_DOUBLEBUF) {
   106             SDL_Flip(screen);
   107         } else {
   108             SDL_UpdateRect(screen, 0, 0, 0, 0);
   109             break;
   110         }
   111     }
   112 }
   113 
   114 SDL_Surface *
   115 CreateScreen(Uint16 w, Uint16 h, Uint8 bpp, Uint32 flags)
   116 {
   117     SDL_Surface *screen;
   118     int i;
   119     SDL_Color palette[NUM_COLORS];
   120 
   121     /* Set the video mode */
   122     screen = SDL_SetVideoMode(w, h, bpp, flags);
   123     if (screen == NULL) {
   124         fprintf(stderr, "Couldn't set display mode: %s\n", SDL_GetError());
   125         return (NULL);
   126     }
   127     fprintf(stderr, "Screen is in %s mode\n",
   128             (screen->flags & SDL_FULLSCREEN) ? "fullscreen" : "windowed");
   129 
   130     if (bpp == 8) {
   131         /* Set a gray colormap, reverse order from white to black */
   132         for (i = 0; i < NUM_COLORS; ++i) {
   133             palette[i].r = (NUM_COLORS - 1) - i * (256 / NUM_COLORS);
   134             palette[i].g = (NUM_COLORS - 1) - i * (256 / NUM_COLORS);
   135             palette[i].b = (NUM_COLORS - 1) - i * (256 / NUM_COLORS);
   136         }
   137         SDL_SetColors(screen, palette, 0, NUM_COLORS);
   138     }
   139 
   140     return (screen);
   141 }
   142 
   143 int
   144 main(int argc, char *argv[])
   145 {
   146     SDL_Surface *screen;
   147     Uint32 videoflags;
   148     int done;
   149     SDL_Event event;
   150     int width, height, bpp;
   151 
   152     /* Initialize SDL */
   153     if (SDL_Init(SDL_INIT_VIDEO) < 0) {
   154         fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
   155         exit(1);
   156     }
   157 
   158     /* See if we try to get a hardware colormap */
   159     width = 640;
   160     height = 480;
   161     bpp = 8;
   162     videoflags = SDL_SWSURFACE;
   163     while (argc > 1) {
   164         --argc;
   165         if (argv[argc - 1] && (strcmp(argv[argc - 1], "-width") == 0)) {
   166             width = atoi(argv[argc]);
   167             --argc;
   168         } else if (argv[argc - 1]
   169                    && (strcmp(argv[argc - 1], "-height") == 0)) {
   170             height = atoi(argv[argc]);
   171             --argc;
   172         } else if (argv[argc - 1] && (strcmp(argv[argc - 1], "-bpp") == 0)) {
   173             bpp = atoi(argv[argc]);
   174             --argc;
   175         } else if (argv[argc] && (strcmp(argv[argc], "-hw") == 0)) {
   176             videoflags |= SDL_HWSURFACE;
   177         } else if (argv[argc] && (strcmp(argv[argc], "-hwpalette") == 0)) {
   178             videoflags |= SDL_HWPALETTE;
   179         } else if (argv[argc] && (strcmp(argv[argc], "-flip") == 0)) {
   180             videoflags |= SDL_DOUBLEBUF;
   181         } else if (argv[argc] && (strcmp(argv[argc], "-noframe") == 0)) {
   182             videoflags |= SDL_NOFRAME;
   183         } else if (argv[argc] && (strcmp(argv[argc], "-resize") == 0)) {
   184             videoflags |= SDL_RESIZABLE;
   185         } else if (argv[argc] && (strcmp(argv[argc], "-fullscreen") == 0)) {
   186             videoflags |= SDL_FULLSCREEN;
   187         } else {
   188             fprintf(stderr,
   189                     "Usage: %s [-width] [-height] [-bpp] [-hw] [-hwpalette] [-flip] [-noframe] [-fullscreen] [-resize]\n",
   190                     argv[0]);
   191             exit(1);
   192         }
   193     }
   194 
   195     /* Set a video mode */
   196     screen = CreateScreen(width, height, bpp, videoflags);
   197     if (screen == NULL) {
   198         exit(2);
   199     }
   200 
   201     DrawBackground(screen);
   202 
   203     /* Wait for a keystroke */
   204     done = 0;
   205     while (!done && SDL_WaitEvent(&event)) {
   206         switch (event.type) {
   207         case SDL_MOUSEBUTTONDOWN:
   208             DrawBox(screen, event.button.x, event.button.y, width, height);
   209             break;
   210         case SDL_KEYDOWN:
   211             /* Ignore ALT-TAB for windows */
   212             if ((event.key.keysym.sym == SDLK_LALT) ||
   213                 (event.key.keysym.sym == SDLK_TAB)) {
   214                 break;
   215             }
   216             /* Center the mouse on <SPACE> */
   217             if (event.key.keysym.sym == SDLK_SPACE) {
   218                 SDL_WarpMouse(width / 2, height / 2);
   219                 break;
   220             }
   221             /* Toggle fullscreen mode on <RETURN> */
   222             if (event.key.keysym.sym == SDLK_RETURN) {
   223                 videoflags ^= SDL_FULLSCREEN;
   224                 screen = CreateScreen(screen->w, screen->h,
   225                                       screen->format->BitsPerPixel,
   226                                       videoflags);
   227                 if (screen == NULL) {
   228                     fprintf(stderr, "Couldn't toggle fullscreen mode\n");
   229                     done = 1;
   230                 }
   231                 DrawBackground(screen);
   232                 break;
   233             }
   234             /* Any other key quits the application... */
   235         case SDL_QUIT:
   236             done = 1;
   237             break;
   238         case SDL_VIDEOEXPOSE:
   239             DrawBackground(screen);
   240             break;
   241         case SDL_VIDEORESIZE:
   242             printf("Screen resized to %dx%d\n", event.resize.w,
   243                    event.resize.h);
   244             screen =
   245                 CreateScreen(event.resize.w, event.resize.h,
   246                              screen->format->BitsPerPixel, videoflags);
   247             if (screen == NULL) {
   248                 fprintf(stderr, "Couldn't resize video mode\n");
   249                 done = 1;
   250             }
   251             DrawBackground(screen);
   252             break;
   253         default:
   254             break;
   255         }
   256     }
   257     SDL_Quit();
   258     return (0);
   259 }