Add device name of Ecore_Event_Key
[platform/upstream/SDL.git] / test / testdraw2.c
1 /*
2   Copyright (C) 1997-2018 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
13 /* Simple program:  draw as many random objects on the screen as possible */
14
15 #include <stdlib.h>
16 #include <stdio.h>
17 #include <time.h>
18
19 #ifdef __EMSCRIPTEN__
20 #include <emscripten/emscripten.h>
21 #endif
22
23 #include "SDL_test_common.h"
24 #define WINDOW_WIDTH 720
25 #define WINDOW_HEIGHT 1280
26 #define NUM_OBJECTS 100
27
28 static SDLTest_CommonState *state;
29 static int num_objects;
30 static SDL_bool cycle_color;
31 static SDL_bool cycle_alpha;
32 static int cycle_direction = 1;
33 static int current_alpha = 255;
34 static int current_color = 255;
35 static SDL_BlendMode blendMode = SDL_BLENDMODE_NONE;
36
37 int done;
38
39 void
40 DrawPoints(SDL_Renderer * renderer)
41 {
42     int i;
43     int x, y;
44     SDL_Rect viewport;
45
46     /* Query the sizes */
47     SDL_RenderGetViewport(renderer, &viewport);
48
49     for (i = 0; i < num_objects * 4; ++i) {
50         /* Cycle the color and alpha, if desired */
51         if (cycle_color) {
52             current_color += cycle_direction;
53             if (current_color < 0) {
54                 current_color = 0;
55                 cycle_direction = -cycle_direction;
56             }
57             if (current_color > 255) {
58                 current_color = 255;
59                 cycle_direction = -cycle_direction;
60             }
61         }
62         if (cycle_alpha) {
63             current_alpha += cycle_direction;
64             if (current_alpha < 0) {
65                 current_alpha = 0;
66                 cycle_direction = -cycle_direction;
67             }
68             if (current_alpha > 255) {
69                 current_alpha = 255;
70                 cycle_direction = -cycle_direction;
71             }
72         }
73         SDL_SetRenderDrawColor(renderer, 255, (Uint8) current_color,
74                                (Uint8) current_color, (Uint8) current_alpha);
75
76         x = rand() % viewport.w;
77         y = rand() % viewport.h;
78         SDL_RenderDrawPoint(renderer, x, y);
79     }
80 }
81
82 void
83 DrawLines(SDL_Renderer * renderer)
84 {
85     int i;
86     int x1, y1, x2, y2;
87     SDL_Rect viewport;
88
89     /* Query the sizes */
90     SDL_RenderGetViewport(renderer, &viewport);
91
92     for (i = 0; i < num_objects; ++i) {
93         /* Cycle the color and alpha, if desired */
94         if (cycle_color) {
95             current_color += cycle_direction;
96             if (current_color < 0) {
97                 current_color = 0;
98                 cycle_direction = -cycle_direction;
99             }
100             if (current_color > 255) {
101                 current_color = 255;
102                 cycle_direction = -cycle_direction;
103             }
104         }
105         if (cycle_alpha) {
106             current_alpha += cycle_direction;
107             if (current_alpha < 0) {
108                 current_alpha = 0;
109                 cycle_direction = -cycle_direction;
110             }
111             if (current_alpha > 255) {
112                 current_alpha = 255;
113                 cycle_direction = -cycle_direction;
114             }
115         }
116         SDL_SetRenderDrawColor(renderer, 255, (Uint8) current_color,
117                                (Uint8) current_color, (Uint8) current_alpha);
118
119         if (i == 0) {
120             SDL_RenderDrawLine(renderer, 0, 0, viewport.w - 1, viewport.h - 1);
121             SDL_RenderDrawLine(renderer, 0, viewport.h - 1, viewport.w - 1, 0);
122             SDL_RenderDrawLine(renderer, 0, viewport.h / 2, viewport.w - 1, viewport.h / 2);
123             SDL_RenderDrawLine(renderer, viewport.w / 2, 0, viewport.w / 2, viewport.h - 1);
124         } else {
125             x1 = (rand() % (viewport.w*2)) - viewport.w;
126             x2 = (rand() % (viewport.w*2)) - viewport.w;
127             y1 = (rand() % (viewport.h*2)) - viewport.h;
128             y2 = (rand() % (viewport.h*2)) - viewport.h;
129             SDL_RenderDrawLine(renderer, x1, y1, x2, y2);
130         }
131     }
132 }
133
134 void
135 DrawRects(SDL_Renderer * renderer)
136 {
137     int i;
138     SDL_Rect rect;
139     SDL_Rect viewport;
140
141     /* Query the sizes */
142     SDL_RenderGetViewport(renderer, &viewport);
143
144     for (i = 0; i < num_objects / 4; ++i) {
145         /* Cycle the color and alpha, if desired */
146         if (cycle_color) {
147             current_color += cycle_direction;
148             if (current_color < 0) {
149                 current_color = 0;
150                 cycle_direction = -cycle_direction;
151             }
152             if (current_color > 255) {
153                 current_color = 255;
154                 cycle_direction = -cycle_direction;
155             }
156         }
157         if (cycle_alpha) {
158             current_alpha += cycle_direction;
159             if (current_alpha < 0) {
160                 current_alpha = 0;
161                 cycle_direction = -cycle_direction;
162             }
163             if (current_alpha > 255) {
164                 current_alpha = 255;
165                 cycle_direction = -cycle_direction;
166             }
167         }
168         SDL_SetRenderDrawColor(renderer, 255, (Uint8) current_color,
169                                (Uint8) current_color, (Uint8) current_alpha);
170
171         rect.w = rand() % (viewport.h / 2);
172         rect.h = rand() % (viewport.h / 2);
173         rect.x = (rand() % (viewport.w*2) - viewport.w) - (rect.w / 2);
174         rect.y = (rand() % (viewport.h*2) - viewport.h) - (rect.h / 2);
175         SDL_RenderFillRect(renderer, &rect);
176     }
177 }
178
179 void
180 loop()
181 {
182     int i;
183     SDL_Event event;
184
185     /* Check for events */
186     while (SDL_PollEvent(&event)) {
187         SDLTest_CommonEvent(state, &event, &done);
188     }
189     for (i = 0; i < state->num_windows; ++i) {
190         SDL_Renderer *renderer = state->renderers[i];
191         if (state->windows[i] == NULL)
192             continue;
193         SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
194         SDL_RenderClear(renderer);
195
196         DrawRects(renderer);
197         DrawLines(renderer);
198         DrawPoints(renderer);
199
200         SDL_RenderPresent(renderer);
201     }
202 #ifdef __EMSCRIPTEN__
203     if (done) {
204         emscripten_cancel_main_loop();
205     }
206 #endif
207 }
208 int
209 SDL_main(int argc, char *argv[])
210 {
211     int i;
212     Uint32 then, now, frames;
213
214     /* Enable standard application logging */
215     SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
216
217     /* Initialize parameters */
218     num_objects = NUM_OBJECTS;
219
220     /* Initialize test framework */
221     state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
222         state->window_w = WINDOW_WIDTH;
223     state->window_h = WINDOW_HEIGHT;
224     if (!state) {
225         return 1;
226     }
227     for (i = 1; i < argc;) {
228         int consumed;
229
230         consumed = SDLTest_CommonArg(state, i);
231         if (consumed == 0) {
232             consumed = -1;
233             if (SDL_strcasecmp(argv[i], "--blend") == 0) {
234                 if (argv[i + 1]) {
235                     if (SDL_strcasecmp(argv[i + 1], "none") == 0) {
236                         blendMode = SDL_BLENDMODE_NONE;
237                         consumed = 2;
238                     } else if (SDL_strcasecmp(argv[i + 1], "blend") == 0) {
239                         blendMode = SDL_BLENDMODE_BLEND;
240                         consumed = 2;
241                     } else if (SDL_strcasecmp(argv[i + 1], "add") == 0) {
242                         blendMode = SDL_BLENDMODE_ADD;
243                         consumed = 2;
244                     } else if (SDL_strcasecmp(argv[i + 1], "mod") == 0) {
245                         blendMode = SDL_BLENDMODE_MOD;
246                         consumed = 2;
247                     }
248                 }
249             } else if (SDL_strcasecmp(argv[i], "--cyclecolor") == 0) {
250                 cycle_color = SDL_TRUE;
251                 consumed = 1;
252             } else if (SDL_strcasecmp(argv[i], "--cyclealpha") == 0) {
253                 cycle_alpha = SDL_TRUE;
254                 consumed = 1;
255             } else if (SDL_isdigit(*argv[i])) {
256                 num_objects = SDL_atoi(argv[i]);
257                 consumed = 1;
258             }
259         }
260         if (consumed < 0) {
261             SDL_Log("Usage: %s %s [--blend none|blend|add|mod] [--cyclecolor] [--cyclealpha]\n",
262                     argv[0], SDLTest_CommonUsage(state));
263             return 1;
264         }
265         i += consumed;
266     }
267     if (!SDLTest_CommonInit(state)) {
268         return 2;
269     }
270
271     /* Create the windows and initialize the renderers */
272     for (i = 0; i < state->num_windows; ++i) {
273         SDL_Renderer *renderer = state->renderers[i];
274         SDL_SetRenderDrawBlendMode(renderer, blendMode);
275         SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
276         SDL_RenderClear(renderer);
277     }
278
279     srand((unsigned int)time(NULL));
280
281     /* Main render loop */
282     frames = 0;
283     then = SDL_GetTicks();
284     done = 0;
285
286 #ifdef __EMSCRIPTEN__
287     emscripten_set_main_loop(loop, 0, 1);
288 #else
289     while (!done) {
290         ++frames;
291         loop();
292                 //SDL_Delay(10000);  /* let it play for awhile. */
293                 //done = 1;
294         }
295 #endif
296
297
298     SDLTest_CommonQuit(state);
299
300     /* Print out some timing information */
301     now = SDL_GetTicks();
302     if (now > then) {
303         double fps = ((double) frames * 1000) / (now - then);
304         SDL_Log("%2.2f frames per second\n", fps);
305     }
306     return 0;
307 }
308
309 /* vi: set ts=4 sw=4 expandtab: */