Show extrans.c syntax highlighted
/*
* Example program for the Allegro library, by Owen Embury.
*
* This program demonstrates how to use the lighting and translucency
* functions.
*/
#include <stdio.h>
#include "allegro.h"
/* AL_RGB -> color mapping table. Not needed, but speeds things up */
AL_RGB_MAP rgb_table;
/* lighting color mapping table */
AL_COLOR_MAP light_table;
/* translucency color mapping table */
AL_COLOR_MAP trans_table;
int main(int argc, char *argv[])
{
AL_PALETTE pal;
AL_BITMAP *s;
AL_BITMAP *spotlight;
AL_BITMAP *truecolor_spotlight;
AL_BITMAP *background;
int i, x, y;
char buf[256];
char *filename;
allegro_init();
al_install_keyboard();
al_install_timer();
al_install_mouse();
if (al_set_gfx_mode(AL_GFX_SAFE, 320, 200, 0, 0) != 0) {
al_set_gfx_mode(AL_GFX_NONE, 0, 0, 0, 0);
al_show_message("Unable to set any graphic mode\n%s\n", al_error);
return 1;
}
/* load the main al_screen image */
if (argc > 1)
filename = argv[1];
else {
al_replace_filename(buf, argv[0], "allegro.pcx", sizeof(buf));
filename = buf;
}
background = al_load_bitmap(filename, pal);
if (!background) {
al_set_gfx_mode(AL_GFX_NONE, 0, 0, 0, 0);
al_show_message("Error reading %s!\n", filename);
return 1;
}
/* this isn't needed, but it speeds up the color table calculations */
al_create_rgb_table(&rgb_table, pal, NULL);
al_rgb_map = &rgb_table;
/* build a color lookup table for lighting effects */
al_create_light_table(&light_table, pal, 0, 0, 0, NULL);
/* build a color lookup table for translucent drawing */
al_create_trans_table(&trans_table, pal, 128, 128, 128, NULL);
al_set_palette(pal);
s = al_create_bitmap(320, 200);
spotlight = al_create_bitmap_ex(8, 128, 128);
truecolor_spotlight = al_create_bitmap_ex(32, 128, 128);
/* generate an 8bpp spotlight image */
al_clear_bitmap(spotlight);
for (i=0; i<256; i++)
al_draw_circle_fill(spotlight, 64, 64, 64-i/4, i);
/* select the lighting table */
al_color_map = &light_table;
/* display a spotlight effect */
do {
al_poll_mouse();
x = al_mouse_x - AL_SCREEN_W/2 - 64 + 160;
y = al_mouse_y - AL_SCREEN_H/2 - 64 + 100;
al_clear_bitmap(s);
/* unluckily we have to do something 'weird' for truecolor modes */
if (al_bitmap_color_depth(al_screen) != 8) {
/* copy background on the truecolor spotlight */
al_blit(background, truecolor_spotlight, x, y , 0, 0, 127, 127);
/* set special write alpha blender */
al_set_write_alpha_blender();
al_drawing_mode(DRAW_MODE_TRANS, 0, 0, 0);
/* draw the alpha channel */
al_draw_rect_fill(truecolor_spotlight, 0, 0, 128, 128, 0);
al_draw_trans_sprite(truecolor_spotlight, spotlight, 0, 0);
/* set alpha blender and draw on the 's' bitmap */
al_drawing_mode(DRAW_MODE_SOLID, 0, 0, 0);
al_set_alpha_blender();
al_draw_trans_sprite(s, truecolor_spotlight, x, y);
/* restore a not-alpha blender */
al_set_trans_blender(0, 0, 0, 128);
}
else {
al_blit(background, s, x, y, x, y, 128, 128);
al_draw_trans_sprite(s, spotlight, x, y);
}
al_blit(s, al_screen, 0, 0, AL_SCREEN_W/2 - 160, AL_SCREEN_H/2 - 100, 320, 200);
} while (!al_key_pressed());
al_clear_keybuf();
/* for the next part we want spotlight and s to share color depth */
if (al_bitmap_color_depth(spotlight) != al_bitmap_color_depth(s)) {
al_destroy_bitmap(spotlight);
spotlight = al_create_bitmap(128, 128);
}
/* generate an overlay image (just shrink the main image) */
al_stretch(background, spotlight, 0, 0, 320, 200, 0, 0, 128, 128);
/* select the translucency table */
al_color_map = &trans_table;
/* select translucency blender */
al_set_trans_blender(0, 0, 0, 128);
/* display a translucent overlay */
do {
al_poll_mouse();
x = al_mouse_x - AL_SCREEN_W/2 - 64 + 160;
y = al_mouse_y - AL_SCREEN_H/2 - 64 + 100;
al_blit(background, s, 0, 0, 0, 0, 320, 200);
al_draw_trans_sprite(s, spotlight, x, y);
al_blit(s, al_screen, 0, 0, AL_SCREEN_W/2 - 160, AL_SCREEN_H/2 - 100, 320, 200);
} while (!al_key_pressed());
al_clear_keybuf();
al_destroy_bitmap(s);
al_destroy_bitmap(spotlight);
al_destroy_bitmap(truecolor_spotlight);
al_destroy_bitmap(background);
return 0;
}
AL_END_OF_MAIN();
See more files for this project here