From bd24f87e18bef29679023ee9a60028f2afdbb470 Mon Sep 17 00:00:00 2001 From: jaroWMR Date: Tue, 1 Oct 2024 19:10:24 +0200 Subject: particle POC --- src/crepe/CMakeLists.txt | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'src/crepe/CMakeLists.txt') diff --git a/src/crepe/CMakeLists.txt b/src/crepe/CMakeLists.txt index 392b7d7..c825529 100644 --- a/src/crepe/CMakeLists.txt +++ b/src/crepe/CMakeLists.txt @@ -1,3 +1,7 @@ target_sources(main PUBLIC - main.cpp + main.cpp + Particle.cpp + ParticleEmitter.cpp ) + +target_include_directories(main PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) -- cgit v1.2.3 From 094ce1806156e191ffb554d4e88e636421cb0242 Mon Sep 17 00:00:00 2001 From: jaroWMR Date: Mon, 7 Oct 2024 10:04:23 +0200 Subject: particle direction is random --- src/crepe/CMakeLists.txt | 1 + src/crepe/ParticleEmitter.cpp | 70 +++++++++++++++++++++++++----------- src/crepe/ParticleEmitter.hpp | 32 +++++++++-------- src/crepe/SDLApp.cpp | 69 ++++++++++++++++++++++++++++++++++++ src/crepe/SDLApp.hpp | 25 +++++++++++++ src/crepe/main.cpp | 82 ++++++++++++++----------------------------- 6 files changed, 188 insertions(+), 91 deletions(-) create mode 100644 src/crepe/SDLApp.cpp create mode 100644 src/crepe/SDLApp.hpp (limited to 'src/crepe/CMakeLists.txt') diff --git a/src/crepe/CMakeLists.txt b/src/crepe/CMakeLists.txt index c825529..933f907 100644 --- a/src/crepe/CMakeLists.txt +++ b/src/crepe/CMakeLists.txt @@ -2,6 +2,7 @@ target_sources(main PUBLIC main.cpp Particle.cpp ParticleEmitter.cpp + SDLApp.cpp ) target_include_directories(main PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) diff --git a/src/crepe/ParticleEmitter.cpp b/src/crepe/ParticleEmitter.cpp index 13aaeae..f575e38 100644 --- a/src/crepe/ParticleEmitter.cpp +++ b/src/crepe/ParticleEmitter.cpp @@ -1,40 +1,70 @@ #include "ParticleEmitter.hpp" +#include +#include +#include +#include // include iostream for std::cout -ParticleEmitter::ParticleEmitter(int maxParticles, float emissionRate) - : maxParticles(maxParticles), emissionRate(emissionRate), elapsedTime(0.0f) { - position = { 0, 0 }; - std::srand(static_cast(std::time(nullptr))); / +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_elapsedTime(0.0f), m_speed(speed), m_speedOffset(speedOffset), m_position{0, 0} { + std::srand(static_cast(std::time(nullptr))); // initialize random seed + + m_minAngle = (360 + angle - (angleOffset % 360)) % 360; // calculate minAngle + m_maxAngle = (360 + angle + (angleOffset % 360)) % 360; // calculate maxAngle + + std::cout << "Initialized ParticleEmitter with:" << std::endl; + std::cout << "Min Angle: " << m_minAngle << " Max Angle: " << m_maxAngle << std::endl; } -void ParticleEmitter::update(float deltaTime) { - elapsedTime += deltaTime; +void ParticleEmitter::update(float deltaTime) { // keep deltaTime as float + m_elapsedTime += deltaTime; - while (elapsedTime >= (1.0f / emissionRate) && particles.size() < maxParticles) { - elapsedTime -= (1.0f / emissionRate); + while (m_elapsedTime >= (1.0f / m_emissionRate) && particles.size() < m_maxParticles) { + m_elapsedTime -= (1.0f / m_emissionRate); emitParticle(); } - for (auto it = particles.begin(); it != particles.end();) { - it->update(deltaTime); + std::vector::iterator it = particles.begin(); + while (it != particles.end()) { + it->update(deltaTime); // update particle if (!it->isAlive()) { - it = particles.erase(it); + it = particles.erase(it); // remove dead particle } else { - ++it; + ++it; // move to next particle } } } void ParticleEmitter::emitParticle() { - Particle::Position initialPosition = { position.x, position.y }; + Particle::Position initialPosition = { m_position.x, m_position.y }; // starting position of the particle + + // generate a random angle within the range [minAngle, maxAngle] + float randomAngle = m_minAngle + (std::rand() % (static_cast(m_maxAngle - m_minAngle + 1))); // random angle within the specified range + + // convert angle to radians for velocity calculation + float angleInRadians = randomAngle * (M_PI / 180.0f); + + // calculate velocity components based on the final angle + float randomSpeedOffset = (static_cast(std::rand()) / RAND_MAX) * (2 * m_speedOffset) - m_speedOffset; // random speed offset + float velocityX = (m_speed + randomSpeedOffset) * std::cos(angleInRadians); // x component of velocity + float velocityY = (m_speed + randomSpeedOffset) * std::sin(angleInRadians); // y component of velocity + Particle::Position initialVelocity = { - static_cast(randFloat(-50.0f, 50.0f, 10.0f, 100.0f)), - static_cast(randFloat(-50.0f, 50.0f, 10.0f, 100.0f)) + static_cast(velocityX), // set x velocity + static_cast(velocityY) // set y velocity }; - particles.emplace_back(2.0f, initialPosition, initialVelocity); + + std::cout << "Emitting particle with:" << std::endl; + std::cout << "Random Angle: " << randomAngle << " (Radians: " << angleInRadians << ")" << std::endl; + std::cout << "Velocity X: " << velocityX << " Velocity Y: " << velocityY << std::endl; + + particles.emplace_back(2.0f, initialPosition, initialVelocity); // create and store the new particle +} + +const std::vector& ParticleEmitter::getParticles() const { + return particles; // return the collection of particles } -float ParticleEmitter::randFloat(float minangle, float maxangle, float minVel, float maxVel) { - float angle = static_cast(rand()) / RAND_MAX * (maxangle - minangle) + minangle; - float velocity = static_cast(rand()) / RAND_MAX * (maxVel - minVel) + minVel; - return velocity * std::cos(angle); +void ParticleEmitter::setPosition(int x, int y) { + m_position.x = x; // set x position of the emitter + m_position.y = y; // set y position of the emitter } diff --git a/src/crepe/ParticleEmitter.hpp b/src/crepe/ParticleEmitter.hpp index 682a2ae..9b3f2ad 100644 --- a/src/crepe/ParticleEmitter.hpp +++ b/src/crepe/ParticleEmitter.hpp @@ -1,28 +1,30 @@ #pragma once #include -#include -#include #include "Particle.hpp" -#include class ParticleEmitter { public: - std::vector particles; + ParticleEmitter(unsigned int maxParticles, unsigned int emissionRate, unsigned int speed, unsigned int speedOffset, unsigned int angle, unsigned int angleOffset); + void update(float deltaTime); // Keep deltaTime as float + const std::vector& getParticles() const; //returns the collection of particles + void setPosition(int x, int y); //sets the position of the emitter +private: + void emitParticle(); //emits a new particle - struct Position { + struct Position { //struct to hold position int x; int y; - } position; - - int maxParticles; - float emissionRate; - float elapsedTime; + }; - ParticleEmitter(int maxParticles, float emissionRate); - void update(float deltaTime); + Position m_position; //position of the emitter + unsigned int m_maxParticles; //maximum number of particles + unsigned int m_emissionRate; //rate of particle emission + float m_elapsedTime; //elapsed time since the last 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 -private: - void emitParticle(); - float randFloat(float minangle, float maxangle, float minVel, float maxVel); + std::vector particles; //collection of particles }; diff --git a/src/crepe/SDLApp.cpp b/src/crepe/SDLApp.cpp new file mode 100644 index 0000000..a5e3621 --- /dev/null +++ b/src/crepe/SDLApp.cpp @@ -0,0 +1,69 @@ +#include "SDLApp.hpp" +#include + +SDLApp::SDLApp(int windowWidth, int windowHeight) + : windowWidth(windowWidth), windowHeight(windowHeight), window(nullptr), renderer(nullptr) {} + +SDLApp::~SDLApp() { + cleanUp(); +} + +bool SDLApp::initialize() { + if (SDL_Init(SDL_INIT_VIDEO) != 0) { + std::cerr << "SDL Initialization Error: " << SDL_GetError() << std::endl; + return false; + } + + window = SDL_CreateWindow("Particle System", + SDL_WINDOWPOS_CENTERED, + SDL_WINDOWPOS_CENTERED, + windowWidth, + windowHeight, + SDL_WINDOW_SHOWN); + if (!window) { + std::cerr << "Window Creation Error: " << SDL_GetError() << std::endl; + return false; + } + + renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); + if (!renderer) { + std::cerr << "Renderer Creation Error: " << SDL_GetError() << std::endl; + return false; + } + + return true; +} + +void SDLApp::handleEvents(bool& running) { + SDL_Event event; + while (SDL_PollEvent(&event)) { + if (event.type == SDL_QUIT) { + running = false; + } + } +} + +void SDLApp::clearScreen() { + SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); + SDL_RenderClear(renderer); +} + +void SDLApp::presentScreen() { + SDL_RenderPresent(renderer); +} + +void SDLApp::drawSquare(int x, int y, int size) { + SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); + SDL_Rect rect = { x, y, size, size }; + SDL_RenderFillRect(renderer, &rect); +} + +void SDLApp::cleanUp() { + if (renderer) { + SDL_DestroyRenderer(renderer); + } + if (window) { + SDL_DestroyWindow(window); + } + SDL_Quit(); +} diff --git a/src/crepe/SDLApp.hpp b/src/crepe/SDLApp.hpp new file mode 100644 index 0000000..92a4e27 --- /dev/null +++ b/src/crepe/SDLApp.hpp @@ -0,0 +1,25 @@ +#ifndef SDLAPP_HPP +#define SDLAPP_HPP + +#include + +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(); + +private: + int windowWidth; + int windowHeight; + SDL_Window* window; + SDL_Renderer* renderer; +}; + +#endif diff --git a/src/crepe/main.cpp b/src/crepe/main.cpp index 73d041c..fc307cc 100644 --- a/src/crepe/main.cpp +++ b/src/crepe/main.cpp @@ -1,83 +1,53 @@ #include #include #include -#include +#include "SDLApp.hpp" #include "ParticleEmitter.hpp" const int WINDOW_WIDTH = 800; const int WINDOW_HEIGHT = 600; -void renderParticles(SDL_Renderer* renderer, const ParticleEmitter& emitter) { - for (const auto& particle : emitter.particles) { - SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); - SDL_Rect rect = { particle.position.x, particle.position.y, 5, 5 }; - SDL_RenderFillRect(renderer, &rect); - } -} - int main(int argc, char* argv[]) { - - if (SDL_Init(SDL_INIT_VIDEO) != 0) { - std::cerr << "SDL Initialization Error: " << SDL_GetError() << std::endl; - return 1; - } + SDLApp app(WINDOW_WIDTH, WINDOW_HEIGHT); - SDL_Window* window = SDL_CreateWindow("Particle System", - SDL_WINDOWPOS_CENTERED, - SDL_WINDOWPOS_CENTERED, - WINDOW_WIDTH, - WINDOW_HEIGHT, - SDL_WINDOW_SHOWN); - if (!window) { - std::cerr << "Window Creation Error: " << SDL_GetError() << std::endl; - SDL_Quit(); + if (!app.initialize()) { + std::cerr << "Failed to initialize SDLApp." << std::endl; return 1; } - // Create SDL renderer - SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); - if (!renderer) { - std::cerr << "Renderer Creation Error: " << SDL_GetError() << std::endl; - SDL_DestroyWindow(window); - SDL_Quit(); - return 1; - } - ParticleEmitter emitter(100, 10.0f); - emitter.position = { 400, 300 }; - ParticleEmitter emitter2(100, 10.0f); - emitter2.position = { 600, 600 }; + 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 = 180; //base angle of particle emission + unsigned int angleOffset = 30; //random offset for particle angle - float deltaTime = 0.1f; - bool running = true; - SDL_Event event; - while (running) { + ParticleEmitter emitter(maxParticles, emissionRate, speed, speedOffset, angle, angleOffset); - while (SDL_PollEvent(&event)) { - if (event.type == SDL_QUIT) { - running = false; - } - } + emitter.setPosition(400, 300); + + float deltaTime = 0.1f; + bool running = true; + while (running) { + app.handleEvents(running); emitter.update(deltaTime); - emitter2.update(deltaTime); - SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); - SDL_RenderClear(renderer); + app.clearScreen(); - renderParticles(renderer, emitter); - renderParticles(renderer, emitter2); + //render particles using the drawSquare method + const std::vector& particles = emitter.getParticles(); + for (std::size_t i = 0; i < particles.size(); ++i) { + app.drawSquare(particles[i].position.x, particles[i].position.y, 5); + } - SDL_RenderPresent(renderer); + app.presentScreen(); - + std::this_thread::sleep_for(std::chrono::milliseconds(17)); } - - SDL_DestroyRenderer(renderer); - SDL_DestroyWindow(window); - SDL_Quit(); - + app.cleanUp(); return 0; } -- cgit v1.2.3 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/CMakeLists.txt | 1 + src/crepe/Particle.cpp | 5 ++-- src/crepe/Particle.hpp | 5 ++-- src/crepe/ParticleEmitter.cpp | 63 ++----------------------------------------- src/crepe/ParticleEmitter.hpp | 11 ++------ src/crepe/ParticleSystem.cpp | 62 ++++++++++++++++++++++++++++++++++++++++++ src/crepe/ParticleSystem.hpp | 14 ++++++++++ src/crepe/main.cpp | 37 +++++++++++++++---------- 8 files changed, 109 insertions(+), 89 deletions(-) create mode 100644 src/crepe/ParticleSystem.cpp create mode 100644 src/crepe/ParticleSystem.hpp (limited to 'src/crepe/CMakeLists.txt') diff --git a/src/crepe/CMakeLists.txt b/src/crepe/CMakeLists.txt index 933f907..43a3835 100644 --- a/src/crepe/CMakeLists.txt +++ b/src/crepe/CMakeLists.txt @@ -2,6 +2,7 @@ target_sources(main PUBLIC main.cpp Particle.cpp ParticleEmitter.cpp + ParticleSystem.cpp SDLApp.cpp ) diff --git a/src/crepe/Particle.cpp b/src/crepe/Particle.cpp index 90957db..f6bbd34 100644 --- a/src/crepe/Particle.cpp +++ b/src/crepe/Particle.cpp @@ -1,12 +1,13 @@ #include "Particle.hpp" + Particle::Particle(float lifespan, Position position, Position velocity) : lifespan(lifespan), position(position), velocity(velocity), timeInLife(0.0f) {} void Particle::update(float deltaTime) { timeInLife += deltaTime; - position.x += static_cast(velocity.x * deltaTime); - position.y += static_cast(velocity.y * deltaTime); + position.x += velocity.x * deltaTime; + position.y += velocity.y * deltaTime; } bool Particle::isAlive() const { diff --git a/src/crepe/Particle.hpp b/src/crepe/Particle.hpp index f8d2770..f71fd67 100644 --- a/src/crepe/Particle.hpp +++ b/src/crepe/Particle.hpp @@ -3,8 +3,8 @@ class Particle { public: struct Position { - int x; - int y; + float x; + float y; }; Position position; @@ -14,6 +14,5 @@ public: Particle(float lifespan, Position position, Position velocity); void update(float deltaTime); bool isAlive() const; -private: float timeInLife; }; diff --git a/src/crepe/ParticleEmitter.cpp b/src/crepe/ParticleEmitter.cpp index f575e38..69f5ace 100644 --- a/src/crepe/ParticleEmitter.cpp +++ b/src/crepe/ParticleEmitter.cpp @@ -1,70 +1,11 @@ #include "ParticleEmitter.hpp" -#include -#include #include -#include // include iostream for std::cout 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_elapsedTime(0.0f), m_speed(speed), m_speedOffset(speedOffset), m_position{0, 0} { + : m_maxParticles(maxParticles), m_emissionRate(emissionRate), m_speed(speed), m_speedOffset(speedOffset), m_position{0, 0} { std::srand(static_cast(std::time(nullptr))); // initialize random seed m_minAngle = (360 + angle - (angleOffset % 360)) % 360; // calculate minAngle m_maxAngle = (360 + angle + (angleOffset % 360)) % 360; // calculate maxAngle - std::cout << "Initialized ParticleEmitter with:" << std::endl; - std::cout << "Min Angle: " << m_minAngle << " Max Angle: " << m_maxAngle << std::endl; -} - -void ParticleEmitter::update(float deltaTime) { // keep deltaTime as float - m_elapsedTime += deltaTime; - - while (m_elapsedTime >= (1.0f / m_emissionRate) && particles.size() < m_maxParticles) { - m_elapsedTime -= (1.0f / m_emissionRate); - emitParticle(); - } - - std::vector::iterator it = particles.begin(); - while (it != particles.end()) { - it->update(deltaTime); // update particle - if (!it->isAlive()) { - it = particles.erase(it); // remove dead particle - } else { - ++it; // move to next particle - } - } -} - -void ParticleEmitter::emitParticle() { - Particle::Position initialPosition = { m_position.x, m_position.y }; // starting position of the particle - - // generate a random angle within the range [minAngle, maxAngle] - float randomAngle = m_minAngle + (std::rand() % (static_cast(m_maxAngle - m_minAngle + 1))); // random angle within the specified range - - // convert angle to radians for velocity calculation - float angleInRadians = randomAngle * (M_PI / 180.0f); - - // calculate velocity components based on the final angle - float randomSpeedOffset = (static_cast(std::rand()) / RAND_MAX) * (2 * m_speedOffset) - m_speedOffset; // random speed offset - float velocityX = (m_speed + randomSpeedOffset) * std::cos(angleInRadians); // x component of velocity - float velocityY = (m_speed + randomSpeedOffset) * std::sin(angleInRadians); // y component of velocity - - Particle::Position initialVelocity = { - static_cast(velocityX), // set x velocity - static_cast(velocityY) // set y velocity - }; - - std::cout << "Emitting particle with:" << std::endl; - std::cout << "Random Angle: " << randomAngle << " (Radians: " << angleInRadians << ")" << std::endl; - std::cout << "Velocity X: " << velocityX << " Velocity Y: " << velocityY << std::endl; - - particles.emplace_back(2.0f, initialPosition, initialVelocity); // create and store the new particle -} - -const std::vector& ParticleEmitter::getParticles() const { - return particles; // return the collection of particles -} - -void ParticleEmitter::setPosition(int x, int y) { - m_position.x = x; // set x position of the emitter - m_position.y = y; // set y position of the emitter -} +} \ No newline at end of file diff --git a/src/crepe/ParticleEmitter.hpp b/src/crepe/ParticleEmitter.hpp index 9b3f2ad..6826531 100644 --- a/src/crepe/ParticleEmitter.hpp +++ b/src/crepe/ParticleEmitter.hpp @@ -6,21 +6,14 @@ class ParticleEmitter { public: ParticleEmitter(unsigned int maxParticles, unsigned int emissionRate, unsigned int speed, unsigned int speedOffset, unsigned int angle, unsigned int angleOffset); - void update(float deltaTime); // Keep deltaTime as float - const std::vector& getParticles() const; //returns the collection of particles - void setPosition(int x, int y); //sets the position of the emitter -private: - void emitParticle(); //emits a new particle - struct Position { //struct to hold position - int x; - int y; + float x; + float y; }; Position m_position; //position of the emitter unsigned int m_maxParticles; //maximum number of particles unsigned int m_emissionRate; //rate of particle emission - float m_elapsedTime; //elapsed time since the last 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 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); +} diff --git a/src/crepe/ParticleSystem.hpp b/src/crepe/ParticleSystem.hpp new file mode 100644 index 0000000..f670415 --- /dev/null +++ b/src/crepe/ParticleSystem.hpp @@ -0,0 +1,14 @@ +#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/main.cpp b/src/crepe/main.cpp index fc307cc..40eb04f 100644 --- a/src/crepe/main.cpp +++ b/src/crepe/main.cpp @@ -3,6 +3,8 @@ #include #include "SDLApp.hpp" #include "ParticleEmitter.hpp" +#include "ParticleSystem.hpp" +#include "Particle.hpp" const int WINDOW_WIDTH = 800; const int WINDOW_HEIGHT = 600; @@ -15,37 +17,44 @@ int main(int argc, char* argv[]) { return 1; } + 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 = 180; //base angle of particle emission - unsigned int angleOffset = 30; //random offset for particle angle + 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 = 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 emitter(maxParticles, emissionRate, speed, speedOffset, angle, angleOffset); + ParticleEmitter emitter2(maxParticles, emissionRate, speed, speedOffset, angle - 90, angleOffset); // Another emitter + emitter2.m_position = {200, 150}; // set the position of the second emitter - emitter.setPosition(400, 300); + std::vector emitters = { emitter2 }; // array of emitters float deltaTime = 0.1f; bool running = true; while (running) { app.handleEvents(running); - emitter.update(deltaTime); + + particleSystem.update(deltaTime, emitters); // update particle system with delta time and emitters app.clearScreen(); - //render particles using the drawSquare method - const std::vector& particles = emitter.getParticles(); - for (std::size_t i = 0; i < particles.size(); ++i) { - app.drawSquare(particles[i].position.x, particles[i].position.y, 5); + // 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 + } } app.presentScreen(); - std::this_thread::sleep_for(std::chrono::milliseconds(17)); + std::this_thread::sleep_for(std::chrono::milliseconds(20)); // simulate ~50 FPS } app.cleanUp(); -- cgit v1.2.3 From 163c9e3eea437daa8ef6007fbdf2f91470066cbf Mon Sep 17 00:00:00 2001 From: jaroWMR Date: Thu, 10 Oct 2024 07:49:38 +0200 Subject: fix for build --- src/CMakeLists.txt | 2 -- src/crepe/CMakeLists.txt | 1 - src/example/CMakeLists.txt | 6 ++---- 3 files changed, 2 insertions(+), 7 deletions(-) (limited to 'src/crepe/CMakeLists.txt') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f6afdc0..446433c 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -36,5 +36,3 @@ target_link_libraries(test_main PRIVATE gtest_main PUBLIC crepe ) - -add_subdirectory(crepe) diff --git a/src/crepe/CMakeLists.txt b/src/crepe/CMakeLists.txt index a02e991..9c65e1e 100644 --- a/src/crepe/CMakeLists.txt +++ b/src/crepe/CMakeLists.txt @@ -3,7 +3,6 @@ target_sources(crepe PUBLIC Asset.cpp Sound.cpp SoundContext.cpp - main.cpp Particle.cpp ParticleEmitter.cpp ParticleSystem.cpp diff --git a/src/example/CMakeLists.txt b/src/example/CMakeLists.txt index f66e08a..36ceba1 100644 --- a/src/example/CMakeLists.txt +++ b/src/example/CMakeLists.txt @@ -15,7 +15,5 @@ endfunction() add_example(audio_internal) add_example(components_internal) add_example(script) - -add_executable(particel EXCLUDE_FROM_ALL particel.cpp) -target_link_libraries(particel PUBLIC crepe) - +add_example(particel) +target_link_libraries(particel PUBLIC SDL2) -- 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/CMakeLists.txt') 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 1f82ffa4d3ee8355215d43bf43edf8cecaca0d1d Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 16 Oct 2024 17:21:04 +0200 Subject: fix user script implementation --- src/crepe/CMakeLists.txt | 1 - src/crepe/ComponentManager.h | 2 +- src/crepe/ComponentManager.hpp | 5 +++-- src/crepe/GameObject.h | 2 +- src/crepe/GameObject.hpp | 4 ++-- src/crepe/Script.cpp | 7 ------- src/crepe/Script.h | 16 ---------------- src/crepe/ScriptSystem.cpp | 27 ++++++++++++++++++++++----- src/crepe/ScriptSystem.h | 12 +++++++++++- src/crepe/api/BehaviorScript.h | 24 ++++++++++++++++++------ src/crepe/api/BehaviorScript.hpp | 18 ++++++++++++++++++ src/crepe/api/CMakeLists.txt | 2 ++ src/crepe/api/Script.cpp | 5 +++++ src/crepe/api/Script.h | 21 +++++++++++++++++++++ src/example/script.cpp | 7 ++++--- 15 files changed, 108 insertions(+), 45 deletions(-) delete mode 100644 src/crepe/Script.cpp delete mode 100644 src/crepe/Script.h create mode 100644 src/crepe/api/BehaviorScript.hpp create mode 100644 src/crepe/api/Script.cpp create mode 100644 src/crepe/api/Script.h (limited to 'src/crepe/CMakeLists.txt') diff --git a/src/crepe/CMakeLists.txt b/src/crepe/CMakeLists.txt index d85aef0..8323490 100644 --- a/src/crepe/CMakeLists.txt +++ b/src/crepe/CMakeLists.txt @@ -9,7 +9,6 @@ target_sources(crepe PUBLIC Rigidbody.cpp Sprite.cpp ScriptSystem.cpp - Script.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES diff --git a/src/crepe/ComponentManager.h b/src/crepe/ComponentManager.h index eab9b45..38f32e4 100644 --- a/src/crepe/ComponentManager.h +++ b/src/crepe/ComponentManager.h @@ -23,7 +23,7 @@ public: public: //! Add a component of a specific type template - void add_component(uint32_t id, Args &&... args); + T & add_component(uint32_t id, Args &&... args); //! Deletes all components of a specific type and id template void delete_components_by_id(uint32_t id); diff --git a/src/crepe/ComponentManager.hpp b/src/crepe/ComponentManager.hpp index c872594..e0242a2 100644 --- a/src/crepe/ComponentManager.hpp +++ b/src/crepe/ComponentManager.hpp @@ -3,12 +3,11 @@ #include #include "ComponentManager.h" -#include "api/BehaviorScript.h" namespace crepe { template -void ComponentManager::add_component(uint32_t id, Args &&... args) { +T & ComponentManager::add_component(uint32_t id, Args &&... args) { using namespace std; static_assert(is_base_of::value, "add_component must recieve a derivative class of Component"); @@ -33,6 +32,8 @@ void ComponentManager::add_component(uint32_t id, Args &&... args) { T * instance = new T(forward(args)...); // store its unique_ptr in the vector<> components[type][id].push_back(unique_ptr(instance)); + + return *instance; } template diff --git a/src/crepe/GameObject.h b/src/crepe/GameObject.h index 3588d9a..b5d6399 100644 --- a/src/crepe/GameObject.h +++ b/src/crepe/GameObject.h @@ -10,7 +10,7 @@ public: GameObject(uint32_t id, std::string name, std::string tag, int layer); template - void add_component(Args &&... args); + T & add_component(Args &&... args); uint32_t id; std::string name; diff --git a/src/crepe/GameObject.hpp b/src/crepe/GameObject.hpp index 5966fbf..8cd1abe 100644 --- a/src/crepe/GameObject.hpp +++ b/src/crepe/GameObject.hpp @@ -7,9 +7,9 @@ namespace crepe { template -void GameObject::add_component(Args &&... args) { +T & GameObject::add_component(Args &&... args) { auto & mgr = ComponentManager::get_instance(); - mgr.add_component(id, std::forward(args)...); + return mgr.add_component(id, std::forward(args)...); } } // namespace crepe diff --git a/src/crepe/Script.cpp b/src/crepe/Script.cpp deleted file mode 100644 index 42e3666..0000000 --- a/src/crepe/Script.cpp +++ /dev/null @@ -1,7 +0,0 @@ -#include "Script.h" - -using namespace crepe; - -void Script::init() { } -void Script::update() { } - diff --git a/src/crepe/Script.h b/src/crepe/Script.h deleted file mode 100644 index ba4073a..0000000 --- a/src/crepe/Script.h +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once - -namespace crepe { - -class Script { -protected: - virtual void init(); - virtual void update(); - // NOTE: additional *events* (like unity's OnDisable and OnEnable) should be - // implemented as member methods in derivative user script classes and - // registered in init(), otherwise this class will balloon in size with each - // added event. -}; - -} - diff --git a/src/crepe/ScriptSystem.cpp b/src/crepe/ScriptSystem.cpp index e40909e..0537c16 100644 --- a/src/crepe/ScriptSystem.cpp +++ b/src/crepe/ScriptSystem.cpp @@ -1,12 +1,16 @@ +#include +#include #include + +#include "ScriptSystem.h" #include "ComponentManager.h" #include "api/BehaviorScript.h" -#include "util/fmt.h" +#include "api/Script.h" #include "util/log.h" -#include "ScriptSystem.h" - +using namespace std; using namespace crepe; +using namespace crepe::api; ScriptSystem::ScriptSystem() { dbg_trace(); @@ -24,10 +28,23 @@ void ScriptSystem::update() { using namespace std; dbg_trace(); + forward_list