Mercurial > sdl-ios-xcode
annotate test/testdraw2.c @ 5167:97423d858a1a
Added an intro to the features and goals of the 2D rendering API.
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Thu, 03 Feb 2011 10:03:55 -0800 |
parents | ad50b3db78bd |
children | d976b67150c5 |
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; | |
193 } | |
194 } | |
195 } else if (SDL_strcasecmp(argv[i], "--cyclecolor") == 0) { | |
196 cycle_color = SDL_TRUE; | |
197 consumed = 1; | |
198 } else if (SDL_strcasecmp(argv[i], "--cyclealpha") == 0) { | |
199 cycle_alpha = SDL_TRUE; | |
200 consumed = 1; | |
201 } else if (SDL_isdigit(*argv[i])) { | |
202 num_objects = SDL_atoi(argv[i]); | |
203 consumed = 1; | |
204 } | |
205 } | |
206 if (consumed < 0) { | |
207 fprintf(stderr, | |
5143
e743b9c3f6d6
Making the API simpler, the blend modes are "none, blend, add" and are supported by all renderers.
Sam Lantinga <slouken@libsdl.org>
parents:
4884
diff
changeset
|
208 "Usage: %s %s [--blend none|blend|add] [--cyclecolor] [--cyclealpha]\n", |
2901 | 209 argv[0], CommonUsage(state)); |
210 return 1; | |
211 } | |
212 i += consumed; | |
213 } | |
214 if (!CommonInit(state)) { | |
215 return 2; | |
216 } | |
217 | |
218 /* Create the windows and initialize the renderers */ | |
219 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
|
220 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
|
221 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
|
222 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
|
223 SDL_RenderClear(renderer); |
2901 | 224 } |
225 | |
4884
27ab20a36eba
- added directx include path to VS2008 solution
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
3685
diff
changeset
|
226 srand((unsigned int)time(NULL)); |
2901 | 227 |
228 /* Main render loop */ | |
229 frames = 0; | |
230 then = SDL_GetTicks(); | |
231 done = 0; | |
232 while (!done) { | |
233 /* Check for events */ | |
234 ++frames; | |
235 while (SDL_PollEvent(&event)) { | |
236 CommonEvent(state, &event, &done); | |
237 } | |
238 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
|
239 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
|
240 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
|
241 SDL_RenderClear(renderer); |
2901 | 242 |
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
|
243 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
|
244 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
|
245 DrawPoints(state->windows[i], renderer); |
2901 | 246 |
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
|
247 SDL_RenderPresent(renderer); |
2901 | 248 } |
249 } | |
250 | |
3371
438ba87e9578
Call CommonQuit() at exit has been added.
Mike Gorchak <lestat@i.com.ua>
parents:
2902
diff
changeset
|
251 CommonQuit(state); |
438ba87e9578
Call CommonQuit() at exit has been added.
Mike Gorchak <lestat@i.com.ua>
parents:
2902
diff
changeset
|
252 |
2901 | 253 /* Print out some timing information */ |
254 now = SDL_GetTicks(); | |
255 if (now > then) { | |
256 double fps = ((double) frames * 1000) / (now - then); | |
257 printf("%2.2f frames per second\n", fps); | |
258 } | |
259 return 0; | |
260 } | |
261 | |
262 /* vi: set ts=4 sw=4 expandtab: */ |