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