comparison test/testsprite2.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 b3ccd1947786
comparison
equal deleted inserted replaced
5149:3052772b59db 5150:ad50b3db78bd
4 #include <stdio.h> 4 #include <stdio.h>
5 #include <time.h> 5 #include <time.h>
6 6
7 #include "common.h" 7 #include "common.h"
8 8
9 #define NUM_SPRITES 100 9 #define NUM_SPRITES 100
10 #define MAX_SPEED 1 10 #define MAX_SPEED 1
11 11
12 static CommonState *state; 12 static CommonState *state;
13 static int num_sprites; 13 static int num_sprites;
14 static SDL_Texture **sprites; 14 static SDL_Texture **sprites;
15 static SDL_bool cycle_color; 15 static SDL_bool cycle_color;
74 } 74 }
75 } 75 }
76 76
77 /* Create textures from the image */ 77 /* Create textures from the image */
78 for (i = 0; i < state->num_windows; ++i) { 78 for (i = 0; i < state->num_windows; ++i) {
79 SDL_SelectRenderer(state->windows[i]); 79 SDL_Renderer *renderer = state->renderers[i];
80 sprites[i] = SDL_CreateTextureFromSurface(0, temp); 80 sprites[i] = SDL_CreateTextureFromSurface(renderer, 0, temp);
81 if (!sprites[i]) { 81 if (!sprites[i]) {
82 SDL_SetColorKey(temp, 0, 0); 82 SDL_SetColorKey(temp, 0, 0);
83 sprites[i] = SDL_CreateTextureFromSurface(0, temp); 83 sprites[i] = SDL_CreateTextureFromSurface(renderer, 0, temp);
84 } 84 }
85 if (!sprites[i]) { 85 if (!sprites[i]) {
86 fprintf(stderr, "Couldn't create texture: %s\n", SDL_GetError()); 86 fprintf(stderr, "Couldn't create texture: %s\n", SDL_GetError());
87 SDL_FreeSurface(temp); 87 SDL_FreeSurface(temp);
88 return (-1); 88 return (-1);
89 } 89 }
94 /* We're ready to roll. :) */ 94 /* We're ready to roll. :) */
95 return (0); 95 return (0);
96 } 96 }
97 97
98 void 98 void
99 MoveSprites(SDL_Window * window, SDL_Texture * sprite) 99 MoveSprites(SDL_Window * window, SDL_Renderer * renderer, SDL_Texture * sprite)
100 { 100 {
101 int i, n; 101 int i, n;
102 int window_w, window_h; 102 int window_w, window_h;
103 SDL_Rect temp; 103 SDL_Rect temp;
104 SDL_Rect *position, *velocity; 104 SDL_Rect *position, *velocity;
105
106 SDL_SelectRenderer(window);
107 105
108 /* Query the sizes */ 106 /* Query the sizes */
109 SDL_GetWindowSize(window, &window_w, &window_h); 107 SDL_GetWindowSize(window, &window_w, &window_h);
110 108
111 /* Cycle the color and alpha, if desired */ 109 /* Cycle the color and alpha, if desired */
134 } 132 }
135 SDL_SetTextureAlphaMod(sprite, (Uint8) current_alpha); 133 SDL_SetTextureAlphaMod(sprite, (Uint8) current_alpha);
136 } 134 }
137 135
138 /* Draw a gray background */ 136 /* Draw a gray background */
139 SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF); 137 SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
140 SDL_RenderClear(); 138 SDL_RenderClear(renderer);
141 139
142 /* Test points */ 140 /* Test points */
143 SDL_SetRenderDrawColor(0xFF, 0x00, 0x00, 0xFF); 141 SDL_SetRenderDrawColor(renderer, 0xFF, 0x00, 0x00, 0xFF);
144 SDL_RenderDrawPoint(0, 0); 142 SDL_RenderDrawPoint(renderer, 0, 0);
145 SDL_RenderDrawPoint(window_w-1, 0); 143 SDL_RenderDrawPoint(renderer, window_w-1, 0);
146 SDL_RenderDrawPoint(0, window_h-1); 144 SDL_RenderDrawPoint(renderer, 0, window_h-1);
147 SDL_RenderDrawPoint(window_w-1, window_h-1); 145 SDL_RenderDrawPoint(renderer, window_w-1, window_h-1);
148 146
149 /* Test horizontal and vertical lines */ 147 /* Test horizontal and vertical lines */
150 SDL_SetRenderDrawColor(0x00, 0xFF, 0x00, 0xFF); 148 SDL_SetRenderDrawColor(renderer, 0x00, 0xFF, 0x00, 0xFF);
151 SDL_RenderDrawLine(1, 0, window_w-2, 0); 149 SDL_RenderDrawLine(renderer, 1, 0, window_w-2, 0);
152 SDL_RenderDrawLine(1, window_h-1, window_w-2, window_h-1); 150 SDL_RenderDrawLine(renderer, 1, window_h-1, window_w-2, window_h-1);
153 SDL_RenderDrawLine(0, 1, 0, window_h-2); 151 SDL_RenderDrawLine(renderer, 0, 1, 0, window_h-2);
154 SDL_RenderDrawLine(window_w-1, 1, window_w-1, window_h-2); 152 SDL_RenderDrawLine(renderer, window_w-1, 1, window_w-1, window_h-2);
155 153
156 /* Test fill and copy */ 154 /* Test fill and copy */
157 SDL_SetRenderDrawColor(0xFF, 0xFF, 0xFF, 0xFF); 155 SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
158 temp.x = 1; 156 temp.x = 1;
159 temp.y = 1; 157 temp.y = 1;
160 temp.w = sprite_w; 158 temp.w = sprite_w;
161 temp.h = sprite_h; 159 temp.h = sprite_h;
162 SDL_RenderFillRect(&temp); 160 SDL_RenderFillRect(renderer, &temp);
163 SDL_RenderCopy(sprite, NULL, &temp); 161 SDL_RenderCopy(renderer, sprite, NULL, &temp);
164 temp.x = window_w-sprite_w-1; 162 temp.x = window_w-sprite_w-1;
165 temp.y = 1; 163 temp.y = 1;
166 temp.w = sprite_w; 164 temp.w = sprite_w;
167 temp.h = sprite_h; 165 temp.h = sprite_h;
168 SDL_RenderFillRect(&temp); 166 SDL_RenderFillRect(renderer, &temp);
169 SDL_RenderCopy(sprite, NULL, &temp); 167 SDL_RenderCopy(renderer, sprite, NULL, &temp);
170 temp.x = 1; 168 temp.x = 1;
171 temp.y = window_h-sprite_h-1; 169 temp.y = window_h-sprite_h-1;
172 temp.w = sprite_w; 170 temp.w = sprite_w;
173 temp.h = sprite_h; 171 temp.h = sprite_h;
174 SDL_RenderFillRect(&temp); 172 SDL_RenderFillRect(renderer, &temp);
175 SDL_RenderCopy(sprite, NULL, &temp); 173 SDL_RenderCopy(renderer, sprite, NULL, &temp);
176 temp.x = window_w-sprite_w-1; 174 temp.x = window_w-sprite_w-1;
177 temp.y = window_h-sprite_h-1; 175 temp.y = window_h-sprite_h-1;
178 temp.w = sprite_w; 176 temp.w = sprite_w;
179 temp.h = sprite_h; 177 temp.h = sprite_h;
180 SDL_RenderFillRect(&temp); 178 SDL_RenderFillRect(renderer, &temp);
181 SDL_RenderCopy(sprite, NULL, &temp); 179 SDL_RenderCopy(renderer, sprite, NULL, &temp);
182 180
183 /* Test diagonal lines */ 181 /* Test diagonal lines */
184 SDL_SetRenderDrawColor(0x00, 0xFF, 0x00, 0xFF); 182 SDL_SetRenderDrawColor(renderer, 0x00, 0xFF, 0x00, 0xFF);
185 SDL_RenderDrawLine(sprite_w, sprite_h, 183 SDL_RenderDrawLine(renderer, sprite_w, sprite_h,
186 window_w-sprite_w-2, window_h-sprite_h-2); 184 window_w-sprite_w-2, window_h-sprite_h-2);
187 SDL_RenderDrawLine(window_w-sprite_w-2, sprite_h, 185 SDL_RenderDrawLine(renderer, window_w-sprite_w-2, sprite_h,
188 sprite_w, window_h-sprite_h-2); 186 sprite_w, window_h-sprite_h-2);
189 187
190 /* Move the sprite, bounce at the wall, and draw */ 188 /* Move the sprite, bounce at the wall, and draw */
191 n = 0; 189 n = 0;
192 for (i = 0; i < num_sprites; ++i) { 190 for (i = 0; i < num_sprites; ++i) {
202 velocity->y = -velocity->y; 200 velocity->y = -velocity->y;
203 position->y += velocity->y; 201 position->y += velocity->y;
204 } 202 }
205 203
206 /* Blit the sprite onto the screen */ 204 /* Blit the sprite onto the screen */
207 SDL_RenderCopy(sprite, NULL, position); 205 SDL_RenderCopy(renderer, sprite, NULL, position);
208 } 206 }
209 207
210 /* Update the screen! */ 208 /* Update the screen! */
211 SDL_RenderPresent(); 209 SDL_RenderPresent(renderer);
212 } 210 }
213 211
214 int 212 int
215 main(int argc, char *argv[]) 213 main(int argc, char *argv[])
216 { 214 {
274 if (!sprites) { 272 if (!sprites) {
275 fprintf(stderr, "Out of memory!\n"); 273 fprintf(stderr, "Out of memory!\n");
276 quit(2); 274 quit(2);
277 } 275 }
278 for (i = 0; i < state->num_windows; ++i) { 276 for (i = 0; i < state->num_windows; ++i) {
279 SDL_SelectRenderer(state->windows[i]); 277 SDL_Renderer *renderer = state->renderers[i];
280 SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF); 278 SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
281 SDL_RenderClear(); 279 SDL_RenderClear(renderer);
282 } 280 }
283 if (LoadSprite("icon.bmp") < 0) { 281 if (LoadSprite("icon.bmp") < 0) {
284 quit(2); 282 quit(2);
285 } 283 }
286 284
312 while (!done) { 310 while (!done) {
313 /* Check for events */ 311 /* Check for events */
314 ++frames; 312 ++frames;
315 while (SDL_PollEvent(&event)) { 313 while (SDL_PollEvent(&event)) {
316 CommonEvent(state, &event, &done); 314 CommonEvent(state, &event, &done);
317 switch (event.type) {
318 case SDL_WINDOWEVENT:
319 switch (event.window.event) {
320 case SDL_WINDOWEVENT_EXPOSED:
321 SDL_SelectRenderer(SDL_GetWindowFromID(event.window.windowID));
322 SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
323 SDL_RenderClear();
324 break;
325 }
326 break;
327 default:
328 break;
329 }
330 } 315 }
331 for (i = 0; i < state->num_windows; ++i) { 316 for (i = 0; i < state->num_windows; ++i) {
332 MoveSprites(state->windows[i], sprites[i]); 317 MoveSprites(state->windows[i], state->renderers[i], sprites[i]);
333 } 318 }
334 } 319 }
335 320
336 /* Print out some timing information */ 321 /* Print out some timing information */
337 now = SDL_GetTicks(); 322 now = SDL_GetTicks();
338 if (now > then) { 323 if (now > then) {
339 double fps = ((double) frames * 1000) / (now - then); 324 double fps = ((double) frames * 1000) / (now - then);
340 printf("%2.2f frames per second\n", fps); 325 printf("%2.2f frames per second\n", fps);
341 } 326 }
342 quit(0); 327 quit(0);
343 return 0; 328 return 0;
344 } 329 }
345 330
346 /* vi: set ts=4 sw=4 expandtab: */ 331 /* vi: set ts=4 sw=4 expandtab: */