I’m having a problem with creating my own Particle Engine in SDL2. I can’t figure out why it’s really laggy, and even using a debugger like gdb
does not help at all. I also tried to double check my particle and particle emitter code, but I can’t find what went wrong. I tried to follow Lazy Foo’s Particle Engine tutorial and made my own implementation. This is made completely using SDL2 and has absolutely no OpenGL code, by the way. Here’s my classes (divided into header and C++ files):
Particle.hpp:
#pragma once
#include "Sprite.hpp"
namespace flux
{
class Particle
{
public:
Particle();
virtual ~Particle();
Particle(const char *spritePath, SDL_Renderer *renderer, float x, float y, float lifetime);
void RenderParticle();
bool IsDestroyed();
private:
float xPos, yPos;
float lifetime;
flux::Sprite *particleSprite;
};
} // namespace flux
Particle.cpp:
#include "Fluxior/Graphics/Particle.hpp"
namespace flux
{
Particle::Particle() : particleSprite(nullptr) {}
Particle::~Particle()
{
delete particleSprite;
particleSprite = nullptr;
}
Particle::Particle(const char *spritePath, SDL_Renderer *renderer, float x, float y, float lifetime)
{
xPos = x - 5.0f + (rand() % 25);
yPos = y - 5.0f + (rand() % 25);
this->lifetime = lifetime;
particleSprite = new flux::Sprite(spritePath, renderer);
}
void Particle::RenderParticle()
{
particleSprite->SetPosAndSize(xPos, yPos, particleSprite->GetFrameWidth(), particleSprite->GetFrameHeight());
particleSprite->RenderSprite();
lifetime -= 1.0f;
}
bool Particle::IsDestroyed()
{
return lifetime <= 0.0f;
}
}
ParticleEmitter.hpp:
#include "Particle.hpp"
namespace flux
{
class ParticleEmitter
{
public:
ParticleEmitter(float x, float y, int numParticles, float particleLifetime);
virtual ~ParticleEmitter();
void EmitParticles(const char *spritePath, SDL_Renderer *renderer);
private:
float xPos, yPos;
int maxParticles;
float particleLifetime;
Particle **particles;
};
} // namespace flux
ParticleEmitter.cpp:
#include "Fluxior/Graphics/ParticleEmitter.hpp"
namespace flux
{
ParticleEmitter::ParticleEmitter(float x, float y, int numParticles, float particleLifetime)
: xPos(x), yPos(y), maxParticles(numParticles), particleLifetime(particleLifetime)
{
particles = new Particle *[maxParticles];
}
ParticleEmitter::~ParticleEmitter()
{
for (int i = 0; i < maxParticles; ++i)
{
delete particles[i];
}
delete[] particles;
}
void ParticleEmitter::EmitParticles(const char *spritePath, SDL_Renderer *renderer)
{
for (int i = 0; i < maxParticles; ++i)
{
particles[i] = new Particle(spritePath, renderer, xPos, yPos, particleLifetime);
}
for (int i = 0; i < maxParticles; ++i)
{
if (particles[i]->IsDestroyed())
{
delete particles[i];
particles[i] = new Particle(spritePath, renderer, xPos, yPos, particleLifetime);
}
}
for (int i = 0; i < maxParticles; ++i)
{
particles[i]->RenderParticle();
}
}
} // namespace flux
I know some might tell me that the fault might be on my Sprite class, but since I’ve tested it for creating and rendering sprites, and even sprite sheet animation, it definitely does not have to do anything with it. Surely it’s with my particle code (Still a beginner in this so sorry if all I needed to do was something simple). Thanks in advance to anyone who tries to help.