Mercurial > sdl-ios-xcode
annotate test/testdraw2.c @ 5247:78ce7bfd0faf
Better window parameter checking
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Thu, 10 Feb 2011 10:37:35 -0800 |
parents | d976b67150c5 |
children |
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 | |
5150
ad50b3db78bd
The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents:
5143
diff
changeset
|
22 DrawPoints(SDL_Window * window, SDL_Renderer * renderer) |
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 | |
2902
83c3a4b0e421
Fixed crash in testdraw2, added more points
Sam Lantinga <slouken@libsdl.org>
parents:
2901
diff
changeset
|
31 for (i = 0; i < num_objects * 4; ++i) { |
2901 | 32 /* Cycle the color and alpha, if desired */ |
33 if (cycle_color) { | |
34 current_color += cycle_direction; | |
35 if (current_color < 0) { | |
36 current_color = 0; | |
37 cycle_direction = -cycle_direction; | |
38 } | |
39 if (current_color > 255) { | |
40 current_color = 255; | |
41 cycle_direction = -cycle_direction; | |
42 } | |
43 } | |
44 if (cycle_alpha) { | |
45 current_alpha += cycle_direction; | |
46 if (current_alpha < 0) { | |
47 current_alpha = 0; | |
48 cycle_direction = -cycle_direction; | |
49 } | |
50 if (current_alpha > 255) { | |
51 current_alpha = 255; | |
52 cycle_direction = -cycle_direction; | |
53 } | |
54 } | |
5150
ad50b3db78bd
The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents:
5143
diff
changeset
|
55 SDL_SetRenderDrawColor(renderer, 255, (Uint8) current_color, |
2901 | 56 (Uint8) current_color, (Uint8) current_alpha); |
57 | |
58 x = rand() % window_w; | |
59 y = rand() % window_h; | |
5150
ad50b3db78bd
The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents:
5143
diff
changeset
|
60 SDL_RenderDrawPoint(renderer, x, y); |
2901 | 61 } |
62 } | |
63 | |
64 void | |
5150
ad50b3db78bd
The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents:
5143
diff
changeset
|
65 DrawLines(SDL_Window * window, SDL_Renderer * renderer) |
2901 | 66 { |
67 int i; | |
68 int x1, y1, x2, y2; | |
69 int window_w, window_h; | |
70 | |
71 /* Query the sizes */ | |
72 SDL_GetWindowSize(window, &window_w, &window_h); | |
73 | |
74 for (i = 0; i < num_objects; ++i) { | |
75 /* Cycle the color and alpha, if desired */ | |
76 if (cycle_color) { | |
77 current_color += cycle_direction; | |
78 if (current_color < 0) { | |
79 current_color = 0; | |
80 cycle_direction = -cycle_direction; | |
81 } | |
82 if (current_color > 255) { | |
83 current_color = 255; | |
84 cycle_direction = -cycle_direction; | |
85 } | |
86 } | |
87 if (cycle_alpha) { | |
88 current_alpha += cycle_direction; | |
89 if (current_alpha < 0) { | |
90 current_alpha = 0; | |
91 cycle_direction = -cycle_direction; | |
92 } | |
93 if (current_alpha > 255) { | |
94 current_alpha = 255; | |
95 cycle_direction = -cycle_direction; | |
96 } | |
97 } | |
5150
ad50b3db78bd
The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents:
5143
diff
changeset
|
98 SDL_SetRenderDrawColor(renderer, 255, (Uint8) current_color, |
2901 | 99 (Uint8) current_color, (Uint8) current_alpha); |
100 | |
101 if (i == 0) { | |
5150
ad50b3db78bd
The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents:
5143
diff
changeset
|
102 SDL_RenderDrawLine(renderer, 0, 0, window_w - 1, window_h - 1); |
ad50b3db78bd
The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents:
5143
diff
changeset
|
103 SDL_RenderDrawLine(renderer, 0, window_h - 1, window_w - 1, 0); |
ad50b3db78bd
The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents:
5143
diff
changeset
|
104 SDL_RenderDrawLine(renderer, 0, window_h / 2, window_w - 1, window_h / 2); |
ad50b3db78bd
The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents:
5143
diff
changeset
|
105 SDL_RenderDrawLine(renderer, window_w / 2, 0, window_w / 2, window_h - 1); |
2901 | 106 } 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
|
107 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
|
108 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
|
109 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
|
110 y2 = (rand() % (window_h*2)) - window_h; |
5150
ad50b3db78bd
The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents:
5143
diff
changeset
|
111 SDL_RenderDrawLine(renderer, x1, y1, x2, y2); |
2901 | 112 } |
113 } | |
114 } | |
115 | |
116 void | |
5150
ad50b3db78bd
The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents:
5143
diff
changeset
|
117 DrawRects(SDL_Window * window, SDL_Renderer * renderer) |
2901 | 118 { |
119 int i; | |
120 SDL_Rect rect; | |
121 int window_w, window_h; | |
122 | |
123 /* Query the sizes */ | |
124 SDL_GetWindowSize(window, &window_w, &window_h); | |
125 | |
2902
83c3a4b0e421
Fixed crash in testdraw2, added more points
Sam Lantinga <slouken@libsdl.org>
parents:
2901
diff
changeset
|
126 for (i = 0; i < num_objects / 4; ++i) { |
2901 | 127 /* Cycle the color and alpha, if desired */ |
128 if (cycle_color) { | |
129 current_color += cycle_direction; | |
130 if (current_color < 0) { | |
131 current_color = 0; | |
132 cycle_direction = -cycle_direction; | |
133 } | |
134 if (current_color > 255) { | |
135 current_color = 255; | |
136 cycle_direction = -cycle_direction; | |
137 } | |
138 } | |
139 if (cycle_alpha) { | |
140 current_alpha += cycle_direction; | |
141 if (current_alpha < 0) { | |
142 current_alpha = 0; | |
143 cycle_direction = -cycle_direction; | |
144 } | |
145 if (current_alpha > 255) { | |
146 current_alpha = 255; | |
147 cycle_direction = -cycle_direction; | |
148 } | |
149 } | |
5150
ad50b3db78bd
The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents:
5143
diff
changeset
|
150 SDL_SetRenderDrawColor(renderer, 255, (Uint8) current_color, |
2901 | 151 (Uint8) current_color, (Uint8) current_alpha); |
152 | |
153 rect.w = rand() % (window_h / 2); | |
154 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
|
155 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
|
156 rect.y = (rand() % (window_h*2) - window_h) - (rect.h / 2); |
5150
ad50b3db78bd
The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents:
5143
diff
changeset
|
157 SDL_RenderFillRect(renderer, &rect); |
2901 | 158 } |
159 } | |
160 | |
161 int | |
162 main(int argc, char *argv[]) | |
163 { | |
164 int i, done; | |
165 SDL_Event event; | |
166 Uint32 then, now, frames; | |
167 | |
168 /* Initialize parameters */ | |
169 num_objects = NUM_OBJECTS; | |
170 | |
171 /* Initialize test framework */ | |
172 state = CommonCreateState(argv, SDL_INIT_VIDEO); | |
173 if (!state) { | |
174 return 1; | |
175 } | |
176 for (i = 1; i < argc;) { | |
177 int consumed; | |
178 | |
179 consumed = CommonArg(state, i); | |
180 if (consumed == 0) { | |
181 consumed = -1; | |
182 if (SDL_strcasecmp(argv[i], "--blend") == 0) { | |
183 if (argv[i + 1]) { | |
184 if (SDL_strcasecmp(argv[i + 1], "none") == 0) { | |
185 blendMode = SDL_BLENDMODE_NONE; | |
186 consumed = 2; | |
187 } else if (SDL_strcasecmp(argv[i + 1], "blend") == 0) { | |
188 blendMode = SDL_BLENDMODE_BLEND; | |
189 consumed = 2; | |
190 } else if (SDL_strcasecmp(argv[i + 1], "add") == 0) { | |
191 blendMode = SDL_BLENDMODE_ADD; | |
192 consumed = 2; | |
5187
d976b67150c5
Restored SDL_BLENDMODE_MOD for MAME
Sam Lantinga <slouken@libsdl.org>
parents:
5150
diff
changeset
|
193 } else if (SDL_strcasecmp(argv[i + 1], "mod") == 0) { |
d976b67150c5
Restored SDL_BLENDMODE_MOD for MAME
Sam Lantinga <slouken@libsdl.org>
parents:
5150
diff
changeset
|
194 blendMode = SDL_BLENDMODE_MOD; |
d976b67150c5
Restored SDL_BLENDMODE_MOD for MAME
Sam Lantinga <slouken@libsdl.org>
parents:
5150
diff
changeset
|
195 consumed = 2; |
2901 | 196 } |
197 } | |
198 } else if (SDL_strcasecmp(argv[i], "--cyclecolor") == 0) { | |
199 cycle_color = SDL_TRUE; | |
200 consumed = 1; | |
201 } else if (SDL_strcasecmp(argv[i], "--cyclealpha") == 0) { | |
202 cycle_alpha = SDL_TRUE; | |
203 consumed = 1; | |
204 } else if (SDL_isdigit(*argv[i])) { | |
205 num_objects = SDL_atoi(argv[i]); | |
206 consumed = 1; | |
207 } | |
208 } | |
209 if (consumed < 0) { | |
210 fprintf(stderr, | |
5187
d976b67150c5
Restored SDL_BLENDMODE_MOD for MAME
Sam Lantinga <slouken@libsdl.org>
parents:
5150
diff
changeset
|
211 "Usage: %s %s [--blend none|blend|add|mod] [--cyclecolor] [--cyclealpha]\n", |
2901 | 212 argv[0], CommonUsage(state)); |
213 return 1; | |
214 } | |
215 i += consumed; | |
216 } | |
217 if (!CommonInit(state)) { | |
218 return 2; | |
219 } | |
220 | |
221 /* Create the windows and initialize the renderers */ | |
222 for (i = 0; i < state->num_windows; ++i) { | |
5150
ad50b3db78bd
The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents:
5143
diff
changeset
|
223 SDL_Renderer *renderer = state->renderers[i]; |
ad50b3db78bd
The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents:
5143
diff
changeset
|
224 SDL_SetRenderDrawBlendMode(renderer, blendMode); |
ad50b3db78bd
The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents:
5143
diff
changeset
|
225 SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF); |
ad50b3db78bd
The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents:
5143
diff
changeset
|
226 SDL_RenderClear(renderer); |
2901 | 227 } |
228 | |
4884
27ab20a36eba
- added directx include path to VS2008 solution
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
3685
diff
changeset
|
229 srand((unsigned int)time(NULL)); |
2901 | 230 |
231 /* Main render loop */ | |
232 frames = 0; | |
233 then = SDL_GetTicks(); | |
234 done = 0; | |
235 while (!done) { | |
236 /* Check for events */ | |
237 ++frames; | |
238 while (SDL_PollEvent(&event)) { | |
239 CommonEvent(state, &event, &done); | |
240 } | |
241 for (i = 0; i < state->num_windows; ++i) { | |
5150
ad50b3db78bd
The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents:
5143
diff
changeset
|
242 SDL_Renderer *renderer = state->renderers[i]; |
ad50b3db78bd
The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents:
5143
diff
changeset
|
243 SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF); |
ad50b3db78bd
The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents:
5143
diff
changeset
|
244 SDL_RenderClear(renderer); |
2901 | 245 |
5150
ad50b3db78bd
The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents:
5143
diff
changeset
|
246 DrawRects(state->windows[i], renderer); |
ad50b3db78bd
The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents:
5143
diff
changeset
|
247 DrawLines(state->windows[i], renderer); |
ad50b3db78bd
The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents:
5143
diff
changeset
|
248 DrawPoints(state->windows[i], renderer); |
2901 | 249 |
5150
ad50b3db78bd
The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents:
5143
diff
changeset
|
250 SDL_RenderPresent(renderer); |
2901 | 251 } |
252 } | |
253 | |
3371
438ba87e9578
Call CommonQuit() at exit has been added.
Mike Gorchak <lestat@i.com.ua>
parents:
2902
diff
changeset
|
254 CommonQuit(state); |
438ba87e9578
Call CommonQuit() at exit has been added.
Mike Gorchak <lestat@i.com.ua>
parents:
2902
diff
changeset
|
255 |
2901 | 256 /* Print out some timing information */ |
257 now = SDL_GetTicks(); | |
258 if (now > then) { | |
259 double fps = ((double) frames * 1000) / (now - then); | |
260 printf("%2.2f frames per second\n", fps); | |
261 } | |
262 return 0; | |
263 } | |
264 | |
265 /* vi: set ts=4 sw=4 expandtab: */ |