Mercurial > sdl-ios-xcode
comparison test/testdraw2.c @ 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.
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Tue, 01 Feb 2011 19:19:43 -0800 |
parents | e743b9c3f6d6 |
children | d976b67150c5 |
comparison
equal
deleted
inserted
replaced
5149:3052772b59db | 5150:ad50b3db78bd |
---|---|
17 static int current_alpha = 255; | 17 static int current_alpha = 255; |
18 static int current_color = 255; | 18 static int current_color = 255; |
19 static SDL_BlendMode blendMode = SDL_BLENDMODE_NONE; | 19 static SDL_BlendMode blendMode = SDL_BLENDMODE_NONE; |
20 | 20 |
21 void | 21 void |
22 DrawPoints(SDL_Window * window) | 22 DrawPoints(SDL_Window * window, SDL_Renderer * renderer) |
23 { | 23 { |
24 int i; | 24 int i; |
25 int x, y; | 25 int x, y; |
26 int window_w, window_h; | 26 int window_w, window_h; |
27 | 27 |
28 /* Query the sizes */ | 28 /* Query the sizes */ |
29 SDL_GetWindowSize(window, &window_w, &window_h); | 29 SDL_GetWindowSize(window, &window_w, &window_h); |
30 | 30 |
31 SDL_SetRenderDrawBlendMode(blendMode); | |
32 for (i = 0; i < num_objects * 4; ++i) { | 31 for (i = 0; i < num_objects * 4; ++i) { |
33 /* Cycle the color and alpha, if desired */ | 32 /* Cycle the color and alpha, if desired */ |
34 if (cycle_color) { | 33 if (cycle_color) { |
35 current_color += cycle_direction; | 34 current_color += cycle_direction; |
36 if (current_color < 0) { | 35 if (current_color < 0) { |
51 if (current_alpha > 255) { | 50 if (current_alpha > 255) { |
52 current_alpha = 255; | 51 current_alpha = 255; |
53 cycle_direction = -cycle_direction; | 52 cycle_direction = -cycle_direction; |
54 } | 53 } |
55 } | 54 } |
56 SDL_SetRenderDrawColor(255, (Uint8) current_color, | 55 SDL_SetRenderDrawColor(renderer, 255, (Uint8) current_color, |
57 (Uint8) current_color, (Uint8) current_alpha); | 56 (Uint8) current_color, (Uint8) current_alpha); |
58 | 57 |
59 x = rand() % window_w; | 58 x = rand() % window_w; |
60 y = rand() % window_h; | 59 y = rand() % window_h; |
61 SDL_RenderDrawPoint(x, y); | 60 SDL_RenderDrawPoint(renderer, x, y); |
62 } | 61 } |
63 SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE); | |
64 } | 62 } |
65 | 63 |
66 void | 64 void |
67 DrawLines(SDL_Window * window) | 65 DrawLines(SDL_Window * window, SDL_Renderer * renderer) |
68 { | 66 { |
69 int i; | 67 int i; |
70 int x1, y1, x2, y2; | 68 int x1, y1, x2, y2; |
71 int window_w, window_h; | 69 int window_w, window_h; |
72 | 70 |
73 /* Query the sizes */ | 71 /* Query the sizes */ |
74 SDL_GetWindowSize(window, &window_w, &window_h); | 72 SDL_GetWindowSize(window, &window_w, &window_h); |
75 | 73 |
76 SDL_SetRenderDrawBlendMode(blendMode); | |
77 for (i = 0; i < num_objects; ++i) { | 74 for (i = 0; i < num_objects; ++i) { |
78 /* Cycle the color and alpha, if desired */ | 75 /* Cycle the color and alpha, if desired */ |
79 if (cycle_color) { | 76 if (cycle_color) { |
80 current_color += cycle_direction; | 77 current_color += cycle_direction; |
81 if (current_color < 0) { | 78 if (current_color < 0) { |
96 if (current_alpha > 255) { | 93 if (current_alpha > 255) { |
97 current_alpha = 255; | 94 current_alpha = 255; |
98 cycle_direction = -cycle_direction; | 95 cycle_direction = -cycle_direction; |
99 } | 96 } |
100 } | 97 } |
101 SDL_SetRenderDrawColor(255, (Uint8) current_color, | 98 SDL_SetRenderDrawColor(renderer, 255, (Uint8) current_color, |
102 (Uint8) current_color, (Uint8) current_alpha); | 99 (Uint8) current_color, (Uint8) current_alpha); |
103 | 100 |
104 if (i == 0) { | 101 if (i == 0) { |
105 SDL_RenderDrawLine(0, 0, window_w - 1, window_h - 1); | 102 SDL_RenderDrawLine(renderer, 0, 0, window_w - 1, window_h - 1); |
106 SDL_RenderDrawLine(0, window_h - 1, window_w - 1, 0); | 103 SDL_RenderDrawLine(renderer, 0, window_h - 1, window_w - 1, 0); |
107 SDL_RenderDrawLine(0, window_h / 2, window_w - 1, window_h / 2); | 104 SDL_RenderDrawLine(renderer, 0, window_h / 2, window_w - 1, window_h / 2); |
108 SDL_RenderDrawLine(window_w / 2, 0, window_w / 2, window_h - 1); | 105 SDL_RenderDrawLine(renderer, window_w / 2, 0, window_w / 2, window_h - 1); |
109 } else { | 106 } else { |
110 x1 = (rand() % (window_w*2)) - window_w; | 107 x1 = (rand() % (window_w*2)) - window_w; |
111 x2 = (rand() % (window_w*2)) - window_w; | 108 x2 = (rand() % (window_w*2)) - window_w; |
112 y1 = (rand() % (window_h*2)) - window_h; | 109 y1 = (rand() % (window_h*2)) - window_h; |
113 y2 = (rand() % (window_h*2)) - window_h; | 110 y2 = (rand() % (window_h*2)) - window_h; |
114 SDL_RenderDrawLine(x1, y1, x2, y2); | 111 SDL_RenderDrawLine(renderer, x1, y1, x2, y2); |
115 } | 112 } |
116 } | 113 } |
117 SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE); | |
118 } | 114 } |
119 | 115 |
120 void | 116 void |
121 DrawRects(SDL_Window * window) | 117 DrawRects(SDL_Window * window, SDL_Renderer * renderer) |
122 { | 118 { |
123 int i; | 119 int i; |
124 SDL_Rect rect; | 120 SDL_Rect rect; |
125 int window_w, window_h; | 121 int window_w, window_h; |
126 | 122 |
127 /* Query the sizes */ | 123 /* Query the sizes */ |
128 SDL_GetWindowSize(window, &window_w, &window_h); | 124 SDL_GetWindowSize(window, &window_w, &window_h); |
129 | 125 |
130 SDL_SetRenderDrawBlendMode(blendMode); | |
131 for (i = 0; i < num_objects / 4; ++i) { | 126 for (i = 0; i < num_objects / 4; ++i) { |
132 /* Cycle the color and alpha, if desired */ | 127 /* Cycle the color and alpha, if desired */ |
133 if (cycle_color) { | 128 if (cycle_color) { |
134 current_color += cycle_direction; | 129 current_color += cycle_direction; |
135 if (current_color < 0) { | 130 if (current_color < 0) { |
150 if (current_alpha > 255) { | 145 if (current_alpha > 255) { |
151 current_alpha = 255; | 146 current_alpha = 255; |
152 cycle_direction = -cycle_direction; | 147 cycle_direction = -cycle_direction; |
153 } | 148 } |
154 } | 149 } |
155 SDL_SetRenderDrawColor(255, (Uint8) current_color, | 150 SDL_SetRenderDrawColor(renderer, 255, (Uint8) current_color, |
156 (Uint8) current_color, (Uint8) current_alpha); | 151 (Uint8) current_color, (Uint8) current_alpha); |
157 | 152 |
158 rect.w = rand() % (window_h / 2); | 153 rect.w = rand() % (window_h / 2); |
159 rect.h = rand() % (window_h / 2); | 154 rect.h = rand() % (window_h / 2); |
160 rect.x = (rand() % (window_w*2) - window_w) - (rect.w / 2); | 155 rect.x = (rand() % (window_w*2) - window_w) - (rect.w / 2); |
161 rect.y = (rand() % (window_h*2) - window_h) - (rect.h / 2); | 156 rect.y = (rand() % (window_h*2) - window_h) - (rect.h / 2); |
162 SDL_RenderFillRect(&rect); | 157 SDL_RenderFillRect(renderer, &rect); |
163 } | 158 } |
164 SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE); | |
165 } | 159 } |
166 | 160 |
167 int | 161 int |
168 main(int argc, char *argv[]) | 162 main(int argc, char *argv[]) |
169 { | 163 { |
221 return 2; | 215 return 2; |
222 } | 216 } |
223 | 217 |
224 /* Create the windows and initialize the renderers */ | 218 /* Create the windows and initialize the renderers */ |
225 for (i = 0; i < state->num_windows; ++i) { | 219 for (i = 0; i < state->num_windows; ++i) { |
226 SDL_SelectRenderer(state->windows[i]); | 220 SDL_Renderer *renderer = state->renderers[i]; |
227 SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF); | 221 SDL_SetRenderDrawBlendMode(renderer, blendMode); |
228 SDL_RenderClear(); | 222 SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF); |
223 SDL_RenderClear(renderer); | |
229 } | 224 } |
230 | 225 |
231 srand((unsigned int)time(NULL)); | 226 srand((unsigned int)time(NULL)); |
232 | 227 |
233 /* Main render loop */ | 228 /* Main render loop */ |
237 while (!done) { | 232 while (!done) { |
238 /* Check for events */ | 233 /* Check for events */ |
239 ++frames; | 234 ++frames; |
240 while (SDL_PollEvent(&event)) { | 235 while (SDL_PollEvent(&event)) { |
241 CommonEvent(state, &event, &done); | 236 CommonEvent(state, &event, &done); |
242 switch (event.type) { | |
243 case SDL_WINDOWEVENT: | |
244 switch (event.window.event) { | |
245 case SDL_WINDOWEVENT_EXPOSED: | |
246 SDL_SelectRenderer(SDL_GetWindowFromID(event.window.windowID)); | |
247 SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF); | |
248 SDL_RenderClear(); | |
249 break; | |
250 } | |
251 break; | |
252 default: | |
253 break; | |
254 } | |
255 } | 237 } |
256 for (i = 0; i < state->num_windows; ++i) { | 238 for (i = 0; i < state->num_windows; ++i) { |
257 SDL_SelectRenderer(state->windows[i]); | 239 SDL_Renderer *renderer = state->renderers[i]; |
258 SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF); | 240 SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF); |
259 SDL_RenderClear(); | 241 SDL_RenderClear(renderer); |
260 | 242 |
261 DrawRects(state->windows[i]); | 243 DrawRects(state->windows[i], renderer); |
262 DrawLines(state->windows[i]); | 244 DrawLines(state->windows[i], renderer); |
263 DrawPoints(state->windows[i]); | 245 DrawPoints(state->windows[i], renderer); |
264 | 246 |
265 SDL_RenderPresent(); | 247 SDL_RenderPresent(renderer); |
266 } | 248 } |
267 } | 249 } |
268 | 250 |
269 CommonQuit(state); | 251 CommonQuit(state); |
270 | 252 |