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