comparison test/testwin.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
11 11
12 #include "SDL.h" 12 #include "SDL.h"
13 13
14 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */ 14 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
15 static void 15 static void
16 quit (int rc) 16 quit(int rc)
17 { 17 {
18 SDL_Quit (); 18 SDL_Quit();
19 exit (rc); 19 exit(rc);
20 } 20 }
21 21
22 void 22 void
23 DrawPict (SDL_Surface * screen, char *bmpfile, 23 DrawPict(SDL_Surface * screen, char *bmpfile,
24 int speedy, int flip, int nofade) 24 int speedy, int flip, int nofade)
25 { 25 {
26 SDL_Surface *picture; 26 SDL_Surface *picture;
27 SDL_Rect dest, update; 27 SDL_Rect dest, update;
28 int i, centered; 28 int i, centered;
29 int ncolors; 29 int ncolors;
31 31
32 /* Load the image into a surface */ 32 /* Load the image into a surface */
33 if (bmpfile == NULL) { 33 if (bmpfile == NULL) {
34 bmpfile = "sample.bmp"; /* Sample image */ 34 bmpfile = "sample.bmp"; /* Sample image */
35 } 35 }
36 fprintf (stderr, "Loading picture: %s\n", bmpfile); 36 fprintf(stderr, "Loading picture: %s\n", bmpfile);
37 picture = SDL_LoadBMP (bmpfile); 37 picture = SDL_LoadBMP(bmpfile);
38 if (picture == NULL) { 38 if (picture == NULL) {
39 fprintf (stderr, "Couldn't load %s: %s\n", bmpfile, SDL_GetError ()); 39 fprintf(stderr, "Couldn't load %s: %s\n", bmpfile, SDL_GetError());
40 return; 40 return;
41 } 41 }
42 42
43 /* Set the display colors -- on a hicolor display this is a no-op */ 43 /* Set the display colors -- on a hicolor display this is a no-op */
44 if (picture->format->palette) { 44 if (picture->format->palette) {
45 ncolors = picture->format->palette->ncolors; 45 ncolors = picture->format->palette->ncolors;
46 colors = (SDL_Color *) malloc (ncolors * sizeof (SDL_Color)); 46 colors = (SDL_Color *) malloc(ncolors * sizeof(SDL_Color));
47 cmap = (SDL_Color *) malloc (ncolors * sizeof (SDL_Color)); 47 cmap = (SDL_Color *) malloc(ncolors * sizeof(SDL_Color));
48 memcpy (colors, picture->format->palette->colors, 48 memcpy(colors, picture->format->palette->colors,
49 ncolors * sizeof (SDL_Color)); 49 ncolors * sizeof(SDL_Color));
50 } else { 50 } else {
51 int r, g, b; 51 int r, g, b;
52 52
53 /* Allocate 256 color palette */ 53 /* Allocate 256 color palette */
54 ncolors = 256; 54 ncolors = 256;
55 colors = (SDL_Color *) malloc (ncolors * sizeof (SDL_Color)); 55 colors = (SDL_Color *) malloc(ncolors * sizeof(SDL_Color));
56 cmap = (SDL_Color *) malloc (ncolors * sizeof (SDL_Color)); 56 cmap = (SDL_Color *) malloc(ncolors * sizeof(SDL_Color));
57 57
58 /* Set a 3,3,2 color cube */ 58 /* Set a 3,3,2 color cube */
59 for (r = 0; r < 8; ++r) { 59 for (r = 0; r < 8; ++r) {
60 for (g = 0; g < 8; ++g) { 60 for (g = 0; g < 8; ++g) {
61 for (b = 0; b < 4; ++b) { 61 for (b = 0; b < 4; ++b) {
65 colors[i].b = b << 6; 65 colors[i].b = b << 6;
66 } 66 }
67 } 67 }
68 } 68 }
69 } 69 }
70 NOTICE ("testwin: setting colors\n"); 70 NOTICE("testwin: setting colors\n");
71 if (!SDL_SetColors (screen, colors, 0, ncolors) && 71 if (!SDL_SetColors(screen, colors, 0, ncolors) &&
72 (screen->format->palette != NULL)) { 72 (screen->format->palette != NULL)) {
73 fprintf (stderr, 73 fprintf(stderr,
74 "Warning: Couldn't set all of the colors, but SDL will map the image\n" 74 "Warning: Couldn't set all of the colors, but SDL will map the image\n"
75 " (colormap fading will suffer - try the -warp option)\n"); 75 " (colormap fading will suffer - try the -warp option)\n");
76 } 76 }
77 77
78 /* Set the screen to black (not really necessary) */ 78 /* Set the screen to black (not really necessary) */
79 if (SDL_LockSurface (screen) == 0) { 79 if (SDL_LockSurface(screen) == 0) {
80 Uint32 black; 80 Uint32 black;
81 Uint8 *pixels; 81 Uint8 *pixels;
82 82
83 black = SDL_MapRGB (screen->format, 0, 0, 0); 83 black = SDL_MapRGB(screen->format, 0, 0, 0);
84 pixels = (Uint8 *) screen->pixels; 84 pixels = (Uint8 *) screen->pixels;
85 for (i = 0; i < screen->h; ++i) { 85 for (i = 0; i < screen->h; ++i) {
86 memset (pixels, black, screen->w * screen->format->BytesPerPixel); 86 memset(pixels, black, screen->w * screen->format->BytesPerPixel);
87 pixels += screen->pitch; 87 pixels += screen->pitch;
88 } 88 }
89 SDL_UnlockSurface (screen); 89 SDL_UnlockSurface(screen);
90 SDL_UpdateRect (screen, 0, 0, 0, 0); 90 SDL_UpdateRect(screen, 0, 0, 0, 0);
91 } 91 }
92 92
93 /* Display the picture */ 93 /* Display the picture */
94 if (speedy) { 94 if (speedy) {
95 SDL_Surface *displayfmt; 95 SDL_Surface *displayfmt;
96 96
97 fprintf (stderr, "Converting picture\n"); 97 fprintf(stderr, "Converting picture\n");
98 displayfmt = SDL_DisplayFormat (picture); 98 displayfmt = SDL_DisplayFormat(picture);
99 if (displayfmt == NULL) { 99 if (displayfmt == NULL) {
100 fprintf (stderr, "Couldn't convert image: %s\n", SDL_GetError ()); 100 fprintf(stderr, "Couldn't convert image: %s\n", SDL_GetError());
101 goto done; 101 goto done;
102 } 102 }
103 SDL_FreeSurface (picture); 103 SDL_FreeSurface(picture);
104 picture = displayfmt; 104 picture = displayfmt;
105 } 105 }
106 printf ("(image surface located in %s memory)\n", 106 printf("(image surface located in %s memory)\n",
107 (picture->flags & SDL_HWSURFACE) ? "video" : "system"); 107 (picture->flags & SDL_HWSURFACE) ? "video" : "system");
108 centered = (screen->w - picture->w) / 2; 108 centered = (screen->w - picture->w) / 2;
109 if (centered < 0) { 109 if (centered < 0) {
110 centered = 0; 110 centered = 0;
111 } 111 }
112 dest.y = (screen->h - picture->h) / 2; 112 dest.y = (screen->h - picture->h) / 2;
113 dest.w = picture->w; 113 dest.w = picture->w;
114 dest.h = picture->h; 114 dest.h = picture->h;
115 NOTICE ("testwin: moving image\n"); 115 NOTICE("testwin: moving image\n");
116 for (i = 0; i <= centered; ++i) { 116 for (i = 0; i <= centered; ++i) {
117 dest.x = i; 117 dest.x = i;
118 update = dest; 118 update = dest;
119 if (SDL_BlitSurface (picture, NULL, screen, &update) < 0) { 119 if (SDL_BlitSurface(picture, NULL, screen, &update) < 0) {
120 fprintf (stderr, "Blit failed: %s\n", SDL_GetError ()); 120 fprintf(stderr, "Blit failed: %s\n", SDL_GetError());
121 break; 121 break;
122 } 122 }
123 if (flip) { 123 if (flip) {
124 SDL_Flip (screen); 124 SDL_Flip(screen);
125 } else { 125 } else {
126 SDL_UpdateRects (screen, 1, &update); 126 SDL_UpdateRects(screen, 1, &update);
127 } 127 }
128 } 128 }
129 129
130 #ifdef SCREENSHOT 130 #ifdef SCREENSHOT
131 if (SDL_SaveBMP (screen, "screen.bmp") < 0) 131 if (SDL_SaveBMP(screen, "screen.bmp") < 0)
132 printf ("Couldn't save screen: %s\n", SDL_GetError ()); 132 printf("Couldn't save screen: %s\n", SDL_GetError());
133 #endif 133 #endif
134 134
135 #ifndef BENCHMARK_SDL 135 #ifndef BENCHMARK_SDL
136 /* Let it sit there for a while */ 136 /* Let it sit there for a while */
137 SDL_Delay (5 * 1000); 137 SDL_Delay(5 * 1000);
138 #endif 138 #endif
139 /* Fade the colormap */ 139 /* Fade the colormap */
140 if (!nofade) { 140 if (!nofade) {
141 int maxstep; 141 int maxstep;
142 SDL_Color final; 142 SDL_Color final;
144 struct 144 struct
145 { 145 {
146 Sint16 r, g, b; 146 Sint16 r, g, b;
147 } cdist[256]; 147 } cdist[256];
148 148
149 NOTICE ("testwin: fading out...\n"); 149 NOTICE("testwin: fading out...\n");
150 memcpy (cmap, colors, ncolors * sizeof (SDL_Color)); 150 memcpy(cmap, colors, ncolors * sizeof(SDL_Color));
151 maxstep = 32 - 1; 151 maxstep = 32 - 1;
152 final.r = 0xFF; 152 final.r = 0xFF;
153 final.g = 0x00; 153 final.g = 0x00;
154 final.b = 0x00; 154 final.b = 0x00;
155 memcpy (palcolors, colors, ncolors * sizeof (SDL_Color)); 155 memcpy(palcolors, colors, ncolors * sizeof(SDL_Color));
156 for (i = 0; i < ncolors; ++i) { 156 for (i = 0; i < ncolors; ++i) {
157 cdist[i].r = final.r - palcolors[i].r; 157 cdist[i].r = final.r - palcolors[i].r;
158 cdist[i].g = final.g - palcolors[i].g; 158 cdist[i].g = final.g - palcolors[i].g;
159 cdist[i].b = final.b - palcolors[i].b; 159 cdist[i].b = final.b - palcolors[i].b;
160 } 160 }
163 for (c = 0; c < ncolors; ++c) { 163 for (c = 0; c < ncolors; ++c) {
164 colors[c].r = palcolors[c].r + ((cdist[c].r * i)) / maxstep; 164 colors[c].r = palcolors[c].r + ((cdist[c].r * i)) / maxstep;
165 colors[c].g = palcolors[c].g + ((cdist[c].g * i)) / maxstep; 165 colors[c].g = palcolors[c].g + ((cdist[c].g * i)) / maxstep;
166 colors[c].b = palcolors[c].b + ((cdist[c].b * i)) / maxstep; 166 colors[c].b = palcolors[c].b + ((cdist[c].b * i)) / maxstep;
167 } 167 }
168 SDL_SetColors (screen, colors, 0, ncolors); 168 SDL_SetColors(screen, colors, 0, ncolors);
169 SDL_Delay (1); 169 SDL_Delay(1);
170 } 170 }
171 final.r = 0x00; 171 final.r = 0x00;
172 final.g = 0x00; 172 final.g = 0x00;
173 final.b = 0x00; 173 final.b = 0x00;
174 memcpy (palcolors, colors, ncolors * sizeof (SDL_Color)); 174 memcpy(palcolors, colors, ncolors * sizeof(SDL_Color));
175 for (i = 0; i < ncolors; ++i) { 175 for (i = 0; i < ncolors; ++i) {
176 cdist[i].r = final.r - palcolors[i].r; 176 cdist[i].r = final.r - palcolors[i].r;
177 cdist[i].g = final.g - palcolors[i].g; 177 cdist[i].g = final.g - palcolors[i].g;
178 cdist[i].b = final.b - palcolors[i].b; 178 cdist[i].b = final.b - palcolors[i].b;
179 } 179 }
183 for (c = 0; c < ncolors; ++c) { 183 for (c = 0; c < ncolors; ++c) {
184 colors[c].r = palcolors[c].r + ((cdist[c].r * i)) / maxstep; 184 colors[c].r = palcolors[c].r + ((cdist[c].r * i)) / maxstep;
185 colors[c].g = palcolors[c].g + ((cdist[c].g * i)) / maxstep; 185 colors[c].g = palcolors[c].g + ((cdist[c].g * i)) / maxstep;
186 colors[c].b = palcolors[c].b + ((cdist[c].b * i)) / maxstep; 186 colors[c].b = palcolors[c].b + ((cdist[c].b * i)) / maxstep;
187 } 187 }
188 SDL_SetColors (screen, colors, 0, ncolors); 188 SDL_SetColors(screen, colors, 0, ncolors);
189 SDL_Delay (1); 189 SDL_Delay(1);
190 } 190 }
191 for (i = 0; i < ncolors; ++i) { 191 for (i = 0; i < ncolors; ++i) {
192 colors[i].r = final.r; 192 colors[i].r = final.r;
193 colors[i].g = final.g; 193 colors[i].g = final.g;
194 colors[i].b = final.b; 194 colors[i].b = final.b;
195 } 195 }
196 SDL_SetColors (screen, colors, 0, ncolors); 196 SDL_SetColors(screen, colors, 0, ncolors);
197 NOTICE ("testwin: fading in...\n"); 197 NOTICE("testwin: fading in...\n");
198 memcpy (palcolors, colors, ncolors * sizeof (SDL_Color)); 198 memcpy(palcolors, colors, ncolors * sizeof(SDL_Color));
199 for (i = 0; i < ncolors; ++i) { 199 for (i = 0; i < ncolors; ++i) {
200 cdist[i].r = cmap[i].r - palcolors[i].r; 200 cdist[i].r = cmap[i].r - palcolors[i].r;
201 cdist[i].g = cmap[i].g - palcolors[i].g; 201 cdist[i].g = cmap[i].g - palcolors[i].g;
202 cdist[i].b = cmap[i].b - palcolors[i].b; 202 cdist[i].b = cmap[i].b - palcolors[i].b;
203 } 203 }
206 for (c = 0; c < ncolors; ++c) { 206 for (c = 0; c < ncolors; ++c) {
207 colors[c].r = palcolors[c].r + ((cdist[c].r * i)) / maxstep; 207 colors[c].r = palcolors[c].r + ((cdist[c].r * i)) / maxstep;
208 colors[c].g = palcolors[c].g + ((cdist[c].g * i)) / maxstep; 208 colors[c].g = palcolors[c].g + ((cdist[c].g * i)) / maxstep;
209 colors[c].b = palcolors[c].b + ((cdist[c].b * i)) / maxstep; 209 colors[c].b = palcolors[c].b + ((cdist[c].b * i)) / maxstep;
210 } 210 }
211 SDL_SetColors (screen, colors, 0, ncolors); 211 SDL_SetColors(screen, colors, 0, ncolors);
212 SDL_Delay (1); 212 SDL_Delay(1);
213 } 213 }
214 NOTICE ("testwin: fading over\n"); 214 NOTICE("testwin: fading over\n");
215 } 215 }
216 216
217 done: 217 done:
218 /* Free the picture and return */ 218 /* Free the picture and return */
219 SDL_FreeSurface (picture); 219 SDL_FreeSurface(picture);
220 free (colors); 220 free(colors);
221 free (cmap); 221 free(cmap);
222 return; 222 return;
223 } 223 }
224 224
225 int 225 int
226 main (int argc, char *argv[]) 226 main(int argc, char *argv[])
227 { 227 {
228 SDL_Surface *screen; 228 SDL_Surface *screen;
229 /* Options */ 229 /* Options */
230 int speedy, flip, nofade; 230 int speedy, flip, nofade;
231 int delay; 231 int delay;
250 w = 640; 250 w = 640;
251 h = 480; 251 h = 480;
252 desired_bpp = 0; 252 desired_bpp = 0;
253 video_flags = 0; 253 video_flags = 0;
254 #endif 254 #endif
255 if (SDL_Init (SDL_INIT_VIDEO) < 0) { 255 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
256 fprintf (stderr, "Couldn't initialize SDL: %s\n", SDL_GetError ()); 256 fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
257 return (1); 257 return (1);
258 } 258 }
259 259
260 while (argc > 1) { 260 while (argc > 1) {
261 if (strcmp (argv[1], "-speedy") == 0) { 261 if (strcmp(argv[1], "-speedy") == 0) {
262 speedy = 1; 262 speedy = 1;
263 argv += 1; 263 argv += 1;
264 argc -= 1; 264 argc -= 1;
265 } else if (strcmp (argv[1], "-nofade") == 0) { 265 } else if (strcmp(argv[1], "-nofade") == 0) {
266 nofade = 1; 266 nofade = 1;
267 argv += 1; 267 argv += 1;
268 argc -= 1; 268 argc -= 1;
269 } else if (strcmp (argv[1], "-delay") == 0) { 269 } else if (strcmp(argv[1], "-delay") == 0) {
270 if (argv[2]) { 270 if (argv[2]) {
271 delay = atoi (argv[2]); 271 delay = atoi(argv[2]);
272 argv += 2; 272 argv += 2;
273 argc -= 2; 273 argc -= 2;
274 } else { 274 } else {
275 fprintf (stderr, "The -delay option requires an argument\n"); 275 fprintf(stderr, "The -delay option requires an argument\n");
276 quit (1); 276 quit(1);
277 } 277 }
278 } else if (strcmp (argv[1], "-width") == 0) { 278 } else if (strcmp(argv[1], "-width") == 0) {
279 if (argv[2] && ((w = atoi (argv[2])) > 0)) { 279 if (argv[2] && ((w = atoi(argv[2])) > 0)) {
280 argv += 2; 280 argv += 2;
281 argc -= 2; 281 argc -= 2;
282 } else { 282 } else {
283 fprintf (stderr, "The -width option requires an argument\n"); 283 fprintf(stderr, "The -width option requires an argument\n");
284 quit (1); 284 quit(1);
285 } 285 }
286 } else if (strcmp (argv[1], "-height") == 0) { 286 } else if (strcmp(argv[1], "-height") == 0) {
287 if (argv[2] && ((h = atoi (argv[2])) > 0)) { 287 if (argv[2] && ((h = atoi(argv[2])) > 0)) {
288 argv += 2; 288 argv += 2;
289 argc -= 2; 289 argc -= 2;
290 } else { 290 } else {
291 fprintf (stderr, "The -height option requires an argument\n"); 291 fprintf(stderr, "The -height option requires an argument\n");
292 quit (1); 292 quit(1);
293 } 293 }
294 } else if (strcmp (argv[1], "-bpp") == 0) { 294 } else if (strcmp(argv[1], "-bpp") == 0) {
295 if (argv[2]) { 295 if (argv[2]) {
296 desired_bpp = atoi (argv[2]); 296 desired_bpp = atoi(argv[2]);
297 argv += 2; 297 argv += 2;
298 argc -= 2; 298 argc -= 2;
299 } else { 299 } else {
300 fprintf (stderr, "The -bpp option requires an argument\n"); 300 fprintf(stderr, "The -bpp option requires an argument\n");
301 quit (1); 301 quit(1);
302 } 302 }
303 } else if (strcmp (argv[1], "-warp") == 0) { 303 } else if (strcmp(argv[1], "-warp") == 0) {
304 video_flags |= SDL_HWPALETTE; 304 video_flags |= SDL_HWPALETTE;
305 argv += 1; 305 argv += 1;
306 argc -= 1; 306 argc -= 1;
307 } else if (strcmp (argv[1], "-hw") == 0) { 307 } else if (strcmp(argv[1], "-hw") == 0) {
308 video_flags |= SDL_HWSURFACE; 308 video_flags |= SDL_HWSURFACE;
309 argv += 1; 309 argv += 1;
310 argc -= 1; 310 argc -= 1;
311 } else if (strcmp (argv[1], "-flip") == 0) { 311 } else if (strcmp(argv[1], "-flip") == 0) {
312 video_flags |= SDL_DOUBLEBUF; 312 video_flags |= SDL_DOUBLEBUF;
313 argv += 1; 313 argv += 1;
314 argc -= 1; 314 argc -= 1;
315 } else if (strcmp (argv[1], "-fullscreen") == 0) { 315 } else if (strcmp(argv[1], "-fullscreen") == 0) {
316 video_flags |= SDL_FULLSCREEN; 316 video_flags |= SDL_FULLSCREEN;
317 argv += 1; 317 argv += 1;
318 argc -= 1; 318 argc -= 1;
319 } else 319 } else
320 break; 320 break;
321 } 321 }
322 322
323 /* Initialize the display */ 323 /* Initialize the display */
324 screen = SDL_SetVideoMode (w, h, desired_bpp, video_flags); 324 screen = SDL_SetVideoMode(w, h, desired_bpp, video_flags);
325 if (screen == NULL) { 325 if (screen == NULL) {
326 fprintf (stderr, "Couldn't set %dx%dx%d video mode: %s\n", 326 fprintf(stderr, "Couldn't set %dx%dx%d video mode: %s\n",
327 w, h, desired_bpp, SDL_GetError ()); 327 w, h, desired_bpp, SDL_GetError());
328 quit (1); 328 quit(1);
329 } 329 }
330 printf ("Set%s %dx%dx%d mode\n", 330 printf("Set%s %dx%dx%d mode\n",
331 screen->flags & SDL_FULLSCREEN ? " fullscreen" : "", 331 screen->flags & SDL_FULLSCREEN ? " fullscreen" : "",
332 screen->w, screen->h, screen->format->BitsPerPixel); 332 screen->w, screen->h, screen->format->BitsPerPixel);
333 printf ("(video surface located in %s memory)\n", 333 printf("(video surface located in %s memory)\n",
334 (screen->flags & SDL_HWSURFACE) ? "video" : "system"); 334 (screen->flags & SDL_HWSURFACE) ? "video" : "system");
335 if (screen->flags & SDL_DOUBLEBUF) { 335 if (screen->flags & SDL_DOUBLEBUF) {
336 printf ("Double-buffering enabled\n"); 336 printf("Double-buffering enabled\n");
337 flip = 1; 337 flip = 1;
338 } 338 }
339 339
340 /* Set the window manager title bar */ 340 /* Set the window manager title bar */
341 SDL_WM_SetCaption ("SDL test window", "testwin"); 341 SDL_WM_SetCaption("SDL test window", "testwin");
342 342
343 /* Do all the drawing work */ 343 /* Do all the drawing work */
344 #ifdef BENCHMARK_SDL 344 #ifdef BENCHMARK_SDL
345 then = SDL_GetTicks (); 345 then = SDL_GetTicks();
346 DrawPict (screen, argv[1], speedy, flip, nofade); 346 DrawPict(screen, argv[1], speedy, flip, nofade);
347 now = SDL_GetTicks (); 347 now = SDL_GetTicks();
348 printf ("Time: %d milliseconds\n", now - then); 348 printf("Time: %d milliseconds\n", now - then);
349 #else 349 #else
350 DrawPict (screen, argv[1], speedy, flip, nofade); 350 DrawPict(screen, argv[1], speedy, flip, nofade);
351 #endif 351 #endif
352 SDL_Delay (delay * 1000); 352 SDL_Delay(delay * 1000);
353 SDL_Quit (); 353 SDL_Quit();
354 return (0); 354 return (0);
355 } 355 }