Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
321 lines (290 loc) · 9.06 KB

testsprite.c

File metadata and controls

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