1 /*
2  *    Example program for the Allegro library, by Shawn Hargreaves.
3  *
4  *    This program demonstrates how to use the translucency functions
5  *    in truecolor video modes. Two image files are loaded from
6  *    disk and displayed moving slowly around the screen. One of
7  *    the images will be tinted to different colors. The other
8  *    image will be faded out with a varying alpha strength, and
9  *    drawn on top of the other image.
10  */
11 
12 
13 #include <allegro.h>
14 
15 
16 
main(int argc,char * argv[])17 int main(int argc, char *argv[])
18 {
19    char buf[256];
20    PALETTE pal;
21    BITMAP *image1;
22    BITMAP *image2;
23    BITMAP *buffer;
24    int r, g, b, a;
25    int x, y, w, h;
26    int x1, y1, x2, y2;
27    int prevx1, prevy1, prevx2, prevy2;
28    int timer;
29    int bpp = -1;
30    int ret = -1;
31 
32    if (allegro_init() != 0)
33       return 1;
34    install_keyboard();
35    install_timer();
36 
37    /* what color depth should we use? */
38    if (argc > 1) {
39       if ((argv[1][0] == '-') || (argv[1][0] == '/'))
40 	 argv[1]++;
41       bpp = atoi(argv[1]);
42       if ((bpp != 15) && (bpp != 16) && (bpp != 24) && (bpp != 32)) {
43 	 allegro_message("Invalid color depth '%s'\n", argv[1]);
44 	 return 1;
45       }
46    }
47 
48    if (bpp > 0) {
49       /* set a user-requested color depth */
50       set_color_depth(bpp);
51       ret = set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0);
52    }
53    else {
54       /* autodetect what color depths are available */
55       static int color_depths[] = { 16, 15, 32, 24, 0 };
56       for (a=0; color_depths[a]; a++) {
57 	 bpp = color_depths[a];
58 	 set_color_depth(bpp);
59 	 ret = set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0);
60 	 if (ret == 0)
61 	    break;
62       }
63    }
64 
65    /* did the video mode set properly? */
66    if (ret != 0) {
67       set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
68       allegro_message("Error setting %d bit graphics mode\n%s\n", bpp,
69 		      allegro_error);
70       return 1;
71    }
72 
73    /* specify that images should be loaded in a truecolor pixel format */
74    set_color_conversion(COLORCONV_TOTAL);
75 
76    /* load the first picture */
77    replace_filename(buf, argv[0], "allegro.pcx", sizeof(buf));
78    image1 = load_bitmap(buf, pal);
79    if (!image1) {
80       set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
81       allegro_message("Error reading %s!\n", buf);
82       return 1;
83    }
84 
85    /* load the second picture */
86    replace_filename(buf, argv[0], "mysha.pcx", sizeof(buf));
87    image2 = load_bitmap(buf, pal);
88    if (!image2) {
89       destroy_bitmap(image1);
90       set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
91       allegro_message("Error reading %s!\n", buf);
92       return 1;
93    }
94 
95    /* create a double buffer bitmap */
96    buffer = create_bitmap(SCREEN_W, SCREEN_H);
97 
98    /* Note that because we loaded the images as truecolor bitmaps, we don't
99     * need to bother setting the palette, and we can display both on screen
100     * at the same time even though the source files use two different 256
101     * color palettes...
102     */
103 
104    prevx1 = prevy1 = prevx2 = prevy2 = 0;
105 
106    textprintf_ex(screen, font, 0, SCREEN_H-8, makecol(255, 255, 255), 0,
107 		 "%d bpp", bpp);
108 
109    while (!keypressed()) {
110       timer = retrace_count;
111       clear_bitmap(buffer);
112 
113       /* the first image moves in a slow circle while being tinted to
114        * different colors...
115        */
116       x1= 160+fixtoi(fixsin(itofix(timer)/16)*160);
117       y1= 140-fixtoi(fixcos(itofix(timer)/16)*140);
118       r = 127-fixtoi(fixcos(itofix(timer)/6)*127);
119       g = 127-fixtoi(fixcos(itofix(timer)/7)*127);
120       b = 127-fixtoi(fixcos(itofix(timer)/8)*127);
121       a = 127-fixtoi(fixcos(itofix(timer)/9)*127);
122       set_trans_blender(r, g, b, 0);
123       draw_lit_sprite(buffer, image1, x1, y1, a);
124       textprintf_ex(screen, font, 0, 0, makecol(r, g, b), 0, "light: %d ", a);
125 
126       /* the second image moves in a faster circle while the alpha value
127        * fades in and out...
128        */
129       x2= 160+fixtoi(fixsin(itofix(timer)/10)*160);
130       y2= 140-fixtoi(fixcos(itofix(timer)/10)*140);
131       a = 127-fixtoi(fixcos(itofix(timer)/4)*127);
132       set_trans_blender(0, 0, 0, a);
133       draw_trans_sprite(buffer, image2, x2, y2);
134       textprintf_ex(screen, font, 0, 8, makecol(a, a, a), 0, "alpha: %d ", a);
135 
136       /* copy the double buffer across to the screen */
137       vsync();
138 
139       x = MIN(x1, prevx1);
140       y = MIN(y1, prevy1);
141       w = MAX(x1, prevx1) + 320 - x;
142       h = MAX(y1, prevy1) + 200 - y;
143       blit(buffer, screen, x, y, x, y, w, h);
144 
145       x = MIN(x2, prevx2);
146       y = MIN(y2, prevy2);
147       w = MAX(x2, prevx2) + 320 - x;
148       h = MAX(y2, prevy2) + 200 - y;
149       blit(buffer, screen, x, y, x, y, w, h);
150 
151       prevx1 = x1;
152       prevy1 = y1;
153       prevx2 = x2;
154       prevy2 = y2;
155    }
156 
157    clear_keybuf();
158 
159    destroy_bitmap(image1);
160    destroy_bitmap(image2);
161    destroy_bitmap(buffer);
162 
163    return 0;
164 }
165 
166 END_OF_MAIN()
167