Mercurial > sdl-ios-xcode
comparison test/testsprite.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 | 6e7ec5cb83c3 |
children | 931d111e737a |
comparison
equal
deleted
inserted
replaced
1667:1fddae038bc8 | 1668:4da1ee79c9af |
---|---|
21 int debug_flip; | 21 int debug_flip; |
22 Uint16 sprite_w, sprite_h; | 22 Uint16 sprite_w, sprite_h; |
23 | 23 |
24 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */ | 24 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */ |
25 static void | 25 static void |
26 quit (int rc) | 26 quit(int rc) |
27 { | 27 { |
28 SDL_Quit (); | 28 SDL_Quit(); |
29 exit (rc); | 29 exit(rc); |
30 } | 30 } |
31 | 31 |
32 int | 32 int |
33 LoadSprite (char *file) | 33 LoadSprite(char *file) |
34 { | 34 { |
35 SDL_Surface *temp; | 35 SDL_Surface *temp; |
36 | 36 |
37 /* Load the sprite image */ | 37 /* Load the sprite image */ |
38 sprite = SDL_LoadBMP (file); | 38 sprite = SDL_LoadBMP(file); |
39 if (sprite == NULL) { | 39 if (sprite == NULL) { |
40 fprintf (stderr, "Couldn't load %s: %s", file, SDL_GetError ()); | 40 fprintf(stderr, "Couldn't load %s: %s", file, SDL_GetError()); |
41 return (-1); | 41 return (-1); |
42 } | 42 } |
43 | 43 |
44 /* Set transparent pixel as the pixel at (0,0) */ | 44 /* Set transparent pixel as the pixel at (0,0) */ |
45 if (sprite->format->palette) { | 45 if (sprite->format->palette) { |
46 SDL_SetColorKey (sprite, (SDL_SRCCOLORKEY | SDL_RLEACCEL), | 46 SDL_SetColorKey(sprite, (SDL_SRCCOLORKEY | SDL_RLEACCEL), |
47 *(Uint8 *) sprite->pixels); | 47 *(Uint8 *) sprite->pixels); |
48 } | 48 } |
49 | 49 |
50 /* Convert sprite to video format */ | 50 /* Convert sprite to video format */ |
51 temp = SDL_DisplayFormat (sprite); | 51 temp = SDL_DisplayFormat(sprite); |
52 SDL_FreeSurface (sprite); | 52 SDL_FreeSurface(sprite); |
53 if (temp == NULL) { | 53 if (temp == NULL) { |
54 fprintf (stderr, "Couldn't convert background: %s\n", | 54 fprintf(stderr, "Couldn't convert background: %s\n", SDL_GetError()); |
55 SDL_GetError ()); | |
56 return (-1); | 55 return (-1); |
57 } | 56 } |
58 sprite = temp; | 57 sprite = temp; |
59 | 58 |
60 /* We're ready to roll. :) */ | 59 /* We're ready to roll. :) */ |
61 return (0); | 60 return (0); |
62 } | 61 } |
63 | 62 |
64 void | 63 void |
65 MoveSprites (SDL_Surface * screen, Uint32 background) | 64 MoveSprites(SDL_Surface * screen, Uint32 background) |
66 { | 65 { |
67 int i, nupdates; | 66 int i, nupdates; |
68 SDL_Rect area, *position, *velocity; | 67 SDL_Rect area, *position, *velocity; |
69 | 68 |
70 nupdates = 0; | 69 nupdates = 0; |
71 /* Erase all the sprites if necessary */ | 70 /* Erase all the sprites if necessary */ |
72 if (sprites_visible) { | 71 if (sprites_visible) { |
73 SDL_FillRect (screen, NULL, background); | 72 SDL_FillRect(screen, NULL, background); |
74 } | 73 } |
75 | 74 |
76 /* Move the sprite, bounce at the wall, and draw */ | 75 /* Move the sprite, bounce at the wall, and draw */ |
77 for (i = 0; i < numsprites; ++i) { | 76 for (i = 0; i < numsprites; ++i) { |
78 position = &positions[i]; | 77 position = &positions[i]; |
88 position->y += velocity->y; | 87 position->y += velocity->y; |
89 } | 88 } |
90 | 89 |
91 /* Blit the sprite onto the screen */ | 90 /* Blit the sprite onto the screen */ |
92 area = *position; | 91 area = *position; |
93 SDL_BlitSurface (sprite, NULL, screen, &area); | 92 SDL_BlitSurface(sprite, NULL, screen, &area); |
94 sprite_rects[nupdates++] = area; | 93 sprite_rects[nupdates++] = area; |
95 } | 94 } |
96 | 95 |
97 if (debug_flip) { | 96 if (debug_flip) { |
98 if ((screen->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF) { | 97 if ((screen->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF) { |
99 static int t = 0; | 98 static int t = 0; |
100 | 99 |
101 Uint32 color = SDL_MapRGB (screen->format, 255, 0, 0); | 100 Uint32 color = SDL_MapRGB(screen->format, 255, 0, 0); |
102 SDL_Rect r; | 101 SDL_Rect r; |
103 r.x = | 102 r.x = |
104 (sin ((float) t * 2 * 3.1459) + 1.0) / 2.0 * (screen->w - 20); | 103 (sin((float) t * 2 * 3.1459) + 1.0) / 2.0 * (screen->w - 20); |
105 r.y = 0; | 104 r.y = 0; |
106 r.w = 20; | 105 r.w = 20; |
107 r.h = screen->h; | 106 r.h = screen->h; |
108 | 107 |
109 SDL_FillRect (screen, &r, color); | 108 SDL_FillRect(screen, &r, color); |
110 t += 2; | 109 t += 2; |
111 } | 110 } |
112 } | 111 } |
113 | 112 |
114 /* Update the screen! */ | 113 /* Update the screen! */ |
115 if ((screen->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF) { | 114 if ((screen->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF) { |
116 SDL_Flip (screen); | 115 SDL_Flip(screen); |
117 } else { | 116 } else { |
118 SDL_UpdateRects (screen, nupdates, sprite_rects); | 117 SDL_UpdateRects(screen, nupdates, sprite_rects); |
119 } | 118 } |
120 sprites_visible = 1; | 119 sprites_visible = 1; |
121 } | 120 } |
122 | 121 |
123 /* This is a way of telling whether or not to use hardware surfaces */ | 122 /* This is a way of telling whether or not to use hardware surfaces */ |
124 Uint32 | 123 Uint32 |
125 FastestFlags (Uint32 flags, int width, int height, int bpp) | 124 FastestFlags(Uint32 flags, int width, int height, int bpp) |
126 { | 125 { |
127 const SDL_VideoInfo *info; | 126 const SDL_VideoInfo *info; |
128 | 127 |
129 /* Hardware acceleration is only used in fullscreen mode */ | 128 /* Hardware acceleration is only used in fullscreen mode */ |
130 flags |= SDL_FULLSCREEN; | 129 flags |= SDL_FULLSCREEN; |
131 | 130 |
132 /* Check for various video capabilities */ | 131 /* Check for various video capabilities */ |
133 info = SDL_GetVideoInfo (); | 132 info = SDL_GetVideoInfo(); |
134 if (info->blit_hw_CC && info->blit_fill) { | 133 if (info->blit_hw_CC && info->blit_fill) { |
135 /* We use accelerated colorkeying and color filling */ | 134 /* We use accelerated colorkeying and color filling */ |
136 flags |= SDL_HWSURFACE; | 135 flags |= SDL_HWSURFACE; |
137 } | 136 } |
138 /* If we have enough video memory, and will use accelerated | 137 /* If we have enough video memory, and will use accelerated |
152 /* Return the flags */ | 151 /* Return the flags */ |
153 return (flags); | 152 return (flags); |
154 } | 153 } |
155 | 154 |
156 int | 155 int |
157 main (int argc, char *argv[]) | 156 main(int argc, char *argv[]) |
158 { | 157 { |
159 SDL_Surface *screen; | 158 SDL_Surface *screen; |
160 Uint8 *mem; | 159 Uint8 *mem; |
161 int width, height; | 160 int width, height; |
162 Uint8 video_bpp; | 161 Uint8 video_bpp; |
165 int i, done; | 164 int i, done; |
166 SDL_Event event; | 165 SDL_Event event; |
167 Uint32 then, now, frames; | 166 Uint32 then, now, frames; |
168 | 167 |
169 /* Initialize SDL */ | 168 /* Initialize SDL */ |
170 if (SDL_Init (SDL_INIT_VIDEO) < 0) { | 169 if (SDL_Init(SDL_INIT_VIDEO) < 0) { |
171 fprintf (stderr, "Couldn't initialize SDL: %s\n", SDL_GetError ()); | 170 fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError()); |
172 return (1); | 171 return (1); |
173 } | 172 } |
174 | 173 |
175 numsprites = NUM_SPRITES; | 174 numsprites = NUM_SPRITES; |
176 videoflags = SDL_SWSURFACE | SDL_ANYFORMAT; | 175 videoflags = SDL_SWSURFACE | SDL_ANYFORMAT; |
178 height = 480; | 177 height = 480; |
179 video_bpp = 8; | 178 video_bpp = 8; |
180 debug_flip = 0; | 179 debug_flip = 0; |
181 while (argc > 1) { | 180 while (argc > 1) { |
182 --argc; | 181 --argc; |
183 if (strcmp (argv[argc - 1], "-width") == 0) { | 182 if (strcmp(argv[argc - 1], "-width") == 0) { |
184 width = atoi (argv[argc]); | 183 width = atoi(argv[argc]); |
185 --argc; | 184 --argc; |
186 } else if (strcmp (argv[argc - 1], "-height") == 0) { | 185 } else if (strcmp(argv[argc - 1], "-height") == 0) { |
187 height = atoi (argv[argc]); | 186 height = atoi(argv[argc]); |
188 --argc; | 187 --argc; |
189 } else if (strcmp (argv[argc - 1], "-bpp") == 0) { | 188 } else if (strcmp(argv[argc - 1], "-bpp") == 0) { |
190 video_bpp = atoi (argv[argc]); | 189 video_bpp = atoi(argv[argc]); |
191 videoflags &= ~SDL_ANYFORMAT; | 190 videoflags &= ~SDL_ANYFORMAT; |
192 --argc; | 191 --argc; |
193 } else if (strcmp (argv[argc], "-fast") == 0) { | 192 } else if (strcmp(argv[argc], "-fast") == 0) { |
194 videoflags = FastestFlags (videoflags, width, height, video_bpp); | 193 videoflags = FastestFlags(videoflags, width, height, video_bpp); |
195 } else if (strcmp (argv[argc], "-hw") == 0) { | 194 } else if (strcmp(argv[argc], "-hw") == 0) { |
196 videoflags ^= SDL_HWSURFACE; | 195 videoflags ^= SDL_HWSURFACE; |
197 } else if (strcmp (argv[argc], "-flip") == 0) { | 196 } else if (strcmp(argv[argc], "-flip") == 0) { |
198 videoflags ^= SDL_DOUBLEBUF; | 197 videoflags ^= SDL_DOUBLEBUF; |
199 } else if (strcmp (argv[argc], "-debugflip") == 0) { | 198 } else if (strcmp(argv[argc], "-debugflip") == 0) { |
200 debug_flip ^= 1; | 199 debug_flip ^= 1; |
201 } else if (strcmp (argv[argc], "-fullscreen") == 0) { | 200 } else if (strcmp(argv[argc], "-fullscreen") == 0) { |
202 videoflags ^= SDL_FULLSCREEN; | 201 videoflags ^= SDL_FULLSCREEN; |
203 } else if (isdigit (argv[argc][0])) { | 202 } else if (isdigit(argv[argc][0])) { |
204 numsprites = atoi (argv[argc]); | 203 numsprites = atoi(argv[argc]); |
205 } else { | 204 } else { |
206 fprintf (stderr, | 205 fprintf(stderr, |
207 "Usage: %s [-bpp N] [-hw] [-flip] [-fast] [-fullscreen] [numsprites]\n", | 206 "Usage: %s [-bpp N] [-hw] [-flip] [-fast] [-fullscreen] [numsprites]\n", |
208 argv[0]); | 207 argv[0]); |
209 quit (1); | 208 quit(1); |
210 } | 209 } |
211 } | 210 } |
212 | 211 |
213 /* Set video mode */ | 212 /* Set video mode */ |
214 screen = SDL_SetVideoMode (width, height, video_bpp, videoflags); | 213 screen = SDL_SetVideoMode(width, height, video_bpp, videoflags); |
215 if (!screen) { | 214 if (!screen) { |
216 fprintf (stderr, "Couldn't set %dx%d video mode: %s\n", | 215 fprintf(stderr, "Couldn't set %dx%d video mode: %s\n", |
217 width, height, SDL_GetError ()); | 216 width, height, SDL_GetError()); |
218 quit (2); | 217 quit(2); |
219 } | 218 } |
220 | 219 |
221 /* Load the sprite */ | 220 /* Load the sprite */ |
222 if (LoadSprite ("icon.bmp") < 0) { | 221 if (LoadSprite("icon.bmp") < 0) { |
223 quit (1); | 222 quit(1); |
224 } | 223 } |
225 | 224 |
226 /* Allocate memory for the sprite info */ | 225 /* Allocate memory for the sprite info */ |
227 mem = (Uint8 *) malloc (4 * sizeof (SDL_Rect) * numsprites); | 226 mem = (Uint8 *) malloc(4 * sizeof(SDL_Rect) * numsprites); |
228 if (mem == NULL) { | 227 if (mem == NULL) { |
229 SDL_FreeSurface (sprite); | 228 SDL_FreeSurface(sprite); |
230 fprintf (stderr, "Out of memory!\n"); | 229 fprintf(stderr, "Out of memory!\n"); |
231 quit (2); | 230 quit(2); |
232 } | 231 } |
233 sprite_rects = (SDL_Rect *) mem; | 232 sprite_rects = (SDL_Rect *) mem; |
234 positions = sprite_rects; | 233 positions = sprite_rects; |
235 sprite_rects += numsprites; | 234 sprite_rects += numsprites; |
236 velocities = sprite_rects; | 235 velocities = sprite_rects; |
237 sprite_rects += numsprites; | 236 sprite_rects += numsprites; |
238 sprite_w = sprite->w; | 237 sprite_w = sprite->w; |
239 sprite_h = sprite->h; | 238 sprite_h = sprite->h; |
240 srand (time (NULL)); | 239 srand(time(NULL)); |
241 for (i = 0; i < numsprites; ++i) { | 240 for (i = 0; i < numsprites; ++i) { |
242 positions[i].x = rand () % (screen->w - sprite_w); | 241 positions[i].x = rand() % (screen->w - sprite_w); |
243 positions[i].y = rand () % (screen->h - sprite_h); | 242 positions[i].y = rand() % (screen->h - sprite_h); |
244 positions[i].w = sprite->w; | 243 positions[i].w = sprite->w; |
245 positions[i].h = sprite->h; | 244 positions[i].h = sprite->h; |
246 velocities[i].x = 0; | 245 velocities[i].x = 0; |
247 velocities[i].y = 0; | 246 velocities[i].y = 0; |
248 while (!velocities[i].x && !velocities[i].y) { | 247 while (!velocities[i].x && !velocities[i].y) { |
249 velocities[i].x = (rand () % (MAX_SPEED * 2 + 1)) - MAX_SPEED; | 248 velocities[i].x = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED; |
250 velocities[i].y = (rand () % (MAX_SPEED * 2 + 1)) - MAX_SPEED; | 249 velocities[i].y = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED; |
251 } | 250 } |
252 } | 251 } |
253 background = SDL_MapRGB (screen->format, 0x00, 0x00, 0x00); | 252 background = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00); |
254 | 253 |
255 /* Print out information about our surfaces */ | 254 /* Print out information about our surfaces */ |
256 printf ("Screen is at %d bits per pixel\n", screen->format->BitsPerPixel); | 255 printf("Screen is at %d bits per pixel\n", screen->format->BitsPerPixel); |
257 if ((screen->flags & SDL_HWSURFACE) == SDL_HWSURFACE) { | 256 if ((screen->flags & SDL_HWSURFACE) == SDL_HWSURFACE) { |
258 printf ("Screen is in video memory\n"); | 257 printf("Screen is in video memory\n"); |
259 } else { | 258 } else { |
260 printf ("Screen is in system memory\n"); | 259 printf("Screen is in system memory\n"); |
261 } | 260 } |
262 if ((screen->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF) { | 261 if ((screen->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF) { |
263 printf ("Screen has double-buffering enabled\n"); | 262 printf("Screen has double-buffering enabled\n"); |
264 } | 263 } |
265 if ((sprite->flags & SDL_HWSURFACE) == SDL_HWSURFACE) { | 264 if ((sprite->flags & SDL_HWSURFACE) == SDL_HWSURFACE) { |
266 printf ("Sprite is in video memory\n"); | 265 printf("Sprite is in video memory\n"); |
267 } else { | 266 } else { |
268 printf ("Sprite is in system memory\n"); | 267 printf("Sprite is in system memory\n"); |
269 } | 268 } |
270 /* Run a sample blit to trigger blit acceleration */ | 269 /* Run a sample blit to trigger blit acceleration */ |
271 { | 270 { |
272 SDL_Rect dst; | 271 SDL_Rect dst; |
273 dst.x = 0; | 272 dst.x = 0; |
274 dst.y = 0; | 273 dst.y = 0; |
275 dst.w = sprite->w; | 274 dst.w = sprite->w; |
276 dst.h = sprite->h; | 275 dst.h = sprite->h; |
277 SDL_BlitSurface (sprite, NULL, screen, &dst); | 276 SDL_BlitSurface(sprite, NULL, screen, &dst); |
278 SDL_FillRect (screen, &dst, background); | 277 SDL_FillRect(screen, &dst, background); |
279 } | 278 } |
280 if ((sprite->flags & SDL_HWACCEL) == SDL_HWACCEL) { | 279 if ((sprite->flags & SDL_HWACCEL) == SDL_HWACCEL) { |
281 printf ("Sprite blit uses hardware acceleration\n"); | 280 printf("Sprite blit uses hardware acceleration\n"); |
282 } | 281 } |
283 if ((sprite->flags & SDL_RLEACCEL) == SDL_RLEACCEL) { | 282 if ((sprite->flags & SDL_RLEACCEL) == SDL_RLEACCEL) { |
284 printf ("Sprite blit uses RLE acceleration\n"); | 283 printf("Sprite blit uses RLE acceleration\n"); |
285 } | 284 } |
286 | 285 |
287 /* Loop, blitting sprites and waiting for a keystroke */ | 286 /* Loop, blitting sprites and waiting for a keystroke */ |
288 frames = 0; | 287 frames = 0; |
289 then = SDL_GetTicks (); | 288 then = SDL_GetTicks(); |
290 done = 0; | 289 done = 0; |
291 sprites_visible = 0; | 290 sprites_visible = 0; |
292 while (!done) { | 291 while (!done) { |
293 /* Check for events */ | 292 /* Check for events */ |
294 ++frames; | 293 ++frames; |
295 while (SDL_PollEvent (&event)) { | 294 while (SDL_PollEvent(&event)) { |
296 switch (event.type) { | 295 switch (event.type) { |
297 case SDL_MOUSEBUTTONDOWN: | 296 case SDL_MOUSEBUTTONDOWN: |
298 SDL_WarpMouse (screen->w / 2, screen->h / 2); | 297 SDL_WarpMouse(screen->w / 2, screen->h / 2); |
299 break; | 298 break; |
300 case SDL_KEYDOWN: | 299 case SDL_KEYDOWN: |
301 /* Any keypress quits the app... */ | 300 /* Any keypress quits the app... */ |
302 case SDL_QUIT: | 301 case SDL_QUIT: |
303 done = 1; | 302 done = 1; |
304 break; | 303 break; |
305 default: | 304 default: |
306 break; | 305 break; |
307 } | 306 } |
308 } | 307 } |
309 MoveSprites (screen, background); | 308 MoveSprites(screen, background); |
310 } | 309 } |
311 SDL_FreeSurface (sprite); | 310 SDL_FreeSurface(sprite); |
312 free (mem); | 311 free(mem); |
313 | 312 |
314 /* Print out some timing information */ | 313 /* Print out some timing information */ |
315 now = SDL_GetTicks (); | 314 now = SDL_GetTicks(); |
316 if (now > then) { | 315 if (now > then) { |
317 printf ("%2.2f frames per second\n", | 316 printf("%2.2f frames per second\n", |
318 ((double) frames * 1000) / (now - then)); | 317 ((double) frames * 1000) / (now - then)); |
319 } | 318 } |
320 SDL_Quit (); | 319 SDL_Quit(); |
321 return (0); | 320 return (0); |
322 } | 321 } |