Mercurial > sdl-ios-xcode
comparison test/testalpha.c @ 1662:782fd950bd46 SDL-1.3
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
WARNING: None of the video drivers have been updated for the new API yet! The API is still under design and very fluid.
The code is now run through a consistent indent format:
indent -i4 -nut -nsc -br -ce
The headers are being converted to automatically generate doxygen documentation.
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Sun, 28 May 2006 13:04:16 +0000 |
parents | 14717b52abc0 |
children | 4da1ee79c9af |
comparison
equal
deleted
inserted
replaced
1661:281d3f4870e5 | 1662:782fd950bd46 |
---|---|
8 #include <string.h> | 8 #include <string.h> |
9 #include <math.h> | 9 #include <math.h> |
10 | 10 |
11 #include "SDL.h" | 11 #include "SDL.h" |
12 | 12 |
13 #define FRAME_TICKS (1000/30) /* 30 frames/second */ | 13 #define FRAME_TICKS (1000/30) /* 30 frames/second */ |
14 | 14 |
15 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */ | 15 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */ |
16 static void quit(int rc) | 16 static void |
17 { | 17 quit (int rc) |
18 SDL_Quit(); | 18 { |
19 exit(rc); | 19 SDL_Quit (); |
20 exit (rc); | |
20 } | 21 } |
21 | 22 |
22 /* Fill the screen with a gradient */ | 23 /* Fill the screen with a gradient */ |
23 static void FillBackground(SDL_Surface *screen) | 24 static void |
24 { | 25 FillBackground (SDL_Surface * screen) |
25 Uint8 *buffer; | 26 { |
26 Uint16 *buffer16; | 27 Uint8 *buffer; |
27 Uint16 color; | 28 Uint16 *buffer16; |
28 Uint8 gradient; | 29 Uint16 color; |
29 int i, k; | 30 Uint8 gradient; |
30 | 31 int i, k; |
31 /* Set the surface pixels and refresh! */ | 32 |
32 if ( SDL_LockSurface(screen) < 0 ) { | 33 /* Set the surface pixels and refresh! */ |
33 fprintf(stderr, "Couldn't lock the display surface: %s\n", | 34 if (SDL_LockSurface (screen) < 0) { |
34 SDL_GetError()); | 35 fprintf (stderr, "Couldn't lock the display surface: %s\n", |
35 quit(2); | 36 SDL_GetError ()); |
36 } | 37 quit (2); |
37 buffer=(Uint8 *)screen->pixels; | 38 } |
38 if (screen->format->BytesPerPixel!=2) { | 39 buffer = (Uint8 *) screen->pixels; |
39 for ( i=0; i<screen->h; ++i ) { | 40 if (screen->format->BytesPerPixel != 2) { |
40 memset(buffer,(i*255)/screen->h, screen->w*screen->format->BytesPerPixel); | 41 for (i = 0; i < screen->h; ++i) { |
41 buffer += screen->pitch; | 42 memset (buffer, (i * 255) / screen->h, |
42 } | 43 screen->w * screen->format->BytesPerPixel); |
43 } | 44 buffer += screen->pitch; |
44 else | 45 } |
45 { | 46 } else { |
46 for ( i=0; i<screen->h; ++i ) { | 47 for (i = 0; i < screen->h; ++i) { |
47 gradient=((i*255)/screen->h); | 48 gradient = ((i * 255) / screen->h); |
48 color = (Uint16)SDL_MapRGB(screen->format, gradient, gradient, gradient); | 49 color = |
49 buffer16=(Uint16*)buffer; | 50 (Uint16) SDL_MapRGB (screen->format, gradient, gradient, |
50 for (k=0; k<screen->w; k++) | 51 gradient); |
51 { | 52 buffer16 = (Uint16 *) buffer; |
52 *(buffer16+k)=color; | 53 for (k = 0; k < screen->w; k++) { |
53 } | 54 *(buffer16 + k) = color; |
54 buffer += screen->pitch; | 55 } |
55 } | 56 buffer += screen->pitch; |
56 } | 57 } |
57 | 58 } |
58 SDL_UnlockSurface(screen); | 59 |
59 SDL_UpdateRect(screen, 0, 0, 0, 0); | 60 SDL_UnlockSurface (screen); |
61 SDL_UpdateRect (screen, 0, 0, 0, 0); | |
60 } | 62 } |
61 | 63 |
62 /* Create a "light" -- a yellowish surface with variable alpha */ | 64 /* Create a "light" -- a yellowish surface with variable alpha */ |
63 SDL_Surface *CreateLight(int radius) | 65 SDL_Surface * |
64 { | 66 CreateLight (int radius) |
65 Uint8 trans, alphamask; | 67 { |
66 int range, addition; | 68 Uint8 trans, alphamask; |
67 int xdist, ydist; | 69 int range, addition; |
68 Uint16 x, y; | 70 int xdist, ydist; |
69 Uint16 skip; | 71 Uint16 x, y; |
70 Uint32 pixel; | 72 Uint16 skip; |
71 SDL_Surface *light; | 73 Uint32 pixel; |
74 SDL_Surface *light; | |
72 | 75 |
73 #ifdef LIGHT_16BIT | 76 #ifdef LIGHT_16BIT |
74 Uint16 *buf; | 77 Uint16 *buf; |
75 | 78 |
76 /* Create a 16 (4/4/4/4) bpp square with a full 4-bit alpha channel */ | 79 /* Create a 16 (4/4/4/4) bpp square with a full 4-bit alpha channel */ |
77 /* Note: this isn't any faster than a 32 bit alpha surface */ | 80 /* Note: this isn't any faster than a 32 bit alpha surface */ |
78 alphamask = 0x0000000F; | 81 alphamask = 0x0000000F; |
79 light = SDL_CreateRGBSurface(SDL_SWSURFACE, 2*radius, 2*radius, 16, | 82 light = SDL_CreateRGBSurface (SDL_SWSURFACE, 2 * radius, 2 * radius, 16, |
80 0x0000F000, 0x00000F00, 0x000000F0, alphamask); | 83 0x0000F000, 0x00000F00, 0x000000F0, |
84 alphamask); | |
81 #else | 85 #else |
82 Uint32 *buf; | 86 Uint32 *buf; |
83 | 87 |
84 /* Create a 32 (8/8/8/8) bpp square with a full 8-bit alpha channel */ | 88 /* Create a 32 (8/8/8/8) bpp square with a full 8-bit alpha channel */ |
85 alphamask = 0x000000FF; | 89 alphamask = 0x000000FF; |
86 light = SDL_CreateRGBSurface(SDL_SWSURFACE, 2*radius, 2*radius, 32, | 90 light = SDL_CreateRGBSurface (SDL_SWSURFACE, 2 * radius, 2 * radius, 32, |
87 0xFF000000, 0x00FF0000, 0x0000FF00, alphamask); | 91 0xFF000000, 0x00FF0000, 0x0000FF00, |
88 if ( light == NULL ) { | 92 alphamask); |
89 fprintf(stderr, "Couldn't create light: %s\n", SDL_GetError()); | 93 if (light == NULL) { |
90 return(NULL); | 94 fprintf (stderr, "Couldn't create light: %s\n", SDL_GetError ()); |
91 } | 95 return (NULL); |
96 } | |
92 #endif | 97 #endif |
93 | 98 |
94 /* Fill with a light yellow-orange color */ | 99 /* Fill with a light yellow-orange color */ |
95 skip = light->pitch-(light->w*light->format->BytesPerPixel); | 100 skip = light->pitch - (light->w * light->format->BytesPerPixel); |
96 #ifdef LIGHT_16BIT | 101 #ifdef LIGHT_16BIT |
97 buf = (Uint16 *)light->pixels; | 102 buf = (Uint16 *) light->pixels; |
98 #else | 103 #else |
99 buf = (Uint32 *)light->pixels; | 104 buf = (Uint32 *) light->pixels; |
100 #endif | 105 #endif |
101 /* Get a tranparent pixel value - we'll add alpha later */ | 106 /* Get a tranparent pixel value - we'll add alpha later */ |
102 pixel = SDL_MapRGBA(light->format, 0xFF, 0xDD, 0x88, 0); | 107 pixel = SDL_MapRGBA (light->format, 0xFF, 0xDD, 0x88, 0); |
103 for ( y=0; y<light->h; ++y ) { | 108 for (y = 0; y < light->h; ++y) { |
104 for ( x=0; x<light->w; ++x ) { | 109 for (x = 0; x < light->w; ++x) { |
105 *buf++ = pixel; | 110 *buf++ = pixel; |
106 } | 111 } |
107 buf += skip; /* Almost always 0, but just in case... */ | 112 buf += skip; /* Almost always 0, but just in case... */ |
108 } | 113 } |
109 | 114 |
110 /* Calculate alpha values for the surface. */ | 115 /* Calculate alpha values for the surface. */ |
111 #ifdef LIGHT_16BIT | 116 #ifdef LIGHT_16BIT |
112 buf = (Uint16 *)light->pixels; | 117 buf = (Uint16 *) light->pixels; |
113 #else | 118 #else |
114 buf = (Uint32 *)light->pixels; | 119 buf = (Uint32 *) light->pixels; |
115 #endif | 120 #endif |
116 for ( y=0; y<light->h; ++y ) { | 121 for (y = 0; y < light->h; ++y) { |
117 for ( x=0; x<light->w; ++x ) { | 122 for (x = 0; x < light->w; ++x) { |
118 /* Slow distance formula (from center of light) */ | 123 /* Slow distance formula (from center of light) */ |
119 xdist = x-(light->w/2); | 124 xdist = x - (light->w / 2); |
120 ydist = y-(light->h/2); | 125 ydist = y - (light->h / 2); |
121 range = (int)sqrt(xdist*xdist+ydist*ydist); | 126 range = (int) sqrt (xdist * xdist + ydist * ydist); |
122 | 127 |
123 /* Scale distance to range of transparency (0-255) */ | 128 /* Scale distance to range of transparency (0-255) */ |
124 if ( range > radius ) { | 129 if (range > radius) { |
125 trans = alphamask; | 130 trans = alphamask; |
126 } else { | 131 } else { |
127 /* Increasing transparency with distance */ | 132 /* Increasing transparency with distance */ |
128 trans = (Uint8)((range*alphamask)/radius); | 133 trans = (Uint8) ((range * alphamask) / radius); |
129 | 134 |
130 /* Lights are very transparent */ | 135 /* Lights are very transparent */ |
131 addition = (alphamask+1)/8; | 136 addition = (alphamask + 1) / 8; |
132 if ( (int)trans+addition > alphamask ) { | 137 if ((int) trans + addition > alphamask) { |
133 trans = alphamask; | 138 trans = alphamask; |
134 } else { | 139 } else { |
135 trans += addition; | 140 trans += addition; |
136 } | 141 } |
137 } | 142 } |
138 /* We set the alpha component as the right N bits */ | 143 /* We set the alpha component as the right N bits */ |
139 *buf++ |= (255-trans); | 144 *buf++ |= (255 - trans); |
140 } | 145 } |
141 buf += skip; /* Almost always 0, but just in case... */ | 146 buf += skip; /* Almost always 0, but just in case... */ |
142 } | 147 } |
143 /* Enable RLE acceleration of this alpha surface */ | 148 /* Enable RLE acceleration of this alpha surface */ |
144 SDL_SetAlpha(light, SDL_SRCALPHA|SDL_RLEACCEL, 0); | 149 SDL_SetAlpha (light, SDL_SRCALPHA | SDL_RLEACCEL, 0); |
145 | 150 |
146 /* We're done! */ | 151 /* We're done! */ |
147 return(light); | 152 return (light); |
148 } | 153 } |
149 | 154 |
150 static Uint32 flashes = 0; | 155 static Uint32 flashes = 0; |
151 static Uint32 flashtime = 0; | 156 static Uint32 flashtime = 0; |
152 | 157 |
153 void FlashLight(SDL_Surface *screen, SDL_Surface *light, int x, int y) | 158 void |
154 { | 159 FlashLight (SDL_Surface * screen, SDL_Surface * light, int x, int y) |
155 SDL_Rect position; | 160 { |
156 Uint32 ticks1; | 161 SDL_Rect position; |
157 Uint32 ticks2; | 162 Uint32 ticks1; |
158 | 163 Uint32 ticks2; |
159 /* Easy, center light */ | 164 |
160 position.x = x-(light->w/2); | 165 /* Easy, center light */ |
161 position.y = y-(light->h/2); | 166 position.x = x - (light->w / 2); |
162 position.w = light->w; | 167 position.y = y - (light->h / 2); |
163 position.h = light->h; | 168 position.w = light->w; |
164 ticks1 = SDL_GetTicks(); | 169 position.h = light->h; |
165 SDL_BlitSurface(light, NULL, screen, &position); | 170 ticks1 = SDL_GetTicks (); |
166 ticks2 = SDL_GetTicks(); | 171 SDL_BlitSurface (light, NULL, screen, &position); |
167 SDL_UpdateRects(screen, 1, &position); | 172 ticks2 = SDL_GetTicks (); |
168 ++flashes; | 173 SDL_UpdateRects (screen, 1, &position); |
169 | 174 ++flashes; |
170 /* Update time spend doing alpha blitting */ | 175 |
171 flashtime += (ticks2-ticks1); | 176 /* Update time spend doing alpha blitting */ |
177 flashtime += (ticks2 - ticks1); | |
172 } | 178 } |
173 | 179 |
174 static int sprite_visible = 0; | 180 static int sprite_visible = 0; |
175 static SDL_Surface *sprite; | 181 static SDL_Surface *sprite; |
176 static SDL_Surface *backing; | 182 static SDL_Surface *backing; |
177 static SDL_Rect position; | 183 static SDL_Rect position; |
178 static int x_vel, y_vel; | 184 static int x_vel, y_vel; |
179 static int alpha_vel; | 185 static int alpha_vel; |
180 | 186 |
181 int LoadSprite(SDL_Surface *screen, char *file) | 187 int |
182 { | 188 LoadSprite (SDL_Surface * screen, char *file) |
183 SDL_Surface *converted; | 189 { |
184 | 190 SDL_Surface *converted; |
185 /* Load the sprite image */ | 191 |
186 sprite = SDL_LoadBMP(file); | 192 /* Load the sprite image */ |
187 if ( sprite == NULL ) { | 193 sprite = SDL_LoadBMP (file); |
188 fprintf(stderr, "Couldn't load %s: %s", file, SDL_GetError()); | 194 if (sprite == NULL) { |
189 return(-1); | 195 fprintf (stderr, "Couldn't load %s: %s", file, SDL_GetError ()); |
190 } | 196 return (-1); |
191 | 197 } |
192 /* Set transparent pixel as the pixel at (0,0) */ | 198 |
193 if ( sprite->format->palette ) { | 199 /* Set transparent pixel as the pixel at (0,0) */ |
194 SDL_SetColorKey(sprite, SDL_SRCCOLORKEY, | 200 if (sprite->format->palette) { |
195 *(Uint8 *)sprite->pixels); | 201 SDL_SetColorKey (sprite, SDL_SRCCOLORKEY, *(Uint8 *) sprite->pixels); |
196 } | 202 } |
197 | 203 |
198 /* Convert sprite to video format */ | 204 /* Convert sprite to video format */ |
199 converted = SDL_DisplayFormat(sprite); | 205 converted = SDL_DisplayFormat (sprite); |
200 SDL_FreeSurface(sprite); | 206 SDL_FreeSurface (sprite); |
201 if ( converted == NULL ) { | 207 if (converted == NULL) { |
202 fprintf(stderr, "Couldn't convert background: %s\n", | 208 fprintf (stderr, "Couldn't convert background: %s\n", |
203 SDL_GetError()); | 209 SDL_GetError ()); |
204 return(-1); | 210 return (-1); |
205 } | 211 } |
206 sprite = converted; | 212 sprite = converted; |
207 | 213 |
208 /* Create the background */ | 214 /* Create the background */ |
209 backing = SDL_CreateRGBSurface(SDL_SWSURFACE, sprite->w, sprite->h, 8, | 215 backing = SDL_CreateRGBSurface (SDL_SWSURFACE, sprite->w, sprite->h, 8, |
210 0, 0, 0, 0); | 216 0, 0, 0, 0); |
211 if ( backing == NULL ) { | 217 if (backing == NULL) { |
212 fprintf(stderr, "Couldn't create background: %s\n", | 218 fprintf (stderr, "Couldn't create background: %s\n", SDL_GetError ()); |
213 SDL_GetError()); | 219 SDL_FreeSurface (sprite); |
214 SDL_FreeSurface(sprite); | 220 return (-1); |
215 return(-1); | 221 } |
216 } | 222 |
217 | 223 /* Convert background to video format */ |
218 /* Convert background to video format */ | 224 converted = SDL_DisplayFormat (backing); |
219 converted = SDL_DisplayFormat(backing); | 225 SDL_FreeSurface (backing); |
220 SDL_FreeSurface(backing); | 226 if (converted == NULL) { |
221 if ( converted == NULL ) { | 227 fprintf (stderr, "Couldn't convert background: %s\n", |
222 fprintf(stderr, "Couldn't convert background: %s\n", | 228 SDL_GetError ()); |
223 SDL_GetError()); | 229 SDL_FreeSurface (sprite); |
224 SDL_FreeSurface(sprite); | 230 return (-1); |
225 return(-1); | 231 } |
226 } | 232 backing = converted; |
227 backing = converted; | 233 |
228 | 234 /* Set the initial position of the sprite */ |
229 /* Set the initial position of the sprite */ | 235 position.x = (screen->w - sprite->w) / 2; |
230 position.x = (screen->w-sprite->w)/2; | 236 position.y = (screen->h - sprite->h) / 2; |
231 position.y = (screen->h-sprite->h)/2; | 237 position.w = sprite->w; |
232 position.w = sprite->w; | 238 position.h = sprite->h; |
233 position.h = sprite->h; | 239 x_vel = 0; |
234 x_vel = 0; y_vel = 0; | 240 y_vel = 0; |
235 alpha_vel = 1; | 241 alpha_vel = 1; |
236 | 242 |
237 /* We're ready to roll. :) */ | 243 /* We're ready to roll. :) */ |
238 return(0); | 244 return (0); |
239 } | 245 } |
240 | 246 |
241 void AttractSprite(Uint16 x, Uint16 y) | 247 void |
242 { | 248 AttractSprite (Uint16 x, Uint16 y) |
243 x_vel = ((int)x-position.x)/10; | 249 { |
244 y_vel = ((int)y-position.y)/10; | 250 x_vel = ((int) x - position.x) / 10; |
245 } | 251 y_vel = ((int) y - position.y) / 10; |
246 | 252 } |
247 void MoveSprite(SDL_Surface *screen, SDL_Surface *light) | 253 |
248 { | 254 void |
249 SDL_Rect updates[2]; | 255 MoveSprite (SDL_Surface * screen, SDL_Surface * light) |
250 int alpha; | 256 { |
251 | 257 SDL_Rect updates[2]; |
252 /* Erase the sprite if it was visible */ | 258 int alpha; |
253 if ( sprite_visible ) { | 259 |
254 updates[0] = position; | 260 /* Erase the sprite if it was visible */ |
255 SDL_BlitSurface(backing, NULL, screen, &updates[0]); | 261 if (sprite_visible) { |
256 } else { | 262 updates[0] = position; |
257 updates[0].x = 0; updates[0].y = 0; | 263 SDL_BlitSurface (backing, NULL, screen, &updates[0]); |
258 updates[0].w = 0; updates[0].h = 0; | 264 } else { |
259 sprite_visible = 1; | 265 updates[0].x = 0; |
260 } | 266 updates[0].y = 0; |
261 | 267 updates[0].w = 0; |
262 /* Since the sprite is off the screen, we can do other drawing | 268 updates[0].h = 0; |
263 without being overwritten by the saved area behind the sprite. | 269 sprite_visible = 1; |
264 */ | 270 } |
265 if ( light != NULL ) { | 271 |
266 int x, y; | 272 /* Since the sprite is off the screen, we can do other drawing |
267 | 273 without being overwritten by the saved area behind the sprite. |
268 SDL_GetMouseState(&x, &y); | 274 */ |
269 FlashLight(screen, light, x, y); | 275 if (light != NULL) { |
270 } | 276 int x, y; |
271 | 277 |
272 /* Move the sprite, bounce at the wall */ | 278 SDL_GetMouseState (&x, &y); |
273 position.x += x_vel; | 279 FlashLight (screen, light, x, y); |
274 if ( (position.x < 0) || (position.x >= screen->w) ) { | 280 } |
275 x_vel = -x_vel; | 281 |
276 position.x += x_vel; | 282 /* Move the sprite, bounce at the wall */ |
277 } | 283 position.x += x_vel; |
278 position.y += y_vel; | 284 if ((position.x < 0) || (position.x >= screen->w)) { |
279 if ( (position.y < 0) || (position.y >= screen->h) ) { | 285 x_vel = -x_vel; |
280 y_vel = -y_vel; | 286 position.x += x_vel; |
281 position.y += y_vel; | 287 } |
282 } | 288 position.y += y_vel; |
283 | 289 if ((position.y < 0) || (position.y >= screen->h)) { |
284 /* Update transparency (fade in and out) */ | 290 y_vel = -y_vel; |
285 alpha = sprite->format->alpha; | 291 position.y += y_vel; |
286 if ( (alpha+alpha_vel) < 0 ) { | 292 } |
287 alpha_vel = -alpha_vel; | 293 |
288 } else | 294 /* Update transparency (fade in and out) */ |
289 if ( (alpha+alpha_vel) > 255 ) { | 295 alpha = sprite->format->alpha; |
290 alpha_vel = -alpha_vel; | 296 if ((alpha + alpha_vel) < 0) { |
291 } | 297 alpha_vel = -alpha_vel; |
292 SDL_SetAlpha(sprite, SDL_SRCALPHA, (Uint8)(alpha+alpha_vel)); | 298 } else if ((alpha + alpha_vel) > 255) { |
293 | 299 alpha_vel = -alpha_vel; |
294 /* Save the area behind the sprite */ | 300 } |
295 updates[1] = position; | 301 SDL_SetAlpha (sprite, SDL_SRCALPHA, (Uint8) (alpha + alpha_vel)); |
296 SDL_BlitSurface(screen, &updates[1], backing, NULL); | 302 |
297 | 303 /* Save the area behind the sprite */ |
298 /* Blit the sprite onto the screen */ | 304 updates[1] = position; |
299 updates[1] = position; | 305 SDL_BlitSurface (screen, &updates[1], backing, NULL); |
300 SDL_BlitSurface(sprite, NULL, screen, &updates[1]); | 306 |
301 | 307 /* Blit the sprite onto the screen */ |
302 /* Make it so! */ | 308 updates[1] = position; |
303 SDL_UpdateRects(screen, 2, updates); | 309 SDL_BlitSurface (sprite, NULL, screen, &updates[1]); |
304 } | 310 |
305 | 311 /* Make it so! */ |
306 void WarpSprite(SDL_Surface *screen, int x, int y) | 312 SDL_UpdateRects (screen, 2, updates); |
307 { | 313 } |
308 SDL_Rect updates[2]; | 314 |
309 | 315 void |
310 /* Erase, move, Draw, update */ | 316 WarpSprite (SDL_Surface * screen, int x, int y) |
311 updates[0] = position; | 317 { |
312 SDL_BlitSurface(backing, NULL, screen, &updates[0]); | 318 SDL_Rect updates[2]; |
313 position.x = x-sprite->w/2; /* Center about X */ | 319 |
314 position.y = y-sprite->h/2; /* Center about Y */ | 320 /* Erase, move, Draw, update */ |
315 updates[1] = position; | 321 updates[0] = position; |
316 SDL_BlitSurface(screen, &updates[1], backing, NULL); | 322 SDL_BlitSurface (backing, NULL, screen, &updates[0]); |
317 updates[1] = position; | 323 position.x = x - sprite->w / 2; /* Center about X */ |
318 SDL_BlitSurface(sprite, NULL, screen, &updates[1]); | 324 position.y = y - sprite->h / 2; /* Center about Y */ |
319 SDL_UpdateRects(screen, 2, updates); | 325 updates[1] = position; |
320 } | 326 SDL_BlitSurface (screen, &updates[1], backing, NULL); |
321 | 327 updates[1] = position; |
322 int main(int argc, char *argv[]) | 328 SDL_BlitSurface (sprite, NULL, screen, &updates[1]); |
323 { | 329 SDL_UpdateRects (screen, 2, updates); |
324 const SDL_VideoInfo *info; | 330 } |
325 SDL_Surface *screen; | 331 |
326 int w, h; | 332 int |
327 Uint8 video_bpp; | 333 main (int argc, char *argv[]) |
328 Uint32 videoflags; | 334 { |
329 int i, done; | 335 const SDL_VideoInfo *info; |
330 SDL_Event event; | 336 SDL_Surface *screen; |
331 SDL_Surface *light; | 337 int w, h; |
332 int mouse_pressed; | 338 Uint8 video_bpp; |
333 Uint32 ticks, lastticks; | 339 Uint32 videoflags; |
334 | 340 int i, done; |
335 | 341 SDL_Event event; |
336 /* Initialize SDL */ | 342 SDL_Surface *light; |
337 if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) { | 343 int mouse_pressed; |
338 fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError()); | 344 Uint32 ticks, lastticks; |
339 return(1); | 345 |
340 } | 346 |
341 | 347 /* Initialize SDL */ |
342 /* Alpha blending doesn't work well at 8-bit color */ | 348 if (SDL_Init (SDL_INIT_VIDEO) < 0) { |
349 fprintf (stderr, "Couldn't initialize SDL: %s\n", SDL_GetError ()); | |
350 return (1); | |
351 } | |
352 | |
353 /* Alpha blending doesn't work well at 8-bit color */ | |
343 #ifdef _WIN32_WCE | 354 #ifdef _WIN32_WCE |
344 /* Pocket PC */ | 355 /* Pocket PC */ |
345 w = 240; | 356 w = 240; |
346 h = 320; | 357 h = 320; |
347 #else | 358 #else |
348 w = 640; | 359 w = 640; |
349 h = 480; | 360 h = 480; |
350 #endif | 361 #endif |
351 info = SDL_GetVideoInfo(); | 362 info = SDL_GetVideoInfo (); |
352 if ( info->vfmt->BitsPerPixel > 8 ) { | 363 if (info->vfmt->BitsPerPixel > 8) { |
353 video_bpp = info->vfmt->BitsPerPixel; | 364 video_bpp = info->vfmt->BitsPerPixel; |
354 } else { | 365 } else { |
355 video_bpp = 16; | 366 video_bpp = 16; |
356 fprintf(stderr, "forced 16 bpp mode\n"); | 367 fprintf (stderr, "forced 16 bpp mode\n"); |
357 } | 368 } |
358 videoflags = SDL_SWSURFACE; | 369 videoflags = SDL_SWSURFACE; |
359 for ( i = 1; argv[i]; ++i ) { | 370 for (i = 1; argv[i]; ++i) { |
360 if ( strcmp(argv[i], "-bpp") == 0 ) { | 371 if (strcmp (argv[i], "-bpp") == 0) { |
361 video_bpp = atoi(argv[++i]); | 372 video_bpp = atoi (argv[++i]); |
362 if (video_bpp<=8) { | 373 if (video_bpp <= 8) { |
363 video_bpp=16; | 374 video_bpp = 16; |
364 fprintf(stderr, "forced 16 bpp mode\n"); | 375 fprintf (stderr, "forced 16 bpp mode\n"); |
365 } | 376 } |
366 } else | 377 } else if (strcmp (argv[i], "-hw") == 0) { |
367 if ( strcmp(argv[i], "-hw") == 0 ) { | 378 videoflags |= SDL_HWSURFACE; |
368 videoflags |= SDL_HWSURFACE; | 379 } else if (strcmp (argv[i], "-warp") == 0) { |
369 } else | 380 videoflags |= SDL_HWPALETTE; |
370 if ( strcmp(argv[i], "-warp") == 0 ) { | 381 } else if (strcmp (argv[i], "-width") == 0 && argv[i + 1]) { |
371 videoflags |= SDL_HWPALETTE; | 382 w = atoi (argv[++i]); |
372 } else | 383 } else if (strcmp (argv[i], "-height") == 0 && argv[i + 1]) { |
373 if ( strcmp(argv[i], "-width") == 0 && argv[i+1] ) { | 384 h = atoi (argv[++i]); |
374 w = atoi(argv[++i]); | 385 } else if (strcmp (argv[i], "-resize") == 0) { |
375 } else | 386 videoflags |= SDL_RESIZABLE; |
376 if ( strcmp(argv[i], "-height") == 0 && argv[i+1] ) { | 387 } else if (strcmp (argv[i], "-noframe") == 0) { |
377 h = atoi(argv[++i]); | 388 videoflags |= SDL_NOFRAME; |
378 } else | 389 } else if (strcmp (argv[i], "-fullscreen") == 0) { |
379 if ( strcmp(argv[i], "-resize") == 0 ) { | 390 videoflags |= SDL_FULLSCREEN; |
380 videoflags |= SDL_RESIZABLE; | 391 } else { |
381 } else | 392 fprintf (stderr, |
382 if ( strcmp(argv[i], "-noframe") == 0 ) { | 393 "Usage: %s [-width N] [-height N] [-bpp N] [-warp] [-hw] [-fullscreen]\n", |
383 videoflags |= SDL_NOFRAME; | 394 argv[0]); |
384 } else | 395 quit (1); |
385 if ( strcmp(argv[i], "-fullscreen") == 0 ) { | 396 } |
386 videoflags |= SDL_FULLSCREEN; | 397 } |
387 } else { | 398 |
388 fprintf(stderr, | 399 /* Set video mode */ |
389 "Usage: %s [-width N] [-height N] [-bpp N] [-warp] [-hw] [-fullscreen]\n", | 400 if ((screen = SDL_SetVideoMode (w, h, video_bpp, videoflags)) == NULL) { |
390 argv[0]); | 401 fprintf (stderr, "Couldn't set %dx%dx%d video mode: %s\n", |
391 quit(1); | 402 w, h, video_bpp, SDL_GetError ()); |
392 } | 403 quit (2); |
393 } | 404 } |
394 | 405 FillBackground (screen); |
395 /* Set video mode */ | 406 |
396 if ( (screen=SDL_SetVideoMode(w,h,video_bpp,videoflags)) == NULL ) { | 407 /* Create the light */ |
397 fprintf(stderr, "Couldn't set %dx%dx%d video mode: %s\n", | 408 light = CreateLight (82); |
398 w, h, video_bpp, SDL_GetError()); | 409 if (light == NULL) { |
399 quit(2); | 410 quit (1); |
400 } | 411 } |
401 FillBackground(screen); | 412 |
402 | 413 /* Load the sprite */ |
403 /* Create the light */ | 414 if (LoadSprite (screen, "icon.bmp") < 0) { |
404 light = CreateLight(82); | 415 SDL_FreeSurface (light); |
405 if ( light == NULL ) { | 416 quit (1); |
406 quit(1); | 417 } |
407 } | 418 |
408 | 419 /* Print out information about our surfaces */ |
409 /* Load the sprite */ | 420 printf ("Screen is at %d bits per pixel\n", screen->format->BitsPerPixel); |
410 if ( LoadSprite(screen, "icon.bmp") < 0 ) { | 421 if ((screen->flags & SDL_HWSURFACE) == SDL_HWSURFACE) { |
411 SDL_FreeSurface(light); | 422 printf ("Screen is in video memory\n"); |
412 quit(1); | 423 } else { |
413 } | 424 printf ("Screen is in system memory\n"); |
414 | 425 } |
415 /* Print out information about our surfaces */ | 426 if ((screen->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF) { |
416 printf("Screen is at %d bits per pixel\n",screen->format->BitsPerPixel); | 427 printf ("Screen has double-buffering enabled\n"); |
417 if ( (screen->flags & SDL_HWSURFACE) == SDL_HWSURFACE ) { | 428 } |
418 printf("Screen is in video memory\n"); | 429 if ((sprite->flags & SDL_HWSURFACE) == SDL_HWSURFACE) { |
419 } else { | 430 printf ("Sprite is in video memory\n"); |
420 printf("Screen is in system memory\n"); | 431 } else { |
421 } | 432 printf ("Sprite is in system memory\n"); |
422 if ( (screen->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF ) { | 433 } |
423 printf("Screen has double-buffering enabled\n"); | 434 |
424 } | 435 /* Run a sample blit to trigger blit acceleration */ |
425 if ( (sprite->flags & SDL_HWSURFACE) == SDL_HWSURFACE ) { | 436 MoveSprite (screen, NULL); |
426 printf("Sprite is in video memory\n"); | 437 if ((sprite->flags & SDL_HWACCEL) == SDL_HWACCEL) { |
427 } else { | 438 printf ("Sprite blit uses hardware alpha acceleration\n"); |
428 printf("Sprite is in system memory\n"); | 439 } else { |
429 } | 440 printf ("Sprite blit dosn't uses hardware alpha acceleration\n"); |
430 | 441 } |
431 /* Run a sample blit to trigger blit acceleration */ | 442 |
432 MoveSprite(screen, NULL); | 443 /* Set a clipping rectangle to clip the outside edge of the screen */ |
433 if ( (sprite->flags & SDL_HWACCEL) == SDL_HWACCEL ) { | 444 { |
434 printf("Sprite blit uses hardware alpha acceleration\n"); | 445 SDL_Rect clip; |
435 } else { | 446 clip.x = 32; |
436 printf("Sprite blit dosn't uses hardware alpha acceleration\n"); | 447 clip.y = 32; |
437 } | 448 clip.w = screen->w - (2 * 32); |
438 | 449 clip.h = screen->h - (2 * 32); |
439 /* Set a clipping rectangle to clip the outside edge of the screen */ | 450 SDL_SetClipRect (screen, &clip); |
440 { SDL_Rect clip; | 451 } |
441 clip.x = 32; | 452 |
442 clip.y = 32; | 453 /* Wait for a keystroke */ |
443 clip.w = screen->w-(2*32); | 454 lastticks = SDL_GetTicks (); |
444 clip.h = screen->h-(2*32); | 455 done = 0; |
445 SDL_SetClipRect(screen, &clip); | 456 mouse_pressed = 0; |
446 } | 457 while (!done) { |
447 | 458 /* Update the frame -- move the sprite */ |
448 /* Wait for a keystroke */ | 459 if (mouse_pressed) { |
449 lastticks = SDL_GetTicks(); | 460 MoveSprite (screen, light); |
450 done = 0; | 461 mouse_pressed = 0; |
451 mouse_pressed = 0; | 462 } else { |
452 while ( !done ) { | 463 MoveSprite (screen, NULL); |
453 /* Update the frame -- move the sprite */ | 464 } |
454 if ( mouse_pressed ) { | 465 |
455 MoveSprite(screen, light); | 466 /* Slow down the loop to 30 frames/second */ |
456 mouse_pressed = 0; | 467 ticks = SDL_GetTicks (); |
457 } else { | 468 if ((ticks - lastticks) < FRAME_TICKS) { |
458 MoveSprite(screen, NULL); | |
459 } | |
460 | |
461 /* Slow down the loop to 30 frames/second */ | |
462 ticks = SDL_GetTicks(); | |
463 if ( (ticks-lastticks) < FRAME_TICKS ) { | |
464 #ifdef CHECK_SLEEP_GRANULARITY | 469 #ifdef CHECK_SLEEP_GRANULARITY |
465 fprintf(stderr, "Sleeping %d ticks\n", FRAME_TICKS-(ticks-lastticks)); | 470 fprintf (stderr, "Sleeping %d ticks\n", |
471 FRAME_TICKS - (ticks - lastticks)); | |
466 #endif | 472 #endif |
467 SDL_Delay(FRAME_TICKS-(ticks-lastticks)); | 473 SDL_Delay (FRAME_TICKS - (ticks - lastticks)); |
468 #ifdef CHECK_SLEEP_GRANULARITY | 474 #ifdef CHECK_SLEEP_GRANULARITY |
469 fprintf(stderr, "Slept %d ticks\n", (SDL_GetTicks()-ticks)); | 475 fprintf (stderr, "Slept %d ticks\n", (SDL_GetTicks () - ticks)); |
470 #endif | 476 #endif |
471 } | 477 } |
472 lastticks = ticks; | 478 lastticks = ticks; |
473 | 479 |
474 /* Check for events */ | 480 /* Check for events */ |
475 while ( SDL_PollEvent(&event) ) { | 481 while (SDL_PollEvent (&event)) { |
476 switch (event.type) { | 482 switch (event.type) { |
477 case SDL_VIDEORESIZE: | 483 case SDL_VIDEORESIZE: |
478 screen = SDL_SetVideoMode(event.resize.w, event.resize.h, video_bpp, videoflags); | 484 screen = |
479 if ( screen ) { | 485 SDL_SetVideoMode (event.resize.w, event.resize.h, |
480 FillBackground(screen); | 486 video_bpp, videoflags); |
481 } | 487 if (screen) { |
482 break; | 488 FillBackground (screen); |
483 /* Attract sprite while mouse is held down */ | 489 } |
484 case SDL_MOUSEMOTION: | 490 break; |
485 if (event.motion.state != 0) { | 491 /* Attract sprite while mouse is held down */ |
486 AttractSprite(event.motion.x, | 492 case SDL_MOUSEMOTION: |
487 event.motion.y); | 493 if (event.motion.state != 0) { |
488 mouse_pressed = 1; | 494 AttractSprite (event.motion.x, event.motion.y); |
489 } | 495 mouse_pressed = 1; |
490 break; | 496 } |
491 case SDL_MOUSEBUTTONDOWN: | 497 break; |
492 if ( event.button.button == 1 ) { | 498 case SDL_MOUSEBUTTONDOWN: |
493 AttractSprite(event.button.x, | 499 if (event.button.button == 1) { |
494 event.button.y); | 500 AttractSprite (event.button.x, event.button.y); |
495 mouse_pressed = 1; | 501 mouse_pressed = 1; |
496 } else { | 502 } else { |
497 SDL_Rect area; | 503 SDL_Rect area; |
498 | 504 |
499 area.x = event.button.x-16; | 505 area.x = event.button.x - 16; |
500 area.y = event.button.y-16; | 506 area.y = event.button.y - 16; |
501 area.w = 32; | 507 area.w = 32; |
502 area.h = 32; | 508 area.h = 32; |
503 SDL_FillRect(screen, &area, 0); | 509 SDL_FillRect (screen, &area, 0); |
504 SDL_UpdateRects(screen,1,&area); | 510 SDL_UpdateRects (screen, 1, &area); |
505 } | 511 } |
506 break; | 512 break; |
507 case SDL_KEYDOWN: | 513 case SDL_KEYDOWN: |
508 if ( event.key.keysym.sym == SDLK_ESCAPE ) { | 514 if (event.key.keysym.sym == SDLK_ESCAPE) { |
509 done = 1; | 515 done = 1; |
510 } | 516 } |
511 break; | 517 break; |
512 case SDL_QUIT: | 518 case SDL_QUIT: |
513 done = 1; | 519 done = 1; |
514 break; | 520 break; |
515 default: | 521 default: |
516 break; | 522 break; |
517 } | 523 } |
518 } | 524 } |
519 } | 525 } |
520 SDL_FreeSurface(light); | 526 SDL_FreeSurface (light); |
521 SDL_FreeSurface(sprite); | 527 SDL_FreeSurface (sprite); |
522 SDL_FreeSurface(backing); | 528 SDL_FreeSurface (backing); |
523 | 529 |
524 /* Print out some timing information */ | 530 /* Print out some timing information */ |
525 if ( flashes > 0 ) { | 531 if (flashes > 0) { |
526 printf("%d alpha blits, ~%4.4f ms per blit\n", | 532 printf ("%d alpha blits, ~%4.4f ms per blit\n", |
527 flashes, (float)flashtime/flashes); | 533 flashes, (float) flashtime / flashes); |
528 } | 534 } |
529 | 535 |
530 SDL_Quit(); | 536 SDL_Quit (); |
531 return(0); | 537 return (0); |
532 } | 538 } |