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