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/CMakeLists.txt | 4 +- src/example/particel.cpp | 84 --------------------------------------- src/example/particle.cpp | 97 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 99 insertions(+), 86 deletions(-) delete mode 100644 src/example/particel.cpp create mode 100644 src/example/particle.cpp (limited to 'src/example') 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