comparison test/testgamma.c @ 1668:4da1ee79c9af SDL-1.3

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