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/example/particle.cpp | 97 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 src/example/particle.cpp (limited to 'src/example/particle.cpp') 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/example/particle.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