Skip to content

Commit

Permalink
cleaned up
Browse files Browse the repository at this point in the history
I removed the unique_ptr stuff because it makes this just to complicated for what
it's doing. This should be the final commit for this (unless I made another stupid mistake)
  • Loading branch information
Mia75owo committed Nov 27, 2023
1 parent a0c1f6b commit c820106
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 94 deletions.
145 changes: 64 additions & 81 deletions Hurrican/src/Partikelsystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
#include "Tileengine.hpp"
#include "Timer.hpp"

#include <memory>

// --------------------------------------------------------------------------------------
// Defines
// --------------------------------------------------------------------------------------
Expand Down Expand Up @@ -4216,19 +4214,6 @@ void PartikelsystemClass::SetTarget(float x, float y) {
ytarget = y;
}

// --------------------------------------------------------------------------------------
// Deletes a particle from the particle_pool
// --------------------------------------------------------------------------------------
#ifndef USE_NO_MEMPOOLING
// FIXME: this is extremely hacky
static void delete_particle(PartikelClass* p) {
PartikelSystem.DeleteParticle(p);
}
void PartikelsystemClass::DeleteParticle(PartikelClass* particle) {
particle_pool.free(particle);
}
#endif

// --------------------------------------------------------------------------------------
// Partikel "Art" hinzufügen
// --------------------------------------------------------------------------------------
Expand All @@ -4252,13 +4237,11 @@ bool PartikelsystemClass::PushPartikel(float x, float y, int Art, PlayerClass *p
return false;


#ifndef USE_NO_MEMPOOLING
// FIXME: this is extremly hacky
PartikelClass *pNew = particle_pool.alloc(); // Neuer zu erstellender Partikel
std::unique_ptr<PartikelClass, void(*)(PartikelClass*)> uNew(pNew, delete_particle);
// Allocate a new particle
#ifdef USE_NO_MEMPOOLING
PartikelClass *pNew = new PartikelClass;
#else
auto pNew = new PartikelClass; // Neuer zu erstellender Partikel
auto uNew = std::unique_ptr<PartikelClass>(pNew);
PartikelClass *pNew = particle_pool.alloc();
#endif

#ifndef NDEBUG
Expand All @@ -4270,26 +4253,45 @@ bool PartikelsystemClass::PushPartikel(float x, float y, int Art, PlayerClass *p

pNew->CreatePartikel(x, y, Art, pParent); // neuen Partikel erzeugen

particles.push_back(std::move(uNew));
particles.push_front(pNew);

return true;
}

// --------------------------------------------------------------------------------------
// Alle Partikel der Liste löschen
// Removes all dead particles
// --------------------------------------------------------------------------------------
void PartikelsystemClass::ClearAll() {
particles.clear();
void PartikelsystemClass::ClearDeadParticles() {
auto iter = particles.begin();
while (iter != particles.end()) {
PartikelClass* particle = *iter;

#ifndef NDEBUG
if (GetNumPartikel() != 0) {
Protokoll << "ERROR: poss. mem leak / mem. corruption in linked list of particles" << std::endl;
}
if (particle->Lebensdauer > 0.0f) {
++iter;
} else {
// Particle's time to die..
#ifdef USE_NO_MEMPOOLING
delete particle;
#else
particle_pool.free(particle);
#endif
iter = particles.erase(iter);
}
}
}

#ifndef USE_NO_MEMPOOLING
// --------------------------------------------------------------------------------------
// Alle Partikel der Liste löschen
// --------------------------------------------------------------------------------------
void PartikelsystemClass::ClearAll() {
#ifdef USE_NO_MEMPOOLING
for (auto& particle: particles) {
delete particle;
}
#else
particle_pool.reinit();
#endif
particles.clear();
}

// --------------------------------------------------------------------------------------
Expand All @@ -4312,8 +4314,9 @@ void PartikelsystemClass::DrawOnly() {
DirectGraphics.SetColorKeyMode();

for (auto& particle: particles) {
if (particle->PartikelArt < ADDITIV_GRENZE)
if (particle->PartikelArt < ADDITIV_GRENZE) {
particle->Render();
}
}

//----- Partikel, die mit additivem Alphablending gerendert werden, durchlaufen
Expand All @@ -4322,8 +4325,9 @@ void PartikelsystemClass::DrawOnly() {
DirectGraphics.SetAdditiveMode();

for (auto& particle: particles) {
if (particle->PartikelArt >= ADDITIV_GRENZE)
if (particle->PartikelArt >= ADDITIV_GRENZE) {
particle->Render();
}
}

DirectGraphics.SetColorKeyMode();
Expand All @@ -4345,28 +4349,21 @@ void PartikelsystemClass::DoPartikelSpecial(bool ShowThem) {
return;
}

ClearDeadParticles();

CurrentPartikelTexture = -1; // Aktuelle Textur gibt es noch keine
DrawMode = DrawModeEnum::NORMAL;

//----- Partikel, die normal oder mit Alphablending gerendert werden, durchlaufen

DirectGraphics.SetColorKeyMode();

auto iter = particles.begin();
while (iter != particles.end()) {
PartikelClass* particle = iter->get();

for (auto& particle: particles) {
if (!ShowThem && particle->PartikelArt < ADDITIV_GRENZE) {
particle->Run();
if (particle->Lebensdauer > 0.0f)
if (particle->Lebensdauer > 0.0f) {
particle->Render();
}

if(particle->Lebensdauer > 0.0f) {
++iter;
} else {
// Particle's time to die..
iter = particles.erase(iter);
}
}
}

Expand All @@ -4376,23 +4373,14 @@ void PartikelsystemClass::DoPartikelSpecial(bool ShowThem) {
CurrentPartikelTexture = -1; // Aktuelle Textur gibt es noch keine
DirectGraphics.SetAdditiveMode();

iter = particles.begin();
while (iter != particles.end()) {
PartikelClass* particle = iter->get();

for (auto& particle: particles) {
if ((ShowThem && (particle->PartikelArt == SCHNEEFLOCKE_END || particle->PartikelArt == EXPLOSION_TRACE_END)) ||
(!ShowThem && particle->PartikelArt >= ADDITIV_GRENZE)) {
particle->Run(); // Partikel animieren/bewegen

if (particle->Lebensdauer > 0.0f)
if (particle->Lebensdauer > 0.0f) {
particle->Render();
}

if(particle->Lebensdauer > 0.0f) {
++iter;
} else {
// Particle's time to die..
iter = particles.erase(iter);
}
}
}

Expand All @@ -4415,54 +4403,36 @@ void PartikelsystemClass::DoPartikel() {
return;
}

ClearDeadParticles();

CurrentPartikelTexture = -1; // Aktuelle Textur gibt es noch keine
DrawMode = DrawModeEnum::NORMAL;

//----- Partikel, die normal oder mit Alphablending gerendert werden, durchlaufen

DirectGraphics.SetColorKeyMode();

auto iter = particles.begin();
while (iter != particles.end()) {
PartikelClass* particle = iter->get();

for (auto& particle: particles) {
if (particle->PartikelArt < ADDITIV_GRENZE) {
particle->Run(); // Partikel animieren/bewegen

if (particle->Lebensdauer > 0.0f) {
particle->Render();
}
}

if (particle->Lebensdauer > 0.0f) {
++iter;
} else {
// Particle's time to die..
iter = particles.erase(iter);
}
}

CurrentPartikelTexture = -1; // Aktuelle Textur gibt es noch keine
DirectGraphics.SetAdditiveMode();

iter = particles.begin();
while (iter != particles.end()) {
PartikelClass* particle = iter->get();

for (auto& particle: particles) {
if (particle->PartikelArt >= ADDITIV_GRENZE) {
particle->Run(); // Partikel animieren/bewegen

if (particle->Lebensdauer > 0.0f) {
particle->Render();
}
}

if (particle->Lebensdauer > 0.0f) {
++iter;
} else {
// Particle's time to die..
iter = particles.erase(iter);
}
}

DirectGraphics.SetColorKeyMode();
Expand Down Expand Up @@ -4498,9 +4468,22 @@ void PartikelsystemClass::SetThunderColor(unsigned char r, unsigned char g, unsi
// damit kein so bunter bonbon scheiss entsteht, wenn mehrere extras gleichzeitig aufgepowert werden
// --------------------------------------------------------------------------------------
void PartikelsystemClass::ClearPowerUpEffects() {
particles.erase(std::remove_if(particles.begin(), particles.end(), [this](const auto& particle) {
return particle->PartikelArt == KRINGEL;
}), particles.end());
auto iter = particles.begin();
while (iter != particles.end()) {
PartikelClass* particle = *iter;

if (particle->PartikelArt != KRINGEL) {
++iter;
} else {
// Particle's time to die..
#ifdef USE_NO_MEMPOOLING
delete particle;
#else
particle_pool.free(particle);
#endif
iter = particles.erase(iter);
}
}
}

// --------------------------------------------------------------------------------------
Expand Down
15 changes: 2 additions & 13 deletions Hurrican/src/Partikelsystem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#include "DataStructures.hpp"

#include <algorithm>
#include <memory>

// --------------------------------------------------------------------------------------
// Defines
Expand Down Expand Up @@ -304,12 +303,7 @@ class PartikelsystemClass {

public:

#ifndef USE_NO_MEMPOOLING
void DeleteParticle(PartikelClass* particle);
std::list<std::unique_ptr<PartikelClass, void(*)(PartikelClass*)>> particles;
#else
std::list<std::unique_ptr<PartikelClass>> particles;
#endif
std::list<PartikelClass*> particles;

PartikelsystemClass(); // Konstruktor
~PartikelsystemClass(); // Destruktor
Expand All @@ -322,12 +316,7 @@ class PartikelsystemClass {
bool PushPartikel(float x, float y, int Art,
PlayerClass *pParent = nullptr); // Partikel "Art" hinzufügen

// DKS - Converted particle linked-list to be singly-linked so this DelNode()
// is a new function that replaces the old DelSel().
// It is now up to the caller to splice the list, this blindly deletes what is passed
// to it and returns the pointer that was in pPtr->pNext, or NULL if pPtr was NULL
// void DelSel (PartikelClass *pTemp); // Ausgewähltes Objekt entfernen

void ClearDeadParticles(); // Removes all dead particles
void ClearAll(); // Alle Objekte löschen
int GetNumPartikel() const; // Zahl der Partikel zurückliefern
void DoPartikel(); // Alle Partikel der Liste animieren/anzeigen
Expand Down

0 comments on commit c820106

Please sign in to comment.