comparison test/testsprite2.c @ 1914:051df511279c

Added a test program framework for easy initialization. Started work on multi-window OpenGL demo
author Sam Lantinga <slouken@libsdl.org>
date Tue, 18 Jul 2006 07:49:51 +0000
parents 06c27a737b7a
children a228436a2404
comparison
equal deleted inserted replaced
1913:83420da906a5 1914:051df511279c
2 2
3 #include <stdlib.h> 3 #include <stdlib.h>
4 #include <stdio.h> 4 #include <stdio.h>
5 #include <time.h> 5 #include <time.h>
6 6
7 #include "SDL.h" 7 #include "common.h"
8 8
9 #define NUM_WINDOWS 4
10 #define WINDOW_W 640
11 #define WINDOW_H 480
12 #define NUM_SPRITES 100 9 #define NUM_SPRITES 100
13 #define MAX_SPEED 1 10 #define MAX_SPEED 1
14 #define BACKGROUND 0x00FFFFFF 11 #define BACKGROUND 0x00FFFFFF
15 12
16 static int num_windows; 13 static CommonState *state;
17 static int num_sprites; 14 static int num_sprites;
18 static SDL_WindowID *windows;
19 static SDL_TextureID *sprites; 15 static SDL_TextureID *sprites;
20 static SDL_Rect *positions; 16 static SDL_Rect *positions;
21 static SDL_Rect *velocities; 17 static SDL_Rect *velocities;
22 static int sprite_w, sprite_h; 18 static int sprite_w, sprite_h;
23 19
24 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */ 20 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
25 static void 21 static void
26 quit(int rc) 22 quit(int rc)
27 { 23 {
28 if (windows) {
29 SDL_free(windows);
30 }
31 if (sprites) { 24 if (sprites) {
32 SDL_free(sprites); 25 SDL_free(sprites);
33 } 26 }
34 if (positions) { 27 if (positions) {
35 SDL_free(positions); 28 SDL_free(positions);
36 } 29 }
37 if (velocities) { 30 if (velocities) {
38 SDL_free(velocities); 31 SDL_free(velocities);
39 } 32 }
40 SDL_Quit(); 33 CommonQuit(state);
41 exit(rc); 34 exit(rc);
42 } 35 }
43 36
44 int 37 int
45 LoadSprite(char *file) 38 LoadSprite(char *file)
60 if (temp->format->palette) { 53 if (temp->format->palette) {
61 SDL_SetColorKey(temp, SDL_SRCCOLORKEY, *(Uint8 *) temp->pixels); 54 SDL_SetColorKey(temp, SDL_SRCCOLORKEY, *(Uint8 *) temp->pixels);
62 } 55 }
63 56
64 /* Create textures from the image */ 57 /* Create textures from the image */
65 for (i = 0; i < num_windows; ++i) { 58 for (i = 0; i < state->num_windows; ++i) {
66 SDL_SelectRenderer(windows[i]); 59 SDL_SelectRenderer(state->windows[i]);
67 sprites[i] = 60 sprites[i] =
68 SDL_CreateTextureFromSurface(0, SDL_TextureAccess_Remote, temp); 61 SDL_CreateTextureFromSurface(0, SDL_TextureAccess_Remote, temp);
69 if (!sprites[i]) { 62 if (!sprites[i]) {
70 fprintf(stderr, "Couldn't create texture: %s\n", SDL_GetError()); 63 fprintf(stderr, "Couldn't create texture: %s\n", SDL_GetError());
71 SDL_FreeSurface(temp); 64 SDL_FreeSurface(temp);
123 } 116 }
124 117
125 int 118 int
126 main(int argc, char *argv[]) 119 main(int argc, char *argv[])
127 { 120 {
128 int window_w, window_h;
129 Uint32 window_flags = SDL_WINDOW_SHOWN;
130 Uint32 render_flags = 0;
131 SDL_DisplayMode *mode, fullscreen_mode;
132 int i, done; 121 int i, done;
133 SDL_Event event; 122 SDL_Event event;
134 Uint32 then, now, frames; 123 Uint32 then, now, frames;
135 124
136 /* Initialize SDL */ 125 /* Initialize parameters */
137 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
138 fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
139 return (1);
140 }
141
142 num_windows = NUM_WINDOWS;
143 num_sprites = NUM_SPRITES; 126 num_sprites = NUM_SPRITES;
144 window_w = WINDOW_W; 127
145 window_h = WINDOW_H; 128 /* Initialize test framework */
146 for (i = 1; i < argc; ++i) { 129 state = CommonCreateState(argv, SDL_INIT_VIDEO);
147 if (strcmp(argv[i], "-width") == 0 && (i + 1 < argc)) { 130 if (!state) {
148 window_w = atoi(argv[++i]); 131 return 1;
149 } else if (strcmp(argv[i], "-height") == 0 && (i + 1 < argc)) { 132 }
150 window_h = atoi(argv[++i]); 133 for (i = 1; i < argc;) {
151 } else if (strcmp(argv[i], "-windows") == 0 && (i + 1 < argc)) { 134 int consumed;
152 num_windows = atoi(argv[++i]); 135
153 window_flags &= ~SDL_WINDOW_FULLSCREEN; 136 consumed = CommonArg(state, i);
154 } else if (strcmp(argv[i], "-fullscreen") == 0) { 137 if (consumed < 0) {
155 num_windows = 1; 138 fprintf(stderr, "Usage: %s %s", argv[0], CommonUsage(state));
156 window_flags |= SDL_WINDOW_FULLSCREEN;
157 } else if (strcmp(argv[i], "-sync") == 0) {
158 render_flags |= SDL_Renderer_PresentVSync;
159 } else if (isdigit(argv[i][0])) {
160 num_sprites = atoi(argv[i]);
161 } else {
162 fprintf(stderr,
163 "Usage: %s [-width N] [-height N] [-windows N] [-fullscreen] [-sync] [numsprites]\n",
164 argv[0]);
165 quit(1); 139 quit(1);
166 } 140 }
167 } 141 if (consumed == 0) {
168 142 num_sprites = SDL_atoi(argv[i]);
169 if (window_flags & SDL_WINDOW_FULLSCREEN) { 143 consumed = 1;
170 SDL_zero(fullscreen_mode); 144 }
171 fullscreen_mode.w = window_w; 145 i += consumed;
172 fullscreen_mode.h = window_h; 146 }
173 SDL_SetFullscreenDisplayMode(&fullscreen_mode); 147 if (!CommonInit(state)) {
148 quit(2);
174 } 149 }
175 150
176 /* Create the windows, initialize the renderers, and load the textures */ 151 /* Create the windows, initialize the renderers, and load the textures */
177 windows = (SDL_WindowID *) SDL_malloc(num_windows * sizeof(*windows)); 152 sprites =
178 sprites = (SDL_TextureID *) SDL_malloc(num_windows * sizeof(*sprites)); 153 (SDL_TextureID *) SDL_malloc(state->num_windows * sizeof(*sprites));
179 if (!windows || !sprites) { 154 if (!sprites) {
180 fprintf(stderr, "Out of memory!\n"); 155 fprintf(stderr, "Out of memory!\n");
181 quit(2); 156 quit(2);
182 } 157 }
183 for (i = 0; i < num_windows; ++i) { 158 for (i = 0; i < state->num_windows; ++i) {
184 char title[32]; 159 SDL_SelectRenderer(state->windows[i]);
185
186 SDL_snprintf(title, sizeof(title), "testsprite %d", i + 1);
187 windows[i] =
188 SDL_CreateWindow(title, SDL_WINDOWPOS_UNDEFINED,
189 SDL_WINDOWPOS_UNDEFINED, window_w, window_h,
190 window_flags);
191 if (!windows[i]) {
192 fprintf(stderr, "Couldn't create window: %s\n", SDL_GetError());
193 quit(2);
194 }
195
196 if (SDL_CreateRenderer(windows[i], -1, render_flags) < 0) {
197 fprintf(stderr, "Couldn't create renderer: %s\n", SDL_GetError());
198 quit(2);
199 }
200 SDL_RenderFill(NULL, BACKGROUND); 160 SDL_RenderFill(NULL, BACKGROUND);
201 } 161 }
202 if (LoadSprite("icon.bmp") < 0) { 162 if (LoadSprite("icon.bmp") < 0) {
203 quit(2); 163 quit(2);
204 } 164 }
210 fprintf(stderr, "Out of memory!\n"); 170 fprintf(stderr, "Out of memory!\n");
211 quit(2); 171 quit(2);
212 } 172 }
213 srand(time(NULL)); 173 srand(time(NULL));
214 for (i = 0; i < num_sprites; ++i) { 174 for (i = 0; i < num_sprites; ++i) {
215 positions[i].x = rand() % (window_w - sprite_w); 175 positions[i].x = rand() % (state->window_w - sprite_w);
216 positions[i].y = rand() % (window_h - sprite_h); 176 positions[i].y = rand() % (state->window_h - sprite_h);
217 positions[i].w = sprite_w; 177 positions[i].w = sprite_w;
218 positions[i].h = sprite_h; 178 positions[i].h = sprite_h;
219 velocities[i].x = 0; 179 velocities[i].x = 0;
220 velocities[i].y = 0; 180 velocities[i].y = 0;
221 while (!velocities[i].x && !velocities[i].y) { 181 while (!velocities[i].x && !velocities[i].y) {
230 done = 0; 190 done = 0;
231 while (!done) { 191 while (!done) {
232 /* Check for events */ 192 /* Check for events */
233 ++frames; 193 ++frames;
234 while (SDL_PollEvent(&event)) { 194 while (SDL_PollEvent(&event)) {
195 CommonEvent(state, &event, &done);
235 switch (event.type) { 196 switch (event.type) {
236 case SDL_WINDOWEVENT: 197 case SDL_WINDOWEVENT:
237 switch (event.window.event) { 198 switch (event.window.event) {
238 case SDL_WINDOWEVENT_EXPOSED: 199 case SDL_WINDOWEVENT_EXPOSED:
239 SDL_SelectRenderer(event.window.windowID); 200 SDL_SelectRenderer(event.window.windowID);
240 SDL_RenderFill(NULL, BACKGROUND); 201 SDL_RenderFill(NULL, BACKGROUND);
241 break; 202 break;
242 case SDL_WINDOWEVENT_CLOSE:
243 done = 1;
244 break;
245 } 203 }
246 break;
247 case SDL_KEYDOWN:
248 /* Any keypress quits the app... */
249 case SDL_QUIT:
250 done = 1;
251 break; 204 break;
252 default: 205 default:
253 break; 206 break;
254 } 207 }
255 } 208 }
256 for (i = 0; i < num_windows; ++i) { 209 for (i = 0; i < state->num_windows; ++i) {
257 MoveSprites(windows[i], sprites[i]); 210 MoveSprites(state->windows[i], sprites[i]);
258 } 211 }
259 } 212 }
260 213
261 /* Print out some timing information */ 214 /* Print out some timing information */
262 now = SDL_GetTicks(); 215 now = SDL_GetTicks();