Debian Codegraph @

test/testdraw2.c

raw
1 /*
2 Copyright (C) 1997-2026 Sam Lantinga <[email protected]>
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
25 #define NUM_OBJECTS 100
26
27 static SDLTest_CommonState *state;
28 static int num_objects;
29 static SDL_bool cycle_color;
30 static SDL_bool cycle_alpha;
31 static int cycle_direction = 1;
32 static int current_alpha = 255;
33 static int current_color = 255;
34 static SDL_BlendMode blendMode = SDL_BLENDMODE_NONE;
35 static Uint32 next_fps_check, frames;
36 static const Uint32 fps_check_delay = 5000;
37
38 int done;
39
40 void 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 DrawLines(SDL_Renderer *renderer)
83 {
84 int i;
85 int x1, y1, x2, y2;
86 SDL_Rect viewport;
87
88 /* Query the sizes */
89 SDL_RenderGetViewport(renderer, &viewport);
90
91 for (i = 0; i < num_objects; ++i) {
92 /* Cycle the color and alpha, if desired */
93 if (cycle_color) {
94 current_color += cycle_direction;
95 if (current_color < 0) {
96 current_color = 0;
97 cycle_direction = -cycle_direction;
98 }
99 if (current_color > 255) {
100 current_color = 255;
101 cycle_direction = -cycle_direction;
102 }
103 }
104 if (cycle_alpha) {
105 current_alpha += cycle_direction;
106 if (current_alpha < 0) {
107 current_alpha = 0;
108 cycle_direction = -cycle_direction;
109 }
110 if (current_alpha > 255) {
111 current_alpha = 255;
112 cycle_direction = -cycle_direction;
113 }
114 }
115 SDL_SetRenderDrawColor(renderer, 255, (Uint8)current_color,
116 (Uint8)current_color, (Uint8)current_alpha);
117
118 if (i == 0) {
119 SDL_RenderDrawLine(renderer, 0, 0, viewport.w - 1, viewport.h - 1);
120 SDL_RenderDrawLine(renderer, 0, viewport.h - 1, viewport.w - 1, 0);
121 SDL_RenderDrawLine(renderer, 0, viewport.h / 2, viewport.w - 1, viewport.h / 2);
122 SDL_RenderDrawLine(renderer, viewport.w / 2, 0, viewport.w / 2, viewport.h - 1);
123 } else {
124 x1 = (rand() % (viewport.w * 2)) - viewport.w;
125 x2 = (rand() % (viewport.w * 2)) - viewport.w;
126 y1 = (rand() % (viewport.h * 2)) - viewport.h;
127 y2 = (rand() % (viewport.h * 2)) - viewport.h;
128 SDL_RenderDrawLine(renderer, x1, y1, x2, y2);
129 }
130 }
131 }
132
133 void DrawRects(SDL_Renderer *renderer)
134 {
135 int i;
136 SDL_Rect rect;
137 SDL_Rect viewport;
138
139 /* Query the sizes */
140 SDL_RenderGetViewport(renderer, &viewport);
141
142 for (i = 0; i < num_objects / 4; ++i) {
143 /* Cycle the color and alpha, if desired */
144 if (cycle_color) {
145 current_color += cycle_direction;
146 if (current_color < 0) {
147 current_color = 0;
148 cycle_direction = -cycle_direction;
149 }
150 if (current_color > 255) {
151 current_color = 255;
152 cycle_direction = -cycle_direction;
153 }
154 }
155 if (cycle_alpha) {
156 current_alpha += cycle_direction;
157 if (current_alpha < 0) {
158 current_alpha = 0;
159 cycle_direction = -cycle_direction;
160 }
161 if (current_alpha > 255) {
162 current_alpha = 255;
163 cycle_direction = -cycle_direction;
164 }
165 }
166 SDL_SetRenderDrawColor(renderer, 255, (Uint8)current_color,
167 (Uint8)current_color, (Uint8)current_alpha);
168
169 rect.w = rand() % (viewport.h / 2);
170 rect.h = rand() % (viewport.h / 2);
171 rect.x = (rand() % (viewport.w * 2) - viewport.w) - (rect.w / 2);
172 rect.y = (rand() % (viewport.h * 2) - viewport.h) - (rect.h / 2);
173 SDL_RenderFillRect(renderer, &rect);
174 }
175 }
176
177 void loop(void)
178 {
179 Uint32 now;
180 int i;
181 SDL_Event event;
182
183 /* Check for events */
184 while (SDL_PollEvent(&event)) {
185 SDLTest_CommonEvent(state, &event, &done);
186 }
187 for (i = 0; i < state->num_windows; ++i) {
188 SDL_Renderer *renderer = state->renderers[i];
189 if (state->windows[i] == NULL) {
190 continue;
191 }
192 SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
193 SDL_RenderClear(renderer);
194
195 DrawRects(renderer);
196 DrawLines(renderer);
197 DrawPoints(renderer);
198
199 SDL_RenderPresent(renderer);
200 }
201 #ifdef __EMSCRIPTEN__
202 if (done) {
203 emscripten_cancel_main_loop();
204 }
205 #endif
206 frames++;
207 now = SDL_GetTicks();
208 if (SDL_TICKS_PASSED(now, next_fps_check)) {
209 /* Print out some timing information */
210 const Uint32 then = next_fps_check - fps_check_delay;
211 const double fps = ((double)frames * 1000) / (now - then);
212 SDL_Log("%2.2f frames per second\n", fps);
213 next_fps_check = now + fps_check_delay;
214 frames = 0;
215 }
216 }
217
218 int main(int argc, char *argv[])
219 {
220 int i;
221
222 /* Enable standard application logging */
223 SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
224
225 /* Initialize parameters */
226 num_objects = NUM_OBJECTS;
227
228 /* Initialize test framework */
229 state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
230 if (!state) {
231 return 1;
232 }
233 for (i = 1; i < argc;) {
234 int consumed;
235
236 consumed = SDLTest_CommonArg(state, i);
237 if (consumed == 0) {
238 consumed = -1;
239 if (SDL_strcasecmp(argv[i], "--blend") == 0) {
240 if (argv[i + 1]) {
241 if (SDL_strcasecmp(argv[i + 1], "none") == 0) {
242 blendMode = SDL_BLENDMODE_NONE;
243 consumed = 2;
244 } else if (SDL_strcasecmp(argv[i + 1], "blend") == 0) {
245 blendMode = SDL_BLENDMODE_BLEND;
246 consumed = 2;
247 } else if (SDL_strcasecmp(argv[i + 1], "add") == 0) {
248 blendMode = SDL_BLENDMODE_ADD;
249 consumed = 2;
250 } else if (SDL_strcasecmp(argv[i + 1], "mod") == 0) {
251 blendMode = SDL_BLENDMODE_MOD;
252 consumed = 2;
253 }
254 }
255 } else if (SDL_strcasecmp(argv[i], "--cyclecolor") == 0) {
256 cycle_color = SDL_TRUE;
257 consumed = 1;
258 } else if (SDL_strcasecmp(argv[i], "--cyclealpha") == 0) {
259 cycle_alpha = SDL_TRUE;
260 consumed = 1;
261 } else if (SDL_isdigit(*argv[i])) {
262 num_objects = SDL_atoi(argv[i]);
263 consumed = 1;
264 }
265 }
266 if (consumed < 0) {
267 static const char *options[] = {
268 "[--blend none|blend|add|mod]",
269 "[--cyclecolor]",
270 "[--cyclealpha]",
271 "[num_objects]",
272 NULL
273 };
274 SDLTest_CommonLogUsage(state, argv[0], options);
275 return 1;
276 }
277 i += consumed;
278 }
279 if (!SDLTest_CommonInit(state)) {
280 return 2;
281 }
282
283 /* Create the windows and initialize the renderers */
284 for (i = 0; i < state->num_windows; ++i) {
285 SDL_Renderer *renderer = state->renderers[i];
286 SDL_SetRenderDrawBlendMode(renderer, blendMode);
287 SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
288 SDL_RenderClear(renderer);
289 }
290
291 srand((unsigned int)time(NULL));
292
293 /* Main render loop */
294 frames = 0;
295 next_fps_check = SDL_GetTicks() + fps_check_delay;
296 done = 0;
297
298 #ifdef __EMSCRIPTEN__
299 emscripten_set_main_loop(loop, 0, 1);
300 #else
301 while (!done) {
302 loop();
303 }
304 #endif
305
306 SDLTest_CommonQuit(state);
307
308 return 0;
309 }
310
311 /* vi: set ts=4 sw=4 expandtab: */