Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
536 lines (478 loc) · 15.4 KB

testalpha.c

File metadata and controls

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