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