• Advertisement
Sign in to follow this  

Allegro Particle System

This topic is 3981 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi all! i've just tried to make a pretty basic particle system as a sort of mini project but have hit into some snags. for some reason when i try to create an object of typr Particle it doesn't work. is somebody could have a quick look at my code i would be much appreciative: #include <allegro.h> #define SCREEN_WIDTH 1400 #define SCREEN_HEIGHT 1050 BITMAP* buffer; int _num = 100; class Particle { int x; int y; int xv; int yv; int life_span; int life; int red; int green; int blue; } Particle particle[_num]; void Set_particle_parameters(); void Kill_Particles(); void Draw_Particles(); int main() { allegro_init(); install_keyboard(); set_color_depth(16); set_gfx_mode(GFX_AUTODETECT, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0); buffer = create_bitmap(SCREEN_WIDTH, SCREEN_HEIGHT); for(int i = 0; i <= _num; i++) particle.life = 0; bool game_running = true; while(game_running == true) { if(key[KEY_ESC]) game_running = false; Set_particle_parameters(); Kill_Particles(); Draw_Particles(); draw_sprite(screen, buffer, 0, 0); } } END_OF_MAIN(); void Set_particle_paramters() { int j= 0; for(int i = 0; i <= _num; i++) { j++; if(j == SCREEN_WIDTH) j = 0; if(particle.life == 0) { particle.life = 1; particle.x = j; particle.y = SCREEN_HEIGHT; particle.xv = 1; particle.yv = rand() % 2 + 1; particle.life_span = rand() % SCREEN_HEIGHT + 1; particle.red = rand() % 254 + 1; particle.green = rand() % 254 + 1; particle.blue = rand() % 254 + 1; } particle.y += particle.yv; } } void Kill_Particles() { for(int i = 0; i <= _num; i++) { if(particle.y == particle.life_span) particle.life = 0; } } void Draw_Particles() { for(int i = 0; i <= _num; i++) { putpixel(buffer, particle.x, particle.y, makecol(particle.red, particle.green, particle.blue)); } }

Share this post


Link to post
Share on other sites
Advertisement
First off, this is an error

for(int i = 0; i <= _num; i++)

Because you initialized your particle array to have _num elements, but the array starts at 0 the final element of your array is _num - 1. It should be

for(int i = 0; i < _num: i++)

Strictly less-than ( < ), not less-than-or-equal-to ( <= )

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement