aboutsummaryrefslogtreecommitdiff
path: root/src/example
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-11-14 11:26:12 +0100
committerLoek Le Blansch <loek@pipeframe.xyz>2024-11-14 11:26:12 +0100
commit01c09a196c3f3e5cefaa4119a95a1cdeb7b9c263 (patch)
tree5667369a85bd06b683c67de42bf0311c2647912b /src/example
parent6e13510f3c6d4155707f748d237bb1fa05243450 (diff)
parent8600b8a29351aae26ec7b22f84aeeef92d8cb421 (diff)
merge `loek/cleanup` into `loek/audio`
Diffstat (limited to 'src/example')
-rw-r--r--src/example/audio_internal.cpp4
-rw-r--r--src/example/components_internal.cpp15
-rw-r--r--src/example/db.cpp8
-rw-r--r--src/example/ecs.cpp17
-rw-r--r--src/example/log.cpp12
-rw-r--r--src/example/physics.cpp8
-rw-r--r--src/example/proxy.cpp9
-rw-r--r--src/example/rendering.cpp12
-rw-r--r--src/example/savemgr.cpp16
-rw-r--r--src/example/scene_manager.cpp35
-rw-r--r--src/example/script.cpp16
11 files changed, 82 insertions, 70 deletions
diff --git a/src/example/audio_internal.cpp b/src/example/audio_internal.cpp
index 1ea839d..ff55a59 100644
--- a/src/example/audio_internal.cpp
+++ b/src/example/audio_internal.cpp
@@ -5,7 +5,7 @@
#include <crepe/api/Config.h>
#include <crepe/facade/Sound.h>
-#include <crepe/util/log.h>
+#include <crepe/util/Log.h>
#include <thread>
@@ -18,7 +18,7 @@ using std::make_unique;
int _ = []() {
// Show dbg_trace() output
auto & cfg = Config::get_instance();
- cfg.log.level = LogLevel::TRACE;
+ cfg.log.level = Log::Level::TRACE;
return 0; // satisfy compiler
}();
diff --git a/src/example/components_internal.cpp b/src/example/components_internal.cpp
index ea1eaad..eeecdc0 100644
--- a/src/example/components_internal.cpp
+++ b/src/example/components_internal.cpp
@@ -13,7 +13,7 @@
#include <crepe/api/Rigidbody.h>
#include <crepe/api/Sprite.h>
-#include <crepe/util/log.h>
+#include <crepe/util/Log.h>
using namespace crepe;
using namespace std;
@@ -23,17 +23,16 @@ using namespace std;
int main() {
dbg_trace();
- auto & mgr = ComponentManager::get_instance();
+ ComponentManager mgr{};
auto start_adding = chrono::high_resolution_clock::now();
GameObject * game_object[OBJ_COUNT];
for (int i = 0; i < OBJ_COUNT; ++i) {
- game_object[i] = new GameObject(i, "Name", "Tag", 0);
-
- game_object[i]->add_component<Sprite>("test");
- game_object[i]->add_component<Rigidbody>(0, 0, i);
+ GameObject obj = mgr.new_object("Name", "Tag");
+ obj.add_component<Sprite>("test");
+ obj.add_component<Rigidbody>(0, 0, i);
}
auto stop_adding = chrono::high_resolution_clock::now();
@@ -45,10 +44,6 @@ int main() {
auto stop_looping = chrono::high_resolution_clock::now();
- for (int i = 0; i < OBJ_COUNT; ++i) {
- delete game_object[i];
- }
-
auto add_time = chrono::duration_cast<chrono::microseconds>(stop_adding
- start_adding);
auto loop_time = chrono::duration_cast<chrono::microseconds>(stop_looping
diff --git a/src/example/db.cpp b/src/example/db.cpp
index 8c06a84..ee4e8fc 100644
--- a/src/example/db.cpp
+++ b/src/example/db.cpp
@@ -1,6 +1,6 @@
#include <crepe/api/Config.h>
#include <crepe/facade/DB.h>
-#include <crepe/util/log.h>
+#include <crepe/util/Log.h>
using namespace crepe;
using namespace std;
@@ -8,7 +8,7 @@ using namespace std;
// run before main
static auto _ = []() {
auto & cfg = Config::get_instance();
- cfg.log.level = LogLevel::TRACE;
+ cfg.log.level = Log::Level::TRACE;
return 0;
}();
@@ -20,11 +20,11 @@ int main() {
const char * test_key = "test-key";
string test_data = "Hello world!";
- dbg_logf("DB has key = %d", db.has(test_key));
+ dbg_logf("DB has key = {}", db.has(test_key));
db.set(test_key, test_data);
- dbg_logf("key = \"%s\"", db.get(test_key).c_str());
+ dbg_logf("key = \"{}\"", db.get(test_key));
return 0;
}
diff --git a/src/example/ecs.cpp b/src/example/ecs.cpp
index e61c398..5f83da1 100644
--- a/src/example/ecs.cpp
+++ b/src/example/ecs.cpp
@@ -9,13 +9,19 @@ using namespace crepe;
using namespace std;
int main() {
+ ComponentManager mgr{};
+
// Create a few GameObjects
try {
- GameObject body(0, "body", "person", Vector2{0, 0}, 0, 1);
- GameObject right_leg(1, "rightLeg", "person", Vector2{1, 1}, 0, 1);
- GameObject left_leg(2, "leftLeg", "person", Vector2{1, 1}, 0, 1);
- GameObject right_foot(3, "rightFoot", "person", Vector2{2, 2}, 0, 1);
- GameObject left_foot(4, "leftFoot", "person", Vector2{2, 2}, 0, 1);
+ GameObject body = mgr.new_object("body", "person", Vector2{0, 0}, 0, 1);
+ GameObject right_leg
+ = mgr.new_object("rightLeg", "person", Vector2{1, 1}, 0, 1);
+ GameObject left_leg
+ = mgr.new_object("leftLeg", "person", Vector2{1, 1}, 0, 1);
+ GameObject right_foot
+ = mgr.new_object("rightFoot", "person", Vector2{2, 2}, 0, 1);
+ GameObject left_foot
+ = mgr.new_object("leftFoot", "person", Vector2{2, 2}, 0, 1);
// Set the parent of each GameObject
right_foot.set_parent(right_leg);
@@ -30,7 +36,6 @@ int main() {
}
// Get the Metadata and Transform components of each GameObject
- ComponentManager & mgr = ComponentManager::get_instance();
vector<reference_wrapper<Metadata>> metadata
= mgr.get_components_by_type<Metadata>();
vector<reference_wrapper<Transform>> transform
diff --git a/src/example/log.cpp b/src/example/log.cpp
index db8aa48..5baa021 100644
--- a/src/example/log.cpp
+++ b/src/example/log.cpp
@@ -4,7 +4,7 @@
*/
#include <crepe/api/Config.h>
-#include <crepe/util/log.h>
+#include <crepe/util/Log.h>
using namespace crepe;
@@ -12,17 +12,17 @@ using namespace crepe;
int _ = []() {
// make sure all log messages get printed
auto & cfg = Config::get_instance();
- cfg.log.level = LogLevel::TRACE;
+ cfg.log.level = Log::Level::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");
+ dbg_log("debug message");
+ Log::logf("info message with variable: {}", 3);
+ Log::logf(Log::Level::WARNING, "warning");
+ Log::logf(Log::Level::ERROR, "error");
return 0;
}
diff --git a/src/example/physics.cpp b/src/example/physics.cpp
index 848f857..ad663a0 100644
--- a/src/example/physics.cpp
+++ b/src/example/physics.cpp
@@ -9,9 +9,10 @@ using namespace crepe;
using namespace std;
int main(int argc, char * argv[]) {
- GameObject * game_object;
- game_object = new GameObject(0, "Name", "Tag", Vector2{0, 0}, 0, 0);
- game_object->add_component<Rigidbody>(Rigidbody::Data{
+ ComponentManager mgr{};
+
+ GameObject game_object = mgr.new_object("Name", "Tag", Vector2{0, 0}, 0, 0);
+ game_object.add_component<Rigidbody>(Rigidbody::Data{
.mass = 1,
.gravity_scale = 1,
.body_type = Rigidbody::BodyType::DYNAMIC,
@@ -19,6 +20,5 @@ int main(int argc, char * argv[]) {
.use_gravity = true,
.bounce = false,
});
- delete game_object;
return 0;
}
diff --git a/src/example/proxy.cpp b/src/example/proxy.cpp
index 0afff41..69451f8 100644
--- a/src/example/proxy.cpp
+++ b/src/example/proxy.cpp
@@ -5,8 +5,8 @@
#include <crepe/ValueBroker.h>
#include <crepe/api/Config.h>
+#include <crepe/util/Log.h>
#include <crepe/util/Proxy.h>
-#include <crepe/util/log.h>
using namespace std;
using namespace crepe;
@@ -17,18 +17,17 @@ void test_ro_val(int val) {}
int main() {
auto & cfg = Config::get_instance();
- cfg.log.level = LogLevel::DEBUG;
+ cfg.log.level = Log::Level::DEBUG;
int real_value = 0;
ValueBroker<int> broker{
[&real_value](const int & target) {
- dbg_logf("set %s to %s", to_string(real_value).c_str(),
- to_string(target).c_str());
+ dbg_logf("set {} to {}", real_value, target);
real_value = target;
},
[&real_value]() -> const int & {
- dbg_logf("get %s", to_string(real_value).c_str());
+ dbg_logf("get {}", real_value);
return real_value;
},
};
diff --git a/src/example/rendering.cpp b/src/example/rendering.cpp
index 827ad07..abd11b1 100644
--- a/src/example/rendering.cpp
+++ b/src/example/rendering.cpp
@@ -2,7 +2,7 @@
#include <crepe/ComponentManager.h>
#include <crepe/api/GameObject.h>
#include <crepe/system/RenderSystem.h>
-#include <crepe/util/log.h>
+#include <crepe/util/Log.h>
#include <crepe/api/AssetManager.h>
#include <crepe/api/Color.h>
@@ -20,9 +20,12 @@ using namespace crepe;
int main() {
dbg_trace();
- auto obj = GameObject(0, "name", "tag", Vector2{0, 0}, 1, 1);
- auto obj1 = GameObject(1, "name", "tag", Vector2{500, 0}, 1, 0.1);
- auto obj2 = GameObject(2, "name", "tag", Vector2{800, 0}, 1, 0.1);
+ ComponentManager mgr{};
+ RenderSystem sys{mgr};
+
+ GameObject obj = mgr.new_object("name", "tag", Vector2{0, 0}, 1, 1);
+ GameObject obj1 = mgr.new_object("name", "tag", Vector2{500, 0}, 1, 0.1);
+ GameObject obj2 = mgr.new_object("name", "tag", Vector2{800, 0}, 1, 0.1);
// Normal adding components
{
@@ -47,7 +50,6 @@ int main() {
}
*/
- 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/savemgr.cpp b/src/example/savemgr.cpp
index 436fb5a..65c4a34 100644
--- a/src/example/savemgr.cpp
+++ b/src/example/savemgr.cpp
@@ -6,8 +6,8 @@
#include <cassert>
#include <crepe/api/Config.h>
#include <crepe/api/SaveManager.h>
+#include <crepe/util/Log.h>
#include <crepe/util/Proxy.h>
-#include <crepe/util/log.h>
using namespace crepe;
@@ -15,7 +15,7 @@ using namespace crepe;
int _ = []() {
// make sure all log messages get printed
auto & cfg = Config::get_instance();
- cfg.log.level = LogLevel::TRACE;
+ cfg.log.level = Log::Level::TRACE;
return 0; // satisfy compiler
}();
@@ -25,19 +25,19 @@ int main() {
SaveManager & mgr = SaveManager::get_instance();
- dbg_logf("has key = %s", mgr.has(key) ? "true" : "false");
+ dbg_logf("has key = {}", mgr.has(key));
ValueBroker<int> prop = mgr.get<int>(key, 0);
Proxy<int> val = mgr.get<int>(key, 0);
- dbg_logf("val = %d", mgr.get<int>(key).get());
+ dbg_logf("val = {}", mgr.get<int>(key).get());
prop.set(1);
- dbg_logf("val = %d", mgr.get<int>(key).get());
+ dbg_logf("val = {}", mgr.get<int>(key).get());
val = 2;
- dbg_logf("val = %d", mgr.get<int>(key).get());
+ dbg_logf("val = {}", mgr.get<int>(key).get());
mgr.set<int>(key, 3);
- dbg_logf("val = %d", mgr.get<int>(key).get());
+ dbg_logf("val = {}", mgr.get<int>(key).get());
- dbg_logf("has key = %s", mgr.has(key) ? "true" : "false");
+ dbg_logf("has key = {}", mgr.has(key));
assert(true == mgr.has(key));
return 0;
diff --git a/src/example/scene_manager.cpp b/src/example/scene_manager.cpp
index f46dc36..24ab72e 100644
--- a/src/example/scene_manager.cpp
+++ b/src/example/scene_manager.cpp
@@ -12,40 +12,51 @@ using namespace std;
class ConcreteScene1 : public Scene {
public:
- ConcreteScene1(string name) : Scene(name) {}
+ using Scene::Scene;
void load_scene() {
- GameObject object1(0, "scene_1", "tag_scene_1", Vector2{0, 0}, 0, 1);
- GameObject object2(1, "scene_1", "tag_scene_1", Vector2{1, 0}, 0, 1);
- GameObject object3(2, "scene_1", "tag_scene_1", Vector2{2, 0}, 0, 1);
+ auto & mgr = this->component_manager;
+ GameObject object1
+ = mgr.new_object("scene_1", "tag_scene_1", Vector2{0, 0}, 0, 1);
+ GameObject object2
+ = mgr.new_object("scene_1", "tag_scene_1", Vector2{1, 0}, 0, 1);
+ GameObject object3
+ = mgr.new_object("scene_1", "tag_scene_1", Vector2{2, 0}, 0, 1);
}
};
class ConcreteScene2 : public Scene {
public:
- ConcreteScene2(string name) : Scene(name) {}
+ using Scene::Scene;
void load_scene() {
- GameObject object1(0, "scene_2", "tag_scene_2", Vector2{0, 0}, 0, 1);
- GameObject object2(1, "scene_2", "tag_scene_2", Vector2{0, 1}, 0, 1);
- GameObject object3(2, "scene_2", "tag_scene_2", Vector2{0, 2}, 0, 1);
- GameObject object4(3, "scene_2", "tag_scene_2", Vector2{0, 3}, 0, 1);
+ auto & mgr = this->component_manager;
+ GameObject object1
+ = mgr.new_object("scene_2", "tag_scene_2", Vector2{0, 0}, 0, 1);
+ GameObject object2
+ = mgr.new_object("scene_2", "tag_scene_2", Vector2{0, 1}, 0, 1);
+ GameObject object3
+ = mgr.new_object("scene_2", "tag_scene_2", Vector2{0, 2}, 0, 1);
+ GameObject object4
+ = mgr.new_object("scene_2", "tag_scene_2", Vector2{0, 3}, 0, 1);
}
};
int main() {
- SceneManager & scene_mgr = SceneManager::get_instance();
+ ComponentManager component_mgr{};
+ SceneManager scene_mgr{component_mgr};
// Add the scenes to the scene manager
scene_mgr.add_scene<ConcreteScene1>("scene1");
scene_mgr.add_scene<ConcreteScene2>("scene2");
- // There is no need to call set_next_scene() at the beginnen, because the first scene will be automatically set as the next scene
+ // There is no need to call set_next_scene() at the beginning because the
+ // first scene will be automatically set as the next scene
+
// Load scene1 (the first scene added)
scene_mgr.load_next_scene();
// Get the Metadata components of each GameObject of Scene1
- ComponentManager & component_mgr = ComponentManager::get_instance();
vector<reference_wrapper<Metadata>> metadata
= component_mgr.get_components_by_type<Metadata>();
diff --git a/src/example/script.cpp b/src/example/script.cpp
index 9e8b147..a23295b 100644
--- a/src/example/script.cpp
+++ b/src/example/script.cpp
@@ -5,7 +5,7 @@
#include <crepe/ComponentManager.h>
#include <crepe/system/ScriptSystem.h>
-#include <crepe/util/log.h>
+#include <crepe/util/Log.h>
#include <crepe/api/BehaviorScript.h>
#include <crepe/api/Config.h>
@@ -20,7 +20,7 @@ using namespace std;
int _ = []() {
// Show dbg_trace() output
auto & cfg = Config::get_instance();
- cfg.log.level = LogLevel::TRACE;
+ cfg.log.level = Log::Level::TRACE;
return 0; // satisfy compiler
}();
@@ -30,20 +30,20 @@ class MyScript : public Script {
void update() {
// 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);
+ dbg_logf("Transform({:.2f}, {:.2f})", test.position.x, test.position.y);
}
};
int main() {
+ ComponentManager component_manager{};
+ ScriptSystem system{component_manager};
+
// Create game object with Transform and BehaviorScript components
- auto obj = GameObject(0, "name", "tag", Vector2{1.2, 3.4}, 0, 1);
+ GameObject obj = component_manager.new_object("name");
obj.add_component<BehaviorScript>().set_script<MyScript>();
- // Get ScriptSystem singleton instance (this would normally be done from the
- // game loop)
- ScriptSystem sys;
// Update all scripts. This should result in MyScript::update being called
- sys.update();
+ system.update();
return EXIT_SUCCESS;
}