aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorjaroWMR <jarorutjes07@gmail.com>2024-10-10 09:30:49 +0200
committerjaroWMR <jarorutjes07@gmail.com>2024-10-10 09:30:49 +0200
commit016070d47f2fff8cfdd49cc0bdf65c14ef922c94 (patch)
tree398daf00fcba5e4edc7dfc5807d55f940af4a58c /src
parentd001c4ba95a72f13c942f1a24eb98fe1584d93a4 (diff)
renamed particel to particle and example uses ecs for poc
Diffstat (limited to 'src')
-rw-r--r--src/crepe/ParticleEmitter.cpp2
-rw-r--r--src/crepe/ParticleSystem.cpp27
-rw-r--r--src/crepe/ParticleSystem.h2
-rw-r--r--src/example/CMakeLists.txt4
-rw-r--r--src/example/particle.cpp (renamed from src/example/particel.cpp)47
5 files changed, 38 insertions, 44 deletions
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 <iostream> // 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<ParticleEmitter>& emitters) {
- // std::cout << "ParticleSystem update" << std::endl;
+void ParticleSystem::update(float deltaTime, std::vector<std::reference_wrapper<ParticleEmitter>>& emitters) {
for (ParticleEmitter& emitter : emitters) {
float updateAmount = 1/static_cast<float>(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<uint32_t>(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<uint32_t>(emitter.m_maxAngle - emitter.m_minAngle + 1)));
}
-
-
float angleInRadians = randomAngle * (M_PI / 180.0f);
float randomSpeedOffset = (static_cast<float>(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<ParticleEmitter>& emitters);
+ void update(float deltaTime, std::vector<std::reference_wrapper<ParticleEmitter>>& 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/particle.cpp
index c66bbef..fc56230 100644
--- a/src/example/particel.cpp
+++ b/src/example/particle.cpp
@@ -5,9 +5,13 @@
#include "ParticleEmitter.h"
#include "ParticleSystem.h"
#include "Particle.h"
+#include <crepe/Component.h>
+#include <crepe/ComponentManager.h>
+#include <crepe/GameObject.h>
#include <chrono>
using namespace crepe;
+using namespace std;
const int WINDOW_WIDTH = 800;
const int WINDOW_HEIGHT = 600;
@@ -16,10 +20,15 @@ int main(int argc, char* argv[]) {
SDLApp app(WINDOW_WIDTH, WINDOW_HEIGHT);
if (!app.initialize()) {
- std::cerr << "Failed to initialize SDLApp." << std::endl;
+ 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
@@ -32,36 +41,40 @@ int main(int argc, char* argv[]) {
float endLifespan = 2.0f; // ending lifespan of particles
// Vector to hold all the emitters
- std::vector<ParticleEmitter> emitters;
+ // vector<ParticleEmitter> emitters;
+ game_object[0]->add_component<ParticleEmitter>(maxParticles, emissionRate, speed, speedOffset, angle, angleOffset, beginLifespan, endLifespan);
+
+ std::vector<std::reference_wrapper<ParticleEmitter>> emitters = mgr.get_components_by_type<ParticleEmitter>();
+
// Loop to create 1000 emitters
- for (unsigned int i = 0; i < 1000; ++i) {
- ParticleEmitter emitter(maxParticles, emissionRate, speed, speedOffset, angle, angleOffset, beginLifespan, endLifespan);
+ // 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
+ // // 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
- }
+ // emitters.push_back(emitter); // Add the emitter to the vector
+ // }
float deltaTime = 0.1f;
bool running = true;
- std::cout << "start loop " << std::endl;
+ cout << "start loop " << endl;
while (running) {
app.handleEvents(running);
// Start timing
- auto start = std::chrono::high_resolution_clock::now();
+ auto start = 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<float, std::milli> duration = end - start; // get duration in milliseconds
+ auto end = chrono::high_resolution_clock::now();
+ chrono::duration<float, milli> duration = end - start; // get duration in milliseconds
- std::cout << "Update took " << duration.count() << " ms" << std::endl;
+ cout << "Update took " << duration.count() << " ms" << endl;
app.clearScreen();
- start = std::chrono::high_resolution_clock::now();
+ 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) {
@@ -71,12 +84,12 @@ int main(int argc, char* argv[]) {
app.presentScreen();
- end = std::chrono::high_resolution_clock::now();
+ end = chrono::high_resolution_clock::now();
duration = end - start; // get duration in milliseconds
- std::cout << "screen took " << duration.count() << " ms" << std::endl;
+ cout << "screen took " << duration.count() << " ms" << endl;
- std::this_thread::sleep_for(std::chrono::milliseconds(20)); // simulate ~50 FPS
+ this_thread::sleep_for(chrono::milliseconds(20)); // simulate ~50 FPS
}
app.cleanUp();