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