Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » need help with simple effect

This thread is locked; no one can reply to it. rss feed Print
need help with simple effect
matt reynolds
Member #7,448
July 2006

i got this code from a hugi article and im trying to change it a little bit to make the snow pile up

1#include "allegro.h"
2 
3using namespace std;
4 
5// data structures for snow flaxes = very simple
6const int total_flakes=900;
7const int total_layers=3;
8 
9struct particle
10{
11 int x,y;
12 int layer;
13};
14 
15// global data - okay for a simple example like this
16particle flakes[total_flakes];
17 
18// This function initalizes the particle flakes
19void initalize_particle_flakes(void)
20{
21 int i;
22 for (i=0;i<total_flakes;i++)
23 {
24 flakes<i>.x=rand()%320; // 0-319 [x]
25 flakes<i>.y=rand()%200; // 0-199 [y]
26 flakes<i>.layer=rand()%total_layers;// [0-2] [layer]
27 }
28}
29
30// This function draws all the particle flakes
31void draw_particle_flakes(void)
32{
33 int i;
34 for (i=0;i<total_flakes;i++)
35 {
36 // note - we are drawing according to the color scale
37 _putpixel(screen, flakes<i>.x, flakes<i>.y,
38 flakes<i>.layer*5+20);
39 }
40}
41
42// This updates the particle flakes
43void update_particle_flakes(void)
44{
45 int i;
46 for (i=0;i<total_flakes;i++)
47 {
48 // drop the particle down - depending on layer
49 // [plus one - since layer zero would result
50 // in no motion otherwize]
51 flakes<i>.y+=flakes<i>.layer+1;
52
53 // check for wrap around
54 if (flakes<i>.y>199)
55 {
56 flakes<i>.x=rand()%320;
57 flakes<i>.y=0;
58 flakes<i>.layer=rand()%total_layers;
59 }
60
61 // new x position
62 flakes<i>.x=(flakes<i>.x+(2-rand()%5)) % 320;
63 
64 }
65}
66
67// main program
68int main(int argc, char *argv[]) {
69 int i;
70
71 allegro_init(); // init Allegro
72 install_keyboard(); // setup allegro keyboard
73
74 set_color_depth(8) ; // 8 bit colour
75 if (set_gfx_mode(GFX_AUTODETECT_FULLSCREEN,
76 320, 200, 0, 0)<0)
77 {
78 set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
79 allegro_message("Failure to init video mode!\n%s\n",
80 allegro_error);
81 }
82 initalize_particle_flakes();
83
84 while ((!key[KEY_ESC])&&(!key[KEY_SPACE])) {
85 update_particle_flakes();
86 vsync(); // you might want to add another vsynch;
87 // depending how fast
88 vsync(); //things are moving on your box
89 vsync();
90
91 clear_bitmap(screen);
92 draw_particle_flakes();
93 };
94
95 return 0;
96}
97END_OF_MAIN();

the only thing i have thought of is maybe something like this but i tryed it and it just made some dynamic snow on the bottom

        int pixx,xs,xpile,ypile
        pixx = 0;
        pixx = _getpixel(screen, xs=rand()%xpile, ypile);
        if (pixx < 1)
       _putpixel(screen, xs, ypile, layers=rand()%3*5+20);

i am a newbie coder and a newbie to allegro but any advice would be great!

anto80
Member #3,230
February 2003
avatar

What do you want to do? particles(like fireworks?)?

[edit] if so, this thread may help you.
http://www.allegro.cc/forums/thread/415131

___________
Currently working on his action/puzzle game CIPHER PUSHER : Blocks/Vortexes/Seafood! Facebook - Twitter - webpage

CursedTyrant
Member #7,080
April 2006
avatar

1#include "allegro.h"
2 
3using namespace std;
4 
5// data structures for snow flaxes = very simple
6const int total_flakes=900;
7const int total_layers=3;
8 
9struct particle
10{
11 float x,y;
12 int layer;
13};
14 
15BITMAP *buffer;
16 
17volatile int Counter;
18void IncreaseCounter() { Counter++; }
19 
20// global data - okay for a simple example like this
21particle flakes[total_flakes];
22 
23// This function initalizes the particle flakes
24void 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
36void 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
48void 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
70int 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}
107END_OF_MAIN();

That should do it. The particles pile up and lie on the bottom of the screen.

- Use timers and floats instead of vsync
- Use that double buffer, don't draw directly to the screen, makes things flicker

---------
Signature.
----
[My Website] | [My YouTube Channel]

matt reynolds
Member #7,448
July 2006

thanks for the help...
wow you cleaned that code up nicley its so much more understandable like that
there is a few new functions and etc you introduced in there i will have to lookup but
for the most part i understand it

Go to: