Resync tip to default. Using named branches is a bad idea.
1 /* Simple program: Move N sprites around on the screen as fast as possible */
9 #define NUM_SPRITES 100
12 static CommonState *state;
13 static int num_sprites;
14 static SDL_Texture **sprites;
15 static SDL_bool cycle_color;
16 static SDL_bool cycle_alpha;
17 static int cycle_direction = 1;
18 static int current_alpha = 0;
19 static int current_color = 0;
20 static SDL_Rect *positions;
21 static SDL_Rect *velocities;
22 static int sprite_w, sprite_h;
23 static SDL_BlendMode blendMode = SDL_BLENDMODE_MASK;
24 static SDL_TextureScaleMode scaleMode = SDL_TEXTURESCALEMODE_NONE;
26 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
44 LoadSprite(char *file)
49 /* Load the sprite image */
50 temp = SDL_LoadBMP(file);
52 fprintf(stderr, "Couldn't load %s: %s", file, SDL_GetError());
58 /* Set transparent pixel as the pixel at (0,0) */
59 if (temp->format->palette) {
60 SDL_SetColorKey(temp, 1, *(Uint8 *) temp->pixels);
62 switch (temp->format->BitsPerPixel) {
64 SDL_SetColorKey(temp, 1, (*(Uint16 *) temp->pixels) & 0x00007FFF);
67 SDL_SetColorKey(temp, 1, *(Uint16 *) temp->pixels);
70 SDL_SetColorKey(temp, 1, (*(Uint32 *) temp->pixels) & 0x00FFFFFF);
73 SDL_SetColorKey(temp, 1, *(Uint32 *) temp->pixels);
78 /* Create textures from the image */
79 for (i = 0; i < state->num_windows; ++i) {
80 SDL_SelectRenderer(state->windows[i]);
81 sprites[i] = SDL_CreateTextureFromSurface(0, temp);
83 SDL_SetColorKey(temp, 0, 0);
84 sprites[i] = SDL_CreateTextureFromSurface(0, temp);
87 fprintf(stderr, "Couldn't create texture: %s\n", SDL_GetError());
88 SDL_FreeSurface(temp);
91 SDL_SetTextureBlendMode(sprites[i], blendMode);
92 SDL_SetTextureScaleMode(sprites[i], scaleMode);
94 SDL_FreeSurface(temp);
96 /* We're ready to roll. :) */
101 MoveSprites(SDL_Window * window, SDL_Texture * sprite)
104 int window_w, window_h;
106 SDL_Rect *position, *velocity;
108 SDL_SelectRenderer(window);
110 /* Query the sizes */
111 SDL_GetWindowSize(window, &window_w, &window_h);
113 /* Cycle the color and alpha, if desired */
115 current_color += cycle_direction;
116 if (current_color < 0) {
118 cycle_direction = -cycle_direction;
120 if (current_color > 255) {
122 cycle_direction = -cycle_direction;
124 SDL_SetTextureColorMod(sprite, 255, (Uint8) current_color,
125 (Uint8) current_color);
128 current_alpha += cycle_direction;
129 if (current_alpha < 0) {
131 cycle_direction = -cycle_direction;
133 if (current_alpha > 255) {
135 cycle_direction = -cycle_direction;
137 SDL_SetTextureAlphaMod(sprite, (Uint8) current_alpha);
140 /* Draw a gray background */
141 SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
145 SDL_SetRenderDrawColor(0xFF, 0x00, 0x00, 0xFF);
146 SDL_RenderDrawPoint(0, 0);
147 SDL_RenderDrawPoint(window_w-1, 0);
148 SDL_RenderDrawPoint(0, window_h-1);
149 SDL_RenderDrawPoint(window_w-1, window_h-1);
151 /* Test horizontal and vertical lines */
152 SDL_SetRenderDrawColor(0x00, 0xFF, 0x00, 0xFF);
153 SDL_RenderDrawLine(1, 0, window_w-2, 0);
154 SDL_RenderDrawLine(1, window_h-1, window_w-2, window_h-1);
155 SDL_RenderDrawLine(0, 1, 0, window_h-2);
156 SDL_RenderDrawLine(window_w-1, 1, window_w-1, window_h-2);
158 /* Test fill and copy */
159 SDL_SetRenderDrawColor(0xFF, 0xFF, 0xFF, 0xFF);
164 SDL_RenderFillRect(&temp);
165 SDL_RenderCopy(sprite, NULL, &temp);
166 temp.x = window_w-sprite_w-1;
170 SDL_RenderFillRect(&temp);
171 SDL_RenderCopy(sprite, NULL, &temp);
173 temp.y = window_h-sprite_h-1;
176 SDL_RenderFillRect(&temp);
177 SDL_RenderCopy(sprite, NULL, &temp);
178 temp.x = window_w-sprite_w-1;
179 temp.y = window_h-sprite_h-1;
182 SDL_RenderFillRect(&temp);
183 SDL_RenderCopy(sprite, NULL, &temp);
185 /* Test diagonal lines */
186 SDL_SetRenderDrawColor(0x00, 0xFF, 0x00, 0xFF);
187 SDL_RenderDrawLine(sprite_w, sprite_h,
188 window_w-sprite_w-2, window_h-sprite_h-2);
189 SDL_RenderDrawLine(window_w-sprite_w-2, sprite_h,
190 sprite_w, window_h-sprite_h-2);
192 /* Move the sprite, bounce at the wall, and draw */
194 for (i = 0; i < num_sprites; ++i) {
195 position = &positions[i];
196 velocity = &velocities[i];
197 position->x += velocity->x;
198 if ((position->x < 0) || (position->x >= (window_w - sprite_w))) {
199 velocity->x = -velocity->x;
200 position->x += velocity->x;
202 position->y += velocity->y;
203 if ((position->y < 0) || (position->y >= (window_h - sprite_h))) {
204 velocity->y = -velocity->y;
205 position->y += velocity->y;
208 /* Blit the sprite onto the screen */
209 SDL_RenderCopy(sprite, NULL, position);
212 /* Update the screen! */
217 main(int argc, char *argv[])
221 Uint32 then, now, frames;
223 /* Initialize parameters */
224 num_sprites = NUM_SPRITES;
226 /* Initialize test framework */
227 state = CommonCreateState(argv, SDL_INIT_VIDEO);
231 for (i = 1; i < argc;) {
234 consumed = CommonArg(state, i);
237 if (SDL_strcasecmp(argv[i], "--blend") == 0) {
239 if (SDL_strcasecmp(argv[i + 1], "none") == 0) {
240 blendMode = SDL_BLENDMODE_NONE;
242 } else if (SDL_strcasecmp(argv[i + 1], "mask") == 0) {
243 blendMode = SDL_BLENDMODE_MASK;
245 } else if (SDL_strcasecmp(argv[i + 1], "blend") == 0) {
246 blendMode = SDL_BLENDMODE_BLEND;
248 } else if (SDL_strcasecmp(argv[i + 1], "add") == 0) {
249 blendMode = SDL_BLENDMODE_ADD;
251 } else if (SDL_strcasecmp(argv[i + 1], "mod") == 0) {
252 blendMode = SDL_BLENDMODE_MOD;
256 } else if (SDL_strcasecmp(argv[i], "--scale") == 0) {
258 if (SDL_strcasecmp(argv[i + 1], "none") == 0) {
259 scaleMode = SDL_TEXTURESCALEMODE_NONE;
261 } else if (SDL_strcasecmp(argv[i + 1], "fast") == 0) {
262 scaleMode = SDL_TEXTURESCALEMODE_FAST;
264 } else if (SDL_strcasecmp(argv[i + 1], "slow") == 0) {
265 scaleMode = SDL_TEXTURESCALEMODE_SLOW;
267 } else if (SDL_strcasecmp(argv[i + 1], "best") == 0) {
268 scaleMode = SDL_TEXTURESCALEMODE_BEST;
272 } else if (SDL_strcasecmp(argv[i], "--cyclecolor") == 0) {
273 cycle_color = SDL_TRUE;
275 } else if (SDL_strcasecmp(argv[i], "--cyclealpha") == 0) {
276 cycle_alpha = SDL_TRUE;
278 } else if (SDL_isdigit(*argv[i])) {
279 num_sprites = SDL_atoi(argv[i]);
285 "Usage: %s %s [--blend none|mask|blend|add|mod] [--scale none|fast|slow|best] [--cyclecolor] [--cyclealpha]\n",
286 argv[0], CommonUsage(state));
291 if (!CommonInit(state)) {
295 /* Create the windows, initialize the renderers, and load the textures */
297 (SDL_Texture **) SDL_malloc(state->num_windows * sizeof(*sprites));
299 fprintf(stderr, "Out of memory!\n");
302 for (i = 0; i < state->num_windows; ++i) {
303 SDL_SelectRenderer(state->windows[i]);
304 SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
307 if (LoadSprite("icon.bmp") < 0) {
311 /* Allocate memory for the sprite info */
312 positions = (SDL_Rect *) SDL_malloc(num_sprites * sizeof(SDL_Rect));
313 velocities = (SDL_Rect *) SDL_malloc(num_sprites * sizeof(SDL_Rect));
314 if (!positions || !velocities) {
315 fprintf(stderr, "Out of memory!\n");
319 if (scaleMode != SDL_TEXTURESCALEMODE_NONE) {
320 sprite_w += sprite_w / 2;
321 sprite_h += sprite_h / 2;
323 for (i = 0; i < num_sprites; ++i) {
324 positions[i].x = rand() % (state->window_w - sprite_w);
325 positions[i].y = rand() % (state->window_h - sprite_h);
326 positions[i].w = sprite_w;
327 positions[i].h = sprite_h;
330 while (!velocities[i].x && !velocities[i].y) {
331 velocities[i].x = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
332 velocities[i].y = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
336 /* Main render loop */
338 then = SDL_GetTicks();
341 /* Check for events */
343 while (SDL_PollEvent(&event)) {
344 CommonEvent(state, &event, &done);
345 switch (event.type) {
346 case SDL_WINDOWEVENT:
347 switch (event.window.event) {
348 case SDL_WINDOWEVENT_EXPOSED:
349 SDL_SelectRenderer(SDL_GetWindowFromID(event.window.windowID));
350 SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
359 for (i = 0; i < state->num_windows; ++i) {
360 MoveSprites(state->windows[i], sprites[i]);
364 /* Print out some timing information */
365 now = SDL_GetTicks();
367 double fps = ((double) frames * 1000) / (now - then);
368 printf("%2.2f frames per second\n", fps);
373 /* vi: set ts=4 sw=4 expandtab: */