diff options
Diffstat (limited to 'src/example')
-rw-r--r-- | src/example/audio_internal.cpp | 4 | ||||
-rw-r--r-- | src/example/components_internal.cpp | 17 | ||||
-rw-r--r-- | src/example/db.cpp | 8 | ||||
-rw-r--r-- | src/example/ecs.cpp | 13 | ||||
-rw-r--r-- | src/example/log.cpp | 12 | ||||
-rw-r--r-- | src/example/particles.cpp | 3 | ||||
-rw-r--r-- | src/example/physics.cpp | 8 | ||||
-rw-r--r-- | src/example/proxy.cpp | 8 | ||||
-rw-r--r-- | src/example/rendering.cpp | 12 | ||||
-rw-r--r-- | src/example/savemgr.cpp | 16 | ||||
-rw-r--r-- | src/example/scene_manager.cpp | 24 | ||||
-rw-r--r-- | src/example/script.cpp | 15 |
12 files changed, 70 insertions, 70 deletions
diff --git a/src/example/audio_internal.cpp b/src/example/audio_internal.cpp index 81f8980..661161a 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 1f8884d..2a232a9 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,14 @@ 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 +42,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 - stop_adding); printf("add time: %ldus\n", add_time.count()); 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 3197100..d5ba51b 100644 --- a/src/example/ecs.cpp +++ b/src/example/ecs.cpp @@ -9,13 +9,15 @@ 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 +32,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 = mgr.get_components_by_type<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/particles.cpp b/src/example/particles.cpp index 3bdf35f..3d5f676 100644 --- a/src/example/particles.cpp +++ b/src/example/particles.cpp @@ -14,7 +14,8 @@ using namespace crepe; using namespace std; int main(int argc, char * argv[]) { - GameObject game_object(0, "", "", Vector2{0, 0}, 0, 0); + ComponentManager mgr{}; + GameObject game_object = mgr.new_object("", "", Vector2{0, 0}, 0, 0); Color color(0, 0, 0, 0); Sprite test_sprite = game_object.add_component<Sprite>( make_shared<Texture>("../asset/texture/img.png"), color, FlipSettings{true, true}); 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 b69ea03..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,17 +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 e0281f2..c9e62f1 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 { @@ -45,7 +48,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 c48d5e3..accec7d 100644 --- a/src/example/scene_manager.cpp +++ b/src/example/scene_manager.cpp @@ -12,29 +12,32 @@ 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"); @@ -47,7 +50,6 @@ int main() { 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 5411b77..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,19 +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; } |