0
|
1
|
|
2 /* Simple program: Move N sprites around on the screen as fast as possible */
|
|
3
|
|
4 #include <stdio.h>
|
|
5 #include <stdlib.h>
|
|
6 #include <string.h>
|
|
7 #include <ctype.h>
|
|
8 #include <time.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
|
|
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;
|
|
69 if ( (position->x < 0) || (position->x >= screen->w) ) {
|
|
70 velocity->x = -velocity->x;
|
|
71 position->x += velocity->x;
|
|
72 }
|
|
73 position->y += velocity->y;
|
|
74 if ( (position->y < 0) || (position->y >= screen->h) ) {
|
|
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 */
|
|
95 Uint32 FastestFlags(Uint32 flags)
|
|
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 */
|
|
115 SDL_Surface *screen = SDL_GetVideoSurface();
|
|
116 if ( info->video_mem > (screen->h*screen->pitch) ) {
|
|
117 flags |= SDL_DOUBLEBUF;
|
|
118 } else {
|
|
119 flags &= ~SDL_HWSURFACE;
|
|
120 }
|
|
121 }
|
|
122
|
|
123 /* Return the flags */
|
|
124 return(flags);
|
|
125 }
|
|
126
|
|
127 int main(int argc, char *argv[])
|
|
128 {
|
|
129 SDL_Surface *screen;
|
|
130 Uint8 *mem;
|
|
131 int width, height;
|
|
132 Uint8 video_bpp;
|
|
133 Uint32 videoflags;
|
|
134 Uint32 background;
|
|
135 int i, done;
|
|
136 SDL_Event event;
|
|
137 Uint32 then, now, frames;
|
|
138
|
|
139 /* Initialize SDL */
|
|
140 if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
|
|
141 fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
|
|
142 exit(1);
|
|
143 }
|
|
144 atexit(SDL_Quit);
|
|
145
|
|
146 numsprites = NUM_SPRITES;
|
|
147 videoflags = SDL_SWSURFACE|SDL_ANYFORMAT;
|
|
148 width = 640;
|
|
149 height = 480;
|
|
150 video_bpp = 8;
|
|
151 while ( argc > 1 ) {
|
|
152 --argc;
|
|
153 if ( strcmp(argv[argc-1], "-width") == 0 ) {
|
|
154 width = atoi(argv[argc]);
|
|
155 --argc;
|
|
156 } else
|
|
157 if ( strcmp(argv[argc-1], "-height") == 0 ) {
|
|
158 height = atoi(argv[argc]);
|
|
159 --argc;
|
|
160 } else
|
|
161 if ( strcmp(argv[argc-1], "-bpp") == 0 ) {
|
|
162 video_bpp = atoi(argv[argc]);
|
|
163 videoflags &= ~SDL_ANYFORMAT;
|
|
164 --argc;
|
|
165 } else
|
|
166 if ( strcmp(argv[argc], "-fast") == 0 ) {
|
|
167 videoflags = FastestFlags(videoflags);
|
|
168 } else
|
|
169 if ( strcmp(argv[argc], "-hw") == 0 ) {
|
|
170 videoflags ^= SDL_HWSURFACE;
|
|
171 } else
|
|
172 if ( strcmp(argv[argc], "-flip") == 0 ) {
|
|
173 videoflags ^= SDL_DOUBLEBUF;
|
|
174 } else
|
|
175 if ( strcmp(argv[argc], "-fullscreen") == 0 ) {
|
|
176 videoflags ^= SDL_FULLSCREEN;
|
|
177 } else
|
|
178 if ( isdigit(argv[argc][0]) ) {
|
|
179 numsprites = atoi(argv[argc]);
|
|
180 } else {
|
|
181 fprintf(stderr,
|
|
182 "Usage: %s [-bpp N] [-hw] [-flip] [-fast] [-fullscreen] [numsprites]\n",
|
|
183 argv[0]);
|
|
184 exit(1);
|
|
185 }
|
|
186 }
|
|
187
|
|
188 /* Set video mode */
|
|
189 screen = SDL_SetVideoMode(width, height, video_bpp, videoflags);
|
|
190 if ( ! screen ) {
|
|
191 fprintf(stderr, "Couldn't set %dx%d video mode: %s\n",
|
|
192 width, height, SDL_GetError());
|
|
193 exit(2);
|
|
194 }
|
|
195
|
|
196 /* Load the sprite */
|
|
197 if ( LoadSprite(screen, "icon.bmp") < 0 ) {
|
|
198 exit(1);
|
|
199 }
|
|
200
|
|
201 /* Allocate memory for the sprite info */
|
|
202 mem = (Uint8 *)malloc(4*sizeof(SDL_Rect)*numsprites);
|
|
203 if ( mem == NULL ) {
|
|
204 SDL_FreeSurface(sprite);
|
|
205 fprintf(stderr, "Out of memory!\n");
|
|
206 exit(2);
|
|
207 }
|
|
208 sprite_rects = (SDL_Rect *)mem;
|
|
209 positions = sprite_rects;
|
|
210 sprite_rects += numsprites;
|
|
211 velocities = sprite_rects;
|
|
212 sprite_rects += numsprites;
|
|
213 srand(time(NULL));
|
|
214 for ( i=0; i<numsprites; ++i ) {
|
|
215 positions[i].x = rand()%screen->w;
|
|
216 positions[i].y = rand()%screen->h;
|
|
217 positions[i].w = sprite->w;
|
|
218 positions[i].h = sprite->h;
|
|
219 velocities[i].x = 0;
|
|
220 velocities[i].y = 0;
|
|
221 while ( ! velocities[i].x && ! velocities[i].y ) {
|
|
222 velocities[i].x = (rand()%(MAX_SPEED*2+1))-MAX_SPEED;
|
|
223 velocities[i].y = (rand()%(MAX_SPEED*2+1))-MAX_SPEED;
|
|
224 }
|
|
225 }
|
|
226 background = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
|
|
227
|
|
228 /* Print out information about our surfaces */
|
|
229 printf("Screen is at %d bits per pixel\n",screen->format->BitsPerPixel);
|
|
230 if ( (screen->flags & SDL_HWSURFACE) == SDL_HWSURFACE ) {
|
|
231 printf("Screen is in video memory\n");
|
|
232 } else {
|
|
233 printf("Screen is in system memory\n");
|
|
234 }
|
|
235 if ( (screen->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF ) {
|
|
236 printf("Screen has double-buffering enabled\n");
|
|
237 }
|
|
238 if ( (sprite->flags & SDL_HWSURFACE) == SDL_HWSURFACE ) {
|
|
239 printf("Sprite is in video memory\n");
|
|
240 } else {
|
|
241 printf("Sprite is in system memory\n");
|
|
242 }
|
|
243 /* Run a sample blit to trigger blit acceleration */
|
|
244 { SDL_Rect dst;
|
|
245 dst.x = 0;
|
|
246 dst.y = 0;
|
|
247 dst.w = sprite->w;
|
|
248 dst.h = sprite->h;
|
|
249 SDL_BlitSurface(sprite, NULL, screen, &dst);
|
|
250 SDL_FillRect(screen, &dst, background);
|
|
251 }
|
|
252 if ( (sprite->flags & SDL_HWACCEL) == SDL_HWACCEL ) {
|
|
253 printf("Sprite blit uses hardware acceleration\n");
|
|
254 }
|
|
255 if ( (sprite->flags & SDL_RLEACCEL) == SDL_RLEACCEL ) {
|
|
256 printf("Sprite blit uses RLE acceleration\n");
|
|
257 }
|
|
258
|
|
259 /* Loop, blitting sprites and waiting for a keystroke */
|
|
260 frames = 0;
|
|
261 then = SDL_GetTicks();
|
|
262 done = 0;
|
|
263 sprites_visible = 0;
|
|
264 while ( !done ) {
|
|
265 /* Check for events */
|
|
266 ++frames;
|
|
267 while ( SDL_PollEvent(&event) ) {
|
|
268 switch (event.type) {
|
|
269 case SDL_KEYDOWN:
|
|
270 /* Any keypress quits the app... */
|
|
271 case SDL_QUIT:
|
|
272 done = 1;
|
|
273 break;
|
|
274 default:
|
|
275 break;
|
|
276 }
|
|
277 }
|
|
278 MoveSprites(screen, background);
|
|
279 }
|
|
280 SDL_FreeSurface(sprite);
|
|
281 free(mem);
|
|
282
|
|
283 /* Print out some timing information */
|
|
284 now = SDL_GetTicks();
|
|
285 if ( now > then ) {
|
|
286 printf("%2.2f frames per second\n",
|
|
287 ((double)frames*1000)/(now-then));
|
|
288 }
|
|
289 return(0);
|
|
290 }
|