1 | #include "allegro.h" |
2 | |
3 | using namespace std; |
4 | |
5 | // data structures for snow flaxes = very simple |
6 | const int total_flakes=900; |
7 | const int total_layers=3; |
8 | |
9 | struct particle |
10 | { |
11 | float x,y; |
12 | int layer; |
13 | }; |
14 | |
15 | BITMAP *buffer; |
16 | |
17 | volatile int Counter; |
18 | void IncreaseCounter() { Counter++; } |
19 | |
20 | // global data - okay for a simple example like this |
21 | particle flakes[total_flakes]; |
22 | |
23 | // This function initalizes the particle flakes |
24 | void initalize_particle_flakes(void) |
25 | { |
26 | int i; |
27 | for (i=0;i<total_flakes;i++) |
28 | { |
29 | flakes<i>.x=rand()%320; // 0-319 [x] |
30 | flakes<i>.y=rand()%200; // 0-199 [y] |
31 | flakes<i>.layer=rand()%total_layers;// [0-2] [layer] |
32 | } |
33 | } |
34 | |
35 | // This function draws all the particle flakes |
36 | void draw_particle_flakes(void) |
37 | { |
38 | int i; |
39 | for (i=0;i<total_flakes;i++) |
40 | { |
41 | // note - we are drawing according to the color scale |
42 | _putpixel(buffer, int(flakes<i>.x), int(flakes<i>.y), |
43 | flakes<i>.layer*5+20); |
44 | } |
45 | } |
46 | |
47 | // This updates the particle flakes |
48 | void update_particle_flakes(void) |
49 | { |
50 | int i; |
51 | for (i=0;i<total_flakes;i++) |
52 | { |
53 | // check for wrap around |
54 | if (flakes<i>.y<SCREEN_H-2 && _getpixel(buffer, int(flakes<i>.x), int(flakes<i>.y+1))!=flakes<i>.layer*5+20) |
55 | { |
56 | if (flakes<i>.y>199) |
57 | { |
58 | flakes<i>.x=rand()%320; |
59 | flakes<i>.y=0; |
60 | flakes<i>.layer=rand()%total_layers; |
61 | } |
62 | flakes<i>.x=(int(flakes<i>.x)+(2-rand()%5)) % 320; |
63 | flakes<i>.y+=flakes<i>.layer*0.1+0.5; |
64 | } |
65 | |
66 | } |
67 | } |
68 | |
69 | // main program |
70 | int main(int argc, char *argv[]) { |
71 | int i; |
72 | |
73 | allegro_init(); // init Allegro |
74 | install_keyboard(); // setup allegro keyboard |
75 | install_timer(); |
76 | |
77 | LOCK_VARIABLE(Counter); |
78 | LOCK_FUNCTION(IncreaseCounter); |
79 | install_int_ex(IncreaseCounter, BPS_TO_TIMER(60)); |
80 | |
81 | set_color_depth(8) ; // 8 bit colour |
82 | if (set_gfx_mode(GFX_AUTODETECT_WINDOWED, |
83 | 320, 200, 0, 0)<0) |
84 | { |
85 | set_gfx_mode(GFX_TEXT, 0, 0, 0, 0); |
86 | allegro_message("Failure to init video mode!\n%s\n", |
87 | allegro_error); |
88 | } |
89 | initalize_particle_flakes(); |
90 | |
91 | buffer = create_bitmap(SCREEN_W, SCREEN_H); |
92 | clear(buffer); |
93 | |
94 | while ((!key[KEY_ESC])&&(!key[KEY_SPACE])) { |
95 | while (Counter>0) |
96 | { |
97 | update_particle_flakes(); |
98 | Counter--; |
99 | } |
100 | clear(buffer); |
101 | draw_particle_flakes(); |
102 | blit(buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H); |
103 | }; |
104 | |
105 | return 0; |
106 | } |
107 | END_OF_MAIN(); |