/[pcsx2_0.9.7]/trunk/3rdparty/SDL-1.3.0-5387/test/testsprite.c
ViewVC logotype

Contents of /trunk/3rdparty/SDL-1.3.0-5387/test/testsprite.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 401 - (show annotations) (download)
Fri Feb 25 17:31:09 2011 UTC (9 years, 9 months ago) by william
File MIME type: text/plain
File size: 9594 byte(s)
Auto Commited Import of: pcsx2-0.9.7-DEBUG (upstream: v0.9.7.4358 local: v0.9.7.313-latest) in ./trunk
1 /* Simple program: Move N sprites around on the screen as fast as possible */
2
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <string.h>
6 #include <ctype.h>
7 #include <time.h>
8 #include <math.h>
9
10 #include "SDL.h"
11
12 #define NUM_SPRITES 100
13 #define MAX_SPEED 1
14
15 SDL_Surface *sprite;
16 int numsprites;
17 SDL_Rect *sprite_rects;
18 SDL_Rect *positions;
19 SDL_Rect *velocities;
20 int sprites_visible;
21 int debug_flip;
22 Uint16 sprite_w, sprite_h;
23
24 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
25 static void
26 quit(int rc)
27 {
28 SDL_Quit();
29 exit(rc);
30 }
31
32 int
33 LoadSprite(char *file)
34 {
35 SDL_Surface *temp;
36
37 /* Load the sprite image */
38 sprite = SDL_LoadBMP(file);
39 if (sprite == NULL) {
40 fprintf(stderr, "Couldn't load %s: %s", file, SDL_GetError());
41 return (-1);
42 }
43
44 /* Set transparent pixel as the pixel at (0,0) */
45 if (sprite->format->palette) {
46 SDL_SetColorKey(sprite, (SDL_SRCCOLORKEY | SDL_RLEACCEL),
47 *(Uint8 *) sprite->pixels);
48 }
49
50 /* Convert sprite to video format */
51 temp = SDL_DisplayFormat(sprite);
52 SDL_FreeSurface(sprite);
53 if (temp == NULL) {
54 fprintf(stderr, "Couldn't convert background: %s\n", SDL_GetError());
55 return (-1);
56 }
57 sprite = temp;
58
59 /* We're ready to roll. :) */
60 return (0);
61 }
62
63 void
64 MoveSprites(SDL_Surface * screen, Uint32 background)
65 {
66 int i, nupdates;
67 SDL_Rect area, *position, *velocity;
68
69 nupdates = 0;
70 /* Erase all the sprites if necessary */
71 if (sprites_visible) {
72 SDL_FillRect(screen, NULL, background);
73 }
74
75 /* Move the sprite, bounce at the wall, and draw */
76 for (i = 0; i < numsprites; ++i) {
77 position = &positions[i];
78 velocity = &velocities[i];
79 position->x += velocity->x;
80 if ((position->x < 0) || (position->x >= (screen->w - sprite_w))) {
81 velocity->x = -velocity->x;
82 position->x += velocity->x;
83 }
84 position->y += velocity->y;
85 if ((position->y < 0) || (position->y >= (screen->h - sprite_w))) {
86 velocity->y = -velocity->y;
87 position->y += velocity->y;
88 }
89
90 /* Blit the sprite onto the screen */
91 area = *position;
92 SDL_BlitSurface(sprite, NULL, screen, &area);
93 sprite_rects[nupdates++] = area;
94 }
95
96 if (debug_flip) {
97 if ((screen->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF) {
98 static int t = 0;
99
100 Uint32 color = SDL_MapRGB(screen->format, 255, 0, 0);
101 SDL_Rect r;
102 r.x =
103 (int)((sin((float) t * 2 * 3.1459) + 1.0) / 2.0 * (screen->w - 20));
104 r.y = 0;
105 r.w = 20;
106 r.h = screen->h;
107
108 SDL_FillRect(screen, &r, color);
109 t += 2;
110 }
111 }
112
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 */
123 Uint32
124 FastestFlags(Uint32 flags, int width, int height, int bpp)
125 {
126 const SDL_VideoInfo *info;
127
128 /* Hardware acceleration is only used in fullscreen mode */
129 flags |= SDL_FULLSCREEN;
130
131 /* Check for various video capabilities */
132 info = SDL_GetVideoInfo();
133 if (info->blit_hw_CC && info->blit_fill) {
134 /* We use accelerated colorkeying and color filling */
135 flags |= SDL_HWSURFACE;
136 }
137 /* If we have enough video memory, and will use accelerated
138 blits directly to it, then use page flipping.
139 */
140 if ((flags & SDL_HWSURFACE) == SDL_HWSURFACE) {
141 /* Direct hardware blitting without double-buffering
142 causes really bad flickering.
143 */
144 if (info->video_mem * 1024 > (Uint32)(height * width * bpp / 8)) {
145 flags |= SDL_DOUBLEBUF;
146 } else {
147 flags &= ~SDL_HWSURFACE;
148 }
149 }
150
151 /* Return the flags */
152 return (flags);
153 }
154
155 int
156 main(int argc, char *argv[])
157 {
158 SDL_Surface *screen;
159 Uint8 *mem;
160 int width, height;
161 Uint8 video_bpp;
162 Uint32 videoflags;
163 Uint32 background;
164 int i, done;
165 SDL_Event event;
166 Uint32 then, now, frames;
167
168 /* Initialize SDL */
169 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
170 fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
171 return (1);
172 }
173
174 numsprites = NUM_SPRITES;
175 videoflags = SDL_SWSURFACE | SDL_ANYFORMAT | SDL_RESIZABLE;
176 width = 640;
177 height = 480;
178 video_bpp = 8;
179 debug_flip = 0;
180 while (argc > 1) {
181 --argc;
182 if (strcmp(argv[argc - 1], "-width") == 0) {
183 width = atoi(argv[argc]);
184 --argc;
185 } else if (strcmp(argv[argc - 1], "-height") == 0) {
186 height = atoi(argv[argc]);
187 --argc;
188 } else if (strcmp(argv[argc - 1], "-bpp") == 0) {
189 video_bpp = atoi(argv[argc]);
190 videoflags &= ~SDL_ANYFORMAT;
191 --argc;
192 } else if (strcmp(argv[argc], "-fast") == 0) {
193 videoflags = FastestFlags(videoflags, width, height, video_bpp);
194 } else if (strcmp(argv[argc], "-hw") == 0) {
195 videoflags ^= SDL_HWSURFACE;
196 } else if (strcmp(argv[argc], "-flip") == 0) {
197 videoflags ^= SDL_DOUBLEBUF;
198 } else if (strcmp(argv[argc], "-debugflip") == 0) {
199 debug_flip ^= 1;
200 } else if (strcmp(argv[argc], "-fullscreen") == 0) {
201 videoflags ^= SDL_FULLSCREEN;
202 } else if (isdigit(argv[argc][0])) {
203 numsprites = atoi(argv[argc]);
204 } else {
205 fprintf(stderr,
206 "Usage: %s [-bpp N] [-hw] [-flip] [-fast] [-fullscreen] [numsprites]\n",
207 argv[0]);
208 quit(1);
209 }
210 }
211
212 /* Set video mode */
213 screen = SDL_SetVideoMode(width, height, video_bpp, videoflags);
214 if (!screen) {
215 fprintf(stderr, "Couldn't set %dx%d video mode: %s\n",
216 width, height, SDL_GetError());
217 quit(2);
218 }
219
220 /* Load the sprite */
221 if (LoadSprite("icon.bmp") < 0) {
222 quit(1);
223 }
224
225 /* Allocate memory for the sprite info */
226 mem = (Uint8 *) malloc(4 * sizeof(SDL_Rect) * numsprites);
227 if (mem == NULL) {
228 SDL_FreeSurface(sprite);
229 fprintf(stderr, "Out of memory!\n");
230 quit(2);
231 }
232 sprite_rects = (SDL_Rect *) mem;
233 positions = sprite_rects;
234 sprite_rects += numsprites;
235 velocities = sprite_rects;
236 sprite_rects += numsprites;
237 sprite_w = sprite->w;
238 sprite_h = sprite->h;
239 srand((unsigned int)time(NULL));
240 for (i = 0; i < numsprites; ++i) {
241 positions[i].x = rand() % (screen->w - sprite_w);
242 positions[i].y = rand() % (screen->h - sprite_h);
243 positions[i].w = sprite->w;
244 positions[i].h = sprite->h;
245 velocities[i].x = 0;
246 velocities[i].y = 0;
247 while (!velocities[i].x && !velocities[i].y) {
248 velocities[i].x = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
249 velocities[i].y = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
250 }
251 }
252
253 /* Clear the background to grey */
254 background = SDL_MapRGB(screen->format, 0xA0, 0xA0, 0xA0);
255 SDL_FillRect(screen, NULL, background);
256 SDL_Flip(screen);
257
258 /* Print out information about our surfaces */
259 printf("Screen is at %d bits per pixel\n", screen->format->BitsPerPixel);
260 if ((screen->flags & SDL_HWSURFACE) == SDL_HWSURFACE) {
261 printf("Screen is in video memory\n");
262 } else {
263 printf("Screen is in system memory\n");
264 }
265 if ((screen->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF) {
266 printf("Screen has double-buffering enabled\n");
267 }
268 if ((sprite->flags & SDL_HWSURFACE) == SDL_HWSURFACE) {
269 printf("Sprite is in video memory\n");
270 } else {
271 printf("Sprite is in system memory\n");
272 }
273 /* Run a sample blit to trigger blit acceleration */
274 {
275 SDL_Rect dst;
276 dst.x = 0;
277 dst.y = 0;
278 dst.w = sprite->w;
279 dst.h = sprite->h;
280 SDL_BlitSurface(sprite, NULL, screen, &dst);
281 SDL_FillRect(screen, &dst, background);
282 }
283 if ((sprite->flags & SDL_HWACCEL) == SDL_HWACCEL) {
284 printf("Sprite blit uses hardware acceleration\n");
285 }
286 if ((sprite->flags & SDL_RLEACCEL) == SDL_RLEACCEL) {
287 printf("Sprite blit uses RLE acceleration\n");
288 }
289
290 /* Loop, blitting sprites and waiting for a keystroke */
291 frames = 0;
292 then = SDL_GetTicks();
293 done = 0;
294 sprites_visible = 0;
295 while (!done) {
296 /* Check for events */
297 ++frames;
298 while (SDL_PollEvent(&event)) {
299 switch (event.type) {
300 case SDL_VIDEORESIZE:
301 screen = SDL_SetVideoMode(event.resize.w, event.resize.h, video_bpp, videoflags);
302 break;
303 case SDL_MOUSEBUTTONDOWN:
304 SDL_WarpMouse(screen->w / 2, screen->h / 2);
305 break;
306 case SDL_KEYDOWN:
307 /* Any keypress quits the app... */
308 case SDL_QUIT:
309 done = 1;
310 break;
311 default:
312 break;
313 }
314 }
315 MoveSprites(screen, background);
316 }
317 SDL_FreeSurface(sprite);
318 free(mem);
319
320 /* Print out some timing information */
321 now = SDL_GetTicks();
322 if (now > then) {
323 double fps = ((double) frames * 1000) / (now - then);
324 printf("%2.2f frames per second\n", fps);
325 }
326 SDL_Quit();
327 return (0);
328 }

  ViewVC Help
Powered by ViewVC 1.1.22