Mercurial > sdl-ios-xcode
annotate test/testsprite.c @ 1178:9867f3d86e44
Real Unicode support for X11. Based on updated version of this patch:
http://lists.arabeyes.org/archives/developer/2004/June/msg00160.html
--ryan.
author | Ryan C. Gordon <icculus@icculus.org> |
---|---|
date | Mon, 21 Nov 2005 00:16:34 +0000 |
parents | be9c9c8f6d53 |
children | 31103dbf1c26 |
rev | line source |
---|---|
0 | 1 /* Simple program: Move N sprites around on the screen as fast as possible */ |
2 | |
3 #include <stdio.h> | |
4 #include <stdlib.h> | |
5 #include <string.h> | |
6 #include <ctype.h> | |
753
b14fdadd8311
Date: Thu, 4 Dec 2003 07:48:40 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
663
diff
changeset
|
7 #include <math.h> |
0 | 8 #include <time.h> |
9 | |
10 #include "SDL.h" | |
11 | |
587
f00ccf8d8edc
Added code to testsprite to detect tearing when flipping
Sam Lantinga <slouken@libsdl.org>
parents:
529
diff
changeset
|
12 #define DEBUG_FLIP 1 |
f00ccf8d8edc
Added code to testsprite to detect tearing when flipping
Sam Lantinga <slouken@libsdl.org>
parents:
529
diff
changeset
|
13 |
0 | 14 #define NUM_SPRITES 100 |
15 #define MAX_SPEED 1 | |
16 | |
17 SDL_Surface *sprite; | |
18 int numsprites; | |
19 SDL_Rect *sprite_rects; | |
20 SDL_Rect *positions; | |
21 SDL_Rect *velocities; | |
22 int sprites_visible; | |
288
2f5a6062db86
Updated for Watcom C++ and LCC compilers
Sam Lantinga <slouken@libsdl.org>
parents:
107
diff
changeset
|
23 Uint16 sprite_w, sprite_h; |
0 | 24 |
1151
be9c9c8f6d53
Removed atexit() from most of the test programs; atexit(SDL_Quit) isn't safe
Ryan C. Gordon <icculus@icculus.org>
parents:
753
diff
changeset
|
25 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */ |
be9c9c8f6d53
Removed atexit() from most of the test programs; atexit(SDL_Quit) isn't safe
Ryan C. Gordon <icculus@icculus.org>
parents:
753
diff
changeset
|
26 static void quit(int rc) |
be9c9c8f6d53
Removed atexit() from most of the test programs; atexit(SDL_Quit) isn't safe
Ryan C. Gordon <icculus@icculus.org>
parents:
753
diff
changeset
|
27 { |
be9c9c8f6d53
Removed atexit() from most of the test programs; atexit(SDL_Quit) isn't safe
Ryan C. Gordon <icculus@icculus.org>
parents:
753
diff
changeset
|
28 SDL_Quit(); |
be9c9c8f6d53
Removed atexit() from most of the test programs; atexit(SDL_Quit) isn't safe
Ryan C. Gordon <icculus@icculus.org>
parents:
753
diff
changeset
|
29 exit(rc); |
be9c9c8f6d53
Removed atexit() from most of the test programs; atexit(SDL_Quit) isn't safe
Ryan C. Gordon <icculus@icculus.org>
parents:
753
diff
changeset
|
30 } |
be9c9c8f6d53
Removed atexit() from most of the test programs; atexit(SDL_Quit) isn't safe
Ryan C. Gordon <icculus@icculus.org>
parents:
753
diff
changeset
|
31 |
0 | 32 int LoadSprite(SDL_Surface *screen, char *file) |
33 { | |
34 SDL_Surface *temp; | |
35 | |
36 /* Load the sprite image */ | |
37 sprite = SDL_LoadBMP(file); | |
38 if ( sprite == NULL ) { | |
39 fprintf(stderr, "Couldn't load %s: %s", file, SDL_GetError()); | |
40 return(-1); | |
41 } | |
42 | |
43 /* Set transparent pixel as the pixel at (0,0) */ | |
44 if ( sprite->format->palette ) { | |
45 SDL_SetColorKey(sprite, (SDL_SRCCOLORKEY|SDL_RLEACCEL), | |
46 *(Uint8 *)sprite->pixels); | |
47 } | |
48 | |
49 /* Convert sprite to video format */ | |
50 temp = SDL_DisplayFormat(sprite); | |
51 SDL_FreeSurface(sprite); | |
52 if ( temp == NULL ) { | |
53 fprintf(stderr, "Couldn't convert background: %s\n", | |
54 SDL_GetError()); | |
55 return(-1); | |
56 } | |
57 sprite = temp; | |
58 | |
59 /* We're ready to roll. :) */ | |
60 return(0); | |
61 } | |
62 | |
63 void MoveSprites(SDL_Surface *screen, Uint32 background) | |
64 { | |
65 int i, nupdates; | |
66 SDL_Rect area, *position, *velocity; | |
67 | |
68 nupdates = 0; | |
69 /* Erase all the sprites if necessary */ | |
70 if ( sprites_visible ) { | |
71 SDL_FillRect(screen, NULL, background); | |
72 } | |
73 | |
74 /* Move the sprite, bounce at the wall, and draw */ | |
75 for ( i=0; i<numsprites; ++i ) { | |
76 position = &positions[i]; | |
77 velocity = &velocities[i]; | |
78 position->x += velocity->x; | |
288
2f5a6062db86
Updated for Watcom C++ and LCC compilers
Sam Lantinga <slouken@libsdl.org>
parents:
107
diff
changeset
|
79 if ( (position->x < 0) || (position->x >= (screen->w - sprite_w)) ) { |
0 | 80 velocity->x = -velocity->x; |
81 position->x += velocity->x; | |
82 } | |
83 position->y += velocity->y; | |
288
2f5a6062db86
Updated for Watcom C++ and LCC compilers
Sam Lantinga <slouken@libsdl.org>
parents:
107
diff
changeset
|
84 if ( (position->y < 0) || (position->y >= (screen->h - sprite_w)) ) { |
0 | 85 velocity->y = -velocity->y; |
86 position->y += velocity->y; | |
87 } | |
88 | |
89 /* Blit the sprite onto the screen */ | |
90 area = *position; | |
91 SDL_BlitSurface(sprite, NULL, screen, &area); | |
92 sprite_rects[nupdates++] = area; | |
93 } | |
94 | |
587
f00ccf8d8edc
Added code to testsprite to detect tearing when flipping
Sam Lantinga <slouken@libsdl.org>
parents:
529
diff
changeset
|
95 #if DEBUG_FLIP |
f00ccf8d8edc
Added code to testsprite to detect tearing when flipping
Sam Lantinga <slouken@libsdl.org>
parents:
529
diff
changeset
|
96 { |
663
8bedd6d61642
Date: Sat, 2 Aug 2003 16:22:51 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
587
diff
changeset
|
97 if ( (screen->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF ) { |
8bedd6d61642
Date: Sat, 2 Aug 2003 16:22:51 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
587
diff
changeset
|
98 static int t = 0; |
8bedd6d61642
Date: Sat, 2 Aug 2003 16:22:51 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
587
diff
changeset
|
99 |
8bedd6d61642
Date: Sat, 2 Aug 2003 16:22:51 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
587
diff
changeset
|
100 Uint32 color = SDL_MapRGB (screen->format, 255, 0, 0); |
8bedd6d61642
Date: Sat, 2 Aug 2003 16:22:51 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
587
diff
changeset
|
101 SDL_Rect r; |
8bedd6d61642
Date: Sat, 2 Aug 2003 16:22:51 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
587
diff
changeset
|
102 r.x = (sin((float)t * 2 * 3.1459) + 1.0) / 2.0 * (screen->w-20); |
8bedd6d61642
Date: Sat, 2 Aug 2003 16:22:51 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
587
diff
changeset
|
103 r.y = 0; |
8bedd6d61642
Date: Sat, 2 Aug 2003 16:22:51 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
587
diff
changeset
|
104 r.w = 20; |
8bedd6d61642
Date: Sat, 2 Aug 2003 16:22:51 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
587
diff
changeset
|
105 r.h = screen->h; |
587
f00ccf8d8edc
Added code to testsprite to detect tearing when flipping
Sam Lantinga <slouken@libsdl.org>
parents:
529
diff
changeset
|
106 |
663
8bedd6d61642
Date: Sat, 2 Aug 2003 16:22:51 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
587
diff
changeset
|
107 SDL_FillRect (screen, &r, color); |
8bedd6d61642
Date: Sat, 2 Aug 2003 16:22:51 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
587
diff
changeset
|
108 t+=2; |
8bedd6d61642
Date: Sat, 2 Aug 2003 16:22:51 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
587
diff
changeset
|
109 } |
587
f00ccf8d8edc
Added code to testsprite to detect tearing when flipping
Sam Lantinga <slouken@libsdl.org>
parents:
529
diff
changeset
|
110 } |
f00ccf8d8edc
Added code to testsprite to detect tearing when flipping
Sam Lantinga <slouken@libsdl.org>
parents:
529
diff
changeset
|
111 #endif |
f00ccf8d8edc
Added code to testsprite to detect tearing when flipping
Sam Lantinga <slouken@libsdl.org>
parents:
529
diff
changeset
|
112 |
0 | 113 /* Update the screen! */ |
114 if ( (screen->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF ) { | |
115 SDL_Flip(screen); | |
116 } else { | |
117 SDL_UpdateRects(screen, nupdates, sprite_rects); | |
118 } | |
119 sprites_visible = 1; | |
120 } | |
121 | |
122 /* This is a way of telling whether or not to use hardware surfaces */ | |
107
01fcac5d146e
Fixed crash in testsprite when using the -fast option (nobody caught this?)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
123 Uint32 FastestFlags(Uint32 flags, int width, int height, int bpp) |
0 | 124 { |
125 const SDL_VideoInfo *info; | |
126 | |
127 /* Hardware acceleration is only used in fullscreen mode */ | |
128 flags |= SDL_FULLSCREEN; | |
129 | |
130 /* Check for various video capabilities */ | |
131 info = SDL_GetVideoInfo(); | |
132 if ( info->blit_hw_CC && info->blit_fill ) { | |
133 /* We use accelerated colorkeying and color filling */ | |
134 flags |= SDL_HWSURFACE; | |
135 } | |
136 /* If we have enough video memory, and will use accelerated | |
137 blits directly to it, then use page flipping. | |
138 */ | |
139 if ( (flags & SDL_HWSURFACE) == SDL_HWSURFACE ) { | |
140 /* Direct hardware blitting without double-buffering | |
141 causes really bad flickering. | |
142 */ | |
107
01fcac5d146e
Fixed crash in testsprite when using the -fast option (nobody caught this?)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
143 if ( info->video_mem*1024 > (height*width*bpp/8) ) { |
0 | 144 flags |= SDL_DOUBLEBUF; |
145 } else { | |
146 flags &= ~SDL_HWSURFACE; | |
147 } | |
148 } | |
149 | |
150 /* Return the flags */ | |
151 return(flags); | |
152 } | |
153 | |
154 int main(int argc, char *argv[]) | |
155 { | |
156 SDL_Surface *screen; | |
157 Uint8 *mem; | |
158 int width, height; | |
159 Uint8 video_bpp; | |
160 Uint32 videoflags; | |
161 Uint32 background; | |
162 int i, done; | |
163 SDL_Event event; | |
164 Uint32 then, now, frames; | |
165 | |
166 /* Initialize SDL */ | |
167 if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) { | |
168 fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError()); | |
1151
be9c9c8f6d53
Removed atexit() from most of the test programs; atexit(SDL_Quit) isn't safe
Ryan C. Gordon <icculus@icculus.org>
parents:
753
diff
changeset
|
169 return(1); |
0 | 170 } |
171 | |
172 numsprites = NUM_SPRITES; | |
173 videoflags = SDL_SWSURFACE|SDL_ANYFORMAT; | |
174 width = 640; | |
175 height = 480; | |
176 video_bpp = 8; | |
177 while ( argc > 1 ) { | |
178 --argc; | |
179 if ( strcmp(argv[argc-1], "-width") == 0 ) { | |
180 width = atoi(argv[argc]); | |
181 --argc; | |
182 } else | |
183 if ( strcmp(argv[argc-1], "-height") == 0 ) { | |
184 height = atoi(argv[argc]); | |
185 --argc; | |
186 } else | |
187 if ( strcmp(argv[argc-1], "-bpp") == 0 ) { | |
188 video_bpp = atoi(argv[argc]); | |
189 videoflags &= ~SDL_ANYFORMAT; | |
190 --argc; | |
191 } else | |
192 if ( strcmp(argv[argc], "-fast") == 0 ) { | |
107
01fcac5d146e
Fixed crash in testsprite when using the -fast option (nobody caught this?)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
193 videoflags = FastestFlags(videoflags, width, height, video_bpp); |
0 | 194 } else |
195 if ( strcmp(argv[argc], "-hw") == 0 ) { | |
196 videoflags ^= SDL_HWSURFACE; | |
197 } else | |
198 if ( strcmp(argv[argc], "-flip") == 0 ) { | |
199 videoflags ^= SDL_DOUBLEBUF; | |
200 } else | |
201 if ( strcmp(argv[argc], "-fullscreen") == 0 ) { | |
202 videoflags ^= SDL_FULLSCREEN; | |
203 } else | |
204 if ( isdigit(argv[argc][0]) ) { | |
205 numsprites = atoi(argv[argc]); | |
206 } else { | |
207 fprintf(stderr, | |
208 "Usage: %s [-bpp N] [-hw] [-flip] [-fast] [-fullscreen] [numsprites]\n", | |
209 argv[0]); | |
1151
be9c9c8f6d53
Removed atexit() from most of the test programs; atexit(SDL_Quit) isn't safe
Ryan C. Gordon <icculus@icculus.org>
parents:
753
diff
changeset
|
210 quit(1); |
0 | 211 } |
212 } | |
213 | |
214 /* Set video mode */ | |
215 screen = SDL_SetVideoMode(width, height, video_bpp, videoflags); | |
216 if ( ! screen ) { | |
217 fprintf(stderr, "Couldn't set %dx%d video mode: %s\n", | |
218 width, height, SDL_GetError()); | |
1151
be9c9c8f6d53
Removed atexit() from most of the test programs; atexit(SDL_Quit) isn't safe
Ryan C. Gordon <icculus@icculus.org>
parents:
753
diff
changeset
|
219 quit(2); |
0 | 220 } |
221 | |
222 /* Load the sprite */ | |
223 if ( LoadSprite(screen, "icon.bmp") < 0 ) { | |
1151
be9c9c8f6d53
Removed atexit() from most of the test programs; atexit(SDL_Quit) isn't safe
Ryan C. Gordon <icculus@icculus.org>
parents:
753
diff
changeset
|
224 quit(1); |
0 | 225 } |
226 | |
227 /* Allocate memory for the sprite info */ | |
228 mem = (Uint8 *)malloc(4*sizeof(SDL_Rect)*numsprites); | |
229 if ( mem == NULL ) { | |
230 SDL_FreeSurface(sprite); | |
231 fprintf(stderr, "Out of memory!\n"); | |
1151
be9c9c8f6d53
Removed atexit() from most of the test programs; atexit(SDL_Quit) isn't safe
Ryan C. Gordon <icculus@icculus.org>
parents:
753
diff
changeset
|
232 quit(2); |
0 | 233 } |
234 sprite_rects = (SDL_Rect *)mem; | |
235 positions = sprite_rects; | |
236 sprite_rects += numsprites; | |
237 velocities = sprite_rects; | |
238 sprite_rects += numsprites; | |
288
2f5a6062db86
Updated for Watcom C++ and LCC compilers
Sam Lantinga <slouken@libsdl.org>
parents:
107
diff
changeset
|
239 sprite_w = sprite->w; |
2f5a6062db86
Updated for Watcom C++ and LCC compilers
Sam Lantinga <slouken@libsdl.org>
parents:
107
diff
changeset
|
240 sprite_h = sprite->h; |
0 | 241 srand(time(NULL)); |
242 for ( i=0; i<numsprites; ++i ) { | |
288
2f5a6062db86
Updated for Watcom C++ and LCC compilers
Sam Lantinga <slouken@libsdl.org>
parents:
107
diff
changeset
|
243 positions[i].x = rand()%(screen->w - sprite_w); |
2f5a6062db86
Updated for Watcom C++ and LCC compilers
Sam Lantinga <slouken@libsdl.org>
parents:
107
diff
changeset
|
244 positions[i].y = rand()%(screen->h - sprite_h); |
0 | 245 positions[i].w = sprite->w; |
246 positions[i].h = sprite->h; | |
247 velocities[i].x = 0; | |
248 velocities[i].y = 0; | |
249 while ( ! velocities[i].x && ! velocities[i].y ) { | |
250 velocities[i].x = (rand()%(MAX_SPEED*2+1))-MAX_SPEED; | |
251 velocities[i].y = (rand()%(MAX_SPEED*2+1))-MAX_SPEED; | |
252 } | |
253 } | |
254 background = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00); | |
255 | |
256 /* Print out information about our surfaces */ | |
257 printf("Screen is at %d bits per pixel\n",screen->format->BitsPerPixel); | |
258 if ( (screen->flags & SDL_HWSURFACE) == SDL_HWSURFACE ) { | |
259 printf("Screen is in video memory\n"); | |
260 } else { | |
261 printf("Screen is in system memory\n"); | |
262 } | |
263 if ( (screen->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF ) { | |
264 printf("Screen has double-buffering enabled\n"); | |
265 } | |
266 if ( (sprite->flags & SDL_HWSURFACE) == SDL_HWSURFACE ) { | |
267 printf("Sprite is in video memory\n"); | |
268 } else { | |
269 printf("Sprite is in system memory\n"); | |
270 } | |
271 /* Run a sample blit to trigger blit acceleration */ | |
272 { SDL_Rect dst; | |
273 dst.x = 0; | |
274 dst.y = 0; | |
275 dst.w = sprite->w; | |
276 dst.h = sprite->h; | |
277 SDL_BlitSurface(sprite, NULL, screen, &dst); | |
278 SDL_FillRect(screen, &dst, background); | |
279 } | |
280 if ( (sprite->flags & SDL_HWACCEL) == SDL_HWACCEL ) { | |
281 printf("Sprite blit uses hardware acceleration\n"); | |
282 } | |
283 if ( (sprite->flags & SDL_RLEACCEL) == SDL_RLEACCEL ) { | |
284 printf("Sprite blit uses RLE acceleration\n"); | |
285 } | |
286 | |
287 /* Loop, blitting sprites and waiting for a keystroke */ | |
288 frames = 0; | |
289 then = SDL_GetTicks(); | |
290 done = 0; | |
291 sprites_visible = 0; | |
292 while ( !done ) { | |
293 /* Check for events */ | |
294 ++frames; | |
295 while ( SDL_PollEvent(&event) ) { | |
296 switch (event.type) { | |
529
21409a7a5fee
*** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents:
288
diff
changeset
|
297 case SDL_MOUSEBUTTONDOWN: |
21409a7a5fee
*** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents:
288
diff
changeset
|
298 SDL_WarpMouse(screen->w/2, screen->h/2); |
21409a7a5fee
*** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents:
288
diff
changeset
|
299 break; |
0 | 300 case SDL_KEYDOWN: |
301 /* Any keypress quits the app... */ | |
302 case SDL_QUIT: | |
303 done = 1; | |
304 break; | |
305 default: | |
306 break; | |
307 } | |
308 } | |
309 MoveSprites(screen, background); | |
310 } | |
311 SDL_FreeSurface(sprite); | |
312 free(mem); | |
313 | |
314 /* Print out some timing information */ | |
315 now = SDL_GetTicks(); | |
316 if ( now > then ) { | |
317 printf("%2.2f frames per second\n", | |
318 ((double)frames*1000)/(now-then)); | |
319 } | |
288
2f5a6062db86
Updated for Watcom C++ and LCC compilers
Sam Lantinga <slouken@libsdl.org>
parents:
107
diff
changeset
|
320 SDL_Quit(); |
0 | 321 return(0); |
322 } |