File: exalpha.c

package info (click to toggle)
allegro4 2%3A4.0.1-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 17,052 kB
  • ctags: 12,972
  • sloc: ansic: 109,525; asm: 16,672; cpp: 3,221; sh: 1,761; makefile: 556; pascal: 105; perl: 73
file content (161 lines) | stat: -rw-r--r-- 4,787 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
 *    Example program for the Allegro library, by Shawn Hargreaves.
 *
 *    This program demonstrates how to use the 32 bit 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];
   BITMAP *background;
   BITMAP *alpha;
   BITMAP *sprite;
   BITMAP *buffer;
   int bpp = -1;
   int ret = -1;
   int x, y, c, a;

   allegro_init(); 
   install_keyboard(); 
   install_mouse(); 
   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)) {
	 allegro_message("Invalid color depth '%s'\n", argv[1]);
	 return 1;
      }
   }

   if (bpp > 0) {
      /* set a user-requested color depth */
      set_color_depth(bpp);
      ret = set_gfx_mode(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];
	 set_color_depth(bpp);
	 ret = set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0);
	 if (ret == 0)
	    break;
      }
   }

   /* did the video mode set properly? */
   if (ret != 0) {
      set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
      allegro_message("Error setting %d bit graphics mode\n%s\n", bpp, allegro_error);
      return 1;
   }

   /* load the background picture */
   replace_filename(buf, argv[0], "allegro.pcx", sizeof(buf));
   background = load_bitmap(buf, NULL);
   if (!background) {
      set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
      allegro_message("Error reading %s!\n", buf);
      return 1;
   }

   /* make a copy of it */
   set_color_depth(32);
   sprite = create_bitmap(background->w, background->h);
   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 set_color_depth(). That is
    * because the disk file is actually only a 256 color graphic: if it
    * was already a 32 bit RGBA sprite, we would probably want to use
    * set_color_conversion(COLORCONV_NONE) instead.
    */
   replace_filename(buf, argv[0], "mysha.pcx", sizeof(buf));
   alpha = load_bitmap(buf, NULL);
   if (!alpha) {
      set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
      allegro_message("Error reading %s!\n", buf);
      return 1;
   }

   /* normally we would have loaded an 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.
    */
   drawing_mode(DRAW_MODE_TRANS, NULL, 0, 0);
   set_write_alpha_blender();

   for (y=0; y<sprite->h; y++) {
      for (x=0; x<sprite->w; x++) {
	 c = getpixel(alpha, x, y);
	 a = getr(c) + getg(c) + getb(c);
	 a = MID(0, a/2-128, 255);

	 putpixel(sprite, x, y, a);
      }
   }

   destroy_bitmap(alpha);

   set_color_depth(bpp);

   /* darken the background image down a bit */
   drawing_mode(DRAW_MODE_TRANS, NULL, 0, 0);
   set_multiply_blender(0, 0, 0, 255);
   rectfill(background, 0, 0, background->w, background->h, makecol(32, 16, 128));
   solid_mode();

   /* create a double buffer bitmap */
   buffer = create_bitmap(SCREEN_W, SCREEN_H);

   /* scale the background image to be the same size as the screen */
   stretch_blit(background, buffer, 0, 0, background->w, background->h, 0, 0, SCREEN_W, SCREEN_H);

   text_mode(-1);
   textprintf(buffer, font, 0, 0, makecol(255, 255, 255), "%dx%d, %dbpp", SCREEN_W, SCREEN_H, bpp);

   destroy_bitmap(background);
   background = create_bitmap(SCREEN_W, SCREEN_H);
   blit(buffer, background, 0, 0, 0, 0, SCREEN_W, SCREEN_H);

   while (!keypressed()) {
      /* draw the alpha sprite */
      x = mouse_x - sprite->w/2;
      y = mouse_y - sprite->h/2;

      set_alpha_blender();
      draw_trans_sprite(buffer, sprite, x, y);

      /* flip it across to the screen */
      blit(buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);

      /* replace the background where we drew the sprite */
      blit(background, buffer, x, y, x, y, sprite->w, sprite->h);
   }

   clear_keybuf();

   destroy_bitmap(background);
   destroy_bitmap(sprite);
   destroy_bitmap(buffer);

   return 0;
}

END_OF_MAIN();