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

Latest commit

 

History

History
339 lines (305 loc) · 9.76 KB

testsprite.c

File metadata and controls

339 lines (305 loc) · 9.76 KB
 
Apr 8, 2011
Apr 8, 2011
1
2
3
4
5
6
7
8
9
10
11
/*
Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely.
*/
Apr 26, 2001
Apr 26, 2001
12
13
14
/* Simple program: Move N sprites around on the screen as fast as possible */
#include <stdlib.h>
Jul 15, 2006
Jul 15, 2006
15
#include <stdio.h>
Oct 4, 2009
Oct 4, 2009
16
17
#include <string.h>
#include <ctype.h>
Apr 26, 2001
Apr 26, 2001
18
#include <time.h>
Jul 23, 2006
Jul 23, 2006
19
#include <math.h>
Apr 26, 2001
Apr 26, 2001
20
21
22
23
24
25
26
27
28
29
30
31
#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
32
int debug_flip;
Feb 28, 2002
Feb 28, 2002
33
Uint16 sprite_w, sprite_h;
Apr 26, 2001
Apr 26, 2001
34
Sep 28, 2005
Sep 28, 2005
35
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
Jul 10, 2006
Jul 10, 2006
36
37
static void
quit(int rc)
Sep 28, 2005
Sep 28, 2005
38
{
Jul 10, 2006
Jul 10, 2006
39
40
SDL_Quit();
exit(rc);
Sep 28, 2005
Sep 28, 2005
41
42
}
Jul 10, 2006
Jul 10, 2006
43
44
int
LoadSprite(char *file)
Apr 26, 2001
Apr 26, 2001
45
{
Jul 10, 2006
Jul 10, 2006
46
SDL_Surface *temp;
Apr 26, 2001
Apr 26, 2001
47
Jul 10, 2006
Jul 10, 2006
48
49
50
51
52
53
/* Load the sprite image */
sprite = SDL_LoadBMP(file);
if (sprite == NULL) {
fprintf(stderr, "Couldn't load %s: %s", file, SDL_GetError());
return (-1);
}
Apr 26, 2001
Apr 26, 2001
54
Jul 10, 2006
Jul 10, 2006
55
56
57
58
59
/* Set transparent pixel as the pixel at (0,0) */
if (sprite->format->palette) {
SDL_SetColorKey(sprite, (SDL_SRCCOLORKEY | SDL_RLEACCEL),
*(Uint8 *) sprite->pixels);
}
Apr 26, 2001
Apr 26, 2001
60
Jul 10, 2006
Jul 10, 2006
61
62
63
64
65
66
67
68
/* 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;
Apr 26, 2001
Apr 26, 2001
69
Jul 10, 2006
Jul 10, 2006
70
71
/* We're ready to roll. :) */
return (0);
Apr 26, 2001
Apr 26, 2001
72
73
}
Jul 10, 2006
Jul 10, 2006
74
75
void
MoveSprites(SDL_Surface * screen, Uint32 background)
Apr 26, 2001
Apr 26, 2001
76
{
Jul 10, 2006
Jul 10, 2006
77
78
int i, nupdates;
SDL_Rect area, *position, *velocity;
Apr 26, 2001
Apr 26, 2001
79
Jul 10, 2006
Jul 10, 2006
80
81
82
83
84
nupdates = 0;
/* Erase all the sprites if necessary */
if (sprites_visible) {
SDL_FillRect(screen, NULL, background);
}
Apr 26, 2001
Apr 26, 2001
85
Jul 10, 2006
Jul 10, 2006
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/* 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
100
Jul 10, 2006
Jul 10, 2006
101
102
103
104
105
/* Blit the sprite onto the screen */
area = *position;
SDL_BlitSurface(sprite, NULL, screen, &area);
sprite_rects[nupdates++] = area;
}
Apr 26, 2001
Apr 26, 2001
106
Jul 10, 2006
Jul 10, 2006
107
108
109
if (debug_flip) {
if ((screen->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF) {
static int t = 0;
Jan 2, 2006
Jan 2, 2006
110
Jul 10, 2006
Jul 10, 2006
111
112
113
Uint32 color = SDL_MapRGB(screen->format, 255, 0, 0);
SDL_Rect r;
r.x =
Sep 16, 2010
Sep 16, 2010
114
(int)((sin((float) t * 2 * 3.1459) + 1.0) / 2.0 * (screen->w - 20));
Jul 10, 2006
Jul 10, 2006
115
116
117
r.y = 0;
r.w = 20;
r.h = screen->h;
Jan 2, 2006
Jan 2, 2006
118
Jul 10, 2006
Jul 10, 2006
119
120
121
122
SDL_FillRect(screen, &r, color);
t += 2;
}
}
Jan 2, 2006
Jan 2, 2006
123
Jul 10, 2006
Jul 10, 2006
124
125
126
127
128
129
130
/* Update the screen! */
if ((screen->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF) {
SDL_Flip(screen);
} else {
SDL_UpdateRects(screen, nupdates, sprite_rects);
}
sprites_visible = 1;
Apr 26, 2001
Apr 26, 2001
131
132
133
}
/* This is a way of telling whether or not to use hardware surfaces */
Jul 10, 2006
Jul 10, 2006
134
135
Uint32
FastestFlags(Uint32 flags, int width, int height, int bpp)
Apr 26, 2001
Apr 26, 2001
136
{
Jul 10, 2006
Jul 10, 2006
137
const SDL_VideoInfo *info;
Apr 26, 2001
Apr 26, 2001
138
Jul 10, 2006
Jul 10, 2006
139
140
/* Hardware acceleration is only used in fullscreen mode */
flags |= SDL_FULLSCREEN;
Apr 26, 2001
Apr 26, 2001
141
Jul 10, 2006
Jul 10, 2006
142
143
144
145
146
147
148
149
150
151
152
153
154
/* 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.
*/
Sep 16, 2010
Sep 16, 2010
155
if (info->video_mem * 1024 > (Uint32)(height * width * bpp / 8)) {
Jul 10, 2006
Jul 10, 2006
156
157
158
159
160
flags |= SDL_DOUBLEBUF;
} else {
flags &= ~SDL_HWSURFACE;
}
}
Apr 26, 2001
Apr 26, 2001
161
Jul 10, 2006
Jul 10, 2006
162
163
/* Return the flags */
return (flags);
Apr 26, 2001
Apr 26, 2001
164
165
}
Jul 10, 2006
Jul 10, 2006
166
167
int
main(int argc, char *argv[])
Apr 26, 2001
Apr 26, 2001
168
{
Jul 10, 2006
Jul 10, 2006
169
170
171
172
173
174
175
176
177
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
178
Jul 10, 2006
Jul 10, 2006
179
180
181
182
183
/* Initialize SDL */
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
return (1);
}
Apr 26, 2001
Apr 26, 2001
184
Jul 10, 2006
Jul 10, 2006
185
numsprites = NUM_SPRITES;
Feb 5, 2011
Feb 5, 2011
186
videoflags = SDL_SWSURFACE | SDL_ANYFORMAT | SDL_RESIZABLE;
Jul 10, 2006
Jul 10, 2006
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
width = 640;
height = 480;
video_bpp = 8;
debug_flip = 0;
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) {
videoflags = FastestFlags(videoflags, width, height, video_bpp);
} 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], "-debugflip") == 0) {
debug_flip ^= 1;
} 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]);
quit(1);
}
}
Apr 26, 2001
Apr 26, 2001
222
Jul 10, 2006
Jul 10, 2006
223
224
225
226
227
228
229
/* 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());
quit(2);
}
Apr 26, 2001
Apr 26, 2001
230
Jul 10, 2006
Jul 10, 2006
231
232
233
234
/* Load the sprite */
if (LoadSprite("icon.bmp") < 0) {
quit(1);
}
Apr 26, 2001
Apr 26, 2001
235
Jul 10, 2006
Jul 10, 2006
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/* 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");
quit(2);
}
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;
Sep 16, 2010
Sep 16, 2010
250
srand((unsigned int)time(NULL));
Jul 10, 2006
Jul 10, 2006
251
252
253
254
255
256
257
258
259
260
261
262
for (i = 0; i < numsprites; ++i) {
positions[i].x = rand() % (screen->w - sprite_w);
positions[i].y = rand() % (screen->h - sprite_h);
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;
}
}
Nov 27, 2008
Nov 27, 2008
263
264
/* Clear the background to grey */
Nov 26, 2008
Nov 26, 2008
265
background = SDL_MapRGB(screen->format, 0xA0, 0xA0, 0xA0);
Nov 27, 2008
Nov 27, 2008
266
267
SDL_FillRect(screen, NULL, background);
SDL_Flip(screen);
Apr 26, 2001
Apr 26, 2001
268
Jul 10, 2006
Jul 10, 2006
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
297
298
299
/* 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");
}
Apr 26, 2001
Apr 26, 2001
300
Jul 10, 2006
Jul 10, 2006
301
302
303
304
305
306
307
308
309
310
/* 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) {
Feb 5, 2011
Feb 5, 2011
311
312
313
case SDL_VIDEORESIZE:
screen = SDL_SetVideoMode(event.resize.w, event.resize.h, video_bpp, videoflags);
break;
Jul 10, 2006
Jul 10, 2006
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
case SDL_MOUSEBUTTONDOWN:
SDL_WarpMouse(screen->w / 2, screen->h / 2);
break;
case SDL_KEYDOWN:
/* Any keypress quits the app... */
case SDL_QUIT:
done = 1;
break;
default:
break;
}
}
MoveSprites(screen, background);
}
SDL_FreeSurface(sprite);
free(mem);
Apr 26, 2001
Apr 26, 2001
330
Jul 10, 2006
Jul 10, 2006
331
332
333
/* Print out some timing information */
now = SDL_GetTicks();
if (now > then) {
Nov 25, 2008
Nov 25, 2008
334
double fps = ((double) frames * 1000) / (now - then);
Nov 25, 2008
Nov 25, 2008
335
printf("%2.2f frames per second\n", fps);
Jul 10, 2006
Jul 10, 2006
336
337
338
}
SDL_Quit();
return (0);
Apr 26, 2001
Apr 26, 2001
339
}