test/testgamma.c
branchSDL-1.3
changeset 1662 782fd950bd46
parent 1151 be9c9c8f6d53
child 1668 4da1ee79c9af
     1.1 --- a/test/testgamma.c	Sun May 21 17:27:13 2006 +0000
     1.2 +++ b/test/testgamma.c	Sun May 28 13:04:16 2006 +0000
     1.3 @@ -9,189 +9,186 @@
     1.4  #include "SDL.h"
     1.5  
     1.6  /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
     1.7 -static void quit(int rc)
     1.8 +static void
     1.9 +quit (int rc)
    1.10  {
    1.11 -	SDL_Quit();
    1.12 -	exit(rc);
    1.13 +    SDL_Quit ();
    1.14 +    exit (rc);
    1.15  }
    1.16  
    1.17  /* Turn a normal gamma value into an appropriate gamma ramp */
    1.18 -void CalculateGamma(double gamma, Uint16 *ramp)
    1.19 +void
    1.20 +CalculateGamma (double gamma, Uint16 * ramp)
    1.21  {
    1.22 -	int i, value;
    1.23 +    int i, value;
    1.24  
    1.25 -	gamma = 1.0 / gamma;
    1.26 -	for ( i=0; i<256; ++i ) {
    1.27 -		value = (int)(pow((double)i/256.0, gamma)*65535.0 + 0.5);
    1.28 -		if ( value > 65535 ) {
    1.29 -			value = 65535;
    1.30 -		}
    1.31 -		ramp[i] = (Uint16)value;
    1.32 -	}
    1.33 +    gamma = 1.0 / gamma;
    1.34 +    for (i = 0; i < 256; ++i) {
    1.35 +        value = (int) (pow ((double) i / 256.0, gamma) * 65535.0 + 0.5);
    1.36 +        if (value > 65535) {
    1.37 +            value = 65535;
    1.38 +        }
    1.39 +        ramp[i] = (Uint16) value;
    1.40 +    }
    1.41  }
    1.42  
    1.43  /* This can be used as a general routine for all of the test programs */
    1.44 -int get_video_args(char *argv[], int *w, int *h, int *bpp, Uint32 *flags)
    1.45 +int
    1.46 +get_video_args (char *argv[], int *w, int *h, int *bpp, Uint32 * flags)
    1.47  {
    1.48 -	int i;
    1.49 +    int i;
    1.50  
    1.51 -	*w = 640;
    1.52 -	*h = 480;
    1.53 -	*bpp = 0;
    1.54 -	*flags = SDL_SWSURFACE;
    1.55 +    *w = 640;
    1.56 +    *h = 480;
    1.57 +    *bpp = 0;
    1.58 +    *flags = SDL_SWSURFACE;
    1.59  
    1.60 -	for ( i=1; argv[i]; ++i ) {
    1.61 -		if ( strcmp(argv[i], "-width") == 0 ) {
    1.62 -			if ( argv[i+1] ) {
    1.63 -				*w = atoi(argv[++i]);
    1.64 -			}
    1.65 -		} else
    1.66 -		if ( strcmp(argv[i], "-height") == 0 ) {
    1.67 -			if ( argv[i+1] ) {
    1.68 -				*h = atoi(argv[++i]);
    1.69 -			}
    1.70 -		} else
    1.71 -		if ( strcmp(argv[i], "-bpp") == 0 ) {
    1.72 -			if ( argv[i+1] ) {
    1.73 -				*bpp = atoi(argv[++i]);
    1.74 -			}
    1.75 -		} else
    1.76 -		if ( strcmp(argv[i], "-fullscreen") == 0 ) {
    1.77 -			*flags |= SDL_FULLSCREEN;
    1.78 -		} else
    1.79 -		if ( strcmp(argv[i], "-hw") == 0 ) {
    1.80 -			*flags |= SDL_HWSURFACE;
    1.81 -		} else
    1.82 -		if ( strcmp(argv[i], "-hwpalette") == 0 ) {
    1.83 -			*flags |= SDL_HWPALETTE;
    1.84 -		} else
    1.85 -			break;
    1.86 -	}
    1.87 -	return i;
    1.88 +    for (i = 1; argv[i]; ++i) {
    1.89 +        if (strcmp (argv[i], "-width") == 0) {
    1.90 +            if (argv[i + 1]) {
    1.91 +                *w = atoi (argv[++i]);
    1.92 +            }
    1.93 +        } else if (strcmp (argv[i], "-height") == 0) {
    1.94 +            if (argv[i + 1]) {
    1.95 +                *h = atoi (argv[++i]);
    1.96 +            }
    1.97 +        } else if (strcmp (argv[i], "-bpp") == 0) {
    1.98 +            if (argv[i + 1]) {
    1.99 +                *bpp = atoi (argv[++i]);
   1.100 +            }
   1.101 +        } else if (strcmp (argv[i], "-fullscreen") == 0) {
   1.102 +            *flags |= SDL_FULLSCREEN;
   1.103 +        } else if (strcmp (argv[i], "-hw") == 0) {
   1.104 +            *flags |= SDL_HWSURFACE;
   1.105 +        } else if (strcmp (argv[i], "-hwpalette") == 0) {
   1.106 +            *flags |= SDL_HWPALETTE;
   1.107 +        } else
   1.108 +            break;
   1.109 +    }
   1.110 +    return i;
   1.111  }
   1.112  
   1.113 -int main(int argc, char *argv[])
   1.114 +int
   1.115 +main (int argc, char *argv[])
   1.116  {
   1.117 -	SDL_Surface *screen;
   1.118 -	SDL_Surface *image;
   1.119 -	float gamma;
   1.120 -	int i;
   1.121 -	int w, h, bpp;
   1.122 -	Uint32 flags;
   1.123 -	Uint16 ramp[256];
   1.124 -	Uint16 red_ramp[256];
   1.125 -	Uint32 then, timeout;
   1.126 +    SDL_Surface *screen;
   1.127 +    SDL_Surface *image;
   1.128 +    float gamma;
   1.129 +    int i;
   1.130 +    int w, h, bpp;
   1.131 +    Uint32 flags;
   1.132 +    Uint16 ramp[256];
   1.133 +    Uint16 red_ramp[256];
   1.134 +    Uint32 then, timeout;
   1.135  
   1.136 -	/* Check command line arguments */
   1.137 -	argv += get_video_args(argv, &w, &h, &bpp, &flags);
   1.138 +    /* Check command line arguments */
   1.139 +    argv += get_video_args (argv, &w, &h, &bpp, &flags);
   1.140  
   1.141 -	/* Initialize SDL */
   1.142 -	if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
   1.143 -		fprintf(stderr,
   1.144 -			"Couldn't initialize SDL: %s\n", SDL_GetError());
   1.145 -		return(1);
   1.146 -	}
   1.147 +    /* Initialize SDL */
   1.148 +    if (SDL_Init (SDL_INIT_VIDEO) < 0) {
   1.149 +        fprintf (stderr, "Couldn't initialize SDL: %s\n", SDL_GetError ());
   1.150 +        return (1);
   1.151 +    }
   1.152  
   1.153 -	/* Initialize the display, always use hardware palette */
   1.154 -	screen = SDL_SetVideoMode(w, h, bpp, flags | SDL_HWPALETTE);
   1.155 -	if ( screen == NULL ) {
   1.156 -		fprintf(stderr, "Couldn't set %dx%d video mode: %s\n",
   1.157 -						w, h, SDL_GetError());
   1.158 -		quit(1);
   1.159 -	}
   1.160 +    /* Initialize the display, always use hardware palette */
   1.161 +    screen = SDL_SetVideoMode (w, h, bpp, flags | SDL_HWPALETTE);
   1.162 +    if (screen == NULL) {
   1.163 +        fprintf (stderr, "Couldn't set %dx%d video mode: %s\n",
   1.164 +                 w, h, SDL_GetError ());
   1.165 +        quit (1);
   1.166 +    }
   1.167  
   1.168 -	/* Set the window manager title bar */
   1.169 -	SDL_WM_SetCaption("SDL gamma test", "testgamma");
   1.170 +    /* Set the window manager title bar */
   1.171 +    SDL_WM_SetCaption ("SDL gamma test", "testgamma");
   1.172  
   1.173 -	/* Set the desired gamma, if any */
   1.174 -	gamma = 1.0f;
   1.175 -	if ( *argv ) {
   1.176 -		gamma = (float)atof(*argv);
   1.177 -	}
   1.178 -	if ( SDL_SetGamma(gamma, gamma, gamma) < 0 ) {
   1.179 -		fprintf(stderr, "Unable to set gamma: %s\n", SDL_GetError());
   1.180 -		quit(1);
   1.181 -	}
   1.182 -
   1.183 -#if 0 /* This isn't supported.  Integrating the gamma ramps isn't exact */
   1.184 -	/* See what gamma was actually set */
   1.185 -	float real[3];
   1.186 -	if ( SDL_GetGamma(&real[0], &real[1], &real[2]) < 0 ) {
   1.187 -		printf("Couldn't get gamma: %s\n", SDL_GetError());
   1.188 -	} else {
   1.189 -		printf("Set gamma values: R=%2.2f, G=%2.2f, B=%2.2f\n",
   1.190 -			real[0], real[1], real[2]);
   1.191 -	}
   1.192 +    /* Set the desired gamma, if any */
   1.193 +    gamma = 1.0f;
   1.194 +    if (*argv) {
   1.195 +        gamma = (float) atof (*argv);
   1.196 +    }
   1.197 +    if (SDL_SetGamma (gamma, gamma, gamma) < 0) {
   1.198 +        fprintf (stderr, "Unable to set gamma: %s\n", SDL_GetError ());
   1.199 +        quit (1);
   1.200 +    }
   1.201 +#if 0                           /* This isn't supported.  Integrating the gamma ramps isn't exact */
   1.202 +    /* See what gamma was actually set */
   1.203 +    float real[3];
   1.204 +    if (SDL_GetGamma (&real[0], &real[1], &real[2]) < 0) {
   1.205 +        printf ("Couldn't get gamma: %s\n", SDL_GetError ());
   1.206 +    } else {
   1.207 +        printf ("Set gamma values: R=%2.2f, G=%2.2f, B=%2.2f\n",
   1.208 +                real[0], real[1], real[2]);
   1.209 +    }
   1.210  #endif
   1.211  
   1.212 -	/* Do all the drawing work */
   1.213 -	image = SDL_LoadBMP("sample.bmp");
   1.214 -	if ( image ) {
   1.215 -		SDL_Rect dst;
   1.216 +    /* Do all the drawing work */
   1.217 +    image = SDL_LoadBMP ("sample.bmp");
   1.218 +    if (image) {
   1.219 +        SDL_Rect dst;
   1.220  
   1.221 -		dst.x = (screen->w - image->w)/2;
   1.222 -		dst.y = (screen->h - image->h)/2;
   1.223 -		dst.w = image->w;
   1.224 -		dst.h = image->h;
   1.225 -		SDL_BlitSurface(image, NULL, screen, &dst);
   1.226 -		SDL_UpdateRects(screen, 1, &dst);
   1.227 -	}
   1.228 +        dst.x = (screen->w - image->w) / 2;
   1.229 +        dst.y = (screen->h - image->h) / 2;
   1.230 +        dst.w = image->w;
   1.231 +        dst.h = image->h;
   1.232 +        SDL_BlitSurface (image, NULL, screen, &dst);
   1.233 +        SDL_UpdateRects (screen, 1, &dst);
   1.234 +    }
   1.235  
   1.236 -	/* Wait a bit, handling events */
   1.237 -	then = SDL_GetTicks();
   1.238 -	timeout = (5*1000);
   1.239 -	while ( (SDL_GetTicks()-then) < timeout ) {
   1.240 -		SDL_Event event;
   1.241 +    /* Wait a bit, handling events */
   1.242 +    then = SDL_GetTicks ();
   1.243 +    timeout = (5 * 1000);
   1.244 +    while ((SDL_GetTicks () - then) < timeout) {
   1.245 +        SDL_Event event;
   1.246  
   1.247 -		while ( SDL_PollEvent(&event) ) {
   1.248 -			switch (event.type) {
   1.249 -			    case SDL_QUIT:	/* Quit now */
   1.250 -				timeout = 0;
   1.251 -				break;
   1.252 -			    case SDL_KEYDOWN:
   1.253 -				switch (event.key.keysym.sym) {
   1.254 -				    case SDLK_SPACE:	/* Go longer.. */
   1.255 -					timeout += (5*1000);
   1.256 -					break;
   1.257 -				    case SDLK_UP:
   1.258 -					gamma += 0.2f;
   1.259 -					SDL_SetGamma(gamma, gamma, gamma);
   1.260 -					break;
   1.261 -				    case SDLK_DOWN:
   1.262 -					gamma -= 0.2f;
   1.263 -					SDL_SetGamma(gamma, gamma, gamma);
   1.264 -					break;
   1.265 -				    case SDLK_ESCAPE:
   1.266 -					timeout = 0;
   1.267 -					break;
   1.268 -				    default:
   1.269 -					break;
   1.270 -				}
   1.271 -				break;
   1.272 -			}
   1.273 -		}
   1.274 -	}
   1.275 +        while (SDL_PollEvent (&event)) {
   1.276 +            switch (event.type) {
   1.277 +            case SDL_QUIT:     /* Quit now */
   1.278 +                timeout = 0;
   1.279 +                break;
   1.280 +            case SDL_KEYDOWN:
   1.281 +                switch (event.key.keysym.sym) {
   1.282 +                case SDLK_SPACE:       /* Go longer.. */
   1.283 +                    timeout += (5 * 1000);
   1.284 +                    break;
   1.285 +                case SDLK_UP:
   1.286 +                    gamma += 0.2f;
   1.287 +                    SDL_SetGamma (gamma, gamma, gamma);
   1.288 +                    break;
   1.289 +                case SDLK_DOWN:
   1.290 +                    gamma -= 0.2f;
   1.291 +                    SDL_SetGamma (gamma, gamma, gamma);
   1.292 +                    break;
   1.293 +                case SDLK_ESCAPE:
   1.294 +                    timeout = 0;
   1.295 +                    break;
   1.296 +                default:
   1.297 +                    break;
   1.298 +                }
   1.299 +                break;
   1.300 +            }
   1.301 +        }
   1.302 +    }
   1.303  
   1.304 -	/* Perform a gamma flash to red using color ramps */
   1.305 -	while ( gamma < 10.0 ) {
   1.306 -		/* Increase the red gamma and decrease everything else... */
   1.307 -		gamma += 0.1f;
   1.308 -		CalculateGamma(gamma, red_ramp);
   1.309 -		CalculateGamma(1.0/gamma, ramp);
   1.310 -		SDL_SetGammaRamp(red_ramp, ramp, ramp);
   1.311 -	}
   1.312 -	/* Finish completely red */
   1.313 -	memset(red_ramp, 255, sizeof(red_ramp));
   1.314 -	memset(ramp, 0, sizeof(ramp));
   1.315 -	SDL_SetGammaRamp(red_ramp, ramp, ramp);
   1.316 +    /* Perform a gamma flash to red using color ramps */
   1.317 +    while (gamma < 10.0) {
   1.318 +        /* Increase the red gamma and decrease everything else... */
   1.319 +        gamma += 0.1f;
   1.320 +        CalculateGamma (gamma, red_ramp);
   1.321 +        CalculateGamma (1.0 / gamma, ramp);
   1.322 +        SDL_SetGammaRamp (red_ramp, ramp, ramp);
   1.323 +    }
   1.324 +    /* Finish completely red */
   1.325 +    memset (red_ramp, 255, sizeof (red_ramp));
   1.326 +    memset (ramp, 0, sizeof (ramp));
   1.327 +    SDL_SetGammaRamp (red_ramp, ramp, ramp);
   1.328  
   1.329 -	/* Now fade out to black */
   1.330 -	for ( i=(red_ramp[0] >> 8); i >= 0; --i ) {
   1.331 -		memset(red_ramp, i, sizeof(red_ramp));
   1.332 -		SDL_SetGammaRamp(red_ramp, NULL, NULL);
   1.333 -	}
   1.334 -	SDL_Delay(1*1000);
   1.335 +    /* Now fade out to black */
   1.336 +    for (i = (red_ramp[0] >> 8); i >= 0; --i) {
   1.337 +        memset (red_ramp, i, sizeof (red_ramp));
   1.338 +        SDL_SetGammaRamp (red_ramp, NULL, NULL);
   1.339 +    }
   1.340 +    SDL_Delay (1 * 1000);
   1.341  
   1.342 -	SDL_Quit();
   1.343 -	return(0);
   1.344 +    SDL_Quit ();
   1.345 +    return (0);
   1.346  }