test/testgamma.c
changeset 0 74212992fb08
child 1151 be9c9c8f6d53
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/testgamma.c	Thu Apr 26 16:45:43 2001 +0000
     1.3 @@ -0,0 +1,190 @@
     1.4 +
     1.5 +/* Bring up a window and manipulate the gamma on it */
     1.6 +
     1.7 +#include <stdlib.h>
     1.8 +#include <stdio.h>
     1.9 +#include <string.h>
    1.10 +#include <math.h>
    1.11 +
    1.12 +#include "SDL.h"
    1.13 +
    1.14 +/* Turn a normal gamma value into an appropriate gamma ramp */
    1.15 +void CalculateGamma(double gamma, Uint16 *ramp)
    1.16 +{
    1.17 +	int i, value;
    1.18 +
    1.19 +	gamma = 1.0 / gamma;
    1.20 +	for ( i=0; i<256; ++i ) {
    1.21 +		value = (int)(pow((double)i/256.0, gamma)*65535.0 + 0.5);
    1.22 +		if ( value > 65535 ) {
    1.23 +			value = 65535;
    1.24 +		}
    1.25 +		ramp[i] = (Uint16)value;
    1.26 +	}
    1.27 +}
    1.28 +
    1.29 +/* This can be used as a general routine for all of the test programs */
    1.30 +int get_video_args(char *argv[], int *w, int *h, int *bpp, Uint32 *flags)
    1.31 +{
    1.32 +	int i;
    1.33 +
    1.34 +	*w = 640;
    1.35 +	*h = 480;
    1.36 +	*bpp = 0;
    1.37 +	*flags = SDL_SWSURFACE;
    1.38 +
    1.39 +	for ( i=1; argv[i]; ++i ) {
    1.40 +		if ( strcmp(argv[i], "-width") == 0 ) {
    1.41 +			if ( argv[i+1] ) {
    1.42 +				*w = atoi(argv[++i]);
    1.43 +			}
    1.44 +		} else
    1.45 +		if ( strcmp(argv[i], "-height") == 0 ) {
    1.46 +			if ( argv[i+1] ) {
    1.47 +				*h = atoi(argv[++i]);
    1.48 +			}
    1.49 +		} else
    1.50 +		if ( strcmp(argv[i], "-bpp") == 0 ) {
    1.51 +			if ( argv[i+1] ) {
    1.52 +				*bpp = atoi(argv[++i]);
    1.53 +			}
    1.54 +		} else
    1.55 +		if ( strcmp(argv[i], "-fullscreen") == 0 ) {
    1.56 +			*flags |= SDL_FULLSCREEN;
    1.57 +		} else
    1.58 +		if ( strcmp(argv[i], "-hw") == 0 ) {
    1.59 +			*flags |= SDL_HWSURFACE;
    1.60 +		} else
    1.61 +		if ( strcmp(argv[i], "-hwpalette") == 0 ) {
    1.62 +			*flags |= SDL_HWPALETTE;
    1.63 +		} else
    1.64 +			break;
    1.65 +	}
    1.66 +	return i;
    1.67 +}
    1.68 +
    1.69 +int main(int argc, char *argv[])
    1.70 +{
    1.71 +	SDL_Surface *screen;
    1.72 +	SDL_Surface *image;
    1.73 +	float gamma;
    1.74 +	int i;
    1.75 +	int w, h, bpp;
    1.76 +	Uint32 flags;
    1.77 +	Uint16 ramp[256];
    1.78 +	Uint16 red_ramp[256];
    1.79 +	Uint32 then, timeout;
    1.80 +
    1.81 +	/* Check command line arguments */
    1.82 +	argv += get_video_args(argv, &w, &h, &bpp, &flags);
    1.83 +
    1.84 +	/* Initialize SDL */
    1.85 +	if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
    1.86 +		fprintf(stderr,
    1.87 +			"Couldn't initialize SDL: %s\n", SDL_GetError());
    1.88 +		exit(1);
    1.89 +	}
    1.90 +	atexit(SDL_Quit);
    1.91 +
    1.92 +	/* Initialize the display, always use hardware palette */
    1.93 +	screen = SDL_SetVideoMode(w, h, bpp, flags | SDL_HWPALETTE);
    1.94 +	if ( screen == NULL ) {
    1.95 +		fprintf(stderr, "Couldn't set %dx%d video mode: %s\n",
    1.96 +						w, h, SDL_GetError());
    1.97 +		exit(1);
    1.98 +	}
    1.99 +
   1.100 +	/* Set the window manager title bar */
   1.101 +	SDL_WM_SetCaption("SDL gamma test", "testgamma");
   1.102 +
   1.103 +	/* Set the desired gamma, if any */
   1.104 +	gamma = 1.0f;
   1.105 +	if ( *argv ) {
   1.106 +		gamma = (float)atof(*argv);
   1.107 +	}
   1.108 +	if ( SDL_SetGamma(gamma, gamma, gamma) < 0 ) {
   1.109 +		fprintf(stderr, "Unable to set gamma: %s\n", SDL_GetError());
   1.110 +		exit(1);
   1.111 +	}
   1.112 +
   1.113 +#if 0 /* This isn't supported.  Integrating the gamma ramps isn't exact */
   1.114 +	/* See what gamma was actually set */
   1.115 +	float real[3];
   1.116 +	if ( SDL_GetGamma(&real[0], &real[1], &real[2]) < 0 ) {
   1.117 +		printf("Couldn't get gamma: %s\n", SDL_GetError());
   1.118 +	} else {
   1.119 +		printf("Set gamma values: R=%2.2f, G=%2.2f, B=%2.2f\n",
   1.120 +			real[0], real[1], real[2]);
   1.121 +	}
   1.122 +#endif
   1.123 +
   1.124 +	/* Do all the drawing work */
   1.125 +	image = SDL_LoadBMP("sample.bmp");
   1.126 +	if ( image ) {
   1.127 +		SDL_Rect dst;
   1.128 +
   1.129 +		dst.x = (screen->w - image->w)/2;
   1.130 +		dst.y = (screen->h - image->h)/2;
   1.131 +		dst.w = image->w;
   1.132 +		dst.h = image->h;
   1.133 +		SDL_BlitSurface(image, NULL, screen, &dst);
   1.134 +		SDL_UpdateRects(screen, 1, &dst);
   1.135 +	}
   1.136 +
   1.137 +	/* Wait a bit, handling events */
   1.138 +	then = SDL_GetTicks();
   1.139 +	timeout = (5*1000);
   1.140 +	while ( (SDL_GetTicks()-then) < timeout ) {
   1.141 +		SDL_Event event;
   1.142 +
   1.143 +		while ( SDL_PollEvent(&event) ) {
   1.144 +			switch (event.type) {
   1.145 +			    case SDL_QUIT:	/* Quit now */
   1.146 +				timeout = 0;
   1.147 +				break;
   1.148 +			    case SDL_KEYDOWN:
   1.149 +				switch (event.key.keysym.sym) {
   1.150 +				    case SDLK_SPACE:	/* Go longer.. */
   1.151 +					timeout += (5*1000);
   1.152 +					break;
   1.153 +				    case SDLK_UP:
   1.154 +					gamma += 0.2f;
   1.155 +					SDL_SetGamma(gamma, gamma, gamma);
   1.156 +					break;
   1.157 +				    case SDLK_DOWN:
   1.158 +					gamma -= 0.2f;
   1.159 +					SDL_SetGamma(gamma, gamma, gamma);
   1.160 +					break;
   1.161 +				    case SDLK_ESCAPE:
   1.162 +					timeout = 0;
   1.163 +					break;
   1.164 +				    default:
   1.165 +					break;
   1.166 +				}
   1.167 +				break;
   1.168 +			}
   1.169 +		}
   1.170 +	}
   1.171 +
   1.172 +	/* Perform a gamma flash to red using color ramps */
   1.173 +	while ( gamma < 10.0 ) {
   1.174 +		/* Increase the red gamma and decrease everything else... */
   1.175 +		gamma += 0.1f;
   1.176 +		CalculateGamma(gamma, red_ramp);
   1.177 +		CalculateGamma(1.0/gamma, ramp);
   1.178 +		SDL_SetGammaRamp(red_ramp, ramp, ramp);
   1.179 +	}
   1.180 +	/* Finish completely red */
   1.181 +	memset(red_ramp, 255, sizeof(red_ramp));
   1.182 +	memset(ramp, 0, sizeof(ramp));
   1.183 +	SDL_SetGammaRamp(red_ramp, ramp, ramp);
   1.184 +
   1.185 +	/* Now fade out to black */
   1.186 +	for ( i=(red_ramp[0] >> 8); i >= 0; --i ) {
   1.187 +		memset(red_ramp, i, sizeof(red_ramp));
   1.188 +		SDL_SetGammaRamp(red_ramp, NULL, NULL);
   1.189 +	}
   1.190 +	SDL_Delay(1*1000);
   1.191 +
   1.192 +	return(0);
   1.193 +}