Show exblend.c syntax highlighted
/*
* Example program for the Allegro library, by Shawn Hargreaves.
*
* This program demonstrates how to use the translucency functions in
* truecolor video modes.
*/
#include "allegro.h"
int main(int argc, char *argv[])
{
char buf[256];
AL_PALETTE pal;
AL_BITMAP *image1;
AL_BITMAP *image2;
AL_BITMAP *buffer;
int r, g, b, a;
int x, y, w, h;
int x1, y1, x2, y2;
int prevx1, prevy1, prevx2, prevy2;
int timer;
int bpp = -1;
int ret = -1;
allegro_init();
al_install_keyboard();
al_install_timer();
/* what color depth should we use? */
if (argc > 1) {
if ((argv[1][0] == '-') || (argv[1][0] == '/'))
argv[1]++;
bpp = atoi(argv[1]);
if ((bpp != 15) && (bpp != 16) && (bpp != 24) && (bpp != 32)) {
al_show_message("Invalid color depth '%s'\n", argv[1]);
return 1;
}
}
if (bpp > 0) {
/* set a user-requested color depth */
al_set_color_depth(bpp);
ret = al_set_gfx_mode(AL_GFX_AUTODETECT, 640, 480, 0, 0);
}
else {
/* autodetect what color depths are available */
static int color_depths[] = { 16, 15, 32, 24, 0 };
for (a=0; color_depths[a]; a++) {
bpp = color_depths[a];
al_set_color_depth(bpp);
ret = al_set_gfx_mode(AL_GFX_AUTODETECT, 640, 480, 0, 0);
if (ret == 0)
break;
}
}
/* did the video mode set properly? */
if (ret != 0) {
al_set_gfx_mode(AL_GFX_NONE, 0, 0, 0, 0);
al_show_message("Error setting %d bit graphics mode\n%s\n", bpp, al_error);
return 1;
}
/* specifiy that images should be loaded in a truecolor pixel format */
al_set_color_conversion(AL_COLORCONV_TOTAL);
/* load the first picture */
al_replace_filename(buf, argv[0], "allegro.pcx", sizeof(buf));
image1 = al_load_bitmap(buf, pal);
if (!image1) {
al_set_gfx_mode(AL_GFX_NONE, 0, 0, 0, 0);
al_show_message("Error reading %s!\n", buf);
return 1;
}
/* load the second picture */
al_replace_filename(buf, argv[0], "mysha.pcx", sizeof(buf));
image2 = al_load_bitmap(buf, pal);
if (!image2) {
al_destroy_bitmap(image1);
al_set_gfx_mode(AL_GFX_NONE, 0, 0, 0, 0);
al_show_message("Error reading %s!\n", buf);
return 1;
}
/* create a double buffer bitmap */
buffer = al_create_bitmap(AL_SCREEN_W, AL_SCREEN_H);
/* Note that because we loaded the images as truecolor bitmaps, we don't
* need to bother setting the palette, and we can display both on al_screen
* at the same time even though the source files use two different 256
* color palettes...
*/
prevx1 = prevy1 = prevx2 = prevy2 = 0;
al_printf_text(al_screen, al_font_8x8, 0, AL_SCREEN_H-8, al_make_color(255, 255, 255), "%d bpp", bpp);
while (!al_key_pressed()) {
timer = al_retrace_count;
al_clear_bitmap(buffer);
/* the first image moves in a slow al_draw_circle while being tinted to
* different colors...
*/
x1= 160+al_fix_to_int(al_fix_sin(al_int_to_fix(timer)/16)*160);
y1= 140-al_fix_to_int(al_fix_cos(al_int_to_fix(timer)/16)*140);
r = 127-al_fix_to_int(al_fix_cos(al_int_to_fix(timer)/6)*127);
g = 127-al_fix_to_int(al_fix_cos(al_int_to_fix(timer)/7)*127);
b = 127-al_fix_to_int(al_fix_cos(al_int_to_fix(timer)/8)*127);
a = 127-al_fix_to_int(al_fix_cos(al_int_to_fix(timer)/9)*127);
al_set_trans_blender(r, g, b, 0);
al_draw_lit_sprite(buffer, image1, x1, y1, a);
al_printf_text(al_screen, al_font_8x8, 0, 0, al_make_color(r, g, b), "light: %d ", a);
/* the second image moves in a faster al_draw_circle while the alpha value
* fades in and out...
*/
x2= 160+al_fix_to_int(al_fix_sin(al_int_to_fix(timer)/10)*160);
y2= 140-al_fix_to_int(al_fix_cos(al_int_to_fix(timer)/10)*140);
a = 127-al_fix_to_int(al_fix_cos(al_int_to_fix(timer)/4)*127);
al_set_trans_blender(0, 0, 0, a);
al_draw_trans_sprite(buffer, image2, x2, y2);
al_printf_text(al_screen, al_font_8x8, 0, 8, al_make_color(a, a, a), "alpha: %d ", a);
/* copy the double buffer across to the al_screen */
al_vsync();
x = MIN(x1, prevx1);
y = MIN(y1, prevy1);
w = MAX(x1, prevx1) + 320 - x;
h = MAX(y1, prevy1) + 200 - y;
al_blit(buffer, al_screen, x, y, x, y, w, h);
x = MIN(x2, prevx2);
y = MIN(y2, prevy2);
w = MAX(x2, prevx2) + 320 - x;
h = MAX(y2, prevy2) + 200 - y;
al_blit(buffer, al_screen, x, y, x, y, w, h);
prevx1 = x1;
prevy1 = y1;
prevx2 = x2;
prevy2 = y2;
}
al_clear_keybuf();
al_destroy_bitmap(image1);
al_destroy_bitmap(image2);
al_destroy_bitmap(buffer);
return 0;
}
AL_END_OF_MAIN();
See more files for this project here