From 3e94ecb3dac5003a3d58210ed1a4d1f1cb2083d1 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Tue, 12 Nov 2024 22:43:32 +0100 Subject: add script unit tests + major refactoring --- src/crepe/ComponentManager.cpp | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'src/crepe/ComponentManager.cpp') diff --git a/src/crepe/ComponentManager.cpp b/src/crepe/ComponentManager.cpp index 85149c8..f6acc1a 100644 --- a/src/crepe/ComponentManager.cpp +++ b/src/crepe/ComponentManager.cpp @@ -1,13 +1,10 @@ +#include "api/GameObject.h" #include "util/log.h" #include "ComponentManager.h" using namespace crepe; - -ComponentManager & ComponentManager::get_instance() { - static ComponentManager instance; - return instance; -} +using namespace std; void ComponentManager::delete_all_components_of_id(game_object_id_t id) { // Loop through all the types (in the unordered_map<>) @@ -26,5 +23,12 @@ void ComponentManager::delete_all_components() { } ComponentManager::ComponentManager() { dbg_trace(); } - ComponentManager::~ComponentManager() { dbg_trace(); } + +GameObject & ComponentManager::new_object(const string & name, const string & tag, const Vector2 & position, double rotation, double scale) { + GameObject * object = new GameObject(*this, this->next_id, name, tag, position, rotation, scale); + this->objects.push_front(unique_ptr(object)); + this->next_id++; + return *object; +} + -- cgit v1.2.3 From 87c74782e486647c46f9ca4d2f857094006e563c Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 13 Nov 2024 12:19:56 +0100 Subject: `make format` --- src/crepe/Collider.cpp | 1 - src/crepe/Component.cpp | 1 - src/crepe/ComponentManager.cpp | 9 ++++++--- src/crepe/ComponentManager.h | 7 +++++-- src/crepe/api/Animator.cpp | 3 ++- src/crepe/api/AssetManager.h | 3 ++- src/crepe/api/BehaviorScript.cpp | 5 +++-- src/crepe/api/GameObject.cpp | 9 +++++---- src/crepe/api/GameObject.h | 6 ++++-- src/crepe/api/LoopManager.h | 5 ++--- src/crepe/api/LoopManager.hpp | 10 ++++++---- src/crepe/api/SceneManager.cpp | 10 +++++----- src/crepe/api/Script.cpp | 1 - src/crepe/api/Script.h | 1 - src/crepe/api/Transform.h | 2 +- src/crepe/api/Vector2.cpp | 1 - src/crepe/facade/SDLContext.cpp | 2 +- src/crepe/facade/Sound.cpp | 3 +-- src/crepe/facade/SoundContext.cpp | 1 - src/crepe/facade/SoundContext.h | 8 ++++---- src/crepe/system/CollisionSystem.cpp | 1 - src/crepe/system/ParticleSystem.cpp | 1 - src/crepe/system/ScriptSystem.cpp | 1 - src/crepe/system/System.cpp | 5 +---- src/example/rendering.cpp | 6 ++++-- src/example/script.cpp | 4 ++-- src/test/PhysicsTest.cpp | 11 +++++++---- src/test/ScriptTest.cpp | 9 ++++----- src/test/main.cpp | 6 +++--- 29 files changed, 68 insertions(+), 64 deletions(-) (limited to 'src/crepe/ComponentManager.cpp') diff --git a/src/crepe/Collider.cpp b/src/crepe/Collider.cpp index b408609..113ba61 100644 --- a/src/crepe/Collider.cpp +++ b/src/crepe/Collider.cpp @@ -1,4 +1,3 @@ #include "Collider.h" using namespace crepe; - diff --git a/src/crepe/Component.cpp b/src/crepe/Component.cpp index d11a37e..acfd35c 100644 --- a/src/crepe/Component.cpp +++ b/src/crepe/Component.cpp @@ -3,4 +3,3 @@ using namespace crepe; Component::Component(game_object_id_t id) : game_object_id(id) {} - diff --git a/src/crepe/ComponentManager.cpp b/src/crepe/ComponentManager.cpp index f6acc1a..8f8437b 100644 --- a/src/crepe/ComponentManager.cpp +++ b/src/crepe/ComponentManager.cpp @@ -25,10 +25,13 @@ void ComponentManager::delete_all_components() { ComponentManager::ComponentManager() { dbg_trace(); } ComponentManager::~ComponentManager() { dbg_trace(); } -GameObject & ComponentManager::new_object(const string & name, const string & tag, const Vector2 & position, double rotation, double scale) { - GameObject * object = new GameObject(*this, this->next_id, name, tag, position, rotation, scale); +GameObject & ComponentManager::new_object(const string & name, + const string & tag, + const Vector2 & position, + double rotation, double scale) { + GameObject * object = new GameObject(*this, this->next_id, name, tag, + position, rotation, scale); this->objects.push_front(unique_ptr(object)); this->next_id++; return *object; } - diff --git a/src/crepe/ComponentManager.h b/src/crepe/ComponentManager.h index 1d67e69..75606e0 100644 --- a/src/crepe/ComponentManager.h +++ b/src/crepe/ComponentManager.h @@ -1,10 +1,10 @@ #pragma once +#include #include #include #include #include -#include #include "Component.h" #include "api/Vector2.h" @@ -101,7 +101,10 @@ public: std::vector> get_components_by_type() const; // TODO: doxygen - GameObject & new_object(const std::string & name, const std::string & tag = "", const Vector2 & position = { 0, 0 }, double rotation = 0, double scale = 0); + GameObject & new_object(const std::string & name, + const std::string & tag = "", + const Vector2 & position = {0, 0}, + double rotation = 0, double scale = 0); private: /** diff --git a/src/crepe/api/Animator.cpp b/src/crepe/api/Animator.cpp index cbf415d..fcc07ef 100644 --- a/src/crepe/api/Animator.cpp +++ b/src/crepe/api/Animator.cpp @@ -9,7 +9,8 @@ using namespace crepe; -Animator::Animator(game_object_id_t id, Sprite & ss, int row, int col, int col_animator) +Animator::Animator(game_object_id_t id, Sprite & ss, int row, int col, + int col_animator) : Component(id), spritesheet(ss), row(row), diff --git a/src/crepe/api/AssetManager.h b/src/crepe/api/AssetManager.h index dbfaef3..86a9902 100644 --- a/src/crepe/api/AssetManager.h +++ b/src/crepe/api/AssetManager.h @@ -56,7 +56,8 @@ public: * cache. */ template - std::shared_ptr cache(const std::string & file_path, bool reload = false); + std::shared_ptr cache(const std::string & file_path, + bool reload = false); }; } // namespace crepe diff --git a/src/crepe/api/BehaviorScript.cpp b/src/crepe/api/BehaviorScript.cpp index 41c144c..c5fecef 100644 --- a/src/crepe/api/BehaviorScript.cpp +++ b/src/crepe/api/BehaviorScript.cpp @@ -3,5 +3,6 @@ using namespace crepe; -BehaviorScript::BehaviorScript(game_object_id_t id, ComponentManager & mgr) : Component(id), component_manager(mgr) {} - +BehaviorScript::BehaviorScript(game_object_id_t id, ComponentManager & mgr) + : Component(id), + component_manager(mgr) {} diff --git a/src/crepe/api/GameObject.cpp b/src/crepe/api/GameObject.cpp index dd20b7f..e05cea1 100644 --- a/src/crepe/api/GameObject.cpp +++ b/src/crepe/api/GameObject.cpp @@ -1,16 +1,18 @@ #include "api/Transform.h" +#include "BehaviorScript.h" #include "GameObject.h" #include "Metadata.h" -#include "BehaviorScript.h" using namespace crepe; using namespace std; -GameObject::GameObject(ComponentManager & component_manager, game_object_id_t id, const std::string & name, +GameObject::GameObject(ComponentManager & component_manager, + game_object_id_t id, const std::string & name, const std::string & tag, const Vector2 & position, double rotation, double scale) - : id(id), component_manager(component_manager) { + : id(id), + component_manager(component_manager) { // Add Transform and Metadata components ComponentManager & mgr = this->component_manager; @@ -37,4 +39,3 @@ BehaviorScript & GameObject::add_component() { ComponentManager & mgr = this->component_manager; return mgr.add_component(this->id, mgr); } - diff --git a/src/crepe/api/GameObject.h b/src/crepe/api/GameObject.h index 7751fd0..e4a2539 100644 --- a/src/crepe/api/GameObject.h +++ b/src/crepe/api/GameObject.h @@ -2,8 +2,8 @@ #include -#include "types.h" #include "Vector2.h" +#include "types.h" namespace crepe { @@ -31,7 +31,9 @@ private: * \param rotation The rotation of the GameObject * \param scale The scale of the GameObject */ - GameObject(ComponentManager & component_manager, game_object_id_t id, const std::string & name, const std::string & tag, const Vector2 & position, double rotation, double scale); + GameObject(ComponentManager & component_manager, game_object_id_t id, + const std::string & name, const std::string & tag, + const Vector2 & position, double rotation, double scale); //! ComponentManager instances GameObject friend class ComponentManager; diff --git a/src/crepe/api/LoopManager.h b/src/crepe/api/LoopManager.h index ef1c14f..288dca2 100644 --- a/src/crepe/api/LoopManager.h +++ b/src/crepe/api/LoopManager.h @@ -2,8 +2,8 @@ #include -#include "../system/System.h" #include "../ComponentManager.h" +#include "../system/System.h" namespace crepe { @@ -74,7 +74,7 @@ protected: T & get_system(); private: - ComponentManager component_manager; + ComponentManager component_manager{}; std::unordered_map> systems; private: @@ -85,4 +85,3 @@ private: } // namespace crepe #include "LoopManager.hpp" - diff --git a/src/crepe/api/LoopManager.hpp b/src/crepe/api/LoopManager.hpp index 20e8d1c..8fb9aa3 100644 --- a/src/crepe/api/LoopManager.hpp +++ b/src/crepe/api/LoopManager.hpp @@ -3,8 +3,8 @@ #include #include -#include "../system/System.h" #include "../Exception.h" +#include "../system/System.h" #include "LoopManager.h" @@ -13,8 +13,9 @@ namespace crepe { template T & LoopManager::get_system() { using namespace std; - static_assert(is_base_of::value, "get_system must recieve a derivative class of System"); - + static_assert(is_base_of::value, + "get_system must recieve a derivative class of System"); + const type_info & type = typeid(T); if (!this->systems.contains(type)) throw Exception("LoopManager: %s is not initialized", type.name()); @@ -29,7 +30,8 @@ T & LoopManager::get_system() { template void LoopManager::load_system() { using namespace std; - static_assert(is_base_of::value, "load_system must recieve a derivative class of System"); + static_assert(is_base_of::value, + "load_system must recieve a derivative class of System"); System * system = new T(this->component_manager); this->systems[typeid(T)] = unique_ptr(system); diff --git a/src/crepe/api/SceneManager.cpp b/src/crepe/api/SceneManager.cpp index 814b7d7..4a38787 100644 --- a/src/crepe/api/SceneManager.cpp +++ b/src/crepe/api/SceneManager.cpp @@ -16,11 +16,11 @@ void SceneManager::load_next_scene() { // next scene not set if (this->next_scene.empty()) return; - auto it = find_if(this->scenes.begin(), this->scenes.end(), - [&next_scene = this->next_scene](unique_ptr & scene) { - return scene->name == next_scene; - } - ); + auto it + = find_if(this->scenes.begin(), this->scenes.end(), + [&next_scene = this->next_scene](unique_ptr & scene) { + return scene->name == next_scene; + }); // next scene not found if (it == this->scenes.end()) return; diff --git a/src/crepe/api/Script.cpp b/src/crepe/api/Script.cpp index 0423315..390cec7 100644 --- a/src/crepe/api/Script.cpp +++ b/src/crepe/api/Script.cpp @@ -1,4 +1,3 @@ #include "Script.h" using namespace crepe; - diff --git a/src/crepe/api/Script.h b/src/crepe/api/Script.h index c5e3cc4..837420f 100644 --- a/src/crepe/api/Script.h +++ b/src/crepe/api/Script.h @@ -49,4 +49,3 @@ private: } // namespace crepe #include "Script.hpp" - diff --git a/src/crepe/api/Transform.h b/src/crepe/api/Transform.h index a1ef406..902dafa 100644 --- a/src/crepe/api/Transform.h +++ b/src/crepe/api/Transform.h @@ -15,7 +15,7 @@ namespace crepe { class Transform : public Component { public: //! Translation (shift) - Vector2 position = { 0, 0 }; + Vector2 position = {0, 0}; //! Rotation, in degrees double rotation = 0; //! Multiplication factor diff --git a/src/crepe/api/Vector2.cpp b/src/crepe/api/Vector2.cpp index aa391fa..7da202a 100644 --- a/src/crepe/api/Vector2.cpp +++ b/src/crepe/api/Vector2.cpp @@ -41,4 +41,3 @@ bool Vector2::operator==(const Vector2 & other) const { bool Vector2::operator!=(const Vector2 & other) const { return !(*this == other); } - diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 46230b4..236bf8c 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -159,7 +159,7 @@ SDLContext::texture_from_path(const std::string & path) { SDL_Surface * tmp = IMG_Load(path.c_str()); if (tmp == nullptr) { - tmp = IMG_Load("../asset/texture/ERROR.png"); + tmp = IMG_Load("../asset/texture/ERROR.png"); } std::unique_ptr> diff --git a/src/crepe/facade/Sound.cpp b/src/crepe/facade/Sound.cpp index 3a6e1e1..b8ea71a 100644 --- a/src/crepe/facade/Sound.cpp +++ b/src/crepe/facade/Sound.cpp @@ -1,7 +1,7 @@ #include -#include "../util/log.h" #include "../Asset.h" +#include "../util/log.h" #include "Sound.h" #include "SoundContext.h" @@ -56,4 +56,3 @@ void Sound::set_looping(bool looping) { if (!ctx.engine.isValidVoiceHandle(this->handle)) return; ctx.engine.setLooping(this->handle, this->looping); } - diff --git a/src/crepe/facade/SoundContext.cpp b/src/crepe/facade/SoundContext.cpp index c89fb03..b5f3db3 100644 --- a/src/crepe/facade/SoundContext.cpp +++ b/src/crepe/facade/SoundContext.cpp @@ -13,4 +13,3 @@ SoundContext::~SoundContext() { dbg_trace(); engine.deinit(); } - diff --git a/src/crepe/facade/SoundContext.h b/src/crepe/facade/SoundContext.h index c360fb8..8d9e396 100644 --- a/src/crepe/facade/SoundContext.h +++ b/src/crepe/facade/SoundContext.h @@ -11,10 +11,10 @@ public: SoundContext(); virtual ~SoundContext(); - SoundContext(const SoundContext &) = delete; - SoundContext(SoundContext &&) = delete; - SoundContext & operator=(const SoundContext &) = delete; - SoundContext & operator=(SoundContext &&) = delete; + SoundContext(const SoundContext &) = delete; + SoundContext(SoundContext &&) = delete; + SoundContext & operator=(const SoundContext &) = delete; + SoundContext & operator=(SoundContext &&) = delete; private: SoLoud::Soloud engine; diff --git a/src/crepe/system/CollisionSystem.cpp b/src/crepe/system/CollisionSystem.cpp index 67f535a..c74ca1d 100644 --- a/src/crepe/system/CollisionSystem.cpp +++ b/src/crepe/system/CollisionSystem.cpp @@ -3,4 +3,3 @@ using namespace crepe; void CollisionSystem::update() {} - diff --git a/src/crepe/system/ParticleSystem.cpp b/src/crepe/system/ParticleSystem.cpp index 64fee4e..fe02682 100644 --- a/src/crepe/system/ParticleSystem.cpp +++ b/src/crepe/system/ParticleSystem.cpp @@ -58,4 +58,3 @@ void ParticleSystem::emit_particle(ParticleEmitter & emitter) { } } } - diff --git a/src/crepe/system/ScriptSystem.cpp b/src/crepe/system/ScriptSystem.cpp index 0a87fcc..644e96e 100644 --- a/src/crepe/system/ScriptSystem.cpp +++ b/src/crepe/system/ScriptSystem.cpp @@ -44,4 +44,3 @@ forward_list> ScriptSystem::get_scripts() { return scripts; } - diff --git a/src/crepe/system/System.cpp b/src/crepe/system/System.cpp index 31b1337..201b9ad 100644 --- a/src/crepe/system/System.cpp +++ b/src/crepe/system/System.cpp @@ -4,7 +4,4 @@ using namespace crepe; -System::System(ComponentManager & mgr) : component_manager(mgr) { - dbg_trace(); -} - +System::System(ComponentManager & mgr) : component_manager(mgr) { dbg_trace(); } diff --git a/src/example/rendering.cpp b/src/example/rendering.cpp index e02f6a3..493169b 100644 --- a/src/example/rendering.cpp +++ b/src/example/rendering.cpp @@ -34,7 +34,9 @@ int main() { } { Color color(0, 0, 0, 0); - obj1.add_component(make_shared("../asset/texture/second.png"), color, FlipSettings{true, true}); + obj1.add_component( + make_shared("../asset/texture/second.png"), color, + FlipSettings{true, true}); } /* @@ -45,7 +47,7 @@ int main() { } */ - auto & sys = crepe::RenderSystem::get_instance(); + auto & sys = crepe::AssetManager::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 4ba2b4b..69d1ce1 100644 --- a/src/example/script.cpp +++ b/src/example/script.cpp @@ -35,8 +35,8 @@ class MyScript : public Script { }; int main() { - ComponentManager component_manager {}; - ScriptSystem system { component_manager }; + ComponentManager component_manager{}; + ScriptSystem system{component_manager}; // Create game object with Transform and BehaviorScript components auto & obj = component_manager.new_object("name"); diff --git a/src/test/PhysicsTest.cpp b/src/test/PhysicsTest.cpp index 5399d54..9ac99aa 100644 --- a/src/test/PhysicsTest.cpp +++ b/src/test/PhysicsTest.cpp @@ -13,7 +13,7 @@ using namespace crepe; class PhysicsTest : public ::testing::Test { public: ComponentManager component_manager; - PhysicsSystem system { component_manager }; + PhysicsSystem system{component_manager}; void SetUp() override { ComponentManager & mgr = this->component_manager; @@ -49,7 +49,8 @@ public: TEST_F(PhysicsTest, gravity) { Config::get_instance().physics.gravity = 1; ComponentManager & mgr = this->component_manager; - vector> transforms = mgr.get_components_by_id(0); + vector> transforms + = mgr.get_components_by_id(0); const Transform & transform = transforms.front().get(); ASSERT_FALSE(transforms.empty()); EXPECT_EQ(transform.position.y, 0); @@ -87,9 +88,11 @@ TEST_F(PhysicsTest, max_velocity) { TEST_F(PhysicsTest, movement) { Config::get_instance().physics.gravity = 0; ComponentManager & mgr = this->component_manager; - vector> rigidbodies = mgr.get_components_by_id(0); + vector> rigidbodies + = mgr.get_components_by_id(0); Rigidbody & rigidbody = rigidbodies.front().get(); - vector> transforms = mgr.get_components_by_id(0); + vector> transforms + = mgr.get_components_by_id(0); const Transform & transform = transforms.front().get(); ASSERT_FALSE(rigidbodies.empty()); ASSERT_FALSE(transforms.empty()); diff --git a/src/test/ScriptTest.cpp b/src/test/ScriptTest.cpp index ea49a35..bab9e52 100644 --- a/src/test/ScriptTest.cpp +++ b/src/test/ScriptTest.cpp @@ -5,11 +5,11 @@ #define protected public #include -#include #include -#include #include +#include #include +#include using namespace std; using namespace crepe; @@ -17,8 +17,8 @@ using namespace testing; class ScriptTest : public Test { public: - ComponentManager component_manager {}; - ScriptSystem system { component_manager }; + ComponentManager component_manager{}; + ScriptSystem system{component_manager}; class MyScript : public Script { // NOTE: default (private) visibility of init and update shouldn't cause @@ -70,4 +70,3 @@ TEST_F(ScriptTest, ListScripts) { } ASSERT_EQ(1, script_count); } - diff --git a/src/test/main.cpp b/src/test/main.cpp index 6830a01..a54d239 100644 --- a/src/test/main.cpp +++ b/src/test/main.cpp @@ -5,11 +5,11 @@ using namespace crepe; using namespace testing; -int main(int argc, char **argv) { - InitGoogleTest(&argc, argv); +int main(int argc, char ** argv) { + InitGoogleTest(&argc, argv); auto & cfg = Config::get_instance(); cfg.log.level = LogLevel::ERROR; - return RUN_ALL_TESTS(); + return RUN_ALL_TESTS(); } -- cgit v1.2.3 From 1df510f4a2dbe0fcb8c9f8a34695abf8d33f9ddc Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 13 Nov 2024 14:35:27 +0100 Subject: update logging to use C++20 format and classes --- src/crepe/CMakeLists.txt | 1 + src/crepe/ComponentManager.cpp | 2 +- src/crepe/Exception.cpp | 13 ++---- src/crepe/Exception.h | 16 ++++++-- src/crepe/Exception.hpp | 13 ++++++ src/crepe/api/Animator.cpp | 2 +- src/crepe/api/AssetManager.cpp | 2 +- src/crepe/api/BehaviorScript.hpp | 2 +- src/crepe/api/Camera.cpp | 2 +- src/crepe/api/Config.h | 4 +- src/crepe/api/LoopTimer.cpp | 2 +- src/crepe/api/SaveManager.cpp | 2 +- src/crepe/api/Script.h | 5 +-- src/crepe/api/Sprite.cpp | 2 +- src/crepe/api/Texture.cpp | 2 +- src/crepe/api/Transform.cpp | 2 +- src/crepe/facade/DB.cpp | 2 +- src/crepe/facade/SDLContext.cpp | 2 +- src/crepe/facade/Sound.cpp | 2 +- src/crepe/facade/SoundContext.cpp | 2 +- src/crepe/system/AnimatorSystem.cpp | 2 +- src/crepe/system/RenderSystem.cpp | 2 +- src/crepe/system/ScriptSystem.cpp | 2 +- src/crepe/util/CMakeLists.txt | 7 ++-- src/crepe/util/Log.cpp | 39 ++++++++++++++++++ src/crepe/util/Log.h | 80 +++++++++++++++++++++++++++++++++++++ src/crepe/util/Log.hpp | 18 +++++++++ src/crepe/util/LogColor.cpp | 19 +-------- src/crepe/util/LogColor.h | 29 +++++++++----- src/crepe/util/fmt.cpp | 35 ---------------- src/crepe/util/fmt.h | 10 ----- src/crepe/util/log.cpp | 53 ------------------------ src/crepe/util/log.h | 39 ------------------ src/example/log.cpp | 13 +++--- 34 files changed, 218 insertions(+), 210 deletions(-) create mode 100644 src/crepe/Exception.hpp create mode 100644 src/crepe/util/Log.cpp create mode 100644 src/crepe/util/Log.h create mode 100644 src/crepe/util/Log.hpp delete mode 100644 src/crepe/util/fmt.cpp delete mode 100644 src/crepe/util/fmt.h delete mode 100644 src/crepe/util/log.cpp delete mode 100644 src/crepe/util/log.h (limited to 'src/crepe/ComponentManager.cpp') diff --git a/src/crepe/CMakeLists.txt b/src/crepe/CMakeLists.txt index fc95bd3..52a781e 100644 --- a/src/crepe/CMakeLists.txt +++ b/src/crepe/CMakeLists.txt @@ -16,6 +16,7 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES ValueBroker.h ValueBroker.hpp Exception.h + Exception.hpp ) add_subdirectory(api) diff --git a/src/crepe/ComponentManager.cpp b/src/crepe/ComponentManager.cpp index 85149c8..7123905 100644 --- a/src/crepe/ComponentManager.cpp +++ b/src/crepe/ComponentManager.cpp @@ -1,4 +1,4 @@ -#include "util/log.h" +#include "util/Log.h" #include "ComponentManager.h" diff --git a/src/crepe/Exception.cpp b/src/crepe/Exception.cpp index bfdbcdd..3217169 100644 --- a/src/crepe/Exception.cpp +++ b/src/crepe/Exception.cpp @@ -1,16 +1,11 @@ -#include - #include "Exception.h" -#include "util/fmt.h" -using namespace std; using namespace crepe; +using namespace std; const char * Exception::what() const noexcept { return error.c_str(); } -Exception::Exception(const char * fmt, ...) { - va_list args; - va_start(args, fmt); - this->error = va_stringf(args, fmt); - va_end(args); +Exception::Exception(const string & msg) { + this->error = msg; } + diff --git a/src/crepe/Exception.h b/src/crepe/Exception.h index 80af068..ed3ab15 100644 --- a/src/crepe/Exception.h +++ b/src/crepe/Exception.h @@ -2,21 +2,31 @@ #include #include +#include namespace crepe { -//! Exception class with printf-style constructor +//! Exception class class Exception : public std::exception { public: - //! printf - Exception(const char * fmt, ...); + //! Exception with plain message + Exception(const std::string & msg); + + //! Exception with \c std::format message + template + Exception(std::format_string fmt, Args&&... args); + //! Get formatted error message const char * what() const noexcept; protected: Exception() = default; + //! Formatted error message std::string error; }; } // namespace crepe + +#include "Exception.hpp" + diff --git a/src/crepe/Exception.hpp b/src/crepe/Exception.hpp new file mode 100644 index 0000000..d813879 --- /dev/null +++ b/src/crepe/Exception.hpp @@ -0,0 +1,13 @@ +#pragma once + +#include "Exception.h" + +namespace crepe { + +template +Exception::Exception(std::format_string fmt, Args&&... args) { + this->error = std::format(fmt, std::forward(args)...); +} + +} + diff --git a/src/crepe/api/Animator.cpp b/src/crepe/api/Animator.cpp index 58fee2a..863dce6 100644 --- a/src/crepe/api/Animator.cpp +++ b/src/crepe/api/Animator.cpp @@ -1,7 +1,7 @@ #include -#include "util/log.h" +#include "util/Log.h" #include "Animator.h" #include "Component.h" diff --git a/src/crepe/api/AssetManager.cpp b/src/crepe/api/AssetManager.cpp index b891760..3925758 100644 --- a/src/crepe/api/AssetManager.cpp +++ b/src/crepe/api/AssetManager.cpp @@ -1,4 +1,4 @@ -#include "util/log.h" +#include "util/Log.h" #include "AssetManager.h" diff --git a/src/crepe/api/BehaviorScript.hpp b/src/crepe/api/BehaviorScript.hpp index 4751607..bc157fd 100644 --- a/src/crepe/api/BehaviorScript.hpp +++ b/src/crepe/api/BehaviorScript.hpp @@ -2,7 +2,7 @@ #include -#include "../util/log.h" +#include "../util/Log.h" #include "BehaviorScript.h" #include "Script.h" diff --git a/src/crepe/api/Camera.cpp b/src/crepe/api/Camera.cpp index 6355a03..6f0deb1 100644 --- a/src/crepe/api/Camera.cpp +++ b/src/crepe/api/Camera.cpp @@ -1,7 +1,7 @@ #include -#include "util/log.h" +#include "util/Log.h" #include "Camera.h" #include "Color.h" diff --git a/src/crepe/api/Config.h b/src/crepe/api/Config.h index 8c9e643..8fd381e 100644 --- a/src/crepe/api/Config.h +++ b/src/crepe/api/Config.h @@ -1,6 +1,6 @@ #pragma once -#include "../util/log.h" +#include "../util/Log.h" namespace crepe { @@ -29,7 +29,7 @@ public: * Only messages with equal or higher priority than this value will be * logged. */ - LogLevel level = LogLevel::INFO; + Log::Level level = Log::Level::INFO; /** * \brief Colored log output * diff --git a/src/crepe/api/LoopTimer.cpp b/src/crepe/api/LoopTimer.cpp index 8f09e41..b3aec22 100644 --- a/src/crepe/api/LoopTimer.cpp +++ b/src/crepe/api/LoopTimer.cpp @@ -1,7 +1,7 @@ #include #include "../facade/SDLContext.h" -#include "../util/log.h" +#include "../util/Log.h" #include "LoopTimer.h" diff --git a/src/crepe/api/SaveManager.cpp b/src/crepe/api/SaveManager.cpp index 43276c5..2974b91 100644 --- a/src/crepe/api/SaveManager.cpp +++ b/src/crepe/api/SaveManager.cpp @@ -1,5 +1,5 @@ #include "../facade/DB.h" -#include "../util/log.h" +#include "../util/Log.h" #include "Config.h" #include "SaveManager.h" diff --git a/src/crepe/api/Script.h b/src/crepe/api/Script.h index 49e625f..ed247ad 100644 --- a/src/crepe/api/Script.h +++ b/src/crepe/api/Script.h @@ -2,12 +2,9 @@ #include -namespace crepe { -class ScriptSystem; -} - namespace crepe { +class ScriptSystem; class BehaviorScript; class Script { diff --git a/src/crepe/api/Sprite.cpp b/src/crepe/api/Sprite.cpp index 6f0433f..ac0079e 100644 --- a/src/crepe/api/Sprite.cpp +++ b/src/crepe/api/Sprite.cpp @@ -1,6 +1,6 @@ #include -#include "../util/log.h" +#include "../util/Log.h" #include "facade/SDLContext.h" #include "Component.h" diff --git a/src/crepe/api/Texture.cpp b/src/crepe/api/Texture.cpp index 5ebd23d..8ddeac5 100644 --- a/src/crepe/api/Texture.cpp +++ b/src/crepe/api/Texture.cpp @@ -1,7 +1,7 @@ #include #include "../facade/SDLContext.h" -#include "../util/log.h" +#include "../util/Log.h" #include "Asset.h" #include "Texture.h" diff --git a/src/crepe/api/Transform.cpp b/src/crepe/api/Transform.cpp index e401120..e9108c4 100644 --- a/src/crepe/api/Transform.cpp +++ b/src/crepe/api/Transform.cpp @@ -1,4 +1,4 @@ -#include "util/log.h" +#include "../util/Log.h" #include "Transform.h" diff --git a/src/crepe/facade/DB.cpp b/src/crepe/facade/DB.cpp index 405f7c4..ec6191b 100644 --- a/src/crepe/facade/DB.cpp +++ b/src/crepe/facade/DB.cpp @@ -1,7 +1,7 @@ #include #include "Exception.h" -#include "util/log.h" +#include "util/Log.h" #include "DB.h" diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 39d0d4d..a28f7bd 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -15,7 +15,7 @@ #include "../api/Sprite.h" #include "../api/Texture.h" #include "../api/Transform.h" -#include "../util/log.h" +#include "../util/Log.h" #include "Exception.h" #include "SDLContext.h" diff --git a/src/crepe/facade/Sound.cpp b/src/crepe/facade/Sound.cpp index 648ec81..49fb8dc 100644 --- a/src/crepe/facade/Sound.cpp +++ b/src/crepe/facade/Sound.cpp @@ -1,4 +1,4 @@ -#include "../util/log.h" +#include "../util/Log.h" #include "Sound.h" #include "SoundContext.h" diff --git a/src/crepe/facade/SoundContext.cpp b/src/crepe/facade/SoundContext.cpp index 5e5a3a9..deb2b62 100644 --- a/src/crepe/facade/SoundContext.cpp +++ b/src/crepe/facade/SoundContext.cpp @@ -1,4 +1,4 @@ -#include "../util/log.h" +#include "../util/Log.h" #include "SoundContext.h" diff --git a/src/crepe/system/AnimatorSystem.cpp b/src/crepe/system/AnimatorSystem.cpp index bf45362..1c101fa 100644 --- a/src/crepe/system/AnimatorSystem.cpp +++ b/src/crepe/system/AnimatorSystem.cpp @@ -5,7 +5,7 @@ #include "api/Animator.h" #include "facade/SDLContext.h" -#include "util/log.h" +#include "util/Log.h" #include "AnimatorSystem.h" #include "ComponentManager.h" diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index 10211a3..3ff5b4f 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -5,7 +5,7 @@ #include "../api/Sprite.h" #include "../api/Transform.h" #include "../facade/SDLContext.h" -#include "../util/log.h" +#include "../util/Log.h" #include "RenderSystem.h" diff --git a/src/crepe/system/ScriptSystem.cpp b/src/crepe/system/ScriptSystem.cpp index f2673e7..aceb218 100644 --- a/src/crepe/system/ScriptSystem.cpp +++ b/src/crepe/system/ScriptSystem.cpp @@ -5,7 +5,7 @@ #include "../ComponentManager.h" #include "../api/BehaviorScript.h" #include "../api/Script.h" -#include "../util/log.h" +#include "../util/Log.h" #include "ScriptSystem.h" diff --git a/src/crepe/util/CMakeLists.txt b/src/crepe/util/CMakeLists.txt index 0fa4343..4be738a 100644 --- a/src/crepe/util/CMakeLists.txt +++ b/src/crepe/util/CMakeLists.txt @@ -1,13 +1,12 @@ target_sources(crepe PUBLIC LogColor.cpp - log.cpp - fmt.cpp + Log.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES LogColor.h - log.h - fmt.h + Log.h + Log.hpp Proxy.h Proxy.hpp ) diff --git a/src/crepe/util/Log.cpp b/src/crepe/util/Log.cpp new file mode 100644 index 0000000..346e08e --- /dev/null +++ b/src/crepe/util/Log.cpp @@ -0,0 +1,39 @@ +#include +#include +#include +#include + +#include "../api/Config.h" +#include "Log.h" + +using namespace crepe; +using namespace std; + +string Log::prefix(const Level & level) { + switch (level) { + case Level::TRACE: + return LogColor().fg_white().str("[TRACE]") + " "; + case Level::DEBUG: + return LogColor().fg_magenta().str("[DEBUG]") + " "; + case Level::INFO: + return LogColor().fg_blue().str("[INFO]") + " "; + case Level::WARNING: + return LogColor().fg_yellow().str("[WARN]") + " "; + case Level::ERROR: + return LogColor().fg_red().str("[ERROR]") + " "; + } + return ""; +} + +void Log::log(const Level & level, const string & msg) { + auto & cfg = Config::get_instance(); + if (level < cfg.log.level) return; + + string out = Log::prefix(level) + msg; + if (!out.ends_with("\n")) out += "\n"; + + // TODO: also log to file or smth + fwrite(out.c_str(), 1, out.size(), stdout); + fflush(stdout); +} + diff --git a/src/crepe/util/Log.h b/src/crepe/util/Log.h new file mode 100644 index 0000000..4e32e9d --- /dev/null +++ b/src/crepe/util/Log.h @@ -0,0 +1,80 @@ +#pragma once + +#include + +// allow user to disable debug macros +#ifndef CREPE_DISABLE_MACROS + +#include "LogColor.h" + +// utility macros +#define _crepe_logf_here(level, fmt, ...) crepe::Log::logf(level, "{}" fmt, crepe::LogColor().fg_white(false).str(std::format("{} ({}:{})", __PRETTY_FUNCTION__, __FILE_NAME__, __LINE__)), __VA_ARGS__) + +// very illegal global function-style macros +// NOLINTBEGIN +#define dbg_logf(fmt, ...) \ + _crepe_logf_here(crepe::Log::Level::DEBUG, ": " fmt, __VA_ARGS__) +#define dbg_log(str) _crepe_logf_here(crepe::Log::Level::DEBUG, ": {}", str) +#define dbg_trace() _crepe_logf_here(crepe::Log::Level::TRACE, "", "") +// NOLINTEND + +#endif + +namespace crepe { + +/** + * \brief Logging utility + * + * This class is used to output log messages to the console and/or log files. + */ +class Log { +public: + //! Log message severity + enum Level { + TRACE, //< Include (internal) function calls + DEBUG, //< Include dbg_logf output + INFO, //< General-purpose messages + WARNING, //< Non-fatal errors + ERROR, //< Fatal errors + }; + + /** + * \brief Log a formatted message + * + * \param level Message severity + * \param msg Formatted message + */ + static void log(const Level & level, const std::string & msg); + + /** + * \brief Format a message and log it + * + * \param level Message severity + * \param fmt Message format + * \param args Format arguments + */ + template + static void logf(const Level & level, std::format_string fmt, Args&&... args); + + /** + * \brief Format a message and log it (with default severity \c INFO) + * + * \param fmt Message format + * \param args Format arguments + */ + template + static void logf(std::format_string fmt, Args&&... args); + +private: + /** + * \brief Output a message prefix depending on the log level + * + * \param level Message severity + */ + static std::string prefix(const Level & level); +}; + +} // namespace crepe + +#include "Log.hpp" + diff --git a/src/crepe/util/Log.hpp b/src/crepe/util/Log.hpp new file mode 100644 index 0000000..58ba475 --- /dev/null +++ b/src/crepe/util/Log.hpp @@ -0,0 +1,18 @@ +#pragma once + +#include "Log.h" + +namespace crepe { + +template +void Log::logf(std::format_string fmt, Args&&... args) { + Log::logf(Level::INFO, fmt, std::forward(args)...); +} + +template +void Log::logf(const Level & level, std::format_string fmt, Args&&... args) { + Log::log(level, std::format(fmt, std::forward(args)...)); +} + +} + diff --git a/src/crepe/util/LogColor.cpp b/src/crepe/util/LogColor.cpp index b5fe3ea..ae44d72 100644 --- a/src/crepe/util/LogColor.cpp +++ b/src/crepe/util/LogColor.cpp @@ -3,14 +3,12 @@ #include "../api/Config.h" #include "LogColor.h" -#include "fmt.h" - using namespace crepe; using namespace std; static constexpr const char * RESET_CODE = "\e[0m"; -const string LogColor::str(const string & content) { +const string LogColor::str(const string & content) const { auto & cfg = Config::get_instance(); string out = content; if (cfg.log.color) out = this->code + out; @@ -19,21 +17,8 @@ const string LogColor::str(const string & content) { return out; } -const char * LogColor::c_str(const char * content) { - this->final = this->str(content == NULL ? "" : content); - return this->final.c_str(); -} - -const char * LogColor::fmt(const char * fmt, ...) { - va_list args; - va_start(args, fmt); - string content = va_stringf(args, fmt); - va_end(args); - return this->c_str(content.c_str()); -} - LogColor & LogColor::add_code(unsigned int code) { - this->code += stringf("\e[%dm", code); + this->code += format("\e[{}m", code); return *this; } diff --git a/src/crepe/util/LogColor.h b/src/crepe/util/LogColor.h index c1170cb..4b65127 100644 --- a/src/crepe/util/LogColor.h +++ b/src/crepe/util/LogColor.h @@ -4,20 +4,19 @@ namespace crepe { +/** + * \brief Utility class for coloring text using ANSI escape codes + * + * \note Most methods in this class return a reference to \c this, which may be + * used to chain multiple display attributes. + */ class LogColor { public: - LogColor() = default; + //! Get color code as stl string (or color content string) + const std::string str(const std::string & content = "") const; public: - //! get color code as c-style string (or color content string) - const char * c_str(const char * content = NULL); - //! color printf-style format string - const char * fmt(const char * fmt, ...); - //! get color code as stl string (or color content string) - const std::string str(const std::string & content = ""); - -public: - //! reset color to default foreground and background color + //! Reset color to default foreground and background color LogColor & reset(); public: @@ -41,11 +40,19 @@ public: LogColor & bg_white(bool bright = false); private: + /** + * \brief Append SGR escape sequence to \c this->code + * + * \param code SGR attribute number + * + * See for magic number + * reference. + */ LogColor & add_code(unsigned int code); private: + //! Color escape sequence std::string code = ""; - std::string final = ""; }; } // namespace crepe diff --git a/src/crepe/util/fmt.cpp b/src/crepe/util/fmt.cpp deleted file mode 100644 index 4b50da8..0000000 --- a/src/crepe/util/fmt.cpp +++ /dev/null @@ -1,35 +0,0 @@ -#include -#include -#include - -#include "fmt.h" - -using namespace std; - -string crepe::va_stringf(va_list args, const char * fmt) { - string out; - - va_list args_copy; - va_copy(args_copy, args); - size_t length = vsnprintf(NULL, 0, fmt, args_copy); - // resize to include terminating null byte - out.resize(length + 1); - va_end(args_copy); - - // vsnprintf adds terminating null byte - vsnprintf(out.data(), out.size(), fmt, args); - // resize to actual length - out.resize(length); - - va_end(args); - - return out; -} - -string crepe::stringf(const char * fmt, ...) { - va_list args; - va_start(args, fmt); - string out = va_stringf(args, fmt); - va_end(args); - return out; -} diff --git a/src/crepe/util/fmt.h b/src/crepe/util/fmt.h deleted file mode 100644 index e319e6e..0000000 --- a/src/crepe/util/fmt.h +++ /dev/null @@ -1,10 +0,0 @@ -#pragma once - -#include - -namespace crepe { - -std::string va_stringf(va_list args, const char * fmt); -std::string stringf(const char * fmt, ...); - -} // namespace crepe diff --git a/src/crepe/util/log.cpp b/src/crepe/util/log.cpp deleted file mode 100644 index 4a8f8e8..0000000 --- a/src/crepe/util/log.cpp +++ /dev/null @@ -1,53 +0,0 @@ -#include -#include -#include -#include - -#include "../api/Config.h" -#include "fmt.h" -#include "log.h" - -using namespace crepe; -using namespace std; - -string log_prefix(LogLevel level) { - switch (level) { - case LogLevel::TRACE: - return LogColor().fg_white().str("[TRACE]") + " "; - case LogLevel::DEBUG: - return LogColor().fg_magenta().str("[DEBUG]") + " "; - case LogLevel::INFO: - return LogColor().fg_blue().str("[INFO]") + " "; - case LogLevel::WARNING: - return LogColor().fg_yellow().str("[WARN]") + " "; - case LogLevel::ERROR: - return LogColor().fg_red().str("[ERROR]") + " "; - } - return ""; -} - -static void log(LogLevel level, const string msg) { - auto & cfg = Config::get_instance(); - if (level < cfg.log.level) return; - - string out = log_prefix(level) + msg; - if (!out.ends_with("\n")) out += "\n"; - - // TODO: also log to file or smth - fwrite(out.c_str(), 1, out.size(), stdout); - fflush(stdout); -} - -void crepe::logf(const char * fmt, ...) { - va_list args; - va_start(args, fmt); - log(LogLevel::DEBUG, va_stringf(args, fmt)); - va_end(args); -} - -void crepe::logf(LogLevel level, const char * fmt, ...) { - va_list args; - va_start(args, fmt); - log(level, va_stringf(args, fmt)); - va_end(args); -} diff --git a/src/crepe/util/log.h b/src/crepe/util/log.h deleted file mode 100644 index 5a1cf00..0000000 --- a/src/crepe/util/log.h +++ /dev/null @@ -1,39 +0,0 @@ -#pragma once - -// allow user to disable debug macros -#ifndef CREPE_DISABLE_MACROS - -#include "LogColor.h" - -// utility macros -#define _crepe_logf_here(level, format, ...) \ - crepe::logf( \ - level, "%s" format, \ - crepe::LogColor().fg_white(false).fmt( \ - "%s (%s:%d)", __PRETTY_FUNCTION__, __FILE_NAME__, __LINE__), \ - __VA_ARGS__) - -// very illegal global function-style macros -// NOLINTBEGIN -#define dbg_logf(fmt, ...) \ - _crepe_logf_here(crepe::LogLevel::DEBUG, ": " fmt, __VA_ARGS__) -#define dbg_log(str) _crepe_logf_here(crepe::LogLevel::DEBUG, "%s: " str, "") -#define dbg_trace() _crepe_logf_here(crepe::LogLevel::TRACE, "%s", "") -// NOLINTEND - -#endif - -namespace crepe { - -enum LogLevel { - TRACE, - DEBUG, - INFO, - WARNING, - ERROR, -}; - -void logf(const char * fmt, ...); -void logf(enum LogLevel level, const char * fmt, ...); - -} // namespace crepe diff --git a/src/example/log.cpp b/src/example/log.cpp index db8aa48..13d592b 100644 --- a/src/example/log.cpp +++ b/src/example/log.cpp @@ -4,7 +4,7 @@ */ #include -#include +#include using namespace crepe; @@ -12,17 +12,18 @@ 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; } -- cgit v1.2.3 From 0aa97807cb9b7f1d1850449dd5a5e45b961445ce Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 13 Nov 2024 18:46:06 +0100 Subject: fix scene_manager example --- src/crepe/ComponentManager.cpp | 8 +++----- src/crepe/ComponentManager.h | 2 +- src/crepe/api/Scene.cpp | 2 +- src/crepe/api/Scene.h | 10 ++++++++-- src/crepe/api/SceneManager.hpp | 9 ++++++--- src/example/components_internal.cpp | 2 +- src/example/ecs.cpp | 15 +++++---------- src/example/physics.cpp | 3 +-- src/example/rendering.cpp | 6 +++--- src/example/scene_manager.cpp | 25 +++++++++++++++---------- src/example/script.cpp | 2 +- 11 files changed, 45 insertions(+), 39 deletions(-) (limited to 'src/crepe/ComponentManager.cpp') diff --git a/src/crepe/ComponentManager.cpp b/src/crepe/ComponentManager.cpp index c6b658c..1e23609 100644 --- a/src/crepe/ComponentManager.cpp +++ b/src/crepe/ComponentManager.cpp @@ -25,13 +25,11 @@ void ComponentManager::delete_all_components() { ComponentManager::ComponentManager() { dbg_trace(); } ComponentManager::~ComponentManager() { dbg_trace(); } -GameObject & ComponentManager::new_object(const string & name, +GameObject ComponentManager::new_object(const string & name, const string & tag, const Vector2 & position, double rotation, double scale) { - GameObject * object = new GameObject(*this, this->next_id, name, tag, - position, rotation, scale); - this->objects.push_front(unique_ptr(object)); + GameObject object{*this, this->next_id, name, tag, position, rotation, scale}; this->next_id++; - return *object; + return object; } diff --git a/src/crepe/ComponentManager.h b/src/crepe/ComponentManager.h index 75606e0..31a8bfa 100644 --- a/src/crepe/ComponentManager.h +++ b/src/crepe/ComponentManager.h @@ -101,7 +101,7 @@ public: std::vector> get_components_by_type() const; // TODO: doxygen - GameObject & new_object(const std::string & name, + GameObject new_object(const std::string & name, const std::string & tag = "", const Vector2 & position = {0, 0}, double rotation = 0, double scale = 0); diff --git a/src/crepe/api/Scene.cpp b/src/crepe/api/Scene.cpp index 933edf4..008d689 100644 --- a/src/crepe/api/Scene.cpp +++ b/src/crepe/api/Scene.cpp @@ -2,4 +2,4 @@ using namespace crepe; -Scene::Scene(const std::string & name) : name(name) {} +Scene::Scene(ComponentManager & mgr, const std::string & name) : component_manager(mgr), name(name) {} diff --git a/src/crepe/api/Scene.h b/src/crepe/api/Scene.h index f8bcc3d..334f306 100644 --- a/src/crepe/api/Scene.h +++ b/src/crepe/api/Scene.h @@ -4,14 +4,20 @@ namespace crepe { +class ComponentManager; + class Scene { public: - Scene(const std::string & name); + Scene(ComponentManager & mgr, const std::string & name); virtual ~Scene() = default; + virtual void load_scene() = 0; public: - std::string name; + const std::string name; + +protected: + ComponentManager & component_manager; }; } // namespace crepe diff --git a/src/crepe/api/SceneManager.hpp b/src/crepe/api/SceneManager.hpp index 8bad7b2..714f690 100644 --- a/src/crepe/api/SceneManager.hpp +++ b/src/crepe/api/SceneManager.hpp @@ -1,13 +1,16 @@ +#pragma once + #include "SceneManager.h" namespace crepe { template void SceneManager::add_scene(const std::string & name) { - static_assert(std::is_base_of::value, - "T must be derived from Scene"); + using namespace std; + static_assert(is_base_of::value, "T must be derived from Scene"); - scenes.emplace_back(make_unique(name)); + Scene * scene = new T(this->component_manager, name); + this->scenes.emplace_back(unique_ptr(scene)); // The first scene added, is the one that will be loaded at the beginning if (next_scene.empty()) { diff --git a/src/example/components_internal.cpp b/src/example/components_internal.cpp index 7a8a08a..eeecdc0 100644 --- a/src/example/components_internal.cpp +++ b/src/example/components_internal.cpp @@ -30,7 +30,7 @@ int main() { GameObject * game_object[OBJ_COUNT]; for (int i = 0; i < OBJ_COUNT; ++i) { - GameObject & obj = mgr.new_object("Name", "Tag"); + GameObject obj = mgr.new_object("Name", "Tag"); obj.add_component("test"); obj.add_component(0, 0, i); } diff --git a/src/example/ecs.cpp b/src/example/ecs.cpp index 9a008ea..06fc563 100644 --- a/src/example/ecs.cpp +++ b/src/example/ecs.cpp @@ -13,16 +13,11 @@ int main() { // Create a few GameObjects try { - 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); + 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); diff --git a/src/example/physics.cpp b/src/example/physics.cpp index 2ebf779..ad663a0 100644 --- a/src/example/physics.cpp +++ b/src/example/physics.cpp @@ -11,8 +11,7 @@ using namespace std; int main(int argc, char * argv[]) { ComponentManager mgr{}; - GameObject & game_object - = mgr.new_object("Name", "Tag", Vector2{0, 0}, 0, 0); + GameObject game_object = mgr.new_object("Name", "Tag", Vector2{0, 0}, 0, 0); game_object.add_component(Rigidbody::Data{ .mass = 1, .gravity_scale = 1, diff --git a/src/example/rendering.cpp b/src/example/rendering.cpp index 74c00e2..abd11b1 100644 --- a/src/example/rendering.cpp +++ b/src/example/rendering.cpp @@ -23,9 +23,9 @@ int main() { ComponentManager mgr{}; RenderSystem sys{mgr}; - auto & obj = mgr.new_object("name", "tag", Vector2{0, 0}, 1, 1); - auto & obj1 = mgr.new_object("name", "tag", Vector2{500, 0}, 1, 0.1); - auto & obj2 = mgr.new_object("name", "tag", Vector2{800, 0}, 1, 0.1); + 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 { diff --git a/src/example/scene_manager.cpp b/src/example/scene_manager.cpp index 5cd7336..1c982aa 100644 --- a/src/example/scene_manager.cpp +++ b/src/example/scene_manager.cpp @@ -12,24 +12,26 @@ 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); } }; @@ -41,7 +43,9 @@ int main() { scene_mgr.add_scene("scene1"); scene_mgr.add_scene("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(); @@ -73,3 +77,4 @@ int main() { return 0; } + diff --git a/src/example/script.cpp b/src/example/script.cpp index c82764c..a23295b 100644 --- a/src/example/script.cpp +++ b/src/example/script.cpp @@ -39,7 +39,7 @@ int main() { ScriptSystem system{component_manager}; // Create game object with Transform and BehaviorScript components - auto & obj = component_manager.new_object("name"); + GameObject obj = component_manager.new_object("name"); obj.add_component().set_script(); // Update all scripts. This should result in MyScript::update being called -- cgit v1.2.3 From d69f1642cb397d68d591718f83028e14620ab340 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 13 Nov 2024 19:22:23 +0100 Subject: `make format` --- src/crepe/ComponentManager.cpp | 10 +++++----- src/crepe/ComponentManager.h | 6 +++--- src/crepe/api/Scene.cpp | 4 +++- src/example/ecs.cpp | 12 ++++++++---- src/example/scene_manager.cpp | 22 ++++++++++++++-------- 5 files changed, 33 insertions(+), 21 deletions(-) (limited to 'src/crepe/ComponentManager.cpp') diff --git a/src/crepe/ComponentManager.cpp b/src/crepe/ComponentManager.cpp index 1e23609..7af0380 100644 --- a/src/crepe/ComponentManager.cpp +++ b/src/crepe/ComponentManager.cpp @@ -25,11 +25,11 @@ void ComponentManager::delete_all_components() { ComponentManager::ComponentManager() { dbg_trace(); } ComponentManager::~ComponentManager() { dbg_trace(); } -GameObject ComponentManager::new_object(const string & name, - const string & tag, - const Vector2 & position, - double rotation, double scale) { - GameObject object{*this, this->next_id, name, tag, position, rotation, scale}; +GameObject ComponentManager::new_object(const string & name, const string & tag, + const Vector2 & position, + double rotation, double scale) { + GameObject object{*this, this->next_id, name, tag, + position, rotation, scale}; this->next_id++; return object; } diff --git a/src/crepe/ComponentManager.h b/src/crepe/ComponentManager.h index 31a8bfa..51c84a4 100644 --- a/src/crepe/ComponentManager.h +++ b/src/crepe/ComponentManager.h @@ -102,9 +102,9 @@ public: // TODO: doxygen GameObject new_object(const std::string & name, - const std::string & tag = "", - const Vector2 & position = {0, 0}, - double rotation = 0, double scale = 0); + const std::string & tag = "", + const Vector2 & position = {0, 0}, + double rotation = 0, double scale = 0); private: /** diff --git a/src/crepe/api/Scene.cpp b/src/crepe/api/Scene.cpp index 008d689..88aa82d 100644 --- a/src/crepe/api/Scene.cpp +++ b/src/crepe/api/Scene.cpp @@ -2,4 +2,6 @@ using namespace crepe; -Scene::Scene(ComponentManager & mgr, const std::string & name) : component_manager(mgr), name(name) {} +Scene::Scene(ComponentManager & mgr, const std::string & name) + : component_manager(mgr), + name(name) {} diff --git a/src/example/ecs.cpp b/src/example/ecs.cpp index 06fc563..5f83da1 100644 --- a/src/example/ecs.cpp +++ b/src/example/ecs.cpp @@ -14,10 +14,14 @@ int main() { // Create a few GameObjects try { 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); + 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); diff --git a/src/example/scene_manager.cpp b/src/example/scene_manager.cpp index 1c982aa..24ab72e 100644 --- a/src/example/scene_manager.cpp +++ b/src/example/scene_manager.cpp @@ -16,9 +16,12 @@ public: void load_scene() { 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); + 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); } }; @@ -28,10 +31,14 @@ public: void load_scene() { 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); + 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); } }; @@ -77,4 +84,3 @@ int main() { return 0; } - -- cgit v1.2.3 From 5bee4515c1089ce3499bc3b74780db94f0c02306 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Fri, 15 Nov 2024 20:26:26 +0100 Subject: process feedback on #26 --- src/crepe/CMakeLists.txt | 3 --- src/crepe/Component.h | 2 ++ src/crepe/ComponentManager.cpp | 11 +++++------ src/crepe/ComponentManager.h | 3 ++- src/crepe/ComponentManager.hpp | 1 - src/crepe/Exception.cpp | 8 -------- src/crepe/Exception.h | 31 ------------------------------- src/crepe/Exception.hpp | 12 ------------ src/crepe/api/Animator.cpp | 2 -- src/crepe/api/Animator.h | 2 -- src/crepe/api/BehaviorScript.h | 3 +-- src/crepe/api/CMakeLists.txt | 1 - src/crepe/api/Camera.h | 2 -- src/crepe/api/Config.h | 17 +++++++++++------ src/crepe/api/LoopManager.hpp | 4 ++-- src/crepe/api/LoopTimer.h | 1 - src/crepe/api/Rigidbody.h | 2 -- src/crepe/api/SceneManager.h | 3 --- src/crepe/api/Script.cpp | 3 --- src/crepe/api/Script.hpp | 8 +++----- src/crepe/api/Sprite.h | 1 - src/crepe/api/Transform.h | 8 +++++--- src/crepe/facade/DB.cpp | 24 +++++++++++++++--------- src/crepe/facade/DB.h | 7 ++++--- src/crepe/facade/SDLContext.cpp | 5 ++--- src/crepe/util/Log.cpp | 9 ++++----- src/crepe/util/Log.h | 2 ++ src/crepe/util/LogColor.cpp | 1 + src/crepe/util/LogColor.h | 24 +++++++++++++++++++++++- src/example/particles.cpp | 3 ++- 30 files changed, 84 insertions(+), 119 deletions(-) delete mode 100644 src/crepe/Exception.cpp delete mode 100644 src/crepe/Exception.h delete mode 100644 src/crepe/Exception.hpp delete mode 100644 src/crepe/api/Script.cpp (limited to 'src/crepe/ComponentManager.cpp') diff --git a/src/crepe/CMakeLists.txt b/src/crepe/CMakeLists.txt index 52a781e..3b05742 100644 --- a/src/crepe/CMakeLists.txt +++ b/src/crepe/CMakeLists.txt @@ -4,7 +4,6 @@ target_sources(crepe PUBLIC ComponentManager.cpp Component.cpp Collider.cpp - Exception.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES @@ -15,8 +14,6 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES Collider.h ValueBroker.h ValueBroker.hpp - Exception.h - Exception.hpp ) add_subdirectory(api) diff --git a/src/crepe/Component.h b/src/crepe/Component.h index 12c10cb..670446f 100644 --- a/src/crepe/Component.h +++ b/src/crepe/Component.h @@ -26,6 +26,8 @@ protected: Component(game_object_id_t id); //! Only the ComponentManager can create components friend class ComponentManager; +public: + virtual ~Component() = default; public: /** diff --git a/src/crepe/ComponentManager.cpp b/src/crepe/ComponentManager.cpp index 7af0380..67c6fc7 100644 --- a/src/crepe/ComponentManager.cpp +++ b/src/crepe/ComponentManager.cpp @@ -6,6 +6,9 @@ using namespace crepe; using namespace std; +ComponentManager::ComponentManager() { dbg_trace(); } +ComponentManager::~ComponentManager() { dbg_trace(); } + void ComponentManager::delete_all_components_of_id(game_object_id_t id) { // Loop through all the types (in the unordered_map<>) for (auto & [type, componentArray] : this->components) { @@ -18,18 +21,14 @@ void ComponentManager::delete_all_components_of_id(game_object_id_t id) { } void ComponentManager::delete_all_components() { - // Clear the whole unordered_map<> this->components.clear(); + this->next_id = 0; } -ComponentManager::ComponentManager() { dbg_trace(); } -ComponentManager::~ComponentManager() { dbg_trace(); } - GameObject ComponentManager::new_object(const string & name, const string & tag, const Vector2 & position, double rotation, double scale) { - GameObject object{*this, this->next_id, name, tag, - position, rotation, scale}; + GameObject object{*this, this->next_id, name, tag, position, rotation, scale}; this->next_id++; return object; } diff --git a/src/crepe/ComponentManager.h b/src/crepe/ComponentManager.h index 51c84a4..ca2e7c6 100644 --- a/src/crepe/ComponentManager.h +++ b/src/crepe/ComponentManager.h @@ -6,9 +6,10 @@ #include #include -#include "Component.h" #include "api/Vector2.h" +#include "Component.h" + namespace crepe { class GameObject; diff --git a/src/crepe/ComponentManager.hpp b/src/crepe/ComponentManager.hpp index 98efb49..0a84468 100644 --- a/src/crepe/ComponentManager.hpp +++ b/src/crepe/ComponentManager.hpp @@ -40,7 +40,6 @@ T & ComponentManager::add_component(game_object_id_t id, Args &&... args) { // Check if the vector size is not greater than get_instances_max int max_instances = instance->get_instances_max(); if (max_instances != -1 && components[type][id].size() >= max_instances) { - // TODO: Exception throw std::runtime_error( "Exceeded maximum number of instances for this component type"); } diff --git a/src/crepe/Exception.cpp b/src/crepe/Exception.cpp deleted file mode 100644 index 5a24e7e..0000000 --- a/src/crepe/Exception.cpp +++ /dev/null @@ -1,8 +0,0 @@ -#include "Exception.h" - -using namespace crepe; -using namespace std; - -const char * Exception::what() const noexcept { return error.c_str(); } - -Exception::Exception(const string & msg) { this->error = msg; } diff --git a/src/crepe/Exception.h b/src/crepe/Exception.h deleted file mode 100644 index 580fc16..0000000 --- a/src/crepe/Exception.h +++ /dev/null @@ -1,31 +0,0 @@ -#pragma once - -#include -#include -#include - -namespace crepe { - -//! Exception class -class Exception : public std::exception { -public: - //! Exception with plain message - Exception(const std::string & msg); - - //! Exception with \c std::format message - template - Exception(std::format_string fmt, Args &&... args); - - //! Get formatted error message - const char * what() const noexcept; - -protected: - Exception() = default; - - //! Formatted error message - std::string error; -}; - -} // namespace crepe - -#include "Exception.hpp" diff --git a/src/crepe/Exception.hpp b/src/crepe/Exception.hpp deleted file mode 100644 index 7c462a3..0000000 --- a/src/crepe/Exception.hpp +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once - -#include "Exception.h" - -namespace crepe { - -template -Exception::Exception(std::format_string fmt, Args &&... args) { - this->error = std::format(fmt, std::forward(args)...); -} - -} // namespace crepe diff --git a/src/crepe/api/Animator.cpp b/src/crepe/api/Animator.cpp index 54b2ec3..cccbc67 100644 --- a/src/crepe/api/Animator.cpp +++ b/src/crepe/api/Animator.cpp @@ -1,6 +1,4 @@ -#include - #include "util/Log.h" #include "Animator.h" diff --git a/src/crepe/api/Animator.h b/src/crepe/api/Animator.h index 75b8139..3573403 100644 --- a/src/crepe/api/Animator.h +++ b/src/crepe/api/Animator.h @@ -1,7 +1,5 @@ #pragma once -#include - #include "Component.h" #include "Sprite.h" diff --git a/src/crepe/api/BehaviorScript.h b/src/crepe/api/BehaviorScript.h index 2982358..1a8910d 100644 --- a/src/crepe/api/BehaviorScript.h +++ b/src/crepe/api/BehaviorScript.h @@ -3,6 +3,7 @@ #include #include "../Component.h" + #include "GameObject.h" namespace crepe { @@ -47,8 +48,6 @@ protected: std::unique_ptr