1 /*
2 * Example program for the Allegro library, by Shawn Hargreaves.
3 *
4 * This program demonstrates how to load and display bitmap files
5 * in truecolor video modes, and how to crossfade between them.
6 * You have to use this example from the command line to specify
7 * as parameters a number of graphic files. Use at least two
8 * files to see the graphical effect. The example will crossfade
9 * from one image to another with each key press until you press
10 * the ESC key.
11 */
12
13
14 #include <allegro.h>
15
16
17 const char *default_argv[] = {
18 "exxfade", "allegro.pcx", "mysha.pcx", "planet.pcx"};
19
20
21
show(const char * name)22 int show(const char *name)
23 {
24 BITMAP *bmp, *buffer;
25 PALETTE pal;
26 int alpha;
27
28 /* load the file */
29 bmp = load_bitmap(name, pal);
30 if (!bmp)
31 return -1;
32
33 buffer = create_bitmap(SCREEN_W, SCREEN_H);
34 blit(screen, buffer, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
35
36 set_palette(pal);
37
38 /* fade it in on top of the previous picture */
39 for (alpha=0; alpha<256; alpha+=8) {
40 set_trans_blender(0, 0, 0, alpha);
41 draw_trans_sprite(buffer, bmp,
42 (SCREEN_W-bmp->w)/2, (SCREEN_H-bmp->h)/2);
43 vsync();
44 blit(buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
45 if (keypressed()) {
46 destroy_bitmap(bmp);
47 destroy_bitmap(buffer);
48 if ((readkey() & 0xFF) == 27)
49 return 1;
50 else
51 return 0;
52 }
53
54 /* slow down animation for modern machines */
55 rest(2);
56 }
57
58 blit(bmp, screen, 0, 0, (SCREEN_W-bmp->w)/2, (SCREEN_H-bmp->h)/2,
59 bmp->w, bmp->h);
60
61 destroy_bitmap(bmp);
62 destroy_bitmap(buffer);
63
64 if ((readkey() & 0xFF) == 27)
65 return 1;
66 else
67 return 0;
68 }
69
70
71
main(int argc,const char * argv[])72 int main(int argc, const char *argv[])
73 {
74 int i;
75
76 if (allegro_init() != 0)
77 return 1;
78
79 if (argc < 2) {
80 /* use a default set of images if the user doesn't specify any */
81 argc = sizeof(default_argv) / sizeof(default_argv[0]);
82 argv = default_argv;
83 }
84
85 install_keyboard();
86
87 /* set the best color depth that we can find */
88 set_color_depth(16);
89 if (set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0) != 0) {
90 set_color_depth(15);
91 if (set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0) != 0) {
92 set_color_depth(32);
93 if (set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0) != 0) {
94 set_color_depth(24);
95 if (set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0) != 0) {
96 set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
97 allegro_message("Error setting graphics mode\n%s\n",
98 allegro_error);
99 return 1;
100 }
101 }
102 }
103 }
104
105 /* load all images in the same color depth as the display */
106 set_color_conversion(COLORCONV_TOTAL);
107
108 /* process all the files on our command line */
109 i=1;
110 for (;;) {
111 switch (show(argv[i])) {
112
113 case -1:
114 /* error */
115 set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
116 allegro_message("Error loading image file '%s'\n", argv[i]);
117 return 1;
118
119 case 0:
120 /* ok! */
121 break;
122
123 case 1:
124 /* quit */
125 allegro_exit();
126 return 0;
127 }
128
129 if (++i >= argc)
130 i=1;
131 }
132
133 return 0;
134 }
135
136 END_OF_MAIN()
137