2 Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
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.
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
13 /* Simple program: draw as many random objects on the screen as possible */
20 #include <emscripten/emscripten.h>
23 #include "SDL_test_common.h"
25 #define SWAP(typ,a,b) do{typ t=a;a=b;b=t;}while(0)
26 #define NUM_OBJECTS 100
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;
37 int mouse_begin_x = -1, mouse_begin_y = -1;
41 DrawPoints(SDL_Renderer * renderer)
48 SDL_RenderGetViewport(renderer, &viewport);
50 for (i = 0; i < num_objects * 4; ++i) {
51 /* Cycle the color and alpha, if desired */
53 current_color += cycle_direction;
54 if (current_color < 0) {
56 cycle_direction = -cycle_direction;
58 if (current_color > 255) {
60 cycle_direction = -cycle_direction;
64 current_alpha += cycle_direction;
65 if (current_alpha < 0) {
67 cycle_direction = -cycle_direction;
69 if (current_alpha > 255) {
71 cycle_direction = -cycle_direction;
74 SDL_SetRenderDrawColor(renderer, 255, (Uint8) current_color,
75 (Uint8) current_color, (Uint8) current_alpha);
77 x = rand() % viewport.w;
78 y = rand() % viewport.h;
79 SDL_RenderDrawPoint(renderer, x, y);
85 SDL_Rect lines[MAX_LINES];
87 add_line(int x1, int y1, int x2, int y2)
89 if (num_lines >= MAX_LINES)
91 if ((x1 == x2) && (y1 == y2))
94 SDL_Log("adding line (%d, %d), (%d, %d)\n", x1, y1, x2, y2);
95 lines[num_lines].x = x1;
96 lines[num_lines].y = y1;
97 lines[num_lines].w = x2;
98 lines[num_lines].h = y2;
105 DrawLines(SDL_Renderer * renderer)
110 /* Query the sizes */
111 SDL_RenderGetViewport(renderer, &viewport);
113 SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
115 for (i = 0; i < num_lines; ++i) {
117 SDL_RenderDrawLine(renderer, 0, 0, viewport.w - 1, viewport.h - 1);
118 SDL_RenderDrawLine(renderer, 0, viewport.h - 1, viewport.w - 1, 0);
119 SDL_RenderDrawLine(renderer, 0, viewport.h / 2, viewport.w - 1, viewport.h / 2);
120 SDL_RenderDrawLine(renderer, viewport.w / 2, 0, viewport.w / 2, viewport.h - 1);
122 SDL_RenderDrawLine(renderer, lines[i].x, lines[i].y, lines[i].w, lines[i].h);
129 SDL_Rect rects[MAX_RECTS];
131 add_rect(int x1, int y1, int x2, int y2)
133 if (num_rects >= MAX_RECTS)
135 if ((x1 == x2) || (y1 == y2))
143 SDL_Log("adding rect (%d, %d), (%d, %d) [%dx%d]\n", x1, y1, x2, y2,
146 rects[num_rects].x = x1;
147 rects[num_rects].y = y1;
148 rects[num_rects].w = x2 - x1;
149 rects[num_rects].h = y2 - y1;
155 DrawRects(SDL_Renderer * renderer)
157 SDL_SetRenderDrawColor(renderer, 255, 127, 0, 255);
158 SDL_RenderFillRects(renderer, rects, num_rects);
162 DrawRectLineIntersections(SDL_Renderer * renderer)
166 SDL_SetRenderDrawColor(renderer, 0, 255, 55, 255);
168 for (i = 0; i < num_rects; i++)
169 for (j = 0; j < num_lines; j++) {
179 if (SDL_IntersectRectAndLine(&r, &x1, &y1, &x2, &y2)) {
180 SDL_RenderDrawLine(renderer, x1, y1, x2, y2);
186 DrawRectRectIntersections(SDL_Renderer * renderer)
190 SDL_SetRenderDrawColor(renderer, 255, 200, 0, 255);
192 for (i = 0; i < num_rects; i++)
193 for (j = i + 1; j < num_rects; j++) {
195 if (SDL_IntersectRect(&rects[i], &rects[j], &r)) {
196 SDL_RenderFillRect(renderer, &r);
207 /* Check for events */
208 while (SDL_PollEvent(&event)) {
209 SDLTest_CommonEvent(state, &event, &done);
210 switch (event.type) {
211 case SDL_MOUSEBUTTONDOWN:
212 mouse_begin_x = event.button.x;
213 mouse_begin_y = event.button.y;
215 case SDL_MOUSEBUTTONUP:
216 if (event.button.button == 3)
217 add_line(mouse_begin_x, mouse_begin_y, event.button.x,
219 if (event.button.button == 1)
220 add_rect(mouse_begin_x, mouse_begin_y, event.button.x,
224 switch (event.key.keysym.sym) {
226 if (event.key.keysym.mod & KMOD_SHIFT)
229 add_line(rand() % 640, rand() % 480, rand() % 640,
233 if (event.key.keysym.mod & KMOD_SHIFT)
236 add_rect(rand() % 640, rand() % 480, rand() % 640,
245 for (i = 0; i < state->num_windows; ++i) {
246 SDL_Renderer *renderer = state->renderers[i];
247 if (state->windows[i] == NULL)
249 SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
250 SDL_RenderClear(renderer);
253 DrawPoints(renderer);
254 DrawRectRectIntersections(renderer);
256 DrawRectLineIntersections(renderer);
258 SDL_RenderPresent(renderer);
260 #ifdef __EMSCRIPTEN__
262 emscripten_cancel_main_loop();
268 main(int argc, char *argv[])
271 Uint32 then, now, frames;
273 /* Enable standard application logging */
274 SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
276 /* Initialize parameters */
277 num_objects = NUM_OBJECTS;
279 /* Initialize test framework */
280 state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
284 for (i = 1; i < argc;) {
287 consumed = SDLTest_CommonArg(state, i);
290 if (SDL_strcasecmp(argv[i], "--blend") == 0) {
292 if (SDL_strcasecmp(argv[i + 1], "none") == 0) {
293 blendMode = SDL_BLENDMODE_NONE;
295 } else if (SDL_strcasecmp(argv[i + 1], "blend") == 0) {
296 blendMode = SDL_BLENDMODE_BLEND;
298 } else if (SDL_strcasecmp(argv[i + 1], "add") == 0) {
299 blendMode = SDL_BLENDMODE_ADD;
301 } else if (SDL_strcasecmp(argv[i + 1], "mod") == 0) {
302 blendMode = SDL_BLENDMODE_MOD;
306 } else if (SDL_strcasecmp(argv[i], "--cyclecolor") == 0) {
307 cycle_color = SDL_TRUE;
309 } else if (SDL_strcasecmp(argv[i], "--cyclealpha") == 0) {
310 cycle_alpha = SDL_TRUE;
312 } else if (SDL_isdigit(*argv[i])) {
313 num_objects = SDL_atoi(argv[i]);
318 SDL_Log("Usage: %s %s [--blend none|blend|add|mod] [--cyclecolor] [--cyclealpha]\n",
319 argv[0], SDLTest_CommonUsage(state));
324 if (!SDLTest_CommonInit(state)) {
328 /* Create the windows and initialize the renderers */
329 for (i = 0; i < state->num_windows; ++i) {
330 SDL_Renderer *renderer = state->renderers[i];
331 SDL_SetRenderDrawBlendMode(renderer, blendMode);
332 SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
333 SDL_RenderClear(renderer);
338 /* Main render loop */
340 then = SDL_GetTicks();
343 #ifdef __EMSCRIPTEN__
344 emscripten_set_main_loop(loop, 0, 1);
352 SDLTest_CommonQuit(state);
354 /* Print out some timing information */
355 now = SDL_GetTicks();
357 double fps = ((double) frames * 1000) / (now - then);
358 SDL_Log("%2.2f frames per second\n", fps);
363 /* vi: set ts=4 sw=4 expandtab: */