comparison test/testsprite2.c @ 1724:6c63fc2bd986 SDL-1.3

Proof of concept done - Win32 GDI implementation mostly complete.
author Sam Lantinga <slouken@libsdl.org>
date Thu, 06 Jul 2006 07:17:11 +0000
parents 931d111e737a
children 98a3207ddde8
comparison
equal deleted inserted replaced
1723:4bdbb9b2bd0a 1724:6c63fc2bd986
3 #include <stdlib.h> 3 #include <stdlib.h>
4 #include <time.h> 4 #include <time.h>
5 5
6 #include "SDL.h" 6 #include "SDL.h"
7 7
8 #define NUM_WINDOWS 2 8 #define NUM_WINDOWS 4
9 #define WINDOW_W 640 9 #define WINDOW_W 640
10 #define WINDOW_H 480 10 #define WINDOW_H 480
11 #define NUM_SPRITES 100 11 #define NUM_SPRITES 100
12 #define MAX_SPEED 1 12 #define MAX_SPEED 1
13 #define BACKGROUND 0x00FFFFFF
13 14
14 static int num_windows; 15 static int num_windows;
15 static int num_sprites; 16 static int num_sprites;
16 static SDL_WindowID *windows; 17 static SDL_WindowID *windows;
17 static SDL_TextureID *sprites; 18 static SDL_TextureID *sprites;
83 int window_w, window_h; 84 int window_w, window_h;
84 SDL_Rect area, *position, *velocity; 85 SDL_Rect area, *position, *velocity;
85 86
86 SDL_SelectRenderer(window); 87 SDL_SelectRenderer(window);
87 88
88 SDL_RenderFill(NULL, 0);
89
90 /* Query the sizes */ 89 /* Query the sizes */
91 SDL_GetWindowSize(window, &window_w, &window_h); 90 SDL_GetWindowSize(window, &window_w, &window_h);
92 91
93 /* Move the sprite, bounce at the wall, and draw */ 92 /* Move the sprite, bounce at the wall, and draw */
94 n = 0; 93 n = 0;
94 for (i = 0; i < num_sprites; ++i) {
95 position = &positions[i];
96 SDL_RenderFill(position, BACKGROUND);
97 }
95 for (i = 0; i < num_sprites; ++i) { 98 for (i = 0; i < num_sprites; ++i) {
96 position = &positions[i]; 99 position = &positions[i];
97 velocity = &velocities[i]; 100 velocity = &velocities[i];
98 position->x += velocity->x; 101 position->x += velocity->x;
99 if ((position->x < 0) || (position->x >= (window_w - sprite_w))) { 102 if ((position->x < 0) || (position->x >= (window_w - sprite_w))) {
166 for (i = 0; i < num_windows; ++i) { 169 for (i = 0; i < num_windows; ++i) {
167 char title[32]; 170 char title[32];
168 171
169 SDL_snprintf(title, sizeof(title), "testsprite %d", i + 1); 172 SDL_snprintf(title, sizeof(title), "testsprite %d", i + 1);
170 windows[i] = 173 windows[i] =
171 SDL_CreateWindow(title, -1, -1, window_w, window_h, 174 SDL_CreateWindow(title, SDL_WINDOWPOS_UNDEFINED,
175 SDL_WINDOWPOS_UNDEFINED, window_w, window_h,
172 SDL_WINDOW_SHOWN); 176 SDL_WINDOW_SHOWN);
173 if (!windows[i]) { 177 if (!windows[i]) {
174 fprintf(stderr, "Couldn't create window: %s\n", SDL_GetError()); 178 fprintf(stderr, "Couldn't create window: %s\n", SDL_GetError());
175 quit(2); 179 quit(2);
176 } 180 }
177 181
178 if (SDL_CreateRenderer(windows[i], -1, 0) < 0) { 182 if (SDL_CreateRenderer(windows[i], -1, 0) < 0) {
179 fprintf(stderr, "Couldn't create renderer: %s\n", SDL_GetError()); 183 fprintf(stderr, "Couldn't create renderer: %s\n", SDL_GetError());
180 quit(2); 184 quit(2);
181 } 185 }
186 SDL_RenderFill(NULL, BACKGROUND);
182 } 187 }
183 if (LoadSprite("icon.bmp") < 0) { 188 if (LoadSprite("icon.bmp") < 0) {
184 quit(2); 189 quit(2);
185 } 190 }
186 191
212 while (!done) { 217 while (!done) {
213 /* Check for events */ 218 /* Check for events */
214 ++frames; 219 ++frames;
215 while (SDL_PollEvent(&event)) { 220 while (SDL_PollEvent(&event)) {
216 switch (event.type) { 221 switch (event.type) {
222 case SDL_WINDOWEVENT:
223 switch (event.window.event) {
224 case SDL_WINDOWEVENT_EXPOSED:
225 SDL_SelectRenderer(event.window.windowID);
226 SDL_RenderFill(NULL, BACKGROUND);
227 break;
228 case SDL_WINDOWEVENT_CLOSE:
229 done = 1;
230 break;
231 }
232 break;
217 case SDL_KEYDOWN: 233 case SDL_KEYDOWN:
218 /* Any keypress quits the app... */ 234 /* Any keypress quits the app... */
219 case SDL_QUIT: 235 case SDL_QUIT:
220 done = 1; 236 done = 1;
221 break; 237 break;