Skip to content

Latest commit

 

History

History
322 lines (290 loc) · 7.84 KB

testsprite.c

File metadata and controls

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