test/testgamma.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 22 Jan 2012 17:21:00 -0500
changeset 6250 cce0ddcf6747
parent 5535 96594ac5fd1a
permissions -rw-r--r--
Updated to SDL 2.0, and SDL 2.0 can now be installed coexisting with SDL 1.2
     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 /* Bring up a window and manipulate the gamma on it */
    14 
    15 #include <stdlib.h>
    16 #include <stdio.h>
    17 #include <string.h>
    18 #include <math.h>
    19 
    20 #include "SDL.h"
    21 
    22 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    23 static void
    24 quit(int rc)
    25 {
    26     SDL_Quit();
    27     exit(rc);
    28 }
    29 
    30 /* This can be used as a general routine for all of the test programs */
    31 int
    32 get_video_args(char *argv[], int *w, int *h, int *bpp, Uint32 * flags)
    33 {
    34     int i;
    35 
    36     *w = 640;
    37     *h = 480;
    38     *bpp = 0;
    39     *flags = SDL_SWSURFACE;
    40 
    41     for (i = 1; argv[i]; ++i) {
    42         if (strcmp(argv[i], "-width") == 0) {
    43             if (argv[i + 1]) {
    44                 *w = atoi(argv[++i]);
    45             }
    46         } else if (strcmp(argv[i], "-height") == 0) {
    47             if (argv[i + 1]) {
    48                 *h = atoi(argv[++i]);
    49             }
    50         } else if (strcmp(argv[i], "-bpp") == 0) {
    51             if (argv[i + 1]) {
    52                 *bpp = atoi(argv[++i]);
    53             }
    54         } else if (strcmp(argv[i], "-fullscreen") == 0) {
    55             *flags |= SDL_FULLSCREEN;
    56         } else if (strcmp(argv[i], "-hw") == 0) {
    57             *flags |= SDL_HWSURFACE;
    58         } else if (strcmp(argv[i], "-hwpalette") == 0) {
    59             *flags |= SDL_HWPALETTE;
    60         } else
    61             break;
    62     }
    63     return i;
    64 }
    65 
    66 int
    67 main(int argc, char *argv[])
    68 {
    69     SDL_Surface *screen;
    70     SDL_Surface *image;
    71     float gamma;
    72     int i;
    73     int w, h, bpp;
    74     Uint32 flags;
    75     Uint16 ramp[256];
    76     Uint16 red_ramp[256];
    77     Uint32 then, timeout;
    78 
    79     /* Check command line arguments */
    80     argv += get_video_args(argv, &w, &h, &bpp, &flags);
    81 
    82     /* Initialize SDL */
    83     if (SDL_Init(SDL_INIT_VIDEO) < 0) {
    84         fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
    85         return (1);
    86     }
    87 
    88     /* Initialize the display, always use hardware palette */
    89     screen = SDL_SetVideoMode(w, h, bpp, flags | SDL_HWPALETTE);
    90     if (screen == NULL) {
    91         fprintf(stderr, "Couldn't set %dx%d video mode: %s\n",
    92                 w, h, SDL_GetError());
    93         quit(1);
    94     }
    95 
    96     /* Set the window manager title bar */
    97     SDL_WM_SetCaption("SDL gamma test", "testgamma");
    98 
    99     /* Set the desired gamma, if any */
   100     gamma = 1.0f;
   101     if (*argv) {
   102         gamma = (float) atof(*argv);
   103     }
   104     if (SDL_SetGamma(gamma, gamma, gamma) < 0) {
   105         fprintf(stderr, "Unable to set gamma: %s\n", SDL_GetError());
   106         quit(1);
   107     }
   108 
   109     /* Do all the drawing work */
   110     image = SDL_LoadBMP("sample.bmp");
   111     if (image) {
   112         SDL_Rect dst;
   113 
   114         dst.x = (screen->w - image->w) / 2;
   115         dst.y = (screen->h - image->h) / 2;
   116         dst.w = image->w;
   117         dst.h = image->h;
   118         SDL_BlitSurface(image, NULL, screen, &dst);
   119         SDL_UpdateRects(screen, 1, &dst);
   120     }
   121 
   122     /* Wait a bit, handling events */
   123     then = SDL_GetTicks();
   124     timeout = (5 * 1000);
   125     while ((SDL_GetTicks() - then) < timeout) {
   126         SDL_Event event;
   127 
   128         while (SDL_PollEvent(&event)) {
   129             switch (event.type) {
   130             case SDL_QUIT:     /* Quit now */
   131                 timeout = 0;
   132                 break;
   133             case SDL_KEYDOWN:
   134                 switch (event.key.keysym.sym) {
   135                 case SDLK_SPACE:       /* Go longer.. */
   136                     timeout += (5 * 1000);
   137                     break;
   138                 case SDLK_UP:
   139                     gamma += 0.2f;
   140                     SDL_SetGamma(gamma, gamma, gamma);
   141                     break;
   142                 case SDLK_DOWN:
   143                     gamma -= 0.2f;
   144                     SDL_SetGamma(gamma, gamma, gamma);
   145                     break;
   146                 case SDLK_ESCAPE:
   147                     timeout = 0;
   148                     break;
   149                 default:
   150                     break;
   151                 }
   152                 break;
   153             }
   154         }
   155     }
   156 
   157     /* Perform a gamma flash to red using color ramps */
   158     while (gamma < 10.0) {
   159         /* Increase the red gamma and decrease everything else... */
   160         gamma += 0.1f;
   161         SDL_CalculateGammaRamp(gamma, red_ramp);
   162         SDL_CalculateGammaRamp(1.0f / gamma, ramp);
   163         SDL_SetGammaRamp(red_ramp, ramp, ramp);
   164     }
   165     /* Finish completely red */
   166     memset(red_ramp, 255, sizeof(red_ramp));
   167     memset(ramp, 0, sizeof(ramp));
   168     SDL_SetGammaRamp(red_ramp, ramp, ramp);
   169 
   170     /* Now fade out to black */
   171     for (i = (red_ramp[0] >> 8); i >= 0; --i) {
   172         memset(red_ramp, i, sizeof(red_ramp));
   173         SDL_SetGammaRamp(red_ramp, NULL, NULL);
   174     }
   175     SDL_Delay(1 * 1000);
   176 
   177     SDL_Quit();
   178     return (0);
   179 }