Jump to content
  • Advertisement
Sign in to follow this  
nullsquared

Particle System?

This topic is 4590 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 everyone! I finally finished my particle system in SDL, however, I'm confused about it. What I'm confused about is, am I doing it right? I have a class called particle, and a particle 'system' which manages all particles. Any suggestions or comments or anything else? This is the first time I made something like this, so it's like going somewhere without a map. Code is as follows (all other functions are self-explainable):
// CPP
#include "CApp_Particle.h"
#include "CApp_Engine.h"
#include "CApp_Common.h"

#include <SDL/SDL.h>

#include <string>
using std::string;

#include <iostream>
using std::cout;

namespace CApp {
    int Particle::m_iCalls = 1;

    Particle::Particle(int x, int y, int Life, int r, int g, int b) {
        SeedRand();

        if (r < 0)
            r = Random(256, -1);
        if (g < 0)
            g = Random(256, -1);
        if (b < 0)
            b = Random(256, -1);

        if (Life < 1)
            Life = Random(101, 49);

        this->Pos.x = x;
        this->Pos.y = y;

        m_Life = Life;

        this->Vel.x = Random(11, -11);
        this->Vel.y = Random(11, -11);

        Sprite::CSurface ThisSurface;

        ThisSurface.Load("Data/Pix" + Cast<string>(m_iCalls) + ".PNG");
        //ThisSurface.pSurface = SDL_CreateRGBSurface(g_pEngine->GetScreen().pSurface->flags,
                                                    //m_Size, m_Size, g_pEngine->GetBpp(),
                                                    //RMASK, GMASK, BMASK, AMASK);

        SDL_FillRect(ThisSurface.pSurface,
                     NULL,
                     SDL_MapRGB(ThisSurface.pSurface->format, r, g, b));

        this->AddAnim(ThisSurface, "Particle");

        m_iCalls += 1;

        if (m_iCalls > 4)
            m_iCalls = 1;
    }

    Particle::~Particle() {
        SDL_FreeSurface(this->GetSprite("Particle").pSurface);
    }

    void Particle::Handle(Event &rEvent) {
        m_Life--;

        if (m_Life < 0)
            this->Stats.Active = false;

        this->Pos.x += this->Vel.x;
        this->Pos.y += this->Vel.y;
    }

    ParticleSystem::ParticleSystem() {

    }

    ParticleSystem::~ParticleSystem() {
        for (std::list<SParticle>::iterator i = m_Particles.begin(); i != m_Particles.end(); i++)
            if ((*i).External == false)
                delete (*i).pParticle;
    }

    void ParticleSystem::AddNewParticle(int x, int y, int Life) {
        SParticle ToPush;

        ToPush.pParticle = new Particle(x, y, Life);
        ToPush.External = false;

        m_Particles.push_back(ToPush);
    }

    void ParticleSystem::AddNewParticle(Particle *pParticle) {
        SParticle ToPush;

        ToPush.pParticle = pParticle;
        ToPush.External = true;

        m_Particles.push_back(ToPush);
    }

    void ParticleSystem::AddNewParticles(int x, int y, int Num) {
        for (int i = 0; i < Num; i++)
            this->AddNewParticle(x, y);
    }

    void ParticleSystem::Run(Event &rEvent) {
        for (std::list<SParticle>::iterator i = m_Particles.begin(); i != m_Particles.end(); i++) {
            if (!(*i).pParticle->Stats.Active) {
                m_Removals.push_back(i);
                continue;
            }

            (*i).pParticle->Handle(rEvent);
            (*i).pParticle->Draw(g_pEngine->GetScreen());
        }

        for (int i = 0; i < m_Removals.size(); i++) {
            if ((*m_Removals).External == false)
                delete (*(m_Removals)).pParticle;

            m_Particles.erase(m_Removals);
        }
        m_Removals.clear();
    }
};

//H
#ifndef CAPP_PARTICLE_H_
#define CAPP_PARTICLE_H_

#include "CApp_Entity.h"
#include "CApp_Event.h"
#include "CApp_Engine.h"
#include "CApp_Common.h"
#include "CApp_Sprite.h"

#include <vector>
#include <list>

#include <SDL/SDL.h>

namespace CApp {
    enum {
        PARTICLE = 1234
    };

    class Particle: public Entity {
        private:
            int m_Life;

        protected:
            static int m_iCalls;

        public:
            Particle(int x, int y, int Life = -1, int r = -1, int g = -1, int b = -1);
            virtual ~Particle();

            virtual void Handle(Event &rEvent);
            virtual void OnCollision(Entity *pWhat) {

            }
    };

    class ParticleSystem {
        private:
            struct SParticle {
                Particle *pParticle;

                bool External;
            };

        private:
            std::vector<std::list<SParticle>::iterator> m_Removals;
            std::list<SParticle> m_Particles;

        public:
            ParticleSystem();
            ~ParticleSystem();

            void Run(Event &rEvent);

            void AddNewParticle(int x, int y, int Life = -1);
            void AddNewParticle(Particle *pParticle);
            void AddNewParticles(int x, int y, int Num);
    };
};

#endif

Since every particle is an entity, I have an add particle function to add from a pointer. This is because a class might inherit from particle and become a better particle, like making the particle animated, giving it attack and defense, and so on... Thanks!

Share this post


Link to post
Share on other sites
Advertisement
A little while ago, I wrote a particle system targeted towards 2D environments named SdlDotNet.Particles. Although it was writen in C# and writen for SDL.NET, you could easily take the concept of it.

Basically you have particles, manipulators and emitters. Particles are simply the partilces that fly around the screen, manipulators are forces that act apon the particles and emitters shoot out particles controled with random properties. You can view the source or see an example of its use.

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!