File: extrans.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 (163 lines) | stat: -rw-r--r-- 4,204 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
162
163
/*
 *    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"



/* RGB -> color mapping table. Not needed, but speeds things up */
RGB_MAP rgb_table;

/* lighting color mapping table */
COLOR_MAP light_table;

/* translucency color mapping table */
COLOR_MAP trans_table;



int main(int argc, char *argv[])
{
   PALETTE pal;
   BITMAP *s;
   BITMAP *spotlight;
   BITMAP *truecolor_spotlight;
   BITMAP *background;
   int i, x, y;
   char buf[256];
   char *filename;

   allegro_init(); 
   install_keyboard();
   install_timer();
   install_mouse(); 

   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;
   }

   /* load the main screen image */
   if (argc > 1)
      filename = argv[1];
   else {
      replace_filename(buf, argv[0], "allegro.pcx", sizeof(buf));
      filename = buf;
   }

   background = load_bitmap(filename, pal);
   if (!background) {
      set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
      allegro_message("Error reading %s!\n", filename);
      return 1;
   }

   /* this isn't needed, but it speeds up the color table calculations */
   create_rgb_table(&rgb_table, pal, NULL);
   rgb_map = &rgb_table;

   /* build a color lookup table for lighting effects */
   create_light_table(&light_table, pal, 0, 0, 0, NULL);

   /* build a color lookup table for translucent drawing */
   create_trans_table(&trans_table, pal, 128, 128, 128, NULL);

   set_palette(pal);

   s = create_bitmap(320, 200);
   spotlight = create_bitmap_ex(8, 128, 128);
   truecolor_spotlight = create_bitmap_ex(32, 128, 128);

   /* generate an 8bpp spotlight image */
   clear_bitmap(spotlight);
   for (i=0; i<256; i++)
      circlefill(spotlight, 64, 64, 64-i/4, i);

   /* select the lighting table */
   color_map = &light_table;

   /* display a spotlight effect */
   do {
      poll_mouse();

      x = mouse_x - SCREEN_W/2 - 64 + 160;
      y = mouse_y - SCREEN_H/2 - 64 + 100;

      clear_bitmap(s);

      /* unluckily we have to do something 'weird' for truecolor modes */
      if (bitmap_color_depth(screen) != 8) {
	 /* copy background on the truecolor spotlight */
	 blit(background, truecolor_spotlight, x, y , 0, 0, 127, 127);
	 /* set special write alpha blender */
	 set_write_alpha_blender();
	 drawing_mode(DRAW_MODE_TRANS, 0, 0, 0);
	 /* draw the alpha channel */
	 rectfill(truecolor_spotlight, 0, 0, 128, 128, 0);
	 draw_trans_sprite(truecolor_spotlight, spotlight, 0, 0);
	 /* set alpha blender and draw on the 's' bitmap */
	 drawing_mode(DRAW_MODE_SOLID, 0, 0, 0);
	 set_alpha_blender();
	 draw_trans_sprite(s, truecolor_spotlight, x, y);
	 /* restore a not-alpha blender */
	 set_trans_blender(0, 0, 0, 128);
      }
      else {
         blit(background, s, x, y, x, y, 128, 128);
         draw_trans_sprite(s, spotlight, x, y);
      }

      blit(s, screen, 0, 0, SCREEN_W/2 - 160, SCREEN_H/2 - 100, 320, 200);

   } while (!keypressed());

   clear_keybuf();

   /* for the next part we want spotlight and s to share color depth */
   if (bitmap_color_depth(spotlight) != bitmap_color_depth(s)) {
      destroy_bitmap(spotlight);
      spotlight = create_bitmap(128, 128);
   }

   /* generate an overlay image (just shrink the main image) */
   stretch_blit(background, spotlight, 0, 0, 320, 200, 0, 0, 128, 128);

   /* select the translucency table */
   color_map = &trans_table;

   /* select translucency blender */
   set_trans_blender(0, 0, 0, 128);

   /* display a translucent overlay */
   do {
      poll_mouse();

      x = mouse_x - SCREEN_W/2 - 64 + 160;
      y = mouse_y - SCREEN_H/2 - 64 + 100;

      blit(background, s, 0, 0, 0, 0, 320, 200);
      draw_trans_sprite(s, spotlight, x, y);

      blit(s, screen, 0, 0, SCREEN_W/2 - 160, SCREEN_H/2 - 100, 320, 200);

   } while (!keypressed());

   clear_keybuf();

   destroy_bitmap(s);
   destroy_bitmap(spotlight);
   destroy_bitmap(truecolor_spotlight);
   destroy_bitmap(background);

   return 0;
}

END_OF_MAIN();