Advertisement Jump to content
Sign in to follow this  

Particle System?

This topic is 4745 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) {

        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_MapRGB(ThisSurface.pSurface->format, r, g, b));

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

        m_iCalls += 1;

        if (m_iCalls > 4)
            m_iCalls = 1;

    Particle::~Particle() {

    void Particle::Handle(Event &rEvent) {

        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;


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

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


    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) {


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



#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 {
            int m_Life;

            static int m_iCalls;

            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 {
            struct SParticle {
                Particle *pParticle;

                bool External;

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


            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);


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
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, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!