Sign in to follow this  
pi_man

Allegro Particle System

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[i].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[i].life == 0) { particle[i].life = 1; particle[i].x = j; particle[i].y = SCREEN_HEIGHT; particle[i].xv = 1; particle[i].yv = rand() % 2 + 1; particle[i].life_span = rand() % SCREEN_HEIGHT + 1; particle[i].red = rand() % 254 + 1; particle[i].green = rand() % 254 + 1; particle[i].blue = rand() % 254 + 1; } particle[i].y += particle[i].yv; } } void Kill_Particles() { for(int i = 0; i <= _num; i++) { if(particle[i].y == particle[i].life_span) particle[i].life = 0; } } void Draw_Particles() { for(int i = 0; i <= _num; i++) { putpixel(buffer, particle[i].x, particle[i].y, makecol(particle[i].red, particle[i].green, particle[i].blue)); } }

Share this post


Link to post
Share on other sites
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this