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