home *** CD-ROM | disk | FTP | other *** search
/ Game Programming - All in One (3rd Edition) / game_prog_all_in_one_3rd_ed.iso / sources / chapter08 / transsprite / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  2006-09-13  |  2.1 KB  |  78 lines

  1. ////////////////////////////////////////////////////////////////
  2. // Game Programming All In One, Third Edition
  3. // Chapter 8 - TransSprite
  4. ////////////////////////////////////////////////////////////////
  5.  
  6. #include <allegro.h>
  7.  
  8. int main()
  9. {
  10.     int x, y, c, a;
  11.     BITMAP *background;
  12.     BITMAP *alpha;
  13.     BITMAP *sprite;
  14.     BITMAP *buffer;
  15.  
  16.     //initialize
  17.     allegro_init(); 
  18.     install_keyboard();
  19.     install_mouse();
  20.     set_color_depth(32);
  21.     set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
  22.  
  23.     //load the background bitmap
  24.     background = load_bitmap("mustang.bmp", NULL);
  25.  
  26.     //load the translucent foreground image
  27.     alpha = load_bitmap("alpha.bmp", NULL);
  28.     sprite = create_bitmap(alpha->w, alpha->h);
  29.  
  30.     //set the alpha channel blend values
  31.     drawing_mode(DRAW_MODE_TRANS, NULL, 0, 0);
  32.     set_write_alpha_blender();
  33.     //blend the two bitmap alpha channels
  34.     for (y=0; y<alpha->h; y++) {
  35.         for (x=0; x<alpha->w; x++) {
  36.             //grab the pixel color
  37.             c = getpixel(alpha, x, y);
  38.             a = getr(c) + getg(c) + getb(c);
  39.             //find the middle alpha value
  40.             a = MID(0, a/2-128, 255);
  41.             //copy the alpha-enabled pixel to the sprite
  42.             putpixel(sprite, x, y, a);
  43.         }
  44.     }
  45.  
  46.     //create a double buffer bitmap
  47.     buffer = create_bitmap(SCREEN_W, SCREEN_H);
  48.  
  49.     //draw the background image
  50.     blit(background, buffer, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
  51.  
  52.     while (!key[KEY_ESC])
  53.     {
  54.         //get the mouse coordinates
  55.         x = mouse_x - sprite->w/2;
  56.         y = mouse_y - sprite->h/2;
  57.  
  58.         //draw the translucent image
  59.         set_alpha_blender();
  60.         draw_trans_sprite(buffer, sprite, x, y);
  61.  
  62.         //draw memory buffer to the screen
  63.         blit(buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
  64.  
  65.         //restore the background
  66.         blit(background, buffer, x, y, x, y, sprite->w, sprite->h);
  67.     }
  68.     
  69.     destroy_bitmap(background);
  70.     destroy_bitmap(sprite);
  71.     destroy_bitmap(buffer);
  72.     destroy_bitmap(alpha);
  73.  
  74.     allegro_exit();
  75.     return 0;
  76. }
  77. END_OF_MAIN()
  78.