From 096e0d0a7199ec9a4059fd5ef8a06a3cf8fcae83 Mon Sep 17 00:00:00 2001 From: jaroWMR Date: Mon, 7 Oct 2024 16:02:09 +0200 Subject: created a particel system --- src/crepe/ParticleSystem.cpp | 62 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 src/crepe/ParticleSystem.cpp (limited to 'src/crepe/ParticleSystem.cpp') diff --git a/src/crepe/ParticleSystem.cpp b/src/crepe/ParticleSystem.cpp new file mode 100644 index 0000000..367f56c --- /dev/null +++ b/src/crepe/ParticleSystem.cpp @@ -0,0 +1,62 @@ +#include "ParticleSystem.hpp" +#include +// #include +#include +#include // include iostream for std::cout +#include "ParticleEmitter.hpp" +#include "Particle.hpp" + +ParticleSystem::ParticleSystem() : m_elapsedTime(0.0f) {} // Initialize m_elapsedTime to 0 + +void ParticleSystem::update(float deltaTime, std::vector& emitters) { + for (ParticleEmitter& emitter : emitters) { + m_elapsedTime = deltaTime; + + //spawn new particles if enough time passed and the max is not achieved + while (m_elapsedTime >= (1.0f / emitter.m_emissionRate) && emitter.particles.size() < emitter.m_maxParticles) { + m_elapsedTime -= (1.0f / emitter.m_emissionRate); + emitParticle(emitter); + } + //update/move particles afterwards delete if not alive. + std::vector::iterator it = emitter.particles.begin(); + while (it != emitter.particles.end()) { + it->update(deltaTime); + if (!it->isAlive()) { + it = emitter.particles.erase(it); + } else { + ++it; + } + } + + } +} + +void ParticleSystem::emitParticle(ParticleEmitter& emitter) { + std::cout << "new emitter:" << std::endl; + Particle::Position initialPosition = { emitter.m_position.x, emitter.m_position.y }; + float randomAngle = 0.0f; + //check if value is overthe 360 degrees + if(emitter.m_maxAngle < emitter.m_minAngle) + { + randomAngle = ((emitter.m_minAngle + (std::rand() % (static_cast(emitter.m_maxAngle + 360 - emitter.m_minAngle + 1))))%360); + } + else + { + randomAngle = emitter.m_minAngle + (std::rand() % (static_cast(emitter.m_maxAngle - emitter.m_minAngle + 1))); + } + + std::cout << "randomAngle:" << randomAngle << std::endl; + float angleInRadians = randomAngle * (M_PI / 180.0f); + float randomSpeedOffset = (static_cast(std::rand()) / RAND_MAX) * (2 * emitter.m_speedOffset) - emitter.m_speedOffset; + float velocityX = (emitter.m_speed + randomSpeedOffset) * std::cos(angleInRadians); + float velocityY = (emitter.m_speed + randomSpeedOffset) * std::sin(angleInRadians); + std::cout << "velocityX:" << velocityX << std::endl; + std::cout << "velocityY:" << velocityY << std::endl; + Particle::Position initialVelocity = { + velocityX, + velocityY + }; + std::cout << "initialVelocityX:" << initialVelocity.x << std::endl; + std::cout << "initialVelocityY:" << initialVelocity.y << std::endl; + emitter.particles.emplace_back(2.0f, initialPosition, initialVelocity); +} -- cgit v1.2.3 From 5cf5cf64d6f9597849ed5558da4f1bc201165fed Mon Sep 17 00:00:00 2001 From: jaroWMR Date: Mon, 7 Oct 2024 16:39:26 +0200 Subject: updated system --- src/crepe/Particle.cpp | 3 ++- src/crepe/ParticleEmitter.cpp | 4 ++-- src/crepe/ParticleEmitter.hpp | 4 +++- src/crepe/ParticleSystem.cpp | 21 +++++++++------------ src/crepe/main.cpp | 37 ++++++++++++++++++++++++++----------- 5 files changed, 42 insertions(+), 27 deletions(-) (limited to 'src/crepe/ParticleSystem.cpp') diff --git a/src/crepe/Particle.cpp b/src/crepe/Particle.cpp index f6bbd34..3254b5b 100644 --- a/src/crepe/Particle.cpp +++ b/src/crepe/Particle.cpp @@ -1,5 +1,5 @@ #include "Particle.hpp" - +#include Particle::Particle(float lifespan, Position position, Position velocity) : lifespan(lifespan), position(position), velocity(velocity), timeInLife(0.0f) {} @@ -11,5 +11,6 @@ void Particle::update(float deltaTime) { } bool Particle::isAlive() const { + std::cout << "lifespan" << lifespan << std::endl; return timeInLife < lifespan; } diff --git a/src/crepe/ParticleEmitter.cpp b/src/crepe/ParticleEmitter.cpp index 69f5ace..f5c18db 100644 --- a/src/crepe/ParticleEmitter.cpp +++ b/src/crepe/ParticleEmitter.cpp @@ -1,8 +1,8 @@ #include "ParticleEmitter.hpp" #include -ParticleEmitter::ParticleEmitter(unsigned int maxParticles, unsigned int emissionRate, unsigned int speed, unsigned int speedOffset, unsigned int angle, unsigned int angleOffset) - : m_maxParticles(maxParticles), m_emissionRate(emissionRate), m_speed(speed), m_speedOffset(speedOffset), m_position{0, 0} { +ParticleEmitter::ParticleEmitter(unsigned int maxParticles, unsigned int emissionRate, unsigned int speed, unsigned int speedOffset, unsigned int angle, unsigned int angleOffset, float m_beginLifespan, float m_endLifespan) + : m_maxParticles(maxParticles), m_emissionRate(emissionRate), m_speed(speed), m_speedOffset(speedOffset), m_position{0, 0}, m_beginLifespan(m_beginLifespan),m_endLifespan(m_endLifespan) { std::srand(static_cast(std::time(nullptr))); // initialize random seed m_minAngle = (360 + angle - (angleOffset % 360)) % 360; // calculate minAngle diff --git a/src/crepe/ParticleEmitter.hpp b/src/crepe/ParticleEmitter.hpp index 6826531..f647c27 100644 --- a/src/crepe/ParticleEmitter.hpp +++ b/src/crepe/ParticleEmitter.hpp @@ -5,7 +5,7 @@ class ParticleEmitter { public: - ParticleEmitter(unsigned int maxParticles, unsigned int emissionRate, unsigned int speed, unsigned int speedOffset, unsigned int angle, unsigned int angleOffset); + ParticleEmitter(unsigned int maxParticles, unsigned int emissionRate, unsigned int speed, unsigned int speedOffset, unsigned int angle, unsigned int angleOffset,float m_beginLifespan,float m_endLifespan); struct Position { //struct to hold position float x; float y; @@ -18,6 +18,8 @@ public: unsigned int m_speedOffset; //offset for random speed variation unsigned int m_minAngle; //min angle of particle emission unsigned int m_maxAngle; //max angle of particle emission + float m_beginLifespan; //begin Lifespan of particle (only visual) + float m_endLifespan; //begin Lifespan of particle std::vector particles; //collection of particles }; diff --git a/src/crepe/ParticleSystem.cpp b/src/crepe/ParticleSystem.cpp index 367f56c..f3cb939 100644 --- a/src/crepe/ParticleSystem.cpp +++ b/src/crepe/ParticleSystem.cpp @@ -10,13 +10,12 @@ ParticleSystem::ParticleSystem() : m_elapsedTime(0.0f) {} // Initialize m_elaps void ParticleSystem::update(float deltaTime, std::vector& emitters) { for (ParticleEmitter& emitter : emitters) { - m_elapsedTime = deltaTime; - - //spawn new particles if enough time passed and the max is not achieved - while (m_elapsedTime >= (1.0f / emitter.m_emissionRate) && emitter.particles.size() < emitter.m_maxParticles) { - m_elapsedTime -= (1.0f / emitter.m_emissionRate); + float updateAmount = 1/static_cast(emitter.m_emissionRate); + for (float i = 0; i < deltaTime; i += updateAmount) + { emitParticle(emitter); } + //update/move particles afterwards delete if not alive. std::vector::iterator it = emitter.particles.begin(); while (it != emitter.particles.end()) { @@ -32,7 +31,7 @@ void ParticleSystem::update(float deltaTime, std::vector& emitt } void ParticleSystem::emitParticle(ParticleEmitter& emitter) { - std::cout << "new emitter:" << std::endl; + // std::cout << "new emitter:" << std::endl; Particle::Position initialPosition = { emitter.m_position.x, emitter.m_position.y }; float randomAngle = 0.0f; //check if value is overthe 360 degrees @@ -45,18 +44,16 @@ void ParticleSystem::emitParticle(ParticleEmitter& emitter) { randomAngle = emitter.m_minAngle + (std::rand() % (static_cast(emitter.m_maxAngle - emitter.m_minAngle + 1))); } - std::cout << "randomAngle:" << randomAngle << std::endl; + float angleInRadians = randomAngle * (M_PI / 180.0f); float randomSpeedOffset = (static_cast(std::rand()) / RAND_MAX) * (2 * emitter.m_speedOffset) - emitter.m_speedOffset; float velocityX = (emitter.m_speed + randomSpeedOffset) * std::cos(angleInRadians); float velocityY = (emitter.m_speed + randomSpeedOffset) * std::sin(angleInRadians); - std::cout << "velocityX:" << velocityX << std::endl; - std::cout << "velocityY:" << velocityY << std::endl; Particle::Position initialVelocity = { velocityX, velocityY }; - std::cout << "initialVelocityX:" << initialVelocity.x << std::endl; - std::cout << "initialVelocityY:" << initialVelocity.y << std::endl; - emitter.particles.emplace_back(2.0f, initialPosition, initialVelocity); + // std::cout << "emitter.m_endLifespan:" << emitter.m_endLifespan << std::endl; + + emitter.particles.emplace_back(emitter.m_endLifespan, initialPosition, initialVelocity); } diff --git a/src/crepe/main.cpp b/src/crepe/main.cpp index 40eb04f..314dbed 100644 --- a/src/crepe/main.cpp +++ b/src/crepe/main.cpp @@ -5,6 +5,7 @@ #include "ParticleEmitter.hpp" #include "ParticleSystem.hpp" #include "Particle.hpp" +#include const int WINDOW_WIDTH = 800; const int WINDOW_HEIGHT = 600; @@ -19,29 +20,43 @@ int main(int argc, char* argv[]) { ParticleSystem particleSystem; - unsigned int maxParticles = 100; // maximum number of particles - unsigned int emissionRate = 10; // particles created per second + unsigned int maxParticles = 10; // maximum number of particles + unsigned int emissionRate = 1; // particles created per second unsigned int speed = 50; // base speed of particles unsigned int speedOffset = 10; // random offset for particle speed unsigned int angle = 90; // base angle of particle emission unsigned int angleOffset = 30; // random offset for particle angle - - ParticleEmitter emitter1(maxParticles, emissionRate, speed, speedOffset, angle, angleOffset); - emitter1.m_position = {200, 200}; // set the position of the first emitter - - ParticleEmitter emitter2(maxParticles, emissionRate, speed, speedOffset, angle - 90, angleOffset); // Another emitter - emitter2.m_position = {200, 150}; // set the position of the second emitter - - std::vector emitters = { emitter2 }; // array of emitters - + float beginLifespan = 0.0f; // beginning lifespan of particles + float endLifespan = 2.0f; // ending lifespan of particles + + // Vector to hold all the emitters + std::vector emitters; + + // Loop to create 1000 emitters + for (unsigned int i = 0; i < 100; ++i) { + ParticleEmitter emitter(maxParticles, emissionRate, speed, speedOffset, angle, angleOffset, beginLifespan, endLifespan); + + // Set a position for each emitter, modifying the position for demonstration + emitter.m_position = {static_cast(200 + (i % 100)), static_cast(200 + (i / 100) * 10)}; // Adjust position for each emitter + + emitters.push_back(emitter); // Add the emitter to the vector + } float deltaTime = 0.1f; bool running = true; while (running) { app.handleEvents(running); + // Start timing + auto start = std::chrono::high_resolution_clock::now(); + particleSystem.update(deltaTime, emitters); // update particle system with delta time and emitters + // End timing + auto end = std::chrono::high_resolution_clock::now(); + std::chrono::duration duration = end - start; // get duration in milliseconds + + std::cout << "Update took " << duration.count() << " ms" << std::endl; app.clearScreen(); // render particles using the drawSquare method from SDLApp -- cgit v1.2.3 From 5f84969c851530ebc430be2cf8e99c945ff7a4a7 Mon Sep 17 00:00:00 2001 From: jaroWMR Date: Mon, 7 Oct 2024 18:07:17 +0200 Subject: improved particle system from 90ms to ~ 0.7ms --- src/crepe/Particle.cpp | 19 +++++++++++++------ src/crepe/Particle.hpp | 12 +++++++----- src/crepe/ParticleEmitter.cpp | 17 +++++++++++++++-- src/crepe/ParticleEmitter.hpp | 8 ++++---- src/crepe/ParticleSystem.cpp | 42 ++++++++++++++++++++++++++++++------------ src/crepe/SDLApp.cpp | 9 +++++++++ src/crepe/SDLApp.hpp | 5 ++++- src/crepe/main.cpp | 15 ++++++++++----- 8 files changed, 92 insertions(+), 35 deletions(-) (limited to 'src/crepe/ParticleSystem.cpp') diff --git a/src/crepe/Particle.cpp b/src/crepe/Particle.cpp index 3254b5b..8cf7f7e 100644 --- a/src/crepe/Particle.cpp +++ b/src/crepe/Particle.cpp @@ -1,16 +1,23 @@ #include "Particle.hpp" #include -Particle::Particle(float lifespan, Position position, Position velocity) - : lifespan(lifespan), position(position), velocity(velocity), timeInLife(0.0f) {} +Particle::Particle() +{ + this->active = false; +} + +void Particle::reset(float lifespan, Position position, Position velocity) { + this->timeInLife = 0; + this->lifespan = lifespan; + this->position = position; + this->velocity = velocity; + this->active = true; +} void Particle::update(float deltaTime) { timeInLife += deltaTime; position.x += velocity.x * deltaTime; position.y += velocity.y * deltaTime; + if(timeInLife >= lifespan)this->active = false; } -bool Particle::isAlive() const { - std::cout << "lifespan" << lifespan << std::endl; - return timeInLife < lifespan; -} diff --git a/src/crepe/Particle.hpp b/src/crepe/Particle.hpp index f71fd67..669a8ab 100644 --- a/src/crepe/Particle.hpp +++ b/src/crepe/Particle.hpp @@ -1,18 +1,20 @@ #pragma once -class Particle { -public: - struct Position { +struct Position { float x; float y; }; +class Particle { +public: + Position position; Position velocity; float lifespan; + bool active; - Particle(float lifespan, Position position, Position velocity); + Particle(); + void reset(float lifespan, Position position, Position velocity); void update(float deltaTime); - bool isAlive() const; float timeInLife; }; diff --git a/src/crepe/ParticleEmitter.cpp b/src/crepe/ParticleEmitter.cpp index f5c18db..3c9cc4e 100644 --- a/src/crepe/ParticleEmitter.cpp +++ b/src/crepe/ParticleEmitter.cpp @@ -1,11 +1,24 @@ #include "ParticleEmitter.hpp" #include +#include "Particle.hpp" +#include ParticleEmitter::ParticleEmitter(unsigned int maxParticles, unsigned int emissionRate, unsigned int speed, unsigned int speedOffset, unsigned int angle, unsigned int angleOffset, float m_beginLifespan, float m_endLifespan) : m_maxParticles(maxParticles), m_emissionRate(emissionRate), m_speed(speed), m_speedOffset(speedOffset), m_position{0, 0}, m_beginLifespan(m_beginLifespan),m_endLifespan(m_endLifespan) { std::srand(static_cast(std::time(nullptr))); // initialize random seed - + std::cout << "Create emitter" << std::endl; m_minAngle = (360 + angle - (angleOffset % 360)) % 360; // calculate minAngle m_maxAngle = (360 + angle + (angleOffset % 360)) % 360; // calculate maxAngle + for (size_t i = 0; i < m_maxParticles; i++) + { + this->particles.emplace_back(); + } + +} -} \ No newline at end of file +ParticleEmitter::~ParticleEmitter() { + std::vector::iterator it = this->particles.begin(); + while (it != this->particles.end()) { + it = this->particles.erase(it); + } +} diff --git a/src/crepe/ParticleEmitter.hpp b/src/crepe/ParticleEmitter.hpp index f647c27..5de5e1e 100644 --- a/src/crepe/ParticleEmitter.hpp +++ b/src/crepe/ParticleEmitter.hpp @@ -3,13 +3,12 @@ #include #include "Particle.hpp" + + class ParticleEmitter { public: ParticleEmitter(unsigned int maxParticles, unsigned int emissionRate, unsigned int speed, unsigned int speedOffset, unsigned int angle, unsigned int angleOffset,float m_beginLifespan,float m_endLifespan); - struct Position { //struct to hold position - float x; - float y; - }; + ~ParticleEmitter(); Position m_position; //position of the emitter unsigned int m_maxParticles; //maximum number of particles @@ -22,4 +21,5 @@ public: float m_endLifespan; //begin Lifespan of particle std::vector particles; //collection of particles + }; diff --git a/src/crepe/ParticleSystem.cpp b/src/crepe/ParticleSystem.cpp index f3cb939..aff7a30 100644 --- a/src/crepe/ParticleSystem.cpp +++ b/src/crepe/ParticleSystem.cpp @@ -9,30 +9,32 @@ ParticleSystem::ParticleSystem() : m_elapsedTime(0.0f) {} // Initialize m_elapsedTime to 0 void ParticleSystem::update(float deltaTime, std::vector& emitters) { + // std::cout << "ParticleSystem update" << std::endl; for (ParticleEmitter& emitter : emitters) { float updateAmount = 1/static_cast(emitter.m_emissionRate); for (float i = 0; i < deltaTime; i += updateAmount) { emitParticle(emitter); } + // std::cout << "after emit" << std::endl; //update/move particles afterwards delete if not alive. - std::vector::iterator it = emitter.particles.begin(); - while (it != emitter.particles.end()) { - it->update(deltaTime); - if (!it->isAlive()) { - it = emitter.particles.erase(it); - } else { - ++it; + for (size_t j = 0; j < emitter.particles.size(); j++) + { + // std::cout << "update" << std::endl; + if(emitter.particles[j].active) + { + emitter.particles[j].update(deltaTime); } - } + } + } } void ParticleSystem::emitParticle(ParticleEmitter& emitter) { // std::cout << "new emitter:" << std::endl; - Particle::Position initialPosition = { emitter.m_position.x, emitter.m_position.y }; + Position initialPosition = { emitter.m_position.x, emitter.m_position.y }; float randomAngle = 0.0f; //check if value is overthe 360 degrees if(emitter.m_maxAngle < emitter.m_minAngle) @@ -49,11 +51,27 @@ void ParticleSystem::emitParticle(ParticleEmitter& emitter) { float randomSpeedOffset = (static_cast(std::rand()) / RAND_MAX) * (2 * emitter.m_speedOffset) - emitter.m_speedOffset; float velocityX = (emitter.m_speed + randomSpeedOffset) * std::cos(angleInRadians); float velocityY = (emitter.m_speed + randomSpeedOffset) * std::sin(angleInRadians); - Particle::Position initialVelocity = { + Position initialVelocity = { velocityX, velocityY }; // std::cout << "emitter.m_endLifespan:" << emitter.m_endLifespan << std::endl; - - emitter.particles.emplace_back(emitter.m_endLifespan, initialPosition, initialVelocity); + for (size_t i = 0; i < emitter.particles.size(); i++) + { + if(!emitter.particles[i].active) + { + // std::cout << "active " << emitter.particles[i].active << std::endl; + // std::cout << "lifespan " << emitter.particles[i].lifespan << std::endl; + // std::cout << "timeInLife " << emitter.particles[i].timeInLife << std::endl; + // std::cout << "emitter.m_endLifespan" << emitter.m_endLifespan << std::endl; + // std::cout << "initialPositionx" << initialPosition.x << std::endl; + // std::cout << "initialPositiony" << initialPosition.y << std::endl; + // std::cout << "initialVelocityx" << initialVelocity.x << std::endl; + // std::cout << "initialVelocityy" << initialVelocity.y << std::endl; + emitter.particles[i].reset(emitter.m_endLifespan, initialPosition, initialVelocity); + break; + } + } + + //emitter.particles.emplace_back(emitter.m_endLifespan, initialPosition, initialVelocity); } diff --git a/src/crepe/SDLApp.cpp b/src/crepe/SDLApp.cpp index a5e3621..0779af1 100644 --- a/src/crepe/SDLApp.cpp +++ b/src/crepe/SDLApp.cpp @@ -1,5 +1,8 @@ #include "SDLApp.hpp" #include +#include +#include "Particle.hpp" +#include "ParticleEmitter.hpp" SDLApp::SDLApp(int windowWidth, int windowHeight) : windowWidth(windowWidth), windowHeight(windowHeight), window(nullptr), renderer(nullptr) {} @@ -58,6 +61,12 @@ void SDLApp::drawSquare(int x, int y, int size) { SDL_RenderFillRect(renderer, &rect); } +SDL_Texture* squareTexture = nullptr; // Load this with an image or create it + + + + + void SDLApp::cleanUp() { if (renderer) { SDL_DestroyRenderer(renderer); diff --git a/src/crepe/SDLApp.hpp b/src/crepe/SDLApp.hpp index 92a4e27..f95d4bc 100644 --- a/src/crepe/SDLApp.hpp +++ b/src/crepe/SDLApp.hpp @@ -2,6 +2,8 @@ #define SDLAPP_HPP #include +#include "Particle.hpp" +#include "ParticleEmitter.hpp" class SDLApp { public: @@ -14,7 +16,8 @@ public: void presentScreen(); void drawSquare(int x, int y, int size); void cleanUp(); - + void drawParticles(const std::vector& emitters); + void drawMultipleSquares(const std::vector& squares); private: int windowWidth; int windowHeight; diff --git a/src/crepe/main.cpp b/src/crepe/main.cpp index 314dbed..58480a9 100644 --- a/src/crepe/main.cpp +++ b/src/crepe/main.cpp @@ -20,7 +20,7 @@ int main(int argc, char* argv[]) { ParticleSystem particleSystem; - unsigned int maxParticles = 10; // maximum number of particles + unsigned int maxParticles = 100; // maximum number of particles unsigned int emissionRate = 1; // particles created per second unsigned int speed = 50; // base speed of particles unsigned int speedOffset = 10; // random offset for particle speed @@ -33,7 +33,7 @@ int main(int argc, char* argv[]) { std::vector emitters; // Loop to create 1000 emitters - for (unsigned int i = 0; i < 100; ++i) { + for (unsigned int i = 0; i < 1000; ++i) { ParticleEmitter emitter(maxParticles, emissionRate, speed, speedOffset, angle, angleOffset, beginLifespan, endLifespan); // Set a position for each emitter, modifying the position for demonstration @@ -43,7 +43,7 @@ int main(int argc, char* argv[]) { } float deltaTime = 0.1f; bool running = true; - + std::cout << "start loop " << std::endl; while (running) { app.handleEvents(running); @@ -59,15 +59,20 @@ int main(int argc, char* argv[]) { std::cout << "Update took " << duration.count() << " ms" << std::endl; app.clearScreen(); + start = std::chrono::high_resolution_clock::now(); // render particles using the drawSquare method from SDLApp for (const ParticleEmitter& emitter : emitters) { for (const Particle& particle : emitter.particles) { - - app.drawSquare(particle.position.x, particle.position.y, 5); // draw each particle + if(particle.active)app.drawSquare(particle.position.x, particle.position.y, 5); // draw each particle } } + app.presentScreen(); + end = std::chrono::high_resolution_clock::now(); + duration = end - start; // get duration in milliseconds + + std::cout << "screen took " << duration.count() << " ms" << std::endl; std::this_thread::sleep_for(std::chrono::milliseconds(20)); // simulate ~50 FPS } -- cgit v1.2.3 From d001c4ba95a72f13c942f1a24eb98fe1584d93a4 Mon Sep 17 00:00:00 2001 From: jaroWMR Date: Thu, 10 Oct 2024 08:38:40 +0200 Subject: Renamed files and Emitter is a component --- src/crepe/CMakeLists.txt | 8 ++++---- src/crepe/Particle.cpp | 4 +++- src/crepe/Particle.h | 24 ++++++++++++++++++++++++ src/crepe/Particle.hpp | 20 -------------------- src/crepe/ParticleEmitter.cpp | 10 ++++++---- src/crepe/ParticleEmitter.h | 32 ++++++++++++++++++++++++++++++++ src/crepe/ParticleEmitter.hpp | 25 ------------------------- src/crepe/ParticleSystem.cpp | 12 +++++++----- src/crepe/ParticleSystem.h | 19 +++++++++++++++++++ src/crepe/ParticleSystem.hpp | 14 -------------- src/crepe/SDLApp.cpp | 6 +++--- src/crepe/SDLApp.h | 28 ++++++++++++++++++++++++++++ src/crepe/SDLApp.hpp | 28 ---------------------------- src/example/particel.cpp | 10 ++++++---- 14 files changed, 132 insertions(+), 108 deletions(-) create mode 100644 src/crepe/Particle.h delete mode 100644 src/crepe/Particle.hpp create mode 100644 src/crepe/ParticleEmitter.h delete mode 100644 src/crepe/ParticleEmitter.hpp create mode 100644 src/crepe/ParticleSystem.h delete mode 100644 src/crepe/ParticleSystem.hpp create mode 100644 src/crepe/SDLApp.h delete mode 100644 src/crepe/SDLApp.hpp (limited to 'src/crepe/ParticleSystem.cpp') diff --git a/src/crepe/CMakeLists.txt b/src/crepe/CMakeLists.txt index 9c65e1e..399200b 100644 --- a/src/crepe/CMakeLists.txt +++ b/src/crepe/CMakeLists.txt @@ -21,10 +21,10 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES Asset.h Sound.h SoundContext.h - ParticleEmitter.hpp - ParticleSystem.hpp - Particle.hpp - SDLApp.hpp + ParticleEmitter.h + ParticleSystem.h + Particle.h + SDLApp.h ComponentManager.h ComponentManager.hpp Component.h diff --git a/src/crepe/Particle.cpp b/src/crepe/Particle.cpp index 8cf7f7e..aa33606 100644 --- a/src/crepe/Particle.cpp +++ b/src/crepe/Particle.cpp @@ -1,6 +1,8 @@ -#include "Particle.hpp" +#include "Particle.h" #include +using namespace crepe; + Particle::Particle() { this->active = false; diff --git a/src/crepe/Particle.h b/src/crepe/Particle.h new file mode 100644 index 0000000..c408ed9 --- /dev/null +++ b/src/crepe/Particle.h @@ -0,0 +1,24 @@ +#pragma once + +namespace crepe { + +struct Position { + float x; + float y; + }; + +class Particle { +public: + + Position position; + Position velocity; + float lifespan; + bool active; + + Particle(); + void reset(float lifespan, Position position, Position velocity); + void update(float deltaTime); + float timeInLife; +}; + +} diff --git a/src/crepe/Particle.hpp b/src/crepe/Particle.hpp deleted file mode 100644 index 669a8ab..0000000 --- a/src/crepe/Particle.hpp +++ /dev/null @@ -1,20 +0,0 @@ -#pragma once - -struct Position { - float x; - float y; - }; - -class Particle { -public: - - Position position; - Position velocity; - float lifespan; - bool active; - - Particle(); - void reset(float lifespan, Position position, Position velocity); - void update(float deltaTime); - float timeInLife; -}; diff --git a/src/crepe/ParticleEmitter.cpp b/src/crepe/ParticleEmitter.cpp index 3c9cc4e..9ae5e76 100644 --- a/src/crepe/ParticleEmitter.cpp +++ b/src/crepe/ParticleEmitter.cpp @@ -1,11 +1,13 @@ -#include "ParticleEmitter.hpp" +#include "ParticleEmitter.h" #include -#include "Particle.hpp" +#include "Particle.h" #include -ParticleEmitter::ParticleEmitter(unsigned int maxParticles, unsigned int emissionRate, unsigned int speed, unsigned int speedOffset, unsigned int angle, unsigned int angleOffset, float m_beginLifespan, float m_endLifespan) +using namespace crepe; + +ParticleEmitter::ParticleEmitter(uint32_t maxParticles, uint32_t emissionRate, uint32_t speed, uint32_t speedOffset, uint32_t angle, uint32_t angleOffset, float m_beginLifespan, float m_endLifespan) : m_maxParticles(maxParticles), m_emissionRate(emissionRate), m_speed(speed), m_speedOffset(speedOffset), m_position{0, 0}, m_beginLifespan(m_beginLifespan),m_endLifespan(m_endLifespan) { - std::srand(static_cast(std::time(nullptr))); // initialize random seed + std::srand(static_cast(std::time(nullptr))); // initialize random seed std::cout << "Create emitter" << std::endl; m_minAngle = (360 + angle - (angleOffset % 360)) % 360; // calculate minAngle m_maxAngle = (360 + angle + (angleOffset % 360)) % 360; // calculate maxAngle diff --git a/src/crepe/ParticleEmitter.h b/src/crepe/ParticleEmitter.h new file mode 100644 index 0000000..477f492 --- /dev/null +++ b/src/crepe/ParticleEmitter.h @@ -0,0 +1,32 @@ +#pragma once + +#include +#include "Particle.h" +#include +#include "Component.h" +# + +namespace crepe { + +class ParticleEmitter : public Component { +public: + ParticleEmitter(uint32_t maxParticles, uint32_t emissionRate, uint32_t speed, uint32_t speedOffset, uint32_t angle, uint32_t angleOffset,float m_beginLifespan,float m_endLifespan); + ~ParticleEmitter(); + + Position m_position; //position of the emitter + uint32_t m_maxParticles; //maximum number of particles + uint32_t m_emissionRate; //rate of particle emission + uint32_t m_speed; //base speed of the particles + uint32_t m_speedOffset; //offset for random speed variation + uint32_t m_minAngle; //min angle of particle emission + uint32_t m_maxAngle; //max angle of particle emission + float m_beginLifespan; //begin Lifespan of particle (only visual) + float m_endLifespan; //begin Lifespan of particle + + std::vector particles; //collection of particles + +}; + +} + + diff --git a/src/crepe/ParticleEmitter.hpp b/src/crepe/ParticleEmitter.hpp deleted file mode 100644 index 5de5e1e..0000000 --- a/src/crepe/ParticleEmitter.hpp +++ /dev/null @@ -1,25 +0,0 @@ -#pragma once - -#include -#include "Particle.hpp" - - - -class ParticleEmitter { -public: - ParticleEmitter(unsigned int maxParticles, unsigned int emissionRate, unsigned int speed, unsigned int speedOffset, unsigned int angle, unsigned int angleOffset,float m_beginLifespan,float m_endLifespan); - ~ParticleEmitter(); - - Position m_position; //position of the emitter - unsigned int m_maxParticles; //maximum number of particles - unsigned int m_emissionRate; //rate of particle emission - unsigned int m_speed; //base speed of the particles - unsigned int m_speedOffset; //offset for random speed variation - unsigned int m_minAngle; //min angle of particle emission - unsigned int m_maxAngle; //max angle of particle emission - float m_beginLifespan; //begin Lifespan of particle (only visual) - float m_endLifespan; //begin Lifespan of particle - - std::vector particles; //collection of particles - -}; diff --git a/src/crepe/ParticleSystem.cpp b/src/crepe/ParticleSystem.cpp index aff7a30..8891e2b 100644 --- a/src/crepe/ParticleSystem.cpp +++ b/src/crepe/ParticleSystem.cpp @@ -1,10 +1,12 @@ -#include "ParticleSystem.hpp" +#include "ParticleSystem.h" #include // #include #include #include // include iostream for std::cout -#include "ParticleEmitter.hpp" -#include "Particle.hpp" +#include "ParticleEmitter.h" +#include "Particle.h" + +using namespace crepe; ParticleSystem::ParticleSystem() : m_elapsedTime(0.0f) {} // Initialize m_elapsedTime to 0 @@ -39,11 +41,11 @@ void ParticleSystem::emitParticle(ParticleEmitter& emitter) { //check if value is overthe 360 degrees if(emitter.m_maxAngle < emitter.m_minAngle) { - randomAngle = ((emitter.m_minAngle + (std::rand() % (static_cast(emitter.m_maxAngle + 360 - emitter.m_minAngle + 1))))%360); + randomAngle = ((emitter.m_minAngle + (std::rand() % (static_cast(emitter.m_maxAngle + 360 - emitter.m_minAngle + 1))))%360); } else { - randomAngle = emitter.m_minAngle + (std::rand() % (static_cast(emitter.m_maxAngle - emitter.m_minAngle + 1))); + randomAngle = emitter.m_minAngle + (std::rand() % (static_cast(emitter.m_maxAngle - emitter.m_minAngle + 1))); } diff --git a/src/crepe/ParticleSystem.h b/src/crepe/ParticleSystem.h new file mode 100644 index 0000000..fd6d110 --- /dev/null +++ b/src/crepe/ParticleSystem.h @@ -0,0 +1,19 @@ +#pragma once + +#include +#include "ParticleEmitter.h" + + +namespace crepe { + +class ParticleSystem { +public: + ParticleSystem(); + void update(float deltaTime, std::vector& emitters); +private: + void emitParticle(ParticleEmitter &emitter); //emits a new particle + + float m_elapsedTime; //elapsed time since the last emission +}; + +} diff --git a/src/crepe/ParticleSystem.hpp b/src/crepe/ParticleSystem.hpp deleted file mode 100644 index f670415..0000000 --- a/src/crepe/ParticleSystem.hpp +++ /dev/null @@ -1,14 +0,0 @@ -#pragma once - -#include -#include "ParticleEmitter.hpp" - -class ParticleSystem { -public: - ParticleSystem(); - void update(float deltaTime, std::vector& emitters); -private: - void emitParticle(ParticleEmitter &emitter); //emits a new particle - - float m_elapsedTime; //elapsed time since the last emission -}; diff --git a/src/crepe/SDLApp.cpp b/src/crepe/SDLApp.cpp index 0779af1..715dd6f 100644 --- a/src/crepe/SDLApp.cpp +++ b/src/crepe/SDLApp.cpp @@ -1,8 +1,8 @@ -#include "SDLApp.hpp" +#include "SDLApp.h" #include #include -#include "Particle.hpp" -#include "ParticleEmitter.hpp" +#include "Particle.h" +#include "ParticleEmitter.h" SDLApp::SDLApp(int windowWidth, int windowHeight) : windowWidth(windowWidth), windowHeight(windowHeight), window(nullptr), renderer(nullptr) {} diff --git a/src/crepe/SDLApp.h b/src/crepe/SDLApp.h new file mode 100644 index 0000000..8915d30 --- /dev/null +++ b/src/crepe/SDLApp.h @@ -0,0 +1,28 @@ +#ifndef SDLAPP_HPP +#define SDLAPP_HPP + +#include +#include "Particle.h" +#include "ParticleEmitter.h" + +class SDLApp { +public: + SDLApp(int windowWidth, int windowHeight); + ~SDLApp(); + + bool initialize(); + void handleEvents(bool& running); + void clearScreen(); + void presentScreen(); + void drawSquare(int x, int y, int size); + void cleanUp(); + void drawParticles(const std::vector& emitters); + void drawMultipleSquares(const std::vector& squares); +private: + int windowWidth; + int windowHeight; + SDL_Window* window; + SDL_Renderer* renderer; +}; + +#endif diff --git a/src/crepe/SDLApp.hpp b/src/crepe/SDLApp.hpp deleted file mode 100644 index f95d4bc..0000000 --- a/src/crepe/SDLApp.hpp +++ /dev/null @@ -1,28 +0,0 @@ -#ifndef SDLAPP_HPP -#define SDLAPP_HPP - -#include -#include "Particle.hpp" -#include "ParticleEmitter.hpp" - -class SDLApp { -public: - SDLApp(int windowWidth, int windowHeight); - ~SDLApp(); - - bool initialize(); - void handleEvents(bool& running); - void clearScreen(); - void presentScreen(); - void drawSquare(int x, int y, int size); - void cleanUp(); - void drawParticles(const std::vector& emitters); - void drawMultipleSquares(const std::vector& squares); -private: - int windowWidth; - int windowHeight; - SDL_Window* window; - SDL_Renderer* renderer; -}; - -#endif diff --git a/src/example/particel.cpp b/src/example/particel.cpp index 58480a9..c66bbef 100644 --- a/src/example/particel.cpp +++ b/src/example/particel.cpp @@ -1,12 +1,14 @@ #include #include #include -#include "SDLApp.hpp" -#include "ParticleEmitter.hpp" -#include "ParticleSystem.hpp" -#include "Particle.hpp" +#include "SDLApp.h" +#include "ParticleEmitter.h" +#include "ParticleSystem.h" +#include "Particle.h" #include +using namespace crepe; + const int WINDOW_WIDTH = 800; const int WINDOW_HEIGHT = 600; -- cgit v1.2.3 From 016070d47f2fff8cfdd49cc0bdf65c14ef922c94 Mon Sep 17 00:00:00 2001 From: jaroWMR Date: Thu, 10 Oct 2024 09:30:49 +0200 Subject: renamed particel to particle and example uses ecs for poc --- src/crepe/ParticleEmitter.cpp | 2 + src/crepe/ParticleSystem.cpp | 27 ++---------- src/crepe/ParticleSystem.h | 2 +- src/example/CMakeLists.txt | 4 +- src/example/particel.cpp | 84 ------------------------------------- src/example/particle.cpp | 97 +++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 105 insertions(+), 111 deletions(-) delete mode 100644 src/example/particel.cpp create mode 100644 src/example/particle.cpp (limited to 'src/crepe/ParticleSystem.cpp') diff --git a/src/crepe/ParticleEmitter.cpp b/src/crepe/ParticleEmitter.cpp index 9ae5e76..30cba7c 100644 --- a/src/crepe/ParticleEmitter.cpp +++ b/src/crepe/ParticleEmitter.cpp @@ -11,6 +11,8 @@ ParticleEmitter::ParticleEmitter(uint32_t maxParticles, uint32_t emissionRate, u std::cout << "Create emitter" << std::endl; m_minAngle = (360 + angle - (angleOffset % 360)) % 360; // calculate minAngle m_maxAngle = (360 + angle + (angleOffset % 360)) % 360; // calculate maxAngle + m_position.x = 400; + m_position.y = 400; for (size_t i = 0; i < m_maxParticles; i++) { this->particles.emplace_back(); diff --git a/src/crepe/ParticleSystem.cpp b/src/crepe/ParticleSystem.cpp index 8891e2b..3a1f653 100644 --- a/src/crepe/ParticleSystem.cpp +++ b/src/crepe/ParticleSystem.cpp @@ -5,40 +5,32 @@ #include // include iostream for std::cout #include "ParticleEmitter.h" #include "Particle.h" +#include "ComponentManager.h" using namespace crepe; ParticleSystem::ParticleSystem() : m_elapsedTime(0.0f) {} // Initialize m_elapsedTime to 0 -void ParticleSystem::update(float deltaTime, std::vector& emitters) { - // std::cout << "ParticleSystem update" << std::endl; +void ParticleSystem::update(float deltaTime, std::vector>& emitters) { for (ParticleEmitter& emitter : emitters) { float updateAmount = 1/static_cast(emitter.m_emissionRate); for (float i = 0; i < deltaTime; i += updateAmount) { emitParticle(emitter); } - // std::cout << "after emit" << std::endl; - - //update/move particles afterwards delete if not alive. for (size_t j = 0; j < emitter.particles.size(); j++) { - // std::cout << "update" << std::endl; if(emitter.particles[j].active) { emitter.particles[j].update(deltaTime); } } - - } } void ParticleSystem::emitParticle(ParticleEmitter& emitter) { - // std::cout << "new emitter:" << std::endl; - Position initialPosition = { emitter.m_position.x, emitter.m_position.y }; + Position initialPosition = { emitter.m_position.x, emitter.m_position.y}; float randomAngle = 0.0f; - //check if value is overthe 360 degrees if(emitter.m_maxAngle < emitter.m_minAngle) { randomAngle = ((emitter.m_minAngle + (std::rand() % (static_cast(emitter.m_maxAngle + 360 - emitter.m_minAngle + 1))))%360); @@ -47,8 +39,6 @@ void ParticleSystem::emitParticle(ParticleEmitter& emitter) { { randomAngle = emitter.m_minAngle + (std::rand() % (static_cast(emitter.m_maxAngle - emitter.m_minAngle + 1))); } - - float angleInRadians = randomAngle * (M_PI / 180.0f); float randomSpeedOffset = (static_cast(std::rand()) / RAND_MAX) * (2 * emitter.m_speedOffset) - emitter.m_speedOffset; float velocityX = (emitter.m_speed + randomSpeedOffset) * std::cos(angleInRadians); @@ -57,23 +47,12 @@ void ParticleSystem::emitParticle(ParticleEmitter& emitter) { velocityX, velocityY }; - // std::cout << "emitter.m_endLifespan:" << emitter.m_endLifespan << std::endl; for (size_t i = 0; i < emitter.particles.size(); i++) { if(!emitter.particles[i].active) { - // std::cout << "active " << emitter.particles[i].active << std::endl; - // std::cout << "lifespan " << emitter.particles[i].lifespan << std::endl; - // std::cout << "timeInLife " << emitter.particles[i].timeInLife << std::endl; - // std::cout << "emitter.m_endLifespan" << emitter.m_endLifespan << std::endl; - // std::cout << "initialPositionx" << initialPosition.x << std::endl; - // std::cout << "initialPositiony" << initialPosition.y << std::endl; - // std::cout << "initialVelocityx" << initialVelocity.x << std::endl; - // std::cout << "initialVelocityy" << initialVelocity.y << std::endl; emitter.particles[i].reset(emitter.m_endLifespan, initialPosition, initialVelocity); break; } } - - //emitter.particles.emplace_back(emitter.m_endLifespan, initialPosition, initialVelocity); } diff --git a/src/crepe/ParticleSystem.h b/src/crepe/ParticleSystem.h index fd6d110..7b14f71 100644 --- a/src/crepe/ParticleSystem.h +++ b/src/crepe/ParticleSystem.h @@ -9,7 +9,7 @@ namespace crepe { class ParticleSystem { public: ParticleSystem(); - void update(float deltaTime, std::vector& emitters); + void update(float deltaTime, std::vector>& emitters); private: void emitParticle(ParticleEmitter &emitter); //emits a new particle diff --git a/src/example/CMakeLists.txt b/src/example/CMakeLists.txt index 36ceba1..cbf8e31 100644 --- a/src/example/CMakeLists.txt +++ b/src/example/CMakeLists.txt @@ -15,5 +15,5 @@ endfunction() add_example(audio_internal) add_example(components_internal) add_example(script) -add_example(particel) -target_link_libraries(particel PUBLIC SDL2) +add_example(particle) +target_link_libraries(particle PUBLIC SDL2) diff --git a/src/example/particel.cpp b/src/example/particel.cpp deleted file mode 100644 index c66bbef..0000000 --- a/src/example/particel.cpp +++ /dev/null @@ -1,84 +0,0 @@ -#include -#include -#include -#include "SDLApp.h" -#include "ParticleEmitter.h" -#include "ParticleSystem.h" -#include "Particle.h" -#include - -using namespace crepe; - -const int WINDOW_WIDTH = 800; -const int WINDOW_HEIGHT = 600; - -int main(int argc, char* argv[]) { - SDLApp app(WINDOW_WIDTH, WINDOW_HEIGHT); - - if (!app.initialize()) { - std::cerr << "Failed to initialize SDLApp." << std::endl; - return 1; - } - - ParticleSystem particleSystem; - - unsigned int maxParticles = 100; // maximum number of particles - unsigned int emissionRate = 1; // particles created per second - unsigned int speed = 50; // base speed of particles - unsigned int speedOffset = 10; // random offset for particle speed - unsigned int angle = 90; // base angle of particle emission - unsigned int angleOffset = 30; // random offset for particle angle - float beginLifespan = 0.0f; // beginning lifespan of particles - float endLifespan = 2.0f; // ending lifespan of particles - - // Vector to hold all the emitters - std::vector emitters; - - // Loop to create 1000 emitters - for (unsigned int i = 0; i < 1000; ++i) { - ParticleEmitter emitter(maxParticles, emissionRate, speed, speedOffset, angle, angleOffset, beginLifespan, endLifespan); - - // Set a position for each emitter, modifying the position for demonstration - emitter.m_position = {static_cast(200 + (i % 100)), static_cast(200 + (i / 100) * 10)}; // Adjust position for each emitter - - emitters.push_back(emitter); // Add the emitter to the vector - } - float deltaTime = 0.1f; - bool running = true; - std::cout << "start loop " << std::endl; - while (running) { - app.handleEvents(running); - - // Start timing - auto start = std::chrono::high_resolution_clock::now(); - - particleSystem.update(deltaTime, emitters); // update particle system with delta time and emitters - - // End timing - auto end = std::chrono::high_resolution_clock::now(); - std::chrono::duration duration = end - start; // get duration in milliseconds - - std::cout << "Update took " << duration.count() << " ms" << std::endl; - app.clearScreen(); - - start = std::chrono::high_resolution_clock::now(); - // render particles using the drawSquare method from SDLApp - for (const ParticleEmitter& emitter : emitters) { - for (const Particle& particle : emitter.particles) { - if(particle.active)app.drawSquare(particle.position.x, particle.position.y, 5); // draw each particle - } - } - - - app.presentScreen(); - end = std::chrono::high_resolution_clock::now(); - duration = end - start; // get duration in milliseconds - - std::cout << "screen took " << duration.count() << " ms" << std::endl; - - std::this_thread::sleep_for(std::chrono::milliseconds(20)); // simulate ~50 FPS - } - - app.cleanUp(); - return 0; -} diff --git a/src/example/particle.cpp b/src/example/particle.cpp new file mode 100644 index 0000000..fc56230 --- /dev/null +++ b/src/example/particle.cpp @@ -0,0 +1,97 @@ +#include +#include +#include +#include "SDLApp.h" +#include "ParticleEmitter.h" +#include "ParticleSystem.h" +#include "Particle.h" +#include +#include +#include +#include + +using namespace crepe; +using namespace std; + +const int WINDOW_WIDTH = 800; +const int WINDOW_HEIGHT = 600; + +int main(int argc, char* argv[]) { + SDLApp app(WINDOW_WIDTH, WINDOW_HEIGHT); + + if (!app.initialize()) { + cerr << "Failed to initialize SDLApp." << endl; + return 1; + } + + auto & mgr = ComponentManager::get_instance(); + GameObject * game_object[1]; + game_object[0] = new GameObject(0, "Name", "Tag", 0); + + + ParticleSystem particleSystem; + + unsigned int maxParticles = 100; // maximum number of particles + unsigned int emissionRate = 1; // particles created per second + unsigned int speed = 50; // base speed of particles + unsigned int speedOffset = 10; // random offset for particle speed + unsigned int angle = 90; // base angle of particle emission + unsigned int angleOffset = 30; // random offset for particle angle + float beginLifespan = 0.0f; // beginning lifespan of particles + float endLifespan = 2.0f; // ending lifespan of particles + + // Vector to hold all the emitters + // vector emitters; + game_object[0]->add_component(maxParticles, emissionRate, speed, speedOffset, angle, angleOffset, beginLifespan, endLifespan); + + std::vector> emitters = mgr.get_components_by_type(); + + + // Loop to create 1000 emitters + // for (unsigned int i = 0; i < 1000; ++i) { + // ParticleEmitter emitter(maxParticles, emissionRate, speed, speedOffset, angle, angleOffset, beginLifespan, endLifespan); + + // // Set a position for each emitter, modifying the position for demonstration + // emitter.m_position = {static_cast(200 + (i % 100)), static_cast(200 + (i / 100) * 10)}; // Adjust position for each emitter + + // emitters.push_back(emitter); // Add the emitter to the vector + // } + float deltaTime = 0.1f; + bool running = true; + cout << "start loop " << endl; + while (running) { + app.handleEvents(running); + + // Start timing + auto start = chrono::high_resolution_clock::now(); + + particleSystem.update(deltaTime, emitters); // update particle system with delta time and emitters + + // End timing + auto end = chrono::high_resolution_clock::now(); + chrono::duration duration = end - start; // get duration in milliseconds + + cout << "Update took " << duration.count() << " ms" << endl; + app.clearScreen(); + + start = chrono::high_resolution_clock::now(); + // render particles using the drawSquare method from SDLApp + for (const ParticleEmitter& emitter : emitters) { + for (const Particle& particle : emitter.particles) { + if(particle.active)app.drawSquare(particle.position.x, particle.position.y, 5); // draw each particle + } + } + + + app.presentScreen(); + end = chrono::high_resolution_clock::now(); + duration = end - start; // get duration in milliseconds + + cout << "screen took " << duration.count() << " ms" << endl; + + this_thread::sleep_for(chrono::milliseconds(20)); // simulate ~50 FPS + } + + app.cleanUp(); + return 0; +} -- cgit v1.2.3 From 0f03cdbf23f57116b7664a7c98c4605fd69bb961 Mon Sep 17 00:00:00 2001 From: jaroWMR Date: Sun, 20 Oct 2024 12:59:18 +0200 Subject: updated particle system --- src/crepe/ParticleSystem.cpp | 7 ++++--- src/crepe/ParticleSystem.h | 2 +- src/crepe/Transform.cpp | 6 ++++++ src/crepe/Transform.h | 16 ++++++++++++++++ src/example/components_internal.cpp | 4 ++-- src/example/particle.cpp | 17 +++++++++++------ 6 files changed, 40 insertions(+), 12 deletions(-) create mode 100644 src/crepe/Transform.cpp create mode 100644 src/crepe/Transform.h (limited to 'src/crepe/ParticleSystem.cpp') diff --git a/src/crepe/ParticleSystem.cpp b/src/crepe/ParticleSystem.cpp index 3a1f653..0dad4cd 100644 --- a/src/crepe/ParticleSystem.cpp +++ b/src/crepe/ParticleSystem.cpp @@ -1,8 +1,6 @@ #include "ParticleSystem.h" #include -// #include #include -#include // include iostream for std::cout #include "ParticleEmitter.h" #include "Particle.h" #include "ComponentManager.h" @@ -11,7 +9,10 @@ using namespace crepe; ParticleSystem::ParticleSystem() : m_elapsedTime(0.0f) {} // Initialize m_elapsedTime to 0 -void ParticleSystem::update(float deltaTime, std::vector>& emitters) { +void ParticleSystem::update() { + ComponentManager& mgr = ComponentManager::get_instance(); + std::vector> emitters = mgr.get_components_by_type(); + float deltaTime = 0.10; for (ParticleEmitter& emitter : emitters) { float updateAmount = 1/static_cast(emitter.m_emissionRate); for (float i = 0; i < deltaTime; i += updateAmount) diff --git a/src/crepe/ParticleSystem.h b/src/crepe/ParticleSystem.h index 7b14f71..c8777f5 100644 --- a/src/crepe/ParticleSystem.h +++ b/src/crepe/ParticleSystem.h @@ -9,7 +9,7 @@ namespace crepe { class ParticleSystem { public: ParticleSystem(); - void update(float deltaTime, std::vector>& emitters); + void update(); private: void emitParticle(ParticleEmitter &emitter); //emits a new particle diff --git a/src/crepe/Transform.cpp b/src/crepe/Transform.cpp new file mode 100644 index 0000000..0e4b0cf --- /dev/null +++ b/src/crepe/Transform.cpp @@ -0,0 +1,6 @@ +#include "Transform.h" + +using namespace crepe; + +Transform::Transform(int mass, int gravityScale, int bodyType) + : mass(mass), gravity_scale(gravityScale), body_type(bodyType) {} diff --git a/src/crepe/Transform.h b/src/crepe/Transform.h new file mode 100644 index 0000000..c72ebc4 --- /dev/null +++ b/src/crepe/Transform.h @@ -0,0 +1,16 @@ +#pragma once + +#include "Component.h" + +namespace crepe { + +class Transform : public Component { +public: + Transform(int mass, int gravityScale, int bodyType); + + int mass; + int gravity_scale; + int body_type; +}; + +} // namespace crepe diff --git a/src/example/components_internal.cpp b/src/example/components_internal.cpp index 54ce295..3ba5b5a 100644 --- a/src/example/components_internal.cpp +++ b/src/example/components_internal.cpp @@ -39,8 +39,8 @@ int main() { auto stop_adding = chrono::high_resolution_clock::now(); auto sprites = mgr.get_components_by_type(); - for (auto sprite : sprites) { - assert(sprite.get().path == "test"); + for (auto & sprite : sprites) { + assert(sprite.path == "test"); } auto stop_looping = chrono::high_resolution_clock::now(); diff --git a/src/example/particle.cpp b/src/example/particle.cpp index fc56230..df1dbfd 100644 --- a/src/example/particle.cpp +++ b/src/example/particle.cpp @@ -24,7 +24,7 @@ int main(int argc, char* argv[]) { return 1; } - auto & mgr = ComponentManager::get_instance(); + GameObject * game_object[1]; game_object[0] = new GameObject(0, "Name", "Tag", 0); @@ -32,19 +32,19 @@ int main(int argc, char* argv[]) { ParticleSystem particleSystem; unsigned int maxParticles = 100; // maximum number of particles - unsigned int emissionRate = 1; // particles created per second + unsigned int emissionRate = 10; // particles created per second unsigned int speed = 50; // base speed of particles unsigned int speedOffset = 10; // random offset for particle speed - unsigned int angle = 90; // base angle of particle emission + unsigned int angle = 270; // base angle of particle emission unsigned int angleOffset = 30; // random offset for particle angle float beginLifespan = 0.0f; // beginning lifespan of particles - float endLifespan = 2.0f; // ending lifespan of particles + float endLifespan = 6.0f; // ending lifespan of particles // Vector to hold all the emitters // vector emitters; game_object[0]->add_component(maxParticles, emissionRate, speed, speedOffset, angle, angleOffset, beginLifespan, endLifespan); - std::vector> emitters = mgr.get_components_by_type(); + // Loop to create 1000 emitters @@ -65,7 +65,10 @@ int main(int argc, char* argv[]) { // Start timing auto start = chrono::high_resolution_clock::now(); - particleSystem.update(deltaTime, emitters); // update particle system with delta time and emitters + + // POC CODE + particleSystem.update(); + // POC CODE // End timing auto end = chrono::high_resolution_clock::now(); @@ -76,6 +79,8 @@ int main(int argc, char* argv[]) { start = chrono::high_resolution_clock::now(); // render particles using the drawSquare method from SDLApp + ComponentManager& mgr = ComponentManager::get_instance(); + std::vector> emitters = mgr.get_components_by_type(); for (const ParticleEmitter& emitter : emitters) { for (const Particle& particle : emitter.particles) { if(particle.active)app.drawSquare(particle.position.x, particle.position.y, 5); // draw each particle -- cgit v1.2.3