Show exalpha.c syntax highlighted
/*
* Example program for the Allegro library, by Shawn Hargreaves.
*
* This program demonstrates how to use the 32 bit AL_RGBA translucency
* functions to store an alpha channel along with a bitmap graphic.
*/
#include "allegro.h"
int main(int argc, char *argv[])
{
char buf[256];
AL_BITMAP *background;
AL_BITMAP *alpha;
AL_BITMAP *sprite;
AL_BITMAP *buffer;
int bpp = -1;
int ret = -1;
int x, y, c, a;
allegro_init();
al_install_keyboard();
al_install_mouse();
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;
}
/* load the background picture */
al_replace_filename(buf, argv[0], "allegro.pcx", sizeof(buf));
background = al_load_bitmap(buf, NULL);
if (!background) {
al_set_gfx_mode(AL_GFX_NONE, 0, 0, 0, 0);
al_show_message("Error reading %s!\n", buf);
return 1;
}
/* make a copy of it */
al_set_color_depth(32);
sprite = al_create_bitmap(background->w, background->h);
al_blit(background, sprite, 0, 0, 0, 0, background->w, background->h);
/* load the alpha sprite image. Note that we specifically force this
* to load in a 32 bit format by calling al_set_color_depth(). That is
* because the disk file is actually only a 256 color graphic: if it
* was already a 32 bit AL_RGBA sprite, we would probably want to use
* al_set_color_conversion(AL_COLORCONV_NONE) instead.
*/
al_replace_filename(buf, argv[0], "mysha.pcx", sizeof(buf));
alpha = al_load_bitmap(buf, NULL);
if (!alpha) {
al_set_gfx_mode(AL_GFX_NONE, 0, 0, 0, 0);
al_show_message("Error reading %s!\n", buf);
return 1;
}
/* normally we would have loaded an AL_RGBA image directly from disk. Since
* I don't have one lying around, and am too lazy to draw one (or I could
* rationalise this by saying that I'm trying to save download size by
* reusing graphics :-) I'll just have to generate an alpha channel in
* code. I do this by using greyscale values from the mouse picture as an
* alpha channel for the Allegro image. Don't worry about this code: you
* wouldn't normally need to write anything like this, because you'd just
* get the right graphics directly out of a datafile.
*/
al_drawing_mode(DRAW_MODE_TRANS, NULL, 0, 0);
al_set_write_alpha_blender();
for (y=0; y<sprite->h; y++) {
for (x=0; x<sprite->w; x++) {
c = al_get_pixel(alpha, x, y);
a = al_get_r(c) + al_get_g(c) + al_get_b(c);
a = MID(0, a/2-128, 255);
al_put_pixel(sprite, x, y, a);
}
}
al_destroy_bitmap(alpha);
al_set_color_depth(bpp);
/* darken the background image down a bit */
al_drawing_mode(DRAW_MODE_TRANS, NULL, 0, 0);
al_set_multiply_blender(0, 0, 0, 255);
al_draw_rect_fill(background, 0, 0, background->w, background->h, al_make_color(32, 16, 128));
al_drawing_mode(DRAW_MODE_SOLID, NULL, 0, 0);
/* create a double buffer bitmap */
buffer = al_create_bitmap(AL_SCREEN_W, AL_SCREEN_H);
/* scale the background image to be the same size as the al_screen */
al_stretch(background, buffer, 0, 0, background->w, background->h, 0, 0, AL_SCREEN_W, AL_SCREEN_H);
al_text_mode(-1);
al_printf_text(buffer, al_font_8x8, 0, 0, al_make_color(255, 255, 255), "%dx%d, %dbpp", AL_SCREEN_W, AL_SCREEN_H, bpp);
al_destroy_bitmap(background);
background = al_create_bitmap(AL_SCREEN_W, AL_SCREEN_H);
al_blit(buffer, background, 0, 0, 0, 0, AL_SCREEN_W, AL_SCREEN_H);
while (!al_key_pressed()) {
/* draw the alpha sprite */
x = al_mouse_x - sprite->w/2;
y = al_mouse_y - sprite->h/2;
al_set_alpha_blender();
al_draw_trans_sprite(buffer, sprite, x, y);
/* flip it across to the al_screen */
al_blit(buffer, al_screen, 0, 0, 0, 0, AL_SCREEN_W, AL_SCREEN_H);
/* replace the background where we drew the sprite */
al_blit(background, buffer, x, y, x, y, sprite->w, sprite->h);
}
al_clear_keybuf();
al_destroy_bitmap(background);
al_destroy_bitmap(sprite);
al_destroy_bitmap(buffer);
return 0;
}
AL_END_OF_MAIN();
See more files for this project here