2 /* Bring up a window and manipulate the gamma on it */
11 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
12 static void quit(int rc
)
18 /* Turn a normal gamma value into an appropriate gamma ramp */
19 void CalculateGamma(double gamma
, Uint16
*ramp
)
24 for ( i
=0; i
<256; ++i
) {
25 value
= (int)(pow((double)i
/256.0, gamma
)*65535.0 + 0.5);
26 if ( value
> 65535 ) {
29 ramp
[i
] = (Uint16
)value
;
33 /* This can be used as a general routine for all of the test programs */
34 int get_video_args(char *argv
[], int *w
, int *h
, int *bpp
, Uint32
*flags
)
41 *flags
= SDL_SWSURFACE
;
43 for ( i
=1; argv
[i
]; ++i
) {
44 if ( strcmp(argv
[i
], "-width") == 0 ) {
49 if ( strcmp(argv
[i
], "-height") == 0 ) {
54 if ( strcmp(argv
[i
], "-bpp") == 0 ) {
56 *bpp
= atoi(argv
[++i
]);
59 if ( strcmp(argv
[i
], "-fullscreen") == 0 ) {
60 *flags
|= SDL_FULLSCREEN
;
62 if ( strcmp(argv
[i
], "-hw") == 0 ) {
63 *flags
|= SDL_HWSURFACE
;
65 if ( strcmp(argv
[i
], "-hwpalette") == 0 ) {
66 *flags
|= SDL_HWPALETTE
;
73 int main(int argc
, char *argv
[])
85 /* Check command line arguments */
86 argv
+= get_video_args(argv
, &w
, &h
, &bpp
, &flags
);
89 if ( SDL_Init(SDL_INIT_VIDEO
) < 0 ) {
91 "Couldn't initialize SDL: %s\n", SDL_GetError());
95 /* Initialize the display, always use hardware palette */
96 screen
= SDL_SetVideoMode(w
, h
, bpp
, flags
| SDL_HWPALETTE
);
97 if ( screen
== NULL
) {
98 fprintf(stderr
, "Couldn't set %dx%d video mode: %s\n",
99 w
, h
, SDL_GetError());
103 /* Set the window manager title bar */
104 SDL_WM_SetCaption("SDL gamma test", "testgamma");
106 /* Set the desired gamma, if any */
109 gamma
= (float)atof(*argv
);
111 if ( SDL_SetGamma(gamma
, gamma
, gamma
) < 0 ) {
112 fprintf(stderr
, "Unable to set gamma: %s\n", SDL_GetError());
116 #if 0 /* This isn't supported. Integrating the gamma ramps isn't exact */
117 /* See what gamma was actually set */
119 if ( SDL_GetGamma(&real
[0], &real
[1], &real
[2]) < 0 ) {
120 printf("Couldn't get gamma: %s\n", SDL_GetError());
122 printf("Set gamma values: R=%2.2f, G=%2.2f, B=%2.2f\n",
123 real
[0], real
[1], real
[2]);
127 /* Do all the drawing work */
128 image
= SDL_LoadBMP("sample.bmp");
132 dst
.x
= (screen
->w
- image
->w
)/2;
133 dst
.y
= (screen
->h
- image
->h
)/2;
136 SDL_BlitSurface(image
, NULL
, screen
, &dst
);
137 SDL_UpdateRects(screen
, 1, &dst
);
140 /* Wait a bit, handling events */
141 then
= SDL_GetTicks();
143 while ( (SDL_GetTicks()-then
) < timeout
) {
146 while ( SDL_PollEvent(&event
) ) {
147 switch (event
.type
) {
148 case SDL_QUIT
: /* Quit now */
152 switch (event
.key
.keysym
.sym
) {
153 case SDLK_SPACE
: /* Go longer.. */
158 SDL_SetGamma(gamma
, gamma
, gamma
);
162 SDL_SetGamma(gamma
, gamma
, gamma
);
175 /* Perform a gamma flash to red using color ramps */
176 while ( gamma
< 10.0 ) {
177 /* Increase the red gamma and decrease everything else... */
179 CalculateGamma(gamma
, red_ramp
);
180 CalculateGamma(1.0/gamma
, ramp
);
181 SDL_SetGammaRamp(red_ramp
, ramp
, ramp
);
183 /* Finish completely red */
184 memset(red_ramp
, 255, sizeof(red_ramp
));
185 memset(ramp
, 0, sizeof(ramp
));
186 SDL_SetGammaRamp(red_ramp
, ramp
, ramp
);
188 /* Now fade out to black */
189 for ( i
=(red_ramp
[0] >> 8); i
>= 0; --i
) {
190 memset(red_ramp
, i
, sizeof(red_ramp
));
191 SDL_SetGammaRamp(red_ramp
, NULL
, NULL
);