aboutsummaryrefslogtreecommitdiff
path: root/src/example
diff options
context:
space:
mode:
authorjaroWMR <jarorutjes07@gmail.com>2024-10-09 19:58:30 +0200
committerjaroWMR <jarorutjes07@gmail.com>2024-10-09 19:58:30 +0200
commit2884c56b066fcafb4f0042799f8a16ea3b238d2a (patch)
tree9c30844807e8ab5f48a17773d19e5b565cb1a59d /src/example
parent5f84969c851530ebc430be2cf8e99c945ff7a4a7 (diff)
parent08d0b07c19edfe8c81dfe4e21c0d4c1ef128b628 (diff)
merge with master
Diffstat (limited to 'src/example')
-rw-r--r--src/example/CMakeLists.txt6
-rw-r--r--src/example/audio_internal.cpp45
-rw-r--r--src/example/particel.cpp82
3 files changed, 133 insertions, 0 deletions
diff --git a/src/example/CMakeLists.txt b/src/example/CMakeLists.txt
new file mode 100644
index 0000000..17e995b
--- /dev/null
+++ b/src/example/CMakeLists.txt
@@ -0,0 +1,6 @@
+add_executable(audio_internal EXCLUDE_FROM_ALL audio_internal.cpp)
+target_link_libraries(audio_internal PUBLIC crepe)
+
+add_executable(particel EXCLUDE_FROM_ALL particel.cpp)
+target_link_libraries(particel PUBLIC crepe)
+
diff --git a/src/example/audio_internal.cpp b/src/example/audio_internal.cpp
new file mode 100644
index 0000000..1199e2d
--- /dev/null
+++ b/src/example/audio_internal.cpp
@@ -0,0 +1,45 @@
+/** \file
+ *
+ * Standalone example for usage of the internal \c Sound class.
+ */
+
+#include <crepe/Sound.h>
+#include <crepe/util/log.h>
+
+#include <chrono>
+#include <thread>
+
+using namespace crepe;
+using namespace std;
+using namespace std::chrono_literals;
+using std::make_unique;
+
+int main() {
+ dbg_trace();
+
+ auto bgm = Sound("../mwe/audio/bgm.ogg");
+ auto sfx1 = Sound("../mwe/audio/sfx1.wav");
+ auto sfx2 = Sound("../mwe/audio/sfx2.wav");
+ auto sfx3 = Sound("../mwe/audio/sfx3.wav");
+
+ bgm.play();
+
+ // play each sample sequentially
+ this_thread::sleep_for(500ms);
+ sfx1.play();
+ this_thread::sleep_for(500ms);
+ sfx2.play();
+ bgm.pause();
+ this_thread::sleep_for(500ms);
+ sfx3.play();
+ bgm.play();
+ this_thread::sleep_for(500ms);
+
+ // play all samples simultaniously
+ sfx1.play();
+ sfx2.play();
+ sfx3.play();
+ this_thread::sleep_for(1000ms);
+
+ return 0;
+}
diff --git a/src/example/particel.cpp b/src/example/particel.cpp
new file mode 100644
index 0000000..58480a9
--- /dev/null
+++ b/src/example/particel.cpp
@@ -0,0 +1,82 @@
+#include <iostream>
+#include <thread>
+#include <chrono>
+#include "SDLApp.hpp"
+#include "ParticleEmitter.hpp"
+#include "ParticleSystem.hpp"
+#include "Particle.hpp"
+#include <chrono>
+
+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<ParticleEmitter> 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<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;
+ 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<float, std::milli> 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;
+}