2901
|
1 |
|
|
2 |
/* Simple program: draw as many random objects on the screen as possible */
|
|
3 |
|
|
4 |
#include <stdlib.h>
|
|
5 |
#include <stdio.h>
|
|
6 |
#include <time.h>
|
|
7 |
|
|
8 |
#include "common.h"
|
|
9 |
|
|
10 |
#define NUM_OBJECTS 100
|
|
11 |
|
|
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;
|
|
20 |
|
|
21 |
void
|
|
22 |
DrawPoints(SDL_WindowID window)
|
|
23 |
{
|
|
24 |
int i;
|
|
25 |
int x, y;
|
|
26 |
int window_w, window_h;
|
|
27 |
|
|
28 |
/* Query the sizes */
|
|
29 |
SDL_GetWindowSize(window, &window_w, &window_h);
|
|
30 |
|
|
31 |
SDL_SetRenderDrawBlendMode(blendMode);
|
|
32 |
for (i = 0; i < num_objects; ++i) {
|
|
33 |
/* Cycle the color and alpha, if desired */
|
|
34 |
if (cycle_color) {
|
|
35 |
current_color += cycle_direction;
|
|
36 |
if (current_color < 0) {
|
|
37 |
current_color = 0;
|
|
38 |
cycle_direction = -cycle_direction;
|
|
39 |
}
|
|
40 |
if (current_color > 255) {
|
|
41 |
current_color = 255;
|
|
42 |
cycle_direction = -cycle_direction;
|
|
43 |
}
|
|
44 |
}
|
|
45 |
if (cycle_alpha) {
|
|
46 |
current_alpha += cycle_direction;
|
|
47 |
if (current_alpha < 0) {
|
|
48 |
current_alpha = 0;
|
|
49 |
cycle_direction = -cycle_direction;
|
|
50 |
}
|
|
51 |
if (current_alpha > 255) {
|
|
52 |
current_alpha = 255;
|
|
53 |
cycle_direction = -cycle_direction;
|
|
54 |
}
|
|
55 |
}
|
|
56 |
SDL_SetRenderDrawColor(255, (Uint8) current_color,
|
|
57 |
(Uint8) current_color, (Uint8) current_alpha);
|
|
58 |
|
|
59 |
x = rand() % window_w;
|
|
60 |
y = rand() % window_h;
|
|
61 |
SDL_RenderPoint(x, y);
|
|
62 |
}
|
|
63 |
SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE);
|
|
64 |
}
|
|
65 |
|
|
66 |
void
|
|
67 |
DrawLines(SDL_WindowID window)
|
|
68 |
{
|
|
69 |
int i;
|
|
70 |
int x1, y1, x2, y2;
|
|
71 |
int window_w, window_h;
|
|
72 |
|
|
73 |
/* Query the sizes */
|
|
74 |
SDL_GetWindowSize(window, &window_w, &window_h);
|
|
75 |
|
|
76 |
SDL_SetRenderDrawBlendMode(blendMode);
|
|
77 |
for (i = 0; i < num_objects; ++i) {
|
|
78 |
/* Cycle the color and alpha, if desired */
|
|
79 |
if (cycle_color) {
|
|
80 |
current_color += cycle_direction;
|
|
81 |
if (current_color < 0) {
|
|
82 |
current_color = 0;
|
|
83 |
cycle_direction = -cycle_direction;
|
|
84 |
}
|
|
85 |
if (current_color > 255) {
|
|
86 |
current_color = 255;
|
|
87 |
cycle_direction = -cycle_direction;
|
|
88 |
}
|
|
89 |
}
|
|
90 |
if (cycle_alpha) {
|
|
91 |
current_alpha += cycle_direction;
|
|
92 |
if (current_alpha < 0) {
|
|
93 |
current_alpha = 0;
|
|
94 |
cycle_direction = -cycle_direction;
|
|
95 |
}
|
|
96 |
if (current_alpha > 255) {
|
|
97 |
current_alpha = 255;
|
|
98 |
cycle_direction = -cycle_direction;
|
|
99 |
}
|
|
100 |
}
|
|
101 |
SDL_SetRenderDrawColor(255, (Uint8) current_color,
|
|
102 |
(Uint8) current_color, (Uint8) current_alpha);
|
|
103 |
|
|
104 |
if (i == 0) {
|
|
105 |
SDL_RenderLine(0, 0, window_w, window_h);
|
|
106 |
SDL_RenderLine(0, window_h, window_w, 0);
|
|
107 |
SDL_RenderLine(0, window_h / 2, window_w, window_h / 2);
|
|
108 |
SDL_RenderLine(window_w / 2, 0, window_w / 2, window_h);
|
|
109 |
} else {
|
|
110 |
x1 = rand() % window_w;
|
|
111 |
x2 = rand() % window_w;
|
|
112 |
y1 = rand() % window_h;
|
|
113 |
y2 = rand() % window_h;
|
|
114 |
SDL_RenderLine(x1, y1, x2, y2);
|
|
115 |
}
|
|
116 |
}
|
|
117 |
SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE);
|
|
118 |
}
|
|
119 |
|
|
120 |
void
|
|
121 |
DrawRects(SDL_WindowID window)
|
|
122 |
{
|
|
123 |
int i;
|
|
124 |
SDL_Rect rect;
|
|
125 |
int window_w, window_h;
|
|
126 |
|
|
127 |
/* Query the sizes */
|
|
128 |
SDL_GetWindowSize(window, &window_w, &window_h);
|
|
129 |
|
|
130 |
SDL_SetRenderDrawBlendMode(blendMode);
|
|
131 |
for (i = 0; i < num_objects/4; ++i) {
|
|
132 |
/* Cycle the color and alpha, if desired */
|
|
133 |
if (cycle_color) {
|
|
134 |
current_color += cycle_direction;
|
|
135 |
if (current_color < 0) {
|
|
136 |
current_color = 0;
|
|
137 |
cycle_direction = -cycle_direction;
|
|
138 |
}
|
|
139 |
if (current_color > 255) {
|
|
140 |
current_color = 255;
|
|
141 |
cycle_direction = -cycle_direction;
|
|
142 |
}
|
|
143 |
}
|
|
144 |
if (cycle_alpha) {
|
|
145 |
current_alpha += cycle_direction;
|
|
146 |
if (current_alpha < 0) {
|
|
147 |
current_alpha = 0;
|
|
148 |
cycle_direction = -cycle_direction;
|
|
149 |
}
|
|
150 |
if (current_alpha > 255) {
|
|
151 |
current_alpha = 255;
|
|
152 |
cycle_direction = -cycle_direction;
|
|
153 |
}
|
|
154 |
}
|
|
155 |
SDL_SetRenderDrawColor(255, (Uint8) current_color,
|
|
156 |
(Uint8) current_color, (Uint8) current_alpha);
|
|
157 |
|
|
158 |
rect.w = rand() % (window_h / 2);
|
|
159 |
rect.h = rand() % (window_h / 2);
|
|
160 |
rect.x = (rand() % window_w) - (rect.w / 2);
|
|
161 |
rect.y = (rand() % window_w) - (rect.h / 2);
|
|
162 |
SDL_RenderFill(&rect);
|
|
163 |
}
|
|
164 |
SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE);
|
|
165 |
}
|
|
166 |
|
|
167 |
int
|
|
168 |
main(int argc, char *argv[])
|
|
169 |
{
|
|
170 |
int i, done;
|
|
171 |
SDL_Event event;
|
|
172 |
Uint32 then, now, frames;
|
|
173 |
|
|
174 |
/* Initialize parameters */
|
|
175 |
num_objects = NUM_OBJECTS;
|
|
176 |
|
|
177 |
/* Initialize test framework */
|
|
178 |
state = CommonCreateState(argv, SDL_INIT_VIDEO);
|
|
179 |
if (!state) {
|
|
180 |
return 1;
|
|
181 |
}
|
|
182 |
for (i = 1; i < argc;) {
|
|
183 |
int consumed;
|
|
184 |
|
|
185 |
consumed = CommonArg(state, i);
|
|
186 |
if (consumed == 0) {
|
|
187 |
consumed = -1;
|
|
188 |
if (SDL_strcasecmp(argv[i], "--blend") == 0) {
|
|
189 |
if (argv[i + 1]) {
|
|
190 |
if (SDL_strcasecmp(argv[i + 1], "none") == 0) {
|
|
191 |
blendMode = SDL_BLENDMODE_NONE;
|
|
192 |
consumed = 2;
|
|
193 |
} else if (SDL_strcasecmp(argv[i + 1], "mask") == 0) {
|
|
194 |
blendMode = SDL_BLENDMODE_MASK;
|
|
195 |
consumed = 2;
|
|
196 |
} else if (SDL_strcasecmp(argv[i + 1], "blend") == 0) {
|
|
197 |
blendMode = SDL_BLENDMODE_BLEND;
|
|
198 |
consumed = 2;
|
|
199 |
} else if (SDL_strcasecmp(argv[i + 1], "add") == 0) {
|
|
200 |
blendMode = SDL_BLENDMODE_ADD;
|
|
201 |
consumed = 2;
|
|
202 |
} else if (SDL_strcasecmp(argv[i + 1], "mod") == 0) {
|
|
203 |
blendMode = SDL_BLENDMODE_MOD;
|
|
204 |
consumed = 2;
|
|
205 |
}
|
|
206 |
}
|
|
207 |
} else if (SDL_strcasecmp(argv[i], "--cyclecolor") == 0) {
|
|
208 |
cycle_color = SDL_TRUE;
|
|
209 |
consumed = 1;
|
|
210 |
} else if (SDL_strcasecmp(argv[i], "--cyclealpha") == 0) {
|
|
211 |
cycle_alpha = SDL_TRUE;
|
|
212 |
consumed = 1;
|
|
213 |
} else if (SDL_isdigit(*argv[i])) {
|
|
214 |
num_objects = SDL_atoi(argv[i]);
|
|
215 |
consumed = 1;
|
|
216 |
}
|
|
217 |
}
|
|
218 |
if (consumed < 0) {
|
|
219 |
fprintf(stderr,
|
|
220 |
"Usage: %s %s [--blend none|mask|blend|add|mod] [--cyclecolor] [--cyclealpha]\n",
|
|
221 |
argv[0], CommonUsage(state));
|
|
222 |
return 1;
|
|
223 |
}
|
|
224 |
i += consumed;
|
|
225 |
}
|
|
226 |
if (!CommonInit(state)) {
|
|
227 |
return 2;
|
|
228 |
}
|
|
229 |
|
|
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);
|
|
234 |
SDL_RenderFill(NULL);
|
|
235 |
}
|
|
236 |
|
|
237 |
srand(time(NULL));
|
|
238 |
|
|
239 |
/* Main render loop */
|
|
240 |
frames = 0;
|
|
241 |
then = SDL_GetTicks();
|
|
242 |
done = 0;
|
|
243 |
while (!done) {
|
|
244 |
/* Check for events */
|
|
245 |
++frames;
|
|
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(event.window.windowID);
|
|
253 |
SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
|
|
254 |
SDL_RenderFill(NULL);
|
|
255 |
break;
|
|
256 |
}
|
|
257 |
break;
|
|
258 |
default:
|
|
259 |
break;
|
|
260 |
}
|
|
261 |
}
|
|
262 |
for (i = 0; i < state->num_windows; ++i) {
|
|
263 |
SDL_SelectRenderer(state->windows[i]);
|
|
264 |
SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
|
|
265 |
SDL_RenderFill(NULL);
|
|
266 |
|
|
267 |
DrawRects(state->windows[i]);
|
|
268 |
DrawLines(state->windows[i]);
|
|
269 |
DrawPoints(state->windows[i]);
|
|
270 |
|
|
271 |
SDL_RenderPresent();
|
|
272 |
}
|
|
273 |
}
|
|
274 |
|
|
275 |
/* Print out some timing information */
|
|
276 |
now = SDL_GetTicks();
|
|
277 |
if (now > then) {
|
|
278 |
double fps = ((double) frames * 1000) / (now - then);
|
|
279 |
printf("%2.2f frames per second\n", fps);
|
|
280 |
}
|
|
281 |
return 0;
|
|
282 |
}
|
|
283 |
|
|
284 |
/* vi: set ts=4 sw=4 expandtab: */
|