[AD] new fade-from example

[ Thread Index | Date Index | More lists.liballeg.org/allegro-developers Archives ]


I make this example some time ago, and today I see it and send you
because it's very easy and powerful:

"This program desmostrates how make a transition effect between two
bitmaps of 16 colors (note 16 colors and not 16 bits per pixel)."

Note: I unknow if the English comments are OK, so, if someone can
      check it, that will be much better...

-- 
David A. Capello
dacap@xxxxxxxxxx
http://www.davidcapello.com.ar
/*
 *    Example program for the Allegro library, by David A. Capello.
 *
 *    This program desmostrates how make a transition effect between two
 *    bitmaps of 16 colors (note 16 colors and not 16 bits per pixel).
 */


#include "allegro.h"



/* make a transition between two bitmaps which uses only the first 16 colors */
void fade_bitmap(BITMAP *source_bmp, AL_CONST PALETTE source_pal,
                 BITMAP *dest_bmp, AL_CONST PALETTE dest_pal, int speed)
{
   PALETTE pal1, pal2;
   int c1, c2;
   int x, y;

   /* dark the screen */
   set_palette(black_palette);

   /* draw the two bitmaps onto the 8 bpp screen (4 bits for the first
      bitmap and the remaining 4 for the other bitmap) */
   for (y=0; y<SCREEN_H; y++) {
      for (x=0; x<SCREEN_W; x++) {
	 c1 = getpixel(source_bmp, x, y);
	 c2 = getpixel(dest_bmp, x, y);

	 putpixel(screen, x, y, (c1) | (c2<<4));
      }
   }

   /* create the two palette colors for the fusion */
   for (y=0; y<16; y++) {
      for (x=0; x<16; x++) {
	 pal1[y*16+x].r = source_pal[x].r;
	 pal1[y*16+x].g = source_pal[x].g;
	 pal1[y*16+x].b = source_pal[x].b;

	 pal2[y*16+x].r = dest_pal[y].r;
	 pal2[y*16+x].g = dest_pal[y].g;
	 pal2[y*16+x].b = dest_pal[y].b;
      }
   }

   /* set the first palette */
   set_palette(pal1);

   /* wait for the user response */
   readkey();

   /* merge to the second palette */
   fade_from(pal1, pal2, speed);

   /* and wait other key */
   readkey();
}



int main()
{
   PALETTE pal1;
   PALETTE pal2;
   BITMAP *bmp1, *bmp2;
   int c;

   allegro_init();
   install_keyboard();

   /* set a graphics mode sized 320x200 */
   if (set_gfx_mode(GFX_SAFE, 320, 200, 0, 0) != 0) {
      set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
      allegro_message("Unable to set any graphic mode\n%s\n", allegro_error);
      return 1;
   }

   /* make the two color palettes */
   for (c=0; c<16; c++) {
     /* the first palette is red */
     pal1[c].r = 63 * c / 15;
     pal1[c].g = 0;
     pal1[c].b = 0;

     /* and second palette is blue */
     pal2[c].r = 0;
     pal2[c].g = 0;
     pal2[c].b = 63 * c / 15;
   }

   /* create two memory bitmaps (with 16 colors) for the fade effect */
   bmp1 = create_bitmap(SCREEN_W, SCREEN_H);
   bmp2 = create_bitmap(SCREEN_W, SCREEN_H);

   clear(bmp1);
   clear(bmp2);

   for (c=0; c<16; c++) {
     /* first bitmap with ellipses */
     ellipsefill(bmp1, bmp1->w/2, bmp1->h/2,
                       (bmp1->w/2) * (15-c) / 16,
		       (bmp1->h/2) * (15-c) / 16, c);

     /* second bitmap with rectangles */
     rectfill(bmp2, bmp2->w/2 - ((bmp1->w/2) * (15-c) / 16),
                    bmp2->h/2 - ((bmp1->h/2) * (15-c) / 16),
		    bmp2->w/2 + ((bmp1->w/2) * (15-c) / 16),
		    bmp2->h/2 + ((bmp1->h/2) * (15-c) / 16), c);
   }

   /* fade from the first to the second bitmap with a slow speed */
   fade_bitmap(bmp1, pal1, bmp2, pal2, 1);

   /* destroy the two bitmaps */
   destroy_bitmap(bmp1);
   destroy_bitmap(bmp2);

   return 0;
}

END_OF_MAIN();


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/