[Tizen] Support Autoconf 2.71
[platform/upstream/SDL.git] / test / testsprite2.c
1 /*
2   Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
3
4   This software is provided 'as-is', without any express or implied
5   warranty.  In no event will the authors be held liable for any damages
6   arising from the use of this software.
7
8   Permission is granted to anyone to use this software for any purpose,
9   including commercial applications, and to alter it and redistribute it
10   freely.
11 */
12 /* Simple program:  Move N sprites around on the screen as fast as possible */
13
14 #include <stdlib.h>
15 #include <stdio.h>
16 #include <time.h>
17
18 #ifdef __EMSCRIPTEN__
19 #include <emscripten/emscripten.h>
20 #endif
21
22 #include "SDL_test.h"
23 #include "SDL_test_common.h"
24 #define WINDOW_WIDTH 720
25 #define WINDOW_HEIGHT 1280
26 #define NUM_SPRITES    100
27 #define MAX_SPEED     1
28
29 static SDLTest_CommonState *state;
30 static int num_sprites;
31 static SDL_Texture **sprites;
32 static SDL_bool cycle_color;
33 static SDL_bool cycle_alpha;
34 static int cycle_direction = 1;
35 static int current_alpha = 0;
36 static int current_color = 0;
37 static SDL_Rect *positions;
38 static SDL_Rect *velocities;
39 static int sprite_w, sprite_h;
40 static SDL_BlendMode blendMode = SDL_BLENDMODE_BLEND;
41 static Uint32 next_fps_check, frames;
42 static const Uint32 fps_check_delay = 5000;
43
44 /* Number of iterations to move sprites - used for visual tests. */
45 /* -1: infinite random moves (default); >=0: enables N deterministic moves */
46 static int iterations = -1;
47
48 int done;
49
50 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
51 static void
52 quit(int rc)
53 {
54     SDL_free(sprites);
55     SDL_free(positions);
56     SDL_free(velocities);
57     SDLTest_CommonQuit(state);
58     exit(rc);
59 }
60
61 int
62 LoadSprite(const char *file)
63 {
64     int i;
65     SDL_Surface *temp;
66
67     /* Load the sprite image */
68     temp = SDL_LoadBMP(file);
69     if (temp == NULL) {
70         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s", file, SDL_GetError());
71         return (-1);
72     }
73     sprite_w = temp->w;
74     sprite_h = temp->h;
75
76     /* Set transparent pixel as the pixel at (0,0) */
77     if (temp->format->palette) {
78         SDL_SetColorKey(temp, 1, *(Uint8 *) temp->pixels);
79     } else {
80         switch (temp->format->BitsPerPixel) {
81         case 15:
82             SDL_SetColorKey(temp, 1, (*(Uint16 *) temp->pixels) & 0x00007FFF);
83             break;
84         case 16:
85             SDL_SetColorKey(temp, 1, *(Uint16 *) temp->pixels);
86             break;
87         case 24:
88             SDL_SetColorKey(temp, 1, (*(Uint32 *) temp->pixels) & 0x00FFFFFF);
89             break;
90         case 32:
91             SDL_SetColorKey(temp, 1, *(Uint32 *) temp->pixels);
92             break;
93         }
94     }
95
96     /* Create textures from the image */
97     for (i = 0; i < state->num_windows; ++i) {
98         SDL_Renderer *renderer = state->renderers[i];
99         sprites[i] = SDL_CreateTextureFromSurface(renderer, temp);
100         if (!sprites[i]) {
101             SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create texture: %s\n", SDL_GetError());
102             SDL_FreeSurface(temp);
103             return (-1);
104         }
105         if (SDL_SetTextureBlendMode(sprites[i], blendMode) < 0) {
106             SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set blend mode: %s\n", SDL_GetError());
107             SDL_FreeSurface(temp);
108             SDL_DestroyTexture(sprites[i]);
109             return (-1);
110         }
111     }
112     SDL_FreeSurface(temp);
113
114     /* We're ready to roll. :) */
115     return (0);
116 }
117
118 void
119 MoveSprites(SDL_Renderer * renderer, SDL_Texture * sprite)
120 {
121     int i;
122     SDL_Rect viewport, temp;
123     SDL_Rect *position, *velocity;
124
125     /* Query the sizes */
126     SDL_RenderGetViewport(renderer, &viewport);
127
128     /* Cycle the color and alpha, if desired */
129     if (cycle_color) {
130         current_color += cycle_direction;
131         if (current_color < 0) {
132             current_color = 0;
133             cycle_direction = -cycle_direction;
134         }
135         if (current_color > 255) {
136             current_color = 255;
137             cycle_direction = -cycle_direction;
138         }
139         SDL_SetTextureColorMod(sprite, 255, (Uint8) current_color,
140                                (Uint8) current_color);
141     }
142     if (cycle_alpha) {
143         current_alpha += cycle_direction;
144         if (current_alpha < 0) {
145             current_alpha = 0;
146             cycle_direction = -cycle_direction;
147         }
148         if (current_alpha > 255) {
149             current_alpha = 255;
150             cycle_direction = -cycle_direction;
151         }
152         SDL_SetTextureAlphaMod(sprite, (Uint8) current_alpha);
153     }
154
155     /* Draw a gray background */
156     SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
157     SDL_RenderClear(renderer);
158
159     /* Test points */
160     SDL_SetRenderDrawColor(renderer, 0xFF, 0x00, 0x00, 0xFF);
161     SDL_RenderDrawPoint(renderer, 0, 0);
162     SDL_RenderDrawPoint(renderer, viewport.w-1, 0);
163     SDL_RenderDrawPoint(renderer, 0, viewport.h-1);
164     SDL_RenderDrawPoint(renderer, viewport.w-1, viewport.h-1);
165
166     /* Test horizontal and vertical lines */
167     SDL_SetRenderDrawColor(renderer, 0x00, 0xFF, 0x00, 0xFF);
168     SDL_RenderDrawLine(renderer, 1, 0, viewport.w-2, 0);
169     SDL_RenderDrawLine(renderer, 1, viewport.h-1, viewport.w-2, viewport.h-1);
170     SDL_RenderDrawLine(renderer, 0, 1, 0, viewport.h-2);
171     SDL_RenderDrawLine(renderer, viewport.w-1, 1, viewport.w-1, viewport.h-2);
172
173
174     /* Test fill and copy */
175     SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
176     temp.x = 1;
177     temp.y = 1;
178     temp.w = sprite_w;
179     temp.h = sprite_h;
180     SDL_RenderFillRect(renderer, &temp);
181     SDL_RenderCopy(renderer, sprite, NULL, &temp);
182     temp.x = viewport.w-sprite_w-1;
183     temp.y = 1;
184     temp.w = sprite_w;
185     temp.h = sprite_h;
186     SDL_RenderFillRect(renderer, &temp);
187     SDL_RenderCopy(renderer, sprite, NULL, &temp);
188     temp.x = 1;
189     temp.y = viewport.h-sprite_h-1;
190     temp.w = sprite_w;
191     temp.h = sprite_h;
192     SDL_RenderFillRect(renderer, &temp);
193     SDL_RenderCopy(renderer, sprite, NULL, &temp);
194     temp.x = viewport.w-sprite_w-1;
195     temp.y = viewport.h-sprite_h-1;
196     temp.w = sprite_w;
197     temp.h = sprite_h;
198     SDL_RenderFillRect(renderer, &temp);
199     SDL_RenderCopy(renderer, sprite, NULL, &temp);
200
201     /* Test diagonal lines */
202     SDL_SetRenderDrawColor(renderer, 0x00, 0xFF, 0x00, 0xFF);
203     SDL_RenderDrawLine(renderer, sprite_w, sprite_h,
204                        viewport.w-sprite_w-2, viewport.h-sprite_h-2);
205     SDL_RenderDrawLine(renderer, viewport.w-sprite_w-2, sprite_h,
206                        sprite_w, viewport.h-sprite_h-2);
207
208     /* Conditionally move the sprites, bounce at the wall */
209     if (iterations == -1 || iterations > 0) {
210         for (i = 0; i < num_sprites; ++i) {
211             position = &positions[i];
212             velocity = &velocities[i];
213             position->x += velocity->x;
214             if ((position->x < 0) || (position->x >= (viewport.w - sprite_w))) {
215                 velocity->x = -velocity->x;
216                 position->x += velocity->x;
217             }
218             position->y += velocity->y;
219             if ((position->y < 0) || (position->y >= (viewport.h - sprite_h))) {
220                 velocity->y = -velocity->y;
221                 position->y += velocity->y;
222             }
223
224         }
225         
226         /* Countdown sprite-move iterations and disable color changes at iteration end - used for visual tests. */
227         if (iterations > 0) {
228             iterations--;
229             if (iterations == 0) {
230                 cycle_alpha = SDL_FALSE;
231                 cycle_color = SDL_FALSE;
232             }
233         }
234     }
235
236     /* Draw sprites */
237     for (i = 0; i < num_sprites; ++i) {
238         position = &positions[i];
239
240         /* Blit the sprite onto the screen */
241         SDL_RenderCopy(renderer, sprite, NULL, position);
242     }
243
244     /* Update the screen! */
245     SDL_RenderPresent(renderer);
246 }
247
248 void
249 loop()
250 {
251     Uint32 now;
252     int i;
253     SDL_Event event;
254
255     /* Check for events */
256     while (SDL_PollEvent(&event)) {
257         SDLTest_CommonEvent(state, &event, &done);
258                 switch(event.type) {
259                 case SDL_KEYDOWN:
260                         if(event.key.keysym.sym == 0)
261                         {
262                                 done = 1;
263                         }
264                         break;
265         }
266     }
267     for (i = 0; i < state->num_windows; ++i) {
268         if (state->windows[i] == NULL)
269             continue;
270         MoveSprites(state->renderers[i], sprites[i]);
271     }
272 #ifdef __EMSCRIPTEN__
273     if (done) {
274         emscripten_cancel_main_loop();
275     }
276 #endif
277
278     frames++;
279     now = SDL_GetTicks();
280     if (SDL_TICKS_PASSED(now, next_fps_check)) {
281         /* Print out some timing information */
282         const Uint32 then = next_fps_check - fps_check_delay;
283         const double fps = ((double) frames * 1000) / (now - then);
284         SDL_Log("%2.2f frames per second\n", fps);
285         next_fps_check = now + fps_check_delay;
286         frames = 0;
287     }
288
289 }
290 int
291 main(int argc, char *argv[])
292 {
293     int i;
294     Uint64 seed;
295     const char *icon = "res/icon.bmp";
296
297     /* Initialize parameters */
298     num_sprites = NUM_SPRITES;
299
300     /* Initialize test framework */
301     state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
302         state->window_w = WINDOW_WIDTH;
303     state->window_h = WINDOW_HEIGHT;
304     if (!state) {
305         return 1;
306     }
307
308     for (i = 1; i < argc;) {
309         int consumed;
310
311         consumed = SDLTest_CommonArg(state, i);
312         if (consumed == 0) {
313             consumed = -1;
314             if (SDL_strcasecmp(argv[i], "--blend") == 0) {
315                 if (argv[i + 1]) {
316                     if (SDL_strcasecmp(argv[i + 1], "none") == 0) {
317                         blendMode = SDL_BLENDMODE_NONE;
318                         consumed = 2;
319                     } else if (SDL_strcasecmp(argv[i + 1], "blend") == 0) {
320                         blendMode = SDL_BLENDMODE_BLEND;
321                         consumed = 2;
322                     } else if (SDL_strcasecmp(argv[i + 1], "add") == 0) {
323                         blendMode = SDL_BLENDMODE_ADD;
324                         consumed = 2;
325                     } else if (SDL_strcasecmp(argv[i + 1], "mod") == 0) {
326                         blendMode = SDL_BLENDMODE_MOD;
327                         consumed = 2;
328                     } else if (SDL_strcasecmp(argv[i + 1], "sub") == 0) {
329                         blendMode = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_SRC_ALPHA, SDL_BLENDFACTOR_ONE, SDL_BLENDOPERATION_SUBTRACT, SDL_BLENDFACTOR_ZERO, SDL_BLENDFACTOR_ONE, SDL_BLENDOPERATION_SUBTRACT);
330                         consumed = 2;
331                     }
332                 }
333             } else if (SDL_strcasecmp(argv[i], "--iterations") == 0) {
334                 if (argv[i + 1]) {
335                     iterations = SDL_atoi(argv[i + 1]);
336                     if (iterations < -1) iterations = -1;
337                     consumed = 2;
338                 }
339             } else if (SDL_strcasecmp(argv[i], "--cyclecolor") == 0) {
340                 cycle_color = SDL_TRUE;
341                 consumed = 1;
342             } else if (SDL_strcasecmp(argv[i], "--cyclealpha") == 0) {
343                 cycle_alpha = SDL_TRUE;
344                 consumed = 1;
345             } else if (SDL_isdigit(*argv[i])) {
346                 num_sprites = SDL_atoi(argv[i]);
347                 consumed = 1;
348             } else if (argv[i][0] != '-') {
349                 icon = argv[i];
350                 consumed = 1;
351             }
352         }
353         if (consumed < 0) {
354             static const char *options[] = { "[--blend none|blend|add|mod]", "[--cyclecolor]", "[--cyclealpha]", "[--iterations N]", "[num_sprites]", "[icon.bmp]", NULL };
355             SDLTest_CommonLogUsage(state, argv[0], options);
356             quit(1);
357         }
358         i += consumed;
359     }
360     if (!SDLTest_CommonInit(state)) {
361         quit(2);
362     }
363
364     /* Create the windows, initialize the renderers, and load the textures */
365     sprites =
366         (SDL_Texture **) SDL_malloc(state->num_windows * sizeof(*sprites));
367     if (!sprites) {
368         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Out of memory!\n");
369         quit(2);
370     }
371     for (i = 0; i < state->num_windows; ++i) {
372         SDL_Renderer *renderer = state->renderers[i];
373         SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
374         SDL_RenderClear(renderer);
375     }
376     if (LoadSprite(icon) < 0) {
377         quit(2);
378     }
379
380
381
382
383
384     /* Allocate memory for the sprite info */
385     positions = (SDL_Rect *) SDL_malloc(num_sprites * sizeof(SDL_Rect));
386     velocities = (SDL_Rect *) SDL_malloc(num_sprites * sizeof(SDL_Rect));
387     if (!positions || !velocities) {
388         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Out of memory!\n");
389         quit(2);
390     }
391
392     /* Position sprites and set their velocities using the fuzzer */ 
393     if (iterations >= 0) {
394         /* Deterministic seed - used for visual tests */
395         seed = (Uint64)iterations;
396     } else {
397         /* Pseudo-random seed generated from the time */
398         seed = (Uint64)time(NULL);
399     }
400     SDLTest_FuzzerInit(seed);
401     for (i = 0; i < num_sprites; ++i) {
402         positions[i].x = SDLTest_RandomIntegerInRange(0, state->window_w - sprite_w);
403         positions[i].y = SDLTest_RandomIntegerInRange(0, state->window_h - sprite_h);
404         positions[i].w = sprite_w;
405         positions[i].h = sprite_h;
406         velocities[i].x = 0;
407         velocities[i].y = 0;
408         while (!velocities[i].x && !velocities[i].y) {
409             velocities[i].x = SDLTest_RandomIntegerInRange(-MAX_SPEED, MAX_SPEED);
410             velocities[i].y = SDLTest_RandomIntegerInRange(-MAX_SPEED, MAX_SPEED);
411         }
412     }
413
414     /* Main render loop */
415     frames = 0;
416     next_fps_check = SDL_GetTicks() + fps_check_delay;
417     done = 0;
418
419 #ifdef __EMSCRIPTEN__
420     emscripten_set_main_loop(loop, 0, 1);
421 #else
422     while (!done) {
423         loop();
424     }
425 #endif
426
427     quit(0);
428     return 0;
429 }
430
431 /* vi: set ts=4 sw=4 expandtab: */