comparison test/testsprite2.c @ 1895:c121d94672cb

SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
author Sam Lantinga <slouken@libsdl.org>
date Mon, 10 Jul 2006 21:04:37 +0000
parents
children f132024010be
comparison
equal deleted inserted replaced
1894:c69cee13dd76 1895:c121d94672cb
1 /* Simple program: Move N sprites around on the screen as fast as possible */
2
3 #include <stdlib.h>
4 #include <time.h>
5
6 #include "SDL.h"
7
8 #define NUM_WINDOWS 4
9 #define WINDOW_W 640
10 #define WINDOW_H 480
11 #define NUM_SPRITES 100
12 #define MAX_SPEED 1
13 #define BACKGROUND 0x00FFFFFF
14
15 static int num_windows;
16 static int num_sprites;
17 static SDL_WindowID *windows;
18 static SDL_TextureID *sprites;
19 static SDL_Rect *positions;
20 static SDL_Rect *velocities;
21 static int sprite_w, sprite_h;
22
23 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
24 static void
25 quit(int rc)
26 {
27 if (windows) {
28 SDL_free(windows);
29 }
30 if (sprites) {
31 SDL_free(sprites);
32 }
33 if (positions) {
34 SDL_free(positions);
35 }
36 if (velocities) {
37 SDL_free(velocities);
38 }
39 SDL_Quit();
40 exit(rc);
41 }
42
43 int
44 LoadSprite(char *file)
45 {
46 int i;
47 SDL_Surface *temp;
48
49 /* Load the sprite image */
50 temp = SDL_LoadBMP(file);
51 if (temp == NULL) {
52 fprintf(stderr, "Couldn't load %s: %s", file, SDL_GetError());
53 return (-1);
54 }
55 sprite_w = temp->w;
56 sprite_h = temp->h;
57
58 /* Set transparent pixel as the pixel at (0,0) */
59 if (temp->format->palette) {
60 SDL_SetColorKey(temp, SDL_SRCCOLORKEY, *(Uint8 *) temp->pixels);
61 }
62
63 /* Create textures from the image */
64 for (i = 0; i < num_windows; ++i) {
65 SDL_SelectRenderer(windows[i]);
66 sprites[i] =
67 SDL_CreateTextureFromSurface(0, SDL_TextureAccess_Remote, temp);
68 if (!sprites[i]) {
69 fprintf(stderr, "Couldn't create texture: %s\n", SDL_GetError());
70 SDL_FreeSurface(temp);
71 return (-1);
72 }
73 }
74 SDL_FreeSurface(temp);
75
76 /* We're ready to roll. :) */
77 return (0);
78 }
79
80 void
81 MoveSprites(SDL_WindowID window, SDL_TextureID sprite)
82 {
83 int i, n;
84 int window_w, window_h;
85 SDL_Rect area, *position, *velocity;
86
87 SDL_SelectRenderer(window);
88
89 /* Query the sizes */
90 SDL_GetWindowSize(window, &window_w, &window_h);
91
92 /* Move the sprite, bounce at the wall, and draw */
93 n = 0;
94 for (i = 0; i < num_sprites; ++i) {
95 position = &positions[i];
96 SDL_RenderFill(position, BACKGROUND);
97 }
98 for (i = 0; i < num_sprites; ++i) {
99 position = &positions[i];
100 velocity = &velocities[i];
101 position->x += velocity->x;
102 if ((position->x < 0) || (position->x >= (window_w - sprite_w))) {
103 velocity->x = -velocity->x;
104 position->x += velocity->x;
105 }
106 position->y += velocity->y;
107 if ((position->y < 0) || (position->y >= (window_h - sprite_w))) {
108 velocity->y = -velocity->y;
109 position->y += velocity->y;
110 }
111
112 /* Blit the sprite onto the screen */
113 SDL_RenderCopy(sprite, NULL, position, SDL_TextureBlendMode_Mask,
114 SDL_TextureScaleMode_None);
115 }
116
117 /* Update the screen! */
118 SDL_RenderPresent();
119 }
120
121 int
122 main(int argc, char *argv[])
123 {
124 int window_w, window_h;
125 Uint32 window_flags = SDL_WINDOW_SHOWN;
126 SDL_DisplayMode *mode, fullscreen_mode;
127 int i, done;
128 SDL_Event event;
129 Uint32 then, now, frames;
130
131 /* Initialize SDL */
132 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
133 fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
134 return (1);
135 }
136
137 num_windows = NUM_WINDOWS;
138 num_sprites = NUM_SPRITES;
139 window_w = WINDOW_W;
140 window_h = WINDOW_H;
141 while (argc > 1) {
142 if (strcmp(argv[argc - 1], "-width") == 0) {
143 window_w = atoi(argv[argc]);
144 --argc;
145 } else if (strcmp(argv[argc - 1], "-height") == 0) {
146 window_h = atoi(argv[argc]);
147 --argc;
148 } else if (strcmp(argv[argc - 1], "-fullscreen") == 0) {
149 num_windows = 1;
150 window_flags |= SDL_WINDOW_FULLSCREEN;
151 --argc;
152 } else if (isdigit(argv[argc][0])) {
153 num_sprites = atoi(argv[argc]);
154 } else {
155 fprintf(stderr,
156 "Usage: %s [-width] [-height] [numsprites]\n", argv[0]);
157 quit(1);
158 }
159 }
160
161 if (window_flags & SDL_WINDOW_FULLSCREEN) {
162 SDL_zero(fullscreen_mode);
163 fullscreen_mode.w = window_w;
164 fullscreen_mode.h = window_h;
165 SDL_SetFullscreenDisplayMode(&fullscreen_mode);
166 }
167
168 /* Create the windows, initialize the renderers, and load the textures */
169 windows = (SDL_WindowID *) SDL_malloc(num_windows * sizeof(*windows));
170 sprites = (SDL_TextureID *) SDL_malloc(num_windows * sizeof(*sprites));
171 if (!windows || !sprites) {
172 fprintf(stderr, "Out of memory!\n");
173 quit(2);
174 }
175 for (i = 0; i < num_windows; ++i) {
176 char title[32];
177
178 SDL_snprintf(title, sizeof(title), "testsprite %d", i + 1);
179 windows[i] =
180 SDL_CreateWindow(title, SDL_WINDOWPOS_UNDEFINED,
181 SDL_WINDOWPOS_UNDEFINED, window_w, window_h,
182 window_flags);
183 if (!windows[i]) {
184 fprintf(stderr, "Couldn't create window: %s\n", SDL_GetError());
185 quit(2);
186 }
187
188 if (SDL_CreateRenderer(windows[i], -1, 0) < 0) {
189 fprintf(stderr, "Couldn't create renderer: %s\n", SDL_GetError());
190 quit(2);
191 }
192 SDL_RenderFill(NULL, BACKGROUND);
193 }
194 if (LoadSprite("icon.bmp") < 0) {
195 quit(2);
196 }
197
198 /* Allocate memory for the sprite info */
199 positions = (SDL_Rect *) SDL_malloc(num_sprites * sizeof(SDL_Rect));
200 velocities = (SDL_Rect *) SDL_malloc(num_sprites * sizeof(SDL_Rect));
201 if (!positions || !velocities) {
202 fprintf(stderr, "Out of memory!\n");
203 quit(2);
204 }
205 srand(time(NULL));
206 for (i = 0; i < num_sprites; ++i) {
207 positions[i].x = rand() % (window_w - sprite_w);
208 positions[i].y = rand() % (window_h - sprite_h);
209 positions[i].w = sprite_w;
210 positions[i].h = sprite_h;
211 velocities[i].x = 0;
212 velocities[i].y = 0;
213 while (!velocities[i].x && !velocities[i].y) {
214 velocities[i].x = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
215 velocities[i].y = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
216 }
217 }
218
219 /* Loop, blitting sprites and waiting for a keystroke */
220 frames = 0;
221 then = SDL_GetTicks();
222 done = 0;
223 while (!done) {
224 /* Check for events */
225 ++frames;
226 while (SDL_PollEvent(&event)) {
227 switch (event.type) {
228 case SDL_WINDOWEVENT:
229 switch (event.window.event) {
230 case SDL_WINDOWEVENT_EXPOSED:
231 SDL_SelectRenderer(event.window.windowID);
232 SDL_RenderFill(NULL, BACKGROUND);
233 break;
234 case SDL_WINDOWEVENT_CLOSE:
235 done = 1;
236 break;
237 }
238 break;
239 case SDL_KEYDOWN:
240 /* Any keypress quits the app... */
241 case SDL_QUIT:
242 done = 1;
243 break;
244 default:
245 break;
246 }
247 }
248 for (i = 0; i < num_windows; ++i) {
249 MoveSprites(windows[i], sprites[i]);
250 }
251 }
252
253 /* Print out some timing information */
254 now = SDL_GetTicks();
255 if (now > then) {
256 printf("%2.2f frames per second\n",
257 ((double) frames * 1000) / (now - then));
258 }
259 quit(0);
260 }
261
262 /* vi: set ts=4 sw=4 expandtab: */