comparison test/testsprite2.c @ 1985:8055185ae4ed

Added source color and alpha modulation support. Added perl script to generate optimized render copy functions.
author Sam Lantinga <slouken@libsdl.org>
date Mon, 28 Aug 2006 03:17:39 +0000
parents a788656ca29a
children 926294b2bb4e
comparison
equal deleted inserted replaced
1984:b910bcabec26 1985:8055185ae4ed
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 #define BACKGROUND 0x00A0A0A0
12 11
13 static CommonState *state; 12 static CommonState *state;
14 static int num_sprites; 13 static int num_sprites;
15 static SDL_TextureID *sprites; 14 static SDL_TextureID *sprites;
15 static SDL_bool cycle_color;
16 static SDL_bool cycle_alpha;
17 static int cycle_direction = 1;
18 static int current_alpha = 0;
19 static int current_color = 0;
16 static SDL_Rect *positions; 20 static SDL_Rect *positions;
17 static SDL_Rect *velocities; 21 static SDL_Rect *velocities;
18 static int sprite_w, sprite_h; 22 static int sprite_w, sprite_h;
19 static SDL_TextureBlendMode blendMode = SDL_TEXTUREBLENDMODE_MASK; 23 static SDL_TextureBlendMode blendMode = SDL_TEXTUREBLENDMODE_MASK;
20 static SDL_TextureScaleMode scaleMode = SDL_TEXTURESCALEMODE_NONE; 24 static SDL_TextureScaleMode scaleMode = SDL_TEXTURESCALEMODE_NONE;
64 if (!sprites[i]) { 68 if (!sprites[i]) {
65 fprintf(stderr, "Couldn't create texture: %s\n", SDL_GetError()); 69 fprintf(stderr, "Couldn't create texture: %s\n", SDL_GetError());
66 SDL_FreeSurface(temp); 70 SDL_FreeSurface(temp);
67 return (-1); 71 return (-1);
68 } 72 }
73 SDL_SetTextureBlendMode(sprites[i], blendMode);
74 SDL_SetTextureScaleMode(sprites[i], scaleMode);
69 } 75 }
70 SDL_FreeSurface(temp); 76 SDL_FreeSurface(temp);
71 77
72 /* We're ready to roll. :) */ 78 /* We're ready to roll. :) */
73 return (0); 79 return (0);
83 SDL_SelectRenderer(window); 89 SDL_SelectRenderer(window);
84 90
85 /* Query the sizes */ 91 /* Query the sizes */
86 SDL_GetWindowSize(window, &window_w, &window_h); 92 SDL_GetWindowSize(window, &window_w, &window_h);
87 93
94 /* Cycle the color and alpha, if desired */
95 if (cycle_color) {
96 current_color += cycle_direction;
97 if (current_color < 0) {
98 current_color = 0;
99 cycle_direction = -cycle_direction;
100 }
101 if (current_color > 255) {
102 current_color = 255;
103 cycle_direction = -cycle_direction;
104 }
105 SDL_SetTextureColorMod(sprite, 255, (Uint8) current_color,
106 (Uint8) current_color);
107 }
108 if (cycle_alpha) {
109 current_alpha += cycle_direction;
110 if (current_alpha < 0) {
111 current_alpha = 0;
112 cycle_direction = -cycle_direction;
113 }
114 if (current_alpha > 255) {
115 current_alpha = 255;
116 cycle_direction = -cycle_direction;
117 }
118 SDL_SetTextureAlphaMod(sprite, (Uint8) current_alpha);
119 }
120
88 /* Move the sprite, bounce at the wall, and draw */ 121 /* Move the sprite, bounce at the wall, and draw */
89 n = 0; 122 n = 0;
90 SDL_RenderFill(NULL, BACKGROUND); 123 SDL_RenderFill(0xA0, 0xA0, 0xA0, 0xFF, NULL);
91 for (i = 0; i < num_sprites; ++i) { 124 for (i = 0; i < num_sprites; ++i) {
92 position = &positions[i]; 125 position = &positions[i];
93 velocity = &velocities[i]; 126 velocity = &velocities[i];
94 position->x += velocity->x; 127 position->x += velocity->x;
95 if ((position->x < 0) || (position->x >= (window_w - sprite_w))) { 128 if ((position->x < 0) || (position->x >= (window_w - sprite_w))) {
101 velocity->y = -velocity->y; 134 velocity->y = -velocity->y;
102 position->y += velocity->y; 135 position->y += velocity->y;
103 } 136 }
104 137
105 /* Blit the sprite onto the screen */ 138 /* Blit the sprite onto the screen */
106 SDL_RenderCopy(sprite, NULL, position, blendMode, scaleMode); 139 SDL_RenderCopy(sprite, NULL, position);
107 } 140 }
108 141
109 /* Update the screen! */ 142 /* Update the screen! */
110 SDL_RenderPresent(); 143 SDL_RenderPresent();
111 } 144 }
164 } else if (SDL_strcasecmp(argv[i + 1], "best") == 0) { 197 } else if (SDL_strcasecmp(argv[i + 1], "best") == 0) {
165 scaleMode = SDL_TEXTURESCALEMODE_BEST; 198 scaleMode = SDL_TEXTURESCALEMODE_BEST;
166 consumed = 2; 199 consumed = 2;
167 } 200 }
168 } 201 }
202 } else if (SDL_strcasecmp(argv[i], "--cyclecolor") == 0) {
203 cycle_color = SDL_TRUE;
204 consumed = 1;
205 } else if (SDL_strcasecmp(argv[i], "--cyclealpha") == 0) {
206 cycle_alpha = SDL_TRUE;
207 consumed = 1;
169 } else if (SDL_isdigit(*argv[i])) { 208 } else if (SDL_isdigit(*argv[i])) {
170 num_sprites = SDL_atoi(argv[i]); 209 num_sprites = SDL_atoi(argv[i]);
171 consumed = 1; 210 consumed = 1;
172 } 211 }
173 } 212 }
174 if (consumed < 0) { 213 if (consumed < 0) {
175 fprintf(stderr, 214 fprintf(stderr,
176 "Usage: %s %s [--blend none|mask|blend|add|mod] [--scale none|fast|slow|best]", 215 "Usage: %s %s [--blend none|mask|blend|add|mod] [--scale none|fast|slow|best] [--cyclecolor] [--cyclealpha]\n",
177 argv[0], CommonUsage(state)); 216 argv[0], CommonUsage(state));
178 quit(1); 217 quit(1);
179 } 218 }
180 i += consumed; 219 i += consumed;
181 } 220 }
190 fprintf(stderr, "Out of memory!\n"); 229 fprintf(stderr, "Out of memory!\n");
191 quit(2); 230 quit(2);
192 } 231 }
193 for (i = 0; i < state->num_windows; ++i) { 232 for (i = 0; i < state->num_windows; ++i) {
194 SDL_SelectRenderer(state->windows[i]); 233 SDL_SelectRenderer(state->windows[i]);
195 SDL_RenderFill(NULL, BACKGROUND); 234 SDL_RenderFill(0xA0, 0xA0, 0xA0, 0xFF, NULL);
196 } 235 }
197 if (LoadSprite("icon.bmp") < 0) { 236 if (LoadSprite("icon.bmp") < 0) {
198 quit(2); 237 quit(2);
199 } 238 }
200 239
235 switch (event.type) { 274 switch (event.type) {
236 case SDL_WINDOWEVENT: 275 case SDL_WINDOWEVENT:
237 switch (event.window.event) { 276 switch (event.window.event) {
238 case SDL_WINDOWEVENT_EXPOSED: 277 case SDL_WINDOWEVENT_EXPOSED:
239 SDL_SelectRenderer(event.window.windowID); 278 SDL_SelectRenderer(event.window.windowID);
240 SDL_RenderFill(NULL, BACKGROUND); 279 SDL_RenderFill(0xA0, 0xA0, 0xA0, 0xFF, NULL);
241 break; 280 break;
242 } 281 }
243 break; 282 break;
244 default: 283 default:
245 break; 284 break;