2 /* Simple program: draw as many random objects on the screen as possible */
10 #define NUM_OBJECTS 100
12 static CommonState *state;
13 static int num_objects;
14 static SDL_bool cycle_color;
15 static SDL_bool cycle_alpha;
16 static int cycle_direction = 1;
17 static int current_alpha = 255;
18 static int current_color = 255;
19 static SDL_BlendMode blendMode = SDL_BLENDMODE_NONE;
22 DrawPoints(SDL_Window * window)
26 int window_w, window_h;
29 SDL_GetWindowSize(window, &window_w, &window_h);
31 SDL_SetRenderDrawBlendMode(blendMode);
32 for (i = 0; i < num_objects * 4; ++i) {
33 /* Cycle the color and alpha, if desired */
35 current_color += cycle_direction;
36 if (current_color < 0) {
38 cycle_direction = -cycle_direction;
40 if (current_color > 255) {
42 cycle_direction = -cycle_direction;
46 current_alpha += cycle_direction;
47 if (current_alpha < 0) {
49 cycle_direction = -cycle_direction;
51 if (current_alpha > 255) {
53 cycle_direction = -cycle_direction;
56 SDL_SetRenderDrawColor(255, (Uint8) current_color,
57 (Uint8) current_color, (Uint8) current_alpha);
59 x = rand() % window_w;
60 y = rand() % window_h;
61 SDL_RenderDrawPoint(x, y);
63 SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE);
67 DrawLines(SDL_Window * window)
71 int window_w, window_h;
74 SDL_GetWindowSize(window, &window_w, &window_h);
76 SDL_SetRenderDrawBlendMode(blendMode);
77 for (i = 0; i < num_objects; ++i) {
78 /* Cycle the color and alpha, if desired */
80 current_color += cycle_direction;
81 if (current_color < 0) {
83 cycle_direction = -cycle_direction;
85 if (current_color > 255) {
87 cycle_direction = -cycle_direction;
91 current_alpha += cycle_direction;
92 if (current_alpha < 0) {
94 cycle_direction = -cycle_direction;
96 if (current_alpha > 255) {
98 cycle_direction = -cycle_direction;
101 SDL_SetRenderDrawColor(255, (Uint8) current_color,
102 (Uint8) current_color, (Uint8) current_alpha);
105 SDL_RenderDrawLine(0, 0, window_w - 1, window_h - 1);
106 SDL_RenderDrawLine(0, window_h - 1, window_w - 1, 0);
107 SDL_RenderDrawLine(0, window_h / 2, window_w - 1, window_h / 2);
108 SDL_RenderDrawLine(window_w / 2, 0, window_w / 2, window_h - 1);
110 x1 = (rand() % (window_w*2)) - window_w;
111 x2 = (rand() % (window_w*2)) - window_w;
112 y1 = (rand() % (window_h*2)) - window_h;
113 y2 = (rand() % (window_h*2)) - window_h;
114 SDL_RenderDrawLine(x1, y1, x2, y2);
117 SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE);
121 DrawRects(SDL_Window * window)
125 int window_w, window_h;
127 /* Query the sizes */
128 SDL_GetWindowSize(window, &window_w, &window_h);
130 SDL_SetRenderDrawBlendMode(blendMode);
131 for (i = 0; i < num_objects / 4; ++i) {
132 /* Cycle the color and alpha, if desired */
134 current_color += cycle_direction;
135 if (current_color < 0) {
137 cycle_direction = -cycle_direction;
139 if (current_color > 255) {
141 cycle_direction = -cycle_direction;
145 current_alpha += cycle_direction;
146 if (current_alpha < 0) {
148 cycle_direction = -cycle_direction;
150 if (current_alpha > 255) {
152 cycle_direction = -cycle_direction;
155 SDL_SetRenderDrawColor(255, (Uint8) current_color,
156 (Uint8) current_color, (Uint8) current_alpha);
158 rect.w = rand() % (window_h / 2);
159 rect.h = rand() % (window_h / 2);
160 rect.x = (rand() % (window_w*2) - window_w) - (rect.w / 2);
161 rect.y = (rand() % (window_h*2) - window_h) - (rect.h / 2);
162 SDL_RenderFillRect(&rect);
164 SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE);
168 main(int argc, char *argv[])
172 Uint32 then, now, frames;
174 /* Initialize parameters */
175 num_objects = NUM_OBJECTS;
177 /* Initialize test framework */
178 state = CommonCreateState(argv, SDL_INIT_VIDEO);
182 for (i = 1; i < argc;) {
185 consumed = CommonArg(state, i);
188 if (SDL_strcasecmp(argv[i], "--blend") == 0) {
190 if (SDL_strcasecmp(argv[i + 1], "none") == 0) {
191 blendMode = SDL_BLENDMODE_NONE;
193 } else if (SDL_strcasecmp(argv[i + 1], "mask") == 0) {
194 blendMode = SDL_BLENDMODE_MASK;
196 } else if (SDL_strcasecmp(argv[i + 1], "blend") == 0) {
197 blendMode = SDL_BLENDMODE_BLEND;
199 } else if (SDL_strcasecmp(argv[i + 1], "add") == 0) {
200 blendMode = SDL_BLENDMODE_ADD;
202 } else if (SDL_strcasecmp(argv[i + 1], "mod") == 0) {
203 blendMode = SDL_BLENDMODE_MOD;
207 } else if (SDL_strcasecmp(argv[i], "--cyclecolor") == 0) {
208 cycle_color = SDL_TRUE;
210 } else if (SDL_strcasecmp(argv[i], "--cyclealpha") == 0) {
211 cycle_alpha = SDL_TRUE;
213 } else if (SDL_isdigit(*argv[i])) {
214 num_objects = SDL_atoi(argv[i]);
220 "Usage: %s %s [--blend none|mask|blend|add|mod] [--cyclecolor] [--cyclealpha]\n",
221 argv[0], CommonUsage(state));
226 if (!CommonInit(state)) {
230 /* Create the windows and initialize the renderers */
231 for (i = 0; i < state->num_windows; ++i) {
232 SDL_SelectRenderer(state->windows[i]);
233 SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
237 srand((unsigned int)time(NULL));
239 /* Main render loop */
241 then = SDL_GetTicks();
244 /* Check for events */
246 while (SDL_PollEvent(&event)) {
247 CommonEvent(state, &event, &done);
248 switch (event.type) {
249 case SDL_WINDOWEVENT:
250 switch (event.window.event) {
251 case SDL_WINDOWEVENT_EXPOSED:
252 SDL_SelectRenderer(SDL_GetWindowFromID(event.window.windowID));
253 SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
262 for (i = 0; i < state->num_windows; ++i) {
263 SDL_SelectRenderer(state->windows[i]);
264 SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
267 DrawRects(state->windows[i]);
268 DrawLines(state->windows[i]);
269 DrawPoints(state->windows[i]);
277 /* Print out some timing information */
278 now = SDL_GetTicks();
280 double fps = ((double) frames * 1000) / (now - then);
281 printf("%2.2f frames per second\n", fps);
286 /* vi: set ts=4 sw=4 expandtab: */