Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » A Simple Particle Explosion

This thread is locked; no one can reply to it. rss feed Print
 1   2 
A Simple Particle Explosion
yohosuff
Member #10,022
July 2008
avatar

Thanks for the explanations. I understand memory leaks now. :)

Neil Black
Member #7,867
October 2006
avatar

I managed to make a little explosion too. I'm quite proud of it. I'd probably use it in combination with an animation of an explosion in the center.

1#include <math.h>
2#include <allegro.h>
3 
4BITMAP* buffer;
5 
6struct Particle_Type
7{
8 double x;
9 double y;
10 double speed;
11 double angle;
12 int life;
13}Particles[150];
14 
15double Max_Angle = 2 * 3.14159;
16 
17void Draw_Stuff()
18{
19 clear_to_color(buffer, makecol(0, 0, 0));
20 for(int i = 0; i < 150; i++)
21 {
22 if(Particles<i>.life)
23 {
24 putpixel(buffer, Particles<i>.x, Particles<i>.y, makecol(255, 255, 255));
25 }
26 }
27 blit(buffer, screen, 0, 0, 0, 0, 640, 480);
28}
29 
30int main()
31{
32 allegro_init();
33 install_keyboard();
34 set_color_depth(16);
35 set_gfx_mode( GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
36
37 buffer = create_bitmap(640, 480);
38
39 while(!key[KEY_ESC])
40 {
41 if(key[KEY_SPACE])
42 {
43 for(int i = 0; i < 150; i++)
44 {
45 Particles<i>.x = 320.0;
46 Particles<i>.y = 240.0;
47 Particles<i>.speed = rand() % 10 + 5;
48 Particles<i>.angle = rand() % 360 + 1;
49 Particles<i>.angle = Particles<i>.angle * 3.14159 / 180;
50 Particles<i>.life = rand() % 10 + 10;
51 }
52 }
53 for(int i = 0; i < 150; i++)
54 {
55 if(Particles<i>.life > 0)
56 {
57 Particles<i>.x += Particles<i>.speed * cos(Particles<i>.angle);
58 Particles<i>.y += Particles<i>.speed * sin(Particles<i>.angle);
59 Particles<i>.life--;
60 }
61 }
62 Draw_Stuff();
63 rest(50);
64 }
65 destroy_bitmap(buffer);
66 allegro_exit();
67}
68END_OF_MAIN()

I get this warning whenever I compile, I know it's because I'm passing doubles to putpixel() but I don't know how to fix it.

[Warning] passing 'double' for converting 2 of 'void putpixel(BITMAP*, int, int, int)'

yohosuff
Member #10,022
July 2008
avatar

That is an awesome particle explosion! :D Also, the code is easy to understand even without the blue little helpy helpers. ;D

Neil Black
Member #7,867
October 2006
avatar

I make my code easy to understand because the more compleximacated stuff makes me think. And I'm too damn tired to think right now.

Thomas Fjellstrom
Member #476
June 2000
avatar

Neil: It runs too fast on my q6600 quad core. It seems even if you didn't program it for multi core systems, X gets its own core as well :o

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Neil Black
Member #7,867
October 2006
avatar

SILENCE!

I was too lazy to implement real timing... would it work right if I used timing instead of rest()? If I had it in a game I'd do it properly.

Thomas Fjellstrom
Member #476
June 2000
avatar

Heh, I'm just bugging you.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Tobias Dammers
Member #2,604
August 2002
avatar

Quote:

I get this warning whenever I compile, I know it's because I'm passing doubles to putpixel() but I don't know how to fix it.

[Warning] passing 'double' for converting 2 of 'void putpixel(BITMAP*, int, int, int)'

C++ uses strong static typing, which means that you need to pass arguments with the correct types.
Some implicit conversions are allowed (i.e., you can assign a value of type A to a variable of type B), but if the conversion can lead to loss of precision (e.g. double -> int), the compiler throws a warning. You need to explicitly cast to tell the compiler that you know what you're doing:

putpixel(buffer, (int)Particles<i>.x, (int)Particles<i>.y, makecol(255, 255, 255));

Note that this produces a sub-pixel inaccuracy around 0, because the default cast rounds towards 0 (negative numbers round up, positive ones round down). Objects crossing the x or y axis will experience a tiny glitch. You can use libm's floor() function to avoid this:

putpixel(buffer, (int)floor(Particles<i>.x), (int)floor(Particles<i>.y), makecol(255, 255, 255));

---
Me make music: Triofobie
---
"We need Tobias and his awesome trombone, too." - Johan Halmén

axilmar
Member #1,204
April 2001

Quote:

C++ uses strong static typing

Type system war!!!! :-)

C++ has a weak static type system, due to static_cast, reinterpret_cast and C-style casts.

imaxcs
Member #4,036
November 2003

And what if those casts didn't exist? I figure much worse... :)

 1   2 


Go to: