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