Advertisement

Particle system problems

Started by May 27, 2010 05:40 PM
1 comment, last by waratte 14 years, 8 months ago
Hi,I've been trying to create a working particle system for the past 2 months and I don't know how to design it so that it can shoot and work on many particles at the same time. I've looked at all the websites and Google search results for making a particle system and they either are all too hard to understand or don't work for me.I really need help and can't figure out how to find a solution to my problem. Here's my code: #include <allegro.h> #include <ctime> #define MAX_PARTICLES 1000 void init(); void deinit(); int number[3] = {0,1,2}; class particle_system{ public: int x; int y; int xvel; int yvel; int color1; int color2; int color3; bool alive; float life; }dust[MAX_PARTICLES]; int main() { init(); while (!key[KEY_ESC]) { for(int iii = 0; iii < MAX_PARTICLES; iii++){ if(key[KEY_ESC])break; while(dust[iii].x < 640 && dust[iii].y < 480 ){ if(dust[iii].life <= 0.0f){dust[iii].alive = false; break; } putpixel(screen, dust[iii].x + dust[iii].xvel,dust[iii].y + dust[iii].yvel,makecol(dust[iii].color1,dust[iii].color2,dust[iii].color3)); rest(10); putpixel(screen, dust[iii].x + dust[iii].xvel,dust[iii].y + dust[iii].yvel,makecol(0,0,0)); dust[iii].x = dust[iii].x + number[rand() % 3]; dust[iii].y = dust[iii].y + number[rand() % 3]; putpixel(screen, dust[iii].x + dust[iii].xvel,dust[iii].y + dust[iii].yvel,makecol(dust[iii].color1,dust[iii].color2,dust[iii].color3)); dust[iii].life = dust[iii].life - 0.05f; } if(dust[iii].life <= 0.0f){dust[iii].alive = true; dust[iii].life = 1.0f; } if(dust[iii].x == 640 || dust[iii].y == 480){dust[iii].x = 10; dust[iii].y = 10; } } } deinit(); return 0; } END_OF_MAIN() void init() { int depth, res; allegro_init(); depth = desktop_color_depth(); if (depth == 0) depth = 32; set_color_depth(depth); res = set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0); if (res != 0) { allegro_message(allegro_error); exit(-1); } install_timer(); install_keyboard(); install_mouse(); srand(time(0)); for(int iii = 0; iii < MAX_PARTICLES; iii++){ dust[iii].x = 10; dust[iii].y = 10; dust[iii].xvel = 1; dust[iii].yvel = 1; dust[iii].color1 = 255; dust[iii].color2 = 255; dust[iii].color3 = 255; dust[iii].alive = true; dust[iii].life = 1.0f; } } void deinit() { clear_keybuf(); }
Waratte from wololo.net/talk forums!
This is the wrong forum for this question - "game design" refers to thinking about what the game will be like for the player, not how it will be programmed.

I also don't have Allegro set up here to test your code.

That aside, there are a couple of more obvious points about your code:
  • What is the "while(dust[iii].x < 640 && dust[iii].y < 480 ){" for? it seems like you move each particle all the way across the screen one at a time, instead of updating each particle once and then repeating
  • Why are you calling rest(10) for every particle? You are introducing an artificial delay which is probably why it is so slow
  • You draw each particle, wait for 10 milliseconds, draw over it in black, move it and then finally draw it at the new position... you should only need to draw each particle once, in the updated position
  • Look into "double buffering": example here. This will allow you to clear the entire screen before every loop, so you don't have to paint over each pixel in black before moving it. Also the player's screen will only be updated once every particle has been moved and drawn in its new position, so you don't have to include the "rest" just to allow you to see the particle
  • Advertisement
    Well... Thanks for helping me solve my problem and next time I'll try to post in the right forum.
    Waratte from wololo.net/talk forums!

    This topic is closed to new replies.

    Advertisement