Mercurial > sdl-ios-xcode
annotate test/testdraw2.c @ 4817:c68e7490e4cf
Fixed a couple of bugs in the general and X11 shape code, and fixed a bug in testshape that was keeping it from recognizing surfaces without alpha. Thanks to Andreas's bit-bashing tip, X11 shaped windows now work entirely, AFAICT.
author | Eli Gottlieb <eligottlieb@gmail.com> |
---|---|
date | Sun, 01 Aug 2010 21:10:42 -0400 |
parents | 64ce267332c6 |
children | 27ab20a36eba |
rev | line source |
---|---|
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 | |
3685
64ce267332c6
Switched from SDL_WindowID and SDL_TextureID to SDL_Window* and SDL_Texture* for code simplicity and improved performance.
Sam Lantinga <slouken@libsdl.org>
parents:
3596
diff
changeset
|
22 DrawPoints(SDL_Window * window) |
2901 | 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); | |
2902
83c3a4b0e421
Fixed crash in testdraw2, added more points
Sam Lantinga <slouken@libsdl.org>
parents:
2901
diff
changeset
|
32 for (i = 0; i < num_objects * 4; ++i) { |
2901 | 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; | |
3596
f638ded38b8a
Added SDL_RenderClear() as a fast method of clearing the screen to the drawing color.
Sam Lantinga <slouken@libsdl.org>
parents:
3546
diff
changeset
|
61 SDL_RenderDrawPoint(x, y); |
2901 | 62 } |
63 SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE); | |
64 } | |
65 | |
66 void | |
3685
64ce267332c6
Switched from SDL_WindowID and SDL_TextureID to SDL_Window* and SDL_Texture* for code simplicity and improved performance.
Sam Lantinga <slouken@libsdl.org>
parents:
3596
diff
changeset
|
67 DrawLines(SDL_Window * window) |
2901 | 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) { | |
3596
f638ded38b8a
Added SDL_RenderClear() as a fast method of clearing the screen to the drawing color.
Sam Lantinga <slouken@libsdl.org>
parents:
3546
diff
changeset
|
105 SDL_RenderDrawLine(0, 0, window_w - 1, window_h - 1); |
f638ded38b8a
Added SDL_RenderClear() as a fast method of clearing the screen to the drawing color.
Sam Lantinga <slouken@libsdl.org>
parents:
3546
diff
changeset
|
106 SDL_RenderDrawLine(0, window_h - 1, window_w - 1, 0); |
f638ded38b8a
Added SDL_RenderClear() as a fast method of clearing the screen to the drawing color.
Sam Lantinga <slouken@libsdl.org>
parents:
3546
diff
changeset
|
107 SDL_RenderDrawLine(0, window_h / 2, window_w - 1, window_h / 2); |
f638ded38b8a
Added SDL_RenderClear() as a fast method of clearing the screen to the drawing color.
Sam Lantinga <slouken@libsdl.org>
parents:
3546
diff
changeset
|
108 SDL_RenderDrawLine(window_w / 2, 0, window_w / 2, window_h - 1); |
2901 | 109 } else { |
3546
65848493e08e
Allow points to be outside the window bounds, stress testing the clipping code.
Sam Lantinga <slouken@libsdl.org>
parents:
3536
diff
changeset
|
110 x1 = (rand() % (window_w*2)) - window_w; |
65848493e08e
Allow points to be outside the window bounds, stress testing the clipping code.
Sam Lantinga <slouken@libsdl.org>
parents:
3536
diff
changeset
|
111 x2 = (rand() % (window_w*2)) - window_w; |
65848493e08e
Allow points to be outside the window bounds, stress testing the clipping code.
Sam Lantinga <slouken@libsdl.org>
parents:
3536
diff
changeset
|
112 y1 = (rand() % (window_h*2)) - window_h; |
65848493e08e
Allow points to be outside the window bounds, stress testing the clipping code.
Sam Lantinga <slouken@libsdl.org>
parents:
3536
diff
changeset
|
113 y2 = (rand() % (window_h*2)) - window_h; |
3596
f638ded38b8a
Added SDL_RenderClear() as a fast method of clearing the screen to the drawing color.
Sam Lantinga <slouken@libsdl.org>
parents:
3546
diff
changeset
|
114 SDL_RenderDrawLine(x1, y1, x2, y2); |
2901 | 115 } |
116 } | |
117 SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE); | |
118 } | |
119 | |
120 void | |
3685
64ce267332c6
Switched from SDL_WindowID and SDL_TextureID to SDL_Window* and SDL_Texture* for code simplicity and improved performance.
Sam Lantinga <slouken@libsdl.org>
parents:
3596
diff
changeset
|
121 DrawRects(SDL_Window * window) |
2901 | 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); | |
2902
83c3a4b0e421
Fixed crash in testdraw2, added more points
Sam Lantinga <slouken@libsdl.org>
parents:
2901
diff
changeset
|
131 for (i = 0; i < num_objects / 4; ++i) { |
2901 | 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); | |
3546
65848493e08e
Allow points to be outside the window bounds, stress testing the clipping code.
Sam Lantinga <slouken@libsdl.org>
parents:
3536
diff
changeset
|
160 rect.x = (rand() % (window_w*2) - window_w) - (rect.w / 2); |
65848493e08e
Allow points to be outside the window bounds, stress testing the clipping code.
Sam Lantinga <slouken@libsdl.org>
parents:
3536
diff
changeset
|
161 rect.y = (rand() % (window_h*2) - window_h) - (rect.h / 2); |
3596
f638ded38b8a
Added SDL_RenderClear() as a fast method of clearing the screen to the drawing color.
Sam Lantinga <slouken@libsdl.org>
parents:
3546
diff
changeset
|
162 SDL_RenderFillRect(&rect); |
2901 | 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); | |
3596
f638ded38b8a
Added SDL_RenderClear() as a fast method of clearing the screen to the drawing color.
Sam Lantinga <slouken@libsdl.org>
parents:
3546
diff
changeset
|
234 SDL_RenderClear(); |
2901 | 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: | |
3685
64ce267332c6
Switched from SDL_WindowID and SDL_TextureID to SDL_Window* and SDL_Texture* for code simplicity and improved performance.
Sam Lantinga <slouken@libsdl.org>
parents:
3596
diff
changeset
|
252 SDL_SelectRenderer(SDL_GetWindowFromID(event.window.windowID)); |
2901 | 253 SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF); |
3596
f638ded38b8a
Added SDL_RenderClear() as a fast method of clearing the screen to the drawing color.
Sam Lantinga <slouken@libsdl.org>
parents:
3546
diff
changeset
|
254 SDL_RenderClear(); |
2901 | 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); | |
3596
f638ded38b8a
Added SDL_RenderClear() as a fast method of clearing the screen to the drawing color.
Sam Lantinga <slouken@libsdl.org>
parents:
3546
diff
changeset
|
265 SDL_RenderClear(); |
2901 | 266 |
267 DrawRects(state->windows[i]); | |
268 DrawLines(state->windows[i]); | |
269 DrawPoints(state->windows[i]); | |
270 | |
271 SDL_RenderPresent(); | |
272 } | |
273 } | |
274 | |
3371
438ba87e9578
Call CommonQuit() at exit has been added.
Mike Gorchak <lestat@i.com.ua>
parents:
2902
diff
changeset
|
275 CommonQuit(state); |
438ba87e9578
Call CommonQuit() at exit has been added.
Mike Gorchak <lestat@i.com.ua>
parents:
2902
diff
changeset
|
276 |
2901 | 277 /* Print out some timing information */ |
278 now = SDL_GetTicks(); | |
279 if (now > then) { | |
280 double fps = ((double) frames * 1000) / (now - then); | |
281 printf("%2.2f frames per second\n", fps); | |
282 } | |
283 return 0; | |
284 } | |
285 | |
286 /* vi: set ts=4 sw=4 expandtab: */ |