aboutsummaryrefslogtreecommitdiff
path: root/src/example
diff options
context:
space:
mode:
Diffstat (limited to 'src/example')
-rw-r--r--src/example/CMakeLists.txt5
-rw-r--r--src/example/Physics.cpp25
-rw-r--r--src/example/components_internal.cpp4
-rw-r--r--src/example/particle.cpp102
4 files changed, 133 insertions, 3 deletions
diff --git a/src/example/CMakeLists.txt b/src/example/CMakeLists.txt
index 7c87afc..368aa2e 100644
--- a/src/example/CMakeLists.txt
+++ b/src/example/CMakeLists.txt
@@ -21,4 +21,7 @@ add_example(components_internal)
add_example(script)
add_example(rendering)
add_example(asset_manager)
-
+add_example(particle)
+add_example(Physics)
+add_example(particle)
+add_example(Physics)
diff --git a/src/example/Physics.cpp b/src/example/Physics.cpp
new file mode 100644
index 0000000..9ad6da0
--- /dev/null
+++ b/src/example/Physics.cpp
@@ -0,0 +1,25 @@
+#include <iostream>
+#include <thread>
+#include <chrono>
+#include "Transform.h"
+#include "Rigidbody.h"
+#include "Force.h"
+#include "PhysicsSystem.h"
+#include <crepe/Component.h>
+#include <crepe/ComponentManager.h>
+#include <crepe/GameObject.h>
+
+using namespace crepe;
+using namespace std;
+
+
+int main(int argc, char* argv[]) {
+ PhysicsSystem physicsSystem;
+ GameObject * game_object[2];
+ game_object[1] = new GameObject(2, "Name", "Tag", 0); // not found not used
+ game_object[0] = new GameObject(5, "Name", "Tag", 0);
+ game_object[0]->add_component<Rigidbody>(1, 1 , BodyType::Dynamic);
+ game_object[0]->add_component<Force>(1 , 0);
+ physicsSystem.update();
+ return 0;
+}
diff --git a/src/example/components_internal.cpp b/src/example/components_internal.cpp
index e2169e1..ae6c765 100644
--- a/src/example/components_internal.cpp
+++ b/src/example/components_internal.cpp
@@ -42,8 +42,8 @@ int main() {
auto stop_adding = chrono::high_resolution_clock::now();
auto sprites = mgr.get_components_by_type<Sprite>();
- 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
new file mode 100644
index 0000000..df1dbfd
--- /dev/null
+++ b/src/example/particle.cpp
@@ -0,0 +1,102 @@
+#include <iostream>
+#include <thread>
+#include <chrono>
+#include "SDLApp.h"
+#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;
+
+int main(int argc, char* argv[]) {
+ SDLApp app(WINDOW_WIDTH, WINDOW_HEIGHT);
+
+ 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();
+
+
+ // POC CODE
+ particleSystem.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;
+}