Mercurial > sdl-ios-xcode
annotate test/testalpha.c @ 734:0310bb01091f
New C2P conversion routine
author | Patrice Mandin <patmandin@gmail.com> |
---|---|
date | Wed, 12 Nov 2003 18:45:48 +0000 |
parents | 609543e2b3a1 |
children | 05c551e5bc64 |
rev | line source |
---|---|
0 | 1 |
2 /* Simple program: Fill a colormap with gray and stripe it down the screen, | |
3 Then move an alpha valued sprite around the screen. | |
4 */ | |
5 | |
6 #include <stdio.h> | |
7 #include <stdlib.h> | |
8 #include <string.h> | |
9 #include <math.h> | |
10 | |
11 #include "SDL.h" | |
12 | |
13 #define FRAME_TICKS (1000/30) /* 30 frames/second */ | |
14 | |
15 /* Create a "light" -- a yellowish surface with variable alpha */ | |
16 SDL_Surface *CreateLight(SDL_Surface *screen, int radius) | |
17 { | |
18 Uint8 trans, alphamask; | |
19 int range, addition; | |
20 int xdist, ydist; | |
21 Uint16 x, y; | |
22 Uint16 skip; | |
23 Uint32 pixel; | |
24 SDL_Surface *light; | |
25 | |
26 #ifdef LIGHT_16BIT | |
27 Uint16 *buf; | |
28 | |
29 /* Create a 16 (4/4/4/4) bpp square with a full 4-bit alpha channel */ | |
30 /* Note: this isn't any faster than a 32 bit alpha surface */ | |
31 alphamask = 0x0000000F; | |
32 light = SDL_CreateRGBSurface(SDL_SWSURFACE, 2*radius, 2*radius, 16, | |
33 0x0000F000, 0x00000F00, 0x000000F0, alphamask); | |
34 #else | |
35 Uint32 *buf; | |
36 | |
37 /* Create a 32 (8/8/8/8) bpp square with a full 8-bit alpha channel */ | |
38 alphamask = 0x000000FF; | |
39 light = SDL_CreateRGBSurface(SDL_SWSURFACE, 2*radius, 2*radius, 32, | |
40 0xFF000000, 0x00FF0000, 0x0000FF00, alphamask); | |
41 if ( light == NULL ) { | |
42 fprintf(stderr, "Couldn't create light: %s\n", SDL_GetError()); | |
43 return(NULL); | |
44 } | |
45 #endif | |
46 | |
47 /* Fill with a light yellow-orange color */ | |
48 skip = light->pitch-(light->w*light->format->BytesPerPixel); | |
49 #ifdef LIGHT_16BIT | |
50 buf = (Uint16 *)light->pixels; | |
51 #else | |
52 buf = (Uint32 *)light->pixels; | |
53 #endif | |
54 /* Get a tranparent pixel value - we'll add alpha later */ | |
55 pixel = SDL_MapRGBA(light->format, 0xFF, 0xDD, 0x88, 0); | |
56 for ( y=0; y<light->h; ++y ) { | |
57 for ( x=0; x<light->w; ++x ) { | |
58 *buf++ = pixel; | |
59 } | |
60 buf += skip; /* Almost always 0, but just in case... */ | |
61 } | |
62 | |
63 /* Calculate alpha values for the surface. */ | |
64 #ifdef LIGHT_16BIT | |
65 buf = (Uint16 *)light->pixels; | |
66 #else | |
67 buf = (Uint32 *)light->pixels; | |
68 #endif | |
69 for ( y=0; y<light->h; ++y ) { | |
70 for ( x=0; x<light->w; ++x ) { | |
71 /* Slow distance formula (from center of light) */ | |
72 xdist = x-(light->w/2); | |
73 ydist = y-(light->h/2); | |
74 range = (int)sqrt(xdist*xdist+ydist*ydist); | |
75 | |
76 /* Scale distance to range of transparency (0-255) */ | |
77 if ( range > radius ) { | |
78 trans = alphamask; | |
79 } else { | |
80 /* Increasing transparency with distance */ | |
81 trans = (Uint8)((range*alphamask)/radius); | |
82 | |
83 /* Lights are very transparent */ | |
84 addition = (alphamask+1)/8; | |
85 if ( (int)trans+addition > alphamask ) { | |
86 trans = alphamask; | |
87 } else { | |
88 trans += addition; | |
89 } | |
90 } | |
91 /* We set the alpha component as the right N bits */ | |
92 *buf++ |= (255-trans); | |
93 } | |
94 buf += skip; /* Almost always 0, but just in case... */ | |
95 } | |
96 /* Enable RLE acceleration of this alpha surface */ | |
97 SDL_SetAlpha(light, SDL_SRCALPHA|SDL_RLEACCEL, 0); | |
98 | |
99 /* We're done! */ | |
100 return(light); | |
101 } | |
102 | |
103 static Uint32 flashes = 0; | |
104 static Uint32 flashtime = 0; | |
105 | |
106 void FlashLight(SDL_Surface *screen, SDL_Surface *light, int x, int y) | |
107 { | |
108 SDL_Rect position; | |
109 Uint32 ticks1; | |
110 Uint32 ticks2; | |
111 | |
112 /* Easy, center light */ | |
113 position.x = x-(light->w/2); | |
114 position.y = y-(light->h/2); | |
115 position.w = light->w; | |
116 position.h = light->h; | |
117 ticks1 = SDL_GetTicks(); | |
118 SDL_BlitSurface(light, NULL, screen, &position); | |
119 ticks2 = SDL_GetTicks(); | |
120 SDL_UpdateRects(screen, 1, &position); | |
121 ++flashes; | |
122 | |
123 /* Update time spend doing alpha blitting */ | |
124 flashtime += (ticks2-ticks1); | |
125 } | |
126 | |
127 static int sprite_visible = 0; | |
128 static SDL_Surface *sprite; | |
129 static SDL_Surface *backing; | |
130 static SDL_Rect position; | |
131 static int x_vel, y_vel; | |
132 static int alpha_vel; | |
133 | |
134 int LoadSprite(SDL_Surface *screen, char *file) | |
135 { | |
136 SDL_Surface *converted; | |
137 | |
138 /* Load the sprite image */ | |
139 sprite = SDL_LoadBMP(file); | |
140 if ( sprite == NULL ) { | |
141 fprintf(stderr, "Couldn't load %s: %s", file, SDL_GetError()); | |
142 return(-1); | |
143 } | |
144 | |
145 /* Set transparent pixel as the pixel at (0,0) */ | |
146 if ( sprite->format->palette ) { | |
147 SDL_SetColorKey(sprite, SDL_SRCCOLORKEY, | |
148 *(Uint8 *)sprite->pixels); | |
149 } | |
150 | |
151 /* Convert sprite to video format */ | |
152 converted = SDL_DisplayFormat(sprite); | |
153 SDL_FreeSurface(sprite); | |
154 if ( converted == NULL ) { | |
155 fprintf(stderr, "Couldn't convert background: %s\n", | |
156 SDL_GetError()); | |
157 return(-1); | |
158 } | |
159 sprite = converted; | |
160 | |
161 /* Create the background */ | |
162 backing = SDL_CreateRGBSurface(SDL_SWSURFACE, sprite->w, sprite->h, 8, | |
163 0, 0, 0, 0); | |
164 if ( backing == NULL ) { | |
165 fprintf(stderr, "Couldn't create background: %s\n", | |
166 SDL_GetError()); | |
167 SDL_FreeSurface(sprite); | |
168 return(-1); | |
169 } | |
170 | |
171 /* Convert background to video format */ | |
172 converted = SDL_DisplayFormat(backing); | |
173 SDL_FreeSurface(backing); | |
174 if ( converted == NULL ) { | |
175 fprintf(stderr, "Couldn't convert background: %s\n", | |
176 SDL_GetError()); | |
177 SDL_FreeSurface(sprite); | |
178 return(-1); | |
179 } | |
180 backing = converted; | |
181 | |
182 /* Set the initial position of the sprite */ | |
183 position.x = (screen->w-sprite->w)/2; | |
184 position.y = (screen->h-sprite->h)/2; | |
185 position.w = sprite->w; | |
186 position.h = sprite->h; | |
187 x_vel = 0; y_vel = 0; | |
188 alpha_vel = 1; | |
189 | |
190 /* We're ready to roll. :) */ | |
191 return(0); | |
192 } | |
193 | |
194 void AttractSprite(Uint16 x, Uint16 y) | |
195 { | |
196 x_vel = ((int)x-position.x)/10; | |
197 y_vel = ((int)y-position.y)/10; | |
198 } | |
199 | |
200 void MoveSprite(SDL_Surface *screen, SDL_Surface *light) | |
201 { | |
202 SDL_Rect updates[2]; | |
203 int alpha; | |
204 | |
205 /* Erase the sprite if it was visible */ | |
206 if ( sprite_visible ) { | |
207 updates[0] = position; | |
208 SDL_BlitSurface(backing, NULL, screen, &updates[0]); | |
209 } else { | |
210 updates[0].x = 0; updates[0].y = 0; | |
211 updates[0].w = 0; updates[0].h = 0; | |
212 sprite_visible = 1; | |
213 } | |
214 | |
215 /* Since the sprite is off the screen, we can do other drawing | |
216 without being overwritten by the saved area behind the sprite. | |
217 */ | |
218 if ( light != NULL ) { | |
219 int x, y; | |
220 | |
221 SDL_GetMouseState(&x, &y); | |
222 FlashLight(screen, light, x, y); | |
223 } | |
224 | |
225 /* Move the sprite, bounce at the wall */ | |
226 position.x += x_vel; | |
227 if ( (position.x < 0) || (position.x >= screen->w) ) { | |
228 x_vel = -x_vel; | |
229 position.x += x_vel; | |
230 } | |
231 position.y += y_vel; | |
232 if ( (position.y < 0) || (position.y >= screen->h) ) { | |
233 y_vel = -y_vel; | |
234 position.y += y_vel; | |
235 } | |
236 | |
237 /* Update transparency (fade in and out) */ | |
238 alpha = sprite->format->alpha; | |
239 if ( (alpha+alpha_vel) < 0 ) { | |
240 alpha_vel = -alpha_vel; | |
241 } else | |
242 if ( (alpha+alpha_vel) > 255 ) { | |
243 alpha_vel = -alpha_vel; | |
244 } | |
245 SDL_SetAlpha(sprite, SDL_SRCALPHA, (Uint8)(alpha+alpha_vel)); | |
246 | |
247 /* Save the area behind the sprite */ | |
248 updates[1] = position; | |
249 SDL_BlitSurface(screen, &updates[1], backing, NULL); | |
250 | |
251 /* Blit the sprite onto the screen */ | |
252 updates[1] = position; | |
253 SDL_BlitSurface(sprite, NULL, screen, &updates[1]); | |
254 | |
255 /* Make it so! */ | |
256 SDL_UpdateRects(screen, 2, updates); | |
257 } | |
258 | |
259 void WarpSprite(SDL_Surface *screen, int x, int y) | |
260 { | |
261 SDL_Rect updates[2]; | |
262 | |
263 /* Erase, move, Draw, update */ | |
264 updates[0] = position; | |
265 SDL_BlitSurface(backing, NULL, screen, &updates[0]); | |
266 position.x = x-sprite->w/2; /* Center about X */ | |
267 position.y = y-sprite->h/2; /* Center about Y */ | |
268 updates[1] = position; | |
269 SDL_BlitSurface(screen, &updates[1], backing, NULL); | |
270 updates[1] = position; | |
271 SDL_BlitSurface(sprite, NULL, screen, &updates[1]); | |
272 SDL_UpdateRects(screen, 2, updates); | |
273 } | |
274 | |
275 int main(int argc, char *argv[]) | |
276 { | |
277 const SDL_VideoInfo *info; | |
278 SDL_Surface *screen; | |
279 Uint8 video_bpp; | |
280 Uint32 videoflags; | |
281 Uint8 *buffer; | |
691
609543e2b3a1
Date: Fri, 15 Aug 2003 09:13:59 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
282 int i, k, done; |
0 | 283 SDL_Event event; |
284 SDL_Surface *light; | |
285 int mouse_pressed; | |
286 Uint32 ticks, lastticks; | |
691
609543e2b3a1
Date: Fri, 15 Aug 2003 09:13:59 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
287 Uint16 *buffer16; |
609543e2b3a1
Date: Fri, 15 Aug 2003 09:13:59 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
288 Uint16 color; |
609543e2b3a1
Date: Fri, 15 Aug 2003 09:13:59 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
289 Uint8 gradient; |
609543e2b3a1
Date: Fri, 15 Aug 2003 09:13:59 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
290 |
0 | 291 |
292 /* Initialize SDL */ | |
293 if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) { | |
294 fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError()); | |
295 exit(1); | |
296 } | |
297 atexit(SDL_Quit); | |
298 | |
299 /* Alpha blending doesn't work well at 8-bit color */ | |
300 info = SDL_GetVideoInfo(); | |
301 if ( info->vfmt->BitsPerPixel > 8 ) { | |
302 video_bpp = info->vfmt->BitsPerPixel; | |
303 } else { | |
304 video_bpp = 16; | |
691
609543e2b3a1
Date: Fri, 15 Aug 2003 09:13:59 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
305 fprintf(stderr, "forced 16 bpp mode\n"); |
0 | 306 } |
307 videoflags = SDL_SWSURFACE; | |
308 while ( argc > 1 ) { | |
309 --argc; | |
310 if ( strcmp(argv[argc-1], "-bpp") == 0 ) { | |
311 video_bpp = atoi(argv[argc]); | |
691
609543e2b3a1
Date: Fri, 15 Aug 2003 09:13:59 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
312 if (video_bpp<=8) { |
609543e2b3a1
Date: Fri, 15 Aug 2003 09:13:59 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
313 video_bpp=16; |
609543e2b3a1
Date: Fri, 15 Aug 2003 09:13:59 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
314 fprintf(stderr, "forced 16 bpp mode\n"); |
609543e2b3a1
Date: Fri, 15 Aug 2003 09:13:59 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
315 } |
0 | 316 --argc; |
317 } else | |
318 if ( strcmp(argv[argc], "-hw") == 0 ) { | |
319 videoflags |= SDL_HWSURFACE; | |
320 } else | |
321 if ( strcmp(argv[argc], "-warp") == 0 ) { | |
322 videoflags |= SDL_HWPALETTE; | |
323 } else | |
324 if ( strcmp(argv[argc], "-fullscreen") == 0 ) { | |
325 videoflags |= SDL_FULLSCREEN; | |
326 } else { | |
327 fprintf(stderr, | |
328 "Usage: %s [-bpp N] [-warp] [-hw] [-fullscreen]\n", | |
329 argv[0]); | |
330 exit(1); | |
331 } | |
332 } | |
333 | |
334 /* Set 640x480 video mode */ | |
335 if ( (screen=SDL_SetVideoMode(640,480,video_bpp,videoflags)) == NULL ) { | |
336 fprintf(stderr, "Couldn't set 640x480x%d video mode: %s\n", | |
337 video_bpp, SDL_GetError()); | |
338 exit(2); | |
339 } | |
340 | |
341 /* Set the surface pixels and refresh! */ | |
342 if ( SDL_LockSurface(screen) < 0 ) { | |
343 fprintf(stderr, "Couldn't lock the display surface: %s\n", | |
344 SDL_GetError()); | |
345 exit(2); | |
346 } | |
347 buffer=(Uint8 *)screen->pixels; | |
691
609543e2b3a1
Date: Fri, 15 Aug 2003 09:13:59 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
348 if (screen->format->BytesPerPixel!=2) { |
609543e2b3a1
Date: Fri, 15 Aug 2003 09:13:59 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
349 for ( i=0; i<screen->h; ++i ) { |
609543e2b3a1
Date: Fri, 15 Aug 2003 09:13:59 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
350 memset(buffer,(i*255)/screen->h, screen->pitch); |
609543e2b3a1
Date: Fri, 15 Aug 2003 09:13:59 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
351 buffer += screen->pitch; |
609543e2b3a1
Date: Fri, 15 Aug 2003 09:13:59 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
352 } |
0 | 353 } |
691
609543e2b3a1
Date: Fri, 15 Aug 2003 09:13:59 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
354 else |
609543e2b3a1
Date: Fri, 15 Aug 2003 09:13:59 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
355 { |
609543e2b3a1
Date: Fri, 15 Aug 2003 09:13:59 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
356 for ( i=0; i<screen->h; ++i ) { |
609543e2b3a1
Date: Fri, 15 Aug 2003 09:13:59 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
357 gradient=((i*255)/screen->h); |
609543e2b3a1
Date: Fri, 15 Aug 2003 09:13:59 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
358 color = SDL_MapRGB(screen->format, gradient, gradient, gradient); |
609543e2b3a1
Date: Fri, 15 Aug 2003 09:13:59 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
359 buffer16=(Uint16*)buffer; |
609543e2b3a1
Date: Fri, 15 Aug 2003 09:13:59 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
360 for (k=0; k<screen->w; k++) |
609543e2b3a1
Date: Fri, 15 Aug 2003 09:13:59 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
361 { |
609543e2b3a1
Date: Fri, 15 Aug 2003 09:13:59 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
362 *(buffer16+k)=color; |
609543e2b3a1
Date: Fri, 15 Aug 2003 09:13:59 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
363 } |
609543e2b3a1
Date: Fri, 15 Aug 2003 09:13:59 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
364 buffer += screen->pitch; |
609543e2b3a1
Date: Fri, 15 Aug 2003 09:13:59 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
365 } |
609543e2b3a1
Date: Fri, 15 Aug 2003 09:13:59 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
366 } |
609543e2b3a1
Date: Fri, 15 Aug 2003 09:13:59 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
367 |
0 | 368 SDL_UnlockSurface(screen); |
369 SDL_UpdateRect(screen, 0, 0, 0, 0); | |
370 | |
371 /* Create the light */ | |
372 light = CreateLight(screen, 82); | |
373 if ( light == NULL ) { | |
374 exit(1); | |
375 } | |
376 | |
377 /* Load the sprite */ | |
378 if ( LoadSprite(screen, "icon.bmp") < 0 ) { | |
379 SDL_FreeSurface(light); | |
380 exit(1); | |
381 } | |
382 | |
383 /* Set a clipping rectangle to clip the outside edge of the screen */ | |
384 { SDL_Rect clip; | |
385 clip.x = 32; | |
386 clip.y = 32; | |
387 clip.w = screen->w-(2*32); | |
388 clip.h = screen->h-(2*32); | |
389 SDL_SetClipRect(screen, &clip); | |
390 } | |
391 | |
392 /* Wait for a keystroke */ | |
393 lastticks = SDL_GetTicks(); | |
394 done = 0; | |
395 mouse_pressed = 0; | |
396 while ( !done ) { | |
397 /* Update the frame -- move the sprite */ | |
398 if ( mouse_pressed ) { | |
399 MoveSprite(screen, light); | |
400 mouse_pressed = 0; | |
401 } else { | |
402 MoveSprite(screen, NULL); | |
403 } | |
404 | |
405 /* Slow down the loop to 30 frames/second */ | |
406 ticks = SDL_GetTicks(); | |
407 if ( (ticks-lastticks) < FRAME_TICKS ) { | |
408 #ifdef CHECK_SLEEP_GRANULARITY | |
409 fprintf(stderr, "Sleeping %d ticks\n", FRAME_TICKS-(ticks-lastticks)); | |
410 #endif | |
411 SDL_Delay(FRAME_TICKS-(ticks-lastticks)); | |
412 #ifdef CHECK_SLEEP_GRANULARITY | |
413 fprintf(stderr, "Slept %d ticks\n", (SDL_GetTicks()-ticks)); | |
414 #endif | |
415 } | |
416 lastticks = ticks; | |
417 | |
418 /* Check for events */ | |
419 while ( SDL_PollEvent(&event) ) { | |
420 switch (event.type) { | |
421 /* Attract sprite while mouse is held down */ | |
422 case SDL_MOUSEMOTION: | |
423 if (event.motion.state != 0) { | |
424 AttractSprite(event.motion.x, | |
425 event.motion.y); | |
426 mouse_pressed = 1; | |
427 } | |
428 break; | |
429 case SDL_MOUSEBUTTONDOWN: | |
430 if ( event.button.button == 1 ) { | |
431 AttractSprite(event.button.x, | |
432 event.button.y); | |
433 mouse_pressed = 1; | |
434 } else { | |
435 SDL_Rect area; | |
436 | |
437 area.x = event.button.x-16; | |
438 area.y = event.button.y-16; | |
439 area.w = 32; | |
440 area.h = 32; | |
441 SDL_FillRect(screen, &area, 0); | |
442 SDL_UpdateRects(screen,1,&area); | |
443 } | |
444 break; | |
445 case SDL_KEYDOWN: | |
446 /* Any keypress quits the app... */ | |
447 case SDL_QUIT: | |
448 done = 1; | |
449 break; | |
450 default: | |
451 break; | |
452 } | |
453 } | |
454 } | |
455 SDL_FreeSurface(light); | |
456 SDL_FreeSurface(sprite); | |
457 SDL_FreeSurface(backing); | |
458 | |
459 /* Print out some timing information */ | |
460 if ( flashes > 0 ) { | |
461 printf("%d alpha blits, ~%4.4f ms per blit\n", | |
462 flashes, (float)flashtime/flashes); | |
463 } | |
464 return(0); | |
465 } |