aboutsummaryrefslogtreecommitdiff
path: root/src/example
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-11-04 08:28:18 +0100
committerLoek Le Blansch <loek@pipeframe.xyz>2024-11-04 08:28:18 +0100
commit06f65659fc6ffde7cabd2135040cbfbf089e5a24 (patch)
treee3570bea52b87b6919550ee81d17927ccbc11cc5 /src/example
parent128969619a22dfc17a9ea35335c0d21c6ad0c954 (diff)
parent6aa8fdd04728b6a499f526de727514ae3d0490b4 (diff)
merge `origin/master` into `master`
Diffstat (limited to 'src/example')
-rw-r--r--src/example/CMakeLists.txt12
-rw-r--r--src/example/asset_manager.cpp39
-rw-r--r--src/example/audio_internal.cpp27
-rw-r--r--src/example/components_internal.cpp13
-rw-r--r--src/example/log.cpp29
-rw-r--r--src/example/particle.cpp102
-rw-r--r--src/example/physics.cpp30
-rw-r--r--src/example/rendering.cpp71
-rw-r--r--src/example/script.cpp41
9 files changed, 340 insertions, 24 deletions
diff --git a/src/example/CMakeLists.txt b/src/example/CMakeLists.txt
index 6df4ce7..fea6f60 100644
--- a/src/example/CMakeLists.txt
+++ b/src/example/CMakeLists.txt
@@ -1,3 +1,6 @@
+# all examples
+add_custom_target(examples)
+
# add_example(target_name [SOURCES...])
function(add_example target_name)
# if SOURCES is not specified
@@ -10,9 +13,14 @@ function(add_example target_name)
add_executable(${target_name} EXCLUDE_FROM_ALL ${sources})
target_link_libraries(${target_name} PUBLIC crepe)
+ add_dependencies(examples ${target_name})
endfunction()
add_example(audio_internal)
-add_example(components_internal)
+# add_example(components_internal)
add_example(script)
-
+add_example(log)
+add_example(rendering)
+add_example(asset_manager)
+add_example(particle)
+add_example(physics)
diff --git a/src/example/asset_manager.cpp b/src/example/asset_manager.cpp
new file mode 100644
index 0000000..7e15d1f
--- /dev/null
+++ b/src/example/asset_manager.cpp
@@ -0,0 +1,39 @@
+
+
+#include <crepe/Sound.h>
+#include <crepe/api/AssetManager.h>
+#include <crepe/api/Texture.h>
+
+using namespace crepe;
+using namespace crepe::api;
+int main() {
+
+ // this needs to be called before the asset manager otherwise the destructor
+ // of sdl is not in the right order
+ { Texture test("../asset/texture/img.png"); }
+ // FIXME: make it so the issue described by the above comment is not possible
+ // (i.e. the order in which internal classes are instantiated should not
+ // impact the way the engine works).
+
+ auto & mgr = AssetManager::get_instance();
+
+ {
+ // TODO: [design] the Sound class can't be directly included by the user as
+ // it includes SoLoud headers.
+ auto bgm = mgr.cache<Sound>("../mwe/audio/bgm.ogg");
+ auto sfx1 = mgr.cache<Sound>("../mwe/audio/sfx1.wav");
+ auto sfx2 = mgr.cache<Sound>("../mwe/audio/sfx2.wav");
+
+ auto img = mgr.cache<Texture>("../asset/texture/img.png");
+ auto img1 = mgr.cache<Texture>("../asset/texture/second.png");
+ }
+
+ {
+ auto bgm = mgr.cache<Sound>("../mwe/audio/bgm.ogg");
+ auto sfx1 = mgr.cache<Sound>("../mwe/audio/sfx1.wav");
+ auto sfx2 = mgr.cache<Sound>("../mwe/audio/sfx2.wav");
+
+ auto img = mgr.cache<Texture>("../asset/texture/img.png");
+ auto img1 = mgr.cache<Texture>("../asset/texture/second.png");
+ }
+}
diff --git a/src/example/audio_internal.cpp b/src/example/audio_internal.cpp
index 1199e2d..f35ad9d 100644
--- a/src/example/audio_internal.cpp
+++ b/src/example/audio_internal.cpp
@@ -5,26 +5,40 @@
#include <crepe/Sound.h>
#include <crepe/util/log.h>
+#include <crepe/api/Config.h>
-#include <chrono>
#include <thread>
using namespace crepe;
+using namespace crepe::api;
using namespace std;
using namespace std::chrono_literals;
using std::make_unique;
-int main() {
- dbg_trace();
+// Unrelated stuff that is not part of this POC
+int _ = [] () {
+ // Show dbg_trace() output
+ auto & cfg = api::Config::get_instance();
+ cfg.log.level = util::LogLevel::TRACE;
+
+ return 0; // satisfy compiler
+}();
+
+
+int main() {
+ // Load a background track (Ogg Vorbis)
auto bgm = Sound("../mwe/audio/bgm.ogg");
+ // Load three short samples (WAV)
auto sfx1 = Sound("../mwe/audio/sfx1.wav");
auto sfx2 = Sound("../mwe/audio/sfx2.wav");
auto sfx3 = Sound("../mwe/audio/sfx3.wav");
+ // Start the background track
bgm.play();
- // play each sample sequentially
+ // Play each sample sequentially while pausing and resuming the background
+ // track
this_thread::sleep_for(500ms);
sfx1.play();
this_thread::sleep_for(500ms);
@@ -35,11 +49,12 @@ int main() {
bgm.play();
this_thread::sleep_for(500ms);
- // play all samples simultaniously
+ // Play all samples simultaniously
sfx1.play();
sfx2.play();
sfx3.play();
this_thread::sleep_for(1000ms);
- return 0;
+ // Stop all audio and exit
+ return EXIT_SUCCESS;
}
diff --git a/src/example/components_internal.cpp b/src/example/components_internal.cpp
index 54ce295..3c68974 100644
--- a/src/example/components_internal.cpp
+++ b/src/example/components_internal.cpp
@@ -6,14 +6,16 @@
#include <cassert>
#include <chrono>
-#include <crepe/Collider.h>
#include <crepe/Component.h>
#include <crepe/ComponentManager.h>
-#include <crepe/GameObject.h>
-#include <crepe/Rigidbody.h>
-#include <crepe/Sprite.h>
+
+#include <crepe/api/GameObject.h>
+#include <crepe/api/Rigidbody.h>
+#include <crepe/api/Sprite.h>
+
#include <crepe/util/log.h>
+using namespace crepe::api;
using namespace crepe;
using namespace std;
@@ -33,14 +35,13 @@ int main() {
game_object[i]->add_component<Sprite>("test");
game_object[i]->add_component<Rigidbody>(0, 0, i);
- game_object[i]->add_component<Collider>(i);
}
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");
+ assert(true);
}
auto stop_looping = chrono::high_resolution_clock::now();
diff --git a/src/example/log.cpp b/src/example/log.cpp
new file mode 100644
index 0000000..04ab9cd
--- /dev/null
+++ b/src/example/log.cpp
@@ -0,0 +1,29 @@
+/** \file
+ *
+ * Standalone example for usage of the logging functions
+ */
+
+#include <crepe/api/Config.h>
+#include <crepe/util/log.h>
+
+using namespace crepe;
+using namespace crepe::util;
+
+// unrelated setup code
+int _ = [] () {
+ // make sure all log messages get printed
+ auto & cfg = api::Config::get_instance();
+ cfg.log.level = util::LogLevel::TRACE;
+
+ return 0; // satisfy compiler
+} ();
+
+int main() {
+ dbg_trace();
+ dbg_logf("test printf parameters: %d", 3);
+ logf(LogLevel::INFO, "info message");
+ logf(LogLevel::WARNING, "warning");
+ logf(LogLevel::ERROR, "error");
+
+ return 0;
+}
diff --git a/src/example/particle.cpp b/src/example/particle.cpp
new file mode 100644
index 0000000..53fa213
--- /dev/null
+++ b/src/example/particle.cpp
@@ -0,0 +1,102 @@
+#include "Particle.h"
+#include "ParticleSystem.h"
+#include "SDLApp.h"
+#include "api/ParticleEmitter.h"
+#include <chrono>
+#include <crepe/Component.h>
+#include <crepe/ComponentManager.h>
+#include <crepe/api/GameObject.h>
+#include <iostream>
+#include <thread>
+
+using namespace crepe::api;
+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);
+
+ // FIXME: all systems are singletons, so this shouldn't even compile.
+ 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
+ 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.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;
+}
diff --git a/src/example/physics.cpp b/src/example/physics.cpp
new file mode 100644
index 0000000..db69b9b
--- /dev/null
+++ b/src/example/physics.cpp
@@ -0,0 +1,30 @@
+#include "PhysicsSystem.h"
+#include <chrono>
+#include <crepe/Component.h>
+#include <crepe/ComponentManager.h>
+#include <crepe/api/Force.h>
+#include <crepe/api/GameObject.h>
+#include <crepe/api/Rigidbody.h>
+#include <crepe/api/Transform.h>
+#include <iostream>
+#include <thread>
+
+using namespace crepe::api;
+using namespace crepe;
+using namespace std;
+
+int main(int argc, char * argv[]) {
+ PhysicsSystem physics_system;
+ 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);
+ Point point = {
+ .x = 0,
+ .y = 0,
+ };
+ game_object[0]->add_component<Transform>(point, 0, 0);
+ game_object[0]->add_component<Rigidbody>(1, 1, BodyType::DYNAMIC);
+ game_object[0]->add_component<Force>(1, 0);
+ physics_system.update();
+ return 0;
+}
diff --git a/src/example/rendering.cpp b/src/example/rendering.cpp
new file mode 100644
index 0000000..1bf448c
--- /dev/null
+++ b/src/example/rendering.cpp
@@ -0,0 +1,71 @@
+
+
+#include <crepe/ComponentManager.h>
+#include <crepe/RenderSystem.h>
+#include <crepe/api/GameObject.h>
+#include <crepe/util/log.h>
+
+#include <crepe/api/AssetManager.h>
+#include <crepe/api/Color.h>
+#include <crepe/api/Point.h>
+#include <crepe/api/Sprite.h>
+#include <crepe/api/Texture.h>
+#include <crepe/api/Transform.h>
+
+#include <chrono>
+#include <memory>
+
+using namespace std;
+using namespace crepe;
+using namespace crepe::api;
+
+int main() {
+
+ dbg_trace();
+
+ auto obj = GameObject(0, "name", "tag", 0);
+ auto obj1 = GameObject(1, "name", "tag", 0);
+ auto obj2 = GameObject(2, "name", "tag", 0);
+
+ auto & mgr = AssetManager::get_instance();
+ // Normal adding components
+ {
+ Color color(0, 0, 0, 0);
+ Point point = {
+ .x = 0,
+ .y = 0,
+ };
+ obj.add_component<Transform>(point, 1, 1);
+ obj.add_component<Sprite>(
+ make_shared<Texture>("../asset/texture/img.png"), color,
+ FlipSettings{true, true});
+ }
+
+ {
+ Color color(0, 0, 0, 0);
+ Point point = {
+ .x = 500,
+ .y = 0,
+ };
+ obj1.add_component<Transform>(point, 0, 0.1);
+ auto img = mgr.cache<Texture>("../asset/texture/second.png");
+ obj1.add_component<Sprite>(img, color, FlipSettings{true, true});
+ }
+
+ {
+ Color color(0, 0, 0, 0);
+ Point point = {
+ .x = 800,
+ .y = 0,
+ };
+ //obj.add_component<Transform>(point, 0, 0.1);
+ auto img = mgr.cache<Texture>("../asset/texture/second.png");
+ obj2.add_component<Sprite>(img, color, FlipSettings{true, true});
+ }
+
+ auto & sys = crepe::RenderSystem::get_instance();
+ auto start = std::chrono::steady_clock::now();
+ while (std::chrono::steady_clock::now() - start < std::chrono::seconds(5)) {
+ sys.update();
+ }
+}
diff --git a/src/example/script.cpp b/src/example/script.cpp
index 28605c7..cda9591 100644
--- a/src/example/script.cpp
+++ b/src/example/script.cpp
@@ -3,31 +3,52 @@
* Standalone example for usage of the script component and system
*/
-#include <crepe/util/log.h>
-#include <crepe/ScriptSystem.h>
#include <crepe/ComponentManager.h>
-#include <crepe/GameObject.h>
+#include <crepe/ScriptSystem.h>
+#include <crepe/util/log.h>
#include <crepe/api/BehaviorScript.h>
+#include <crepe/api/Config.h>
+#include <crepe/api/GameObject.h>
+#include <crepe/api/Script.h>
+#include <crepe/api/Transform.h>
using namespace crepe;
+using namespace crepe::api;
using namespace std;
-class MyScript : public api::BehaviorScript {
+// Unrelated stuff that is not part of this POC
+int _ = [] () {
+ // Show dbg_trace() output
+ auto & cfg = api::Config::get_instance();
+ cfg.log.level = util::LogLevel::TRACE;
+
+ return 0; // satisfy compiler
+}();
+
+
+
+// User-defined script:
+class MyScript : public Script {
void update() {
- dbg_trace();
+ // Retrieve component from the same GameObject this script is on
+ Transform & test = get_component<Transform>();
+ dbg_logf("Transform(%.2f, %.2f)", test.position.x, test.position.y);
}
};
int main() {
- dbg_trace();
-
+ // Create game object with Transform and BehaviorScript components
auto obj = GameObject(0, "name", "tag", 0);
- obj.add_component<MyScript>();
+ obj.add_component<Transform>(Point { .x = 1.2, .y = 3.4, }, 0, 0);
+ obj.add_component<BehaviorScript>().set_script<MyScript>();
+ // Get ScriptSystem singleton instance (this would normally be done from the
+ // game loop)
auto & sys = ScriptSystem::get_instance();
- sys.update(); // -> MyScript::update
+ // Update all scripts. This should result in MyScript::update being called
+ sys.update();
- return 0;
+ return EXIT_SUCCESS;
}