diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-23 21:54:28 +0200 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-23 21:54:28 +0200 |
commit | 9b7be419c9dcc6ebd1e504713c7b2676ca3d2fdf (patch) | |
tree | 97a2fc5ce1ec1345bd6f44889682ea9a2ffafd76 /src/example/particle.cpp | |
parent | 080ad535e6fc6666b919b1a21b6986aaf9b678eb (diff) |
`clang-format`
Diffstat (limited to 'src/example/particle.cpp')
-rw-r--r-- | src/example/particle.cpp | 158 |
1 files changed, 78 insertions, 80 deletions
diff --git a/src/example/particle.cpp b/src/example/particle.cpp index fb89a3d..66b1441 100644 --- a/src/example/particle.cpp +++ b/src/example/particle.cpp @@ -1,14 +1,13 @@ -#include <iostream> -#include <thread> -#include <chrono> +#include "Particle.h" +#include "ParticleSystem.h" #include "SDLApp.h" #include "api/ParticleEmitter.h" -#include "ParticleSystem.h" -#include "Particle.h" +#include <chrono> #include <crepe/Component.h> #include <crepe/ComponentManager.h> #include <crepe/api/GameObject.h> -#include <chrono> +#include <iostream> +#include <thread> using namespace crepe::api; using namespace crepe; @@ -17,87 +16,86 @@ 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); +int main(int argc, char * argv[]) { + SDLApp app(WINDOW_WIDTH, WINDOW_HEIGHT); - if (!app.initialize()) { - cerr << "Failed to initialize SDLApp." << endl; - return 1; - } + if (!app.initialize()) { + cerr << "Failed to initialize SDLApp." << endl; + return 1; + } - 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 = 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 = 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 = 6.0f; // ending lifespan of particles - - // Vector to hold all the emitters - // vector<ParticleEmitter> emitters; - game_object[0]->add_component<ParticleEmitter>(maxParticles, emissionRate, speed, speedOffset, angle, angleOffset, beginLifespan, endLifespan); - - - - - // 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<float>(200 + (i % 100)), static_cast<float>(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 particle_system; + + unsigned int max_particles = 100; // maximum number of particles + unsigned int emission_rate = 10; // particles created per second + unsigned int speed = 50; // base speed of particles + unsigned int speed_offset = 10; // random offset for particle speed + unsigned int angle = 270; // base angle of particle emission + unsigned int angle_offset = 30; // random offset for particle angle + float begin_lifespan = 0.0f; // beginning lifespan of particles + float end_lifespan = 6.0f; // ending lifespan of particles + + // Vector to hold all the emitters + // vector<ParticleEmitter> emitters; + game_object[0]->add_component<ParticleEmitter>( + max_particles, emission_rate, speed, speed_offset, angle, angle_offset, + begin_lifespan, end_lifespan); + + // 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<float>(200 + (i % 100)), static_cast<float>(200 + (i / 100) * 10)}; // Adjust position for each emitter + + // emitters.push_back(emitter); // Add the emitter to the vector + // } + float delta_time = 0.1f; + bool running = true; + cout << "start loop " << endl; + while (running) { + app.handle_events(running); + + // Start timing + auto start = chrono::high_resolution_clock::now(); // POC CODE - particleSystem.update(); + particle_system.update(); // POC CODE - // End timing - auto end = chrono::high_resolution_clock::now(); - chrono::duration<float, milli> 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 - ComponentManager& mgr = ComponentManager::get_instance(); - std::vector<std::reference_wrapper<ParticleEmitter>> emitters = mgr.get_components_by_type<ParticleEmitter>(); - 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; + // End timing + auto end = chrono::high_resolution_clock::now(); + chrono::duration<float, milli> duration + = end - start; // get duration in milliseconds + + cout << "Update took " << duration.count() << " ms" << endl; + app.clear_screen(); + + start = chrono::high_resolution_clock::now(); + // render particles using the draw_square method from SDLApp + ComponentManager & mgr = ComponentManager::get_instance(); + std::vector<std::reference_wrapper<ParticleEmitter>> emitters + = mgr.get_components_by_type<ParticleEmitter>(); + for (const ParticleEmitter & emitter : emitters) { + for (const Particle & particle : emitter.particles) { + if (particle.active) + app.draw_square(particle.position.x, particle.position.y, + 5); // draw each particle + } + } + + app.present_screen(); + 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.clean_up(); + return 0; } |