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