aboutsummaryrefslogtreecommitdiff
path: root/src/example
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-10-24 10:52:21 +0200
committerLoek Le Blansch <loek@pipeframe.xyz>2024-10-24 10:52:21 +0200
commit39054829fa69bcfa2b468015dc3852a2f8deac9f (patch)
treee7726d90fb3c23a15919360dd10bdf709e7e087a /src/example
parentdd9940720cde6975f79d65e08075687c47f0aec6 (diff)
parent5447ddd896eb49ea9fd9f9191a277fd1d5730aa3 (diff)
merge master into loek/config
Diffstat (limited to 'src/example')
-rw-r--r--src/example/CMakeLists.txt11
-rw-r--r--src/example/asset_manager.cpp39
-rw-r--r--src/example/components_internal.cpp13
-rw-r--r--src/example/log.cpp8
-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.cpp4
8 files changed, 264 insertions, 14 deletions
diff --git a/src/example/CMakeLists.txt b/src/example/CMakeLists.txt
index 4ef1cd1..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,10 +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/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
index 2d693d1..86ef193 100644
--- a/src/example/log.cpp
+++ b/src/example/log.cpp
@@ -12,13 +12,13 @@ using namespace crepe::util;
int main() {
auto & cfg = api::Config::get_instance();
// make sure all log messages get printed
- cfg.log.level = util::log_level::TRACE;
+ cfg.log.level = util::LogLevel::TRACE;
dbg_trace();
dbg_logf("cfg.log.color is equal to %d", cfg.log.color);
- logf(log_level::INFO, "info message!");
- logf(log_level::WARNING, "very scary warning");
- logf(log_level::ERROR, "fatal error!!!");
+ logf(LogLevel::INFO, "info message!");
+ logf(LogLevel::WARNING, "very scary warning");
+ logf(LogLevel::ERROR, "fatal 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 97e99b3..f737a90 100644
--- a/src/example/script.cpp
+++ b/src/example/script.cpp
@@ -4,12 +4,12 @@
*/
#include <crepe/ComponentManager.h>
-#include <crepe/GameObject.h>
#include <crepe/ScriptSystem.h>
#include <crepe/util/log.h>
#include <crepe/api/Config.h>
#include <crepe/api/BehaviorScript.h>
+#include <crepe/api/GameObject.h>
#include <crepe/api/Script.h>
using namespace crepe;
@@ -22,7 +22,7 @@ class MyScript : public Script {
int main() {
auto & cfg = api::Config::get_instance();
- cfg.log.level = util::log_level::TRACE;
+ cfg.log.level = util::LogLevel::TRACE;
auto obj = GameObject(0, "name", "tag", 0);
obj.add_component<BehaviorScript>().set_script<MyScript>();