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/CMakeLists.txt | 1 - src/crepe/Asset.cpp | 18 ++++++---- src/crepe/Asset.h | 2 +- src/crepe/Collider.cpp | 1 - src/crepe/Collider.h | 2 +- src/crepe/Component.cpp | 4 +-- src/crepe/Component.h | 21 ++++++----- src/crepe/ComponentManager.cpp | 16 +++++---- src/crepe/ComponentManager.h | 27 +++++++-------- src/crepe/ComponentManager.hpp | 5 ++- src/crepe/Resource.h | 33 ++++++++++++++++++ src/crepe/api/Animator.cpp | 4 +-- src/crepe/api/Animator.h | 3 +- src/crepe/api/BehaviorScript.cpp | 4 +++ src/crepe/api/BehaviorScript.h | 20 +++++++++-- src/crepe/api/BehaviorScript.hpp | 4 +-- src/crepe/api/CMakeLists.txt | 10 +++--- src/crepe/api/Camera.cpp | 7 ++-- src/crepe/api/Camera.h | 2 +- src/crepe/api/GameObject.cpp | 9 ++--- src/crepe/api/GameObject.h | 17 ++++++--- src/crepe/api/GameObject.hpp | 2 +- src/crepe/api/Metadata.cpp | 4 +-- src/crepe/api/Metadata.h | 2 +- src/crepe/api/ParticleEmitter.cpp | 4 +-- src/crepe/api/ParticleEmitter.h | 2 +- src/crepe/api/Rigidbody.cpp | 4 +-- src/crepe/api/Rigidbody.h | 2 +- src/crepe/api/Script.cpp | 1 + src/crepe/api/Script.h | 15 ++++++-- src/crepe/api/Script.hpp | 6 ++-- src/crepe/api/Sprite.cpp | 4 +-- src/crepe/api/Sprite.h | 2 +- src/crepe/api/Transform.cpp | 4 +-- src/crepe/api/Transform.h | 2 +- src/crepe/facade/Sound.cpp | 31 ++++++++--------- src/crepe/facade/Sound.h | 14 ++++---- src/crepe/facade/SoundContext.cpp | 6 +--- src/crepe/facade/SoundContext.h | 13 ++++--- src/crepe/system/CMakeLists.txt | 19 +++++----- src/crepe/system/ScriptSystem.cpp | 23 ++++++++---- src/crepe/system/ScriptSystem.h | 5 +-- src/crepe/system/System.cpp | 6 ++++ src/crepe/system/System.h | 7 +++- src/test/CMakeLists.txt | 6 ++-- src/test/PhysicsTest.cpp | 4 ++- src/test/ScriptTest.cpp | 73 +++++++++++++++++++++++++++++++++++++++ src/test/audio.cpp | 10 ------ src/test/dummy.cpp | 3 -- src/test/main.cpp | 15 ++++++++ 50 files changed, 338 insertions(+), 161 deletions(-) create mode 100644 src/crepe/Resource.h create mode 100644 src/crepe/system/System.cpp create mode 100644 src/test/ScriptTest.cpp delete mode 100644 src/test/audio.cpp delete mode 100644 src/test/dummy.cpp create mode 100644 src/test/main.cpp (limited to 'src') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e4922df..445a8b2 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -38,6 +38,5 @@ install( target_link_libraries(test_main PRIVATE gtest - PRIVATE gtest_main PUBLIC crepe ) diff --git a/src/crepe/Asset.cpp b/src/crepe/Asset.cpp index 8a2a11c..3d4df53 100644 --- a/src/crepe/Asset.cpp +++ b/src/crepe/Asset.cpp @@ -1,16 +1,20 @@ #include #include "Asset.h" +#include "Exception.h" using namespace crepe; +using namespace std; -Asset::Asset(const std::string & src) { - // FIXME: restore this - // this->src = std::filesystem::canonical(src); - this->src = src; - this->file = std::ifstream(this->src, std::ios::in | std::ios::binary); +Asset::Asset(const string & src) : src(src) { + try { + this->src = filesystem::canonical(src); + } catch (filesystem::filesystem_error & e) { + throw Exception("Asset error: %s", e.what()); + } + this->file = ifstream(this->src, ios::in | ios::binary); } -const std::istream & Asset::read() { return this->file; } +const istream & Asset::read() { return this->file; } -const char * Asset::canonical() { return this->src.c_str(); } +const char * Asset::canonical() const { return this->src.c_str(); } diff --git a/src/crepe/Asset.h b/src/crepe/Asset.h index 0cb5834..d4e4ba1 100644 --- a/src/crepe/Asset.h +++ b/src/crepe/Asset.h @@ -23,7 +23,7 @@ public: //! Get an input stream to the contents of this resource const std::istream & read(); //! Get the canonical path to this resource - const char * canonical(); + const char * canonical() const; private: std::string src; diff --git a/src/crepe/Collider.cpp b/src/crepe/Collider.cpp index bbec488..b408609 100644 --- a/src/crepe/Collider.cpp +++ b/src/crepe/Collider.cpp @@ -2,4 +2,3 @@ using namespace crepe; -Collider::Collider(game_object_id_t id) : Component(id) {} diff --git a/src/crepe/Collider.h b/src/crepe/Collider.h index 827f83d..a1e9095 100644 --- a/src/crepe/Collider.h +++ b/src/crepe/Collider.h @@ -6,7 +6,7 @@ namespace crepe { class Collider : public Component { public: - Collider(game_object_id_t id); + using Component::Component; int size; }; diff --git a/src/crepe/Component.cpp b/src/crepe/Component.cpp index 73c2d07..c133739 100644 --- a/src/crepe/Component.cpp +++ b/src/crepe/Component.cpp @@ -1,6 +1,6 @@ #include "Component.h" -#include "types.h" using namespace crepe; -Component::Component(game_object_id_t id) : game_object_id(id) {} +Component::Component(const Data & data) : data(data) {} + diff --git a/src/crepe/Component.h b/src/crepe/Component.h index 0fe60b2..c6d72df 100644 --- a/src/crepe/Component.h +++ b/src/crepe/Component.h @@ -2,8 +2,6 @@ #include "types.h" -#include - namespace crepe { class ComponentManager; @@ -15,13 +13,21 @@ class ComponentManager; * interface for all components. */ class Component { +public: + struct Data { + //! The ID of the GameObject this component belongs to + const game_object_id_t id; + //! The manager of this component + ComponentManager & component_manager; + }; + protected: - //! Only the ComponentManager can create components - friend class crepe::ComponentManager; /** - * \param id The id of the GameObject this component belongs to + * \param base Data */ - Component(game_object_id_t id); + Component(const Data & base); + //! Only the ComponentManager can create components + friend class crepe::ComponentManager; public: virtual ~Component() = default; @@ -37,8 +43,7 @@ public: virtual int get_instances_max() const { return -1; } public: - //! The id of the GameObject this component belongs to - const game_object_id_t game_object_id; + Data data; //! Whether the component is active bool active = true; }; 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; +} + diff --git a/src/crepe/ComponentManager.h b/src/crepe/ComponentManager.h index c8c196c..e37bc4a 100644 --- a/src/crepe/ComponentManager.h +++ b/src/crepe/ComponentManager.h @@ -1,15 +1,18 @@ #pragma once -#include #include #include #include #include +#include #include "Component.h" +#include "api/Vector2.h" namespace crepe { +class GameObject; + /** * \brief Manages all components * @@ -18,18 +21,10 @@ namespace crepe { */ class ComponentManager { public: - /** - * \brief Get the instance of the ComponentManager - * - * \return The instance of the ComponentManager - */ - static ComponentManager & get_instance(); - ComponentManager(const ComponentManager &) = delete; - ComponentManager(ComponentManager &&) = delete; - ComponentManager & operator=(const ComponentManager &) = delete; - ComponentManager & operator=(ComponentManager &&) = delete; - ~ComponentManager(); + ComponentManager(); // dbg_trace + ~ComponentManager(); // dbg_trace +public: /** * \brief Add a component to the ComponentManager * @@ -100,8 +95,8 @@ public: template std::vector> get_components_by_type() const; -private: - ComponentManager(); + // TODO: doxygen + GameObject & new_object(const std::string & name, const std::string & tag = "", const Vector2 & position = { 0, 0 }, double rotation = 0, double scale = 0); private: /** @@ -118,6 +113,10 @@ private: std::unordered_map>>> components; + + //! ID of next GameObject + game_object_id_t next_id = 0; + std::forward_list> objects; }; } // namespace crepe diff --git a/src/crepe/ComponentManager.hpp b/src/crepe/ComponentManager.hpp index 98efb49..89a8536 100644 --- a/src/crepe/ComponentManager.hpp +++ b/src/crepe/ComponentManager.hpp @@ -31,7 +31,10 @@ T & ComponentManager::add_component(game_object_id_t id, Args &&... args) { // Create a new component of type T (arguments directly forwarded). The // constructor must be called by ComponentManager. - T * instance_ptr = new T(id, forward(args)...); + T * instance_ptr = new T(Component::Data { + .id = id, + .component_manager = *this, + }, forward(args)...); if (instance_ptr == nullptr) throw std::bad_alloc(); T & instance_ref = *instance_ptr; diff --git a/src/crepe/Resource.h b/src/crepe/Resource.h new file mode 100644 index 0000000..dcf3dbd --- /dev/null +++ b/src/crepe/Resource.h @@ -0,0 +1,33 @@ +#pragma once + +#include + +namespace crepe { + +class ResourceManager; +class Asset; + +/** + * Resource is an interface class used to represent a (deserialized) game + * resource (e.g. textures, sounds). + */ +class Resource { +private: + /** + * \brief Prototype pattern clone function. + * + * \param src Source file of new resource (abstraction for file saved on + * disk) + * + * \returns New instance of concrete resource + */ + virtual std::unique_ptr clone(const Asset & src) const = 0; + /** + * The resource manager uses \c clone to create new instances of the concrete + * resource class. This may be used to inherit references to classes that + * would otherwise need to be implemented as singletons. + */ + friend class ResourceManager; +}; + +} // namespace crepe diff --git a/src/crepe/api/Animator.cpp b/src/crepe/api/Animator.cpp index 58fee2a..ad60981 100644 --- a/src/crepe/api/Animator.cpp +++ b/src/crepe/api/Animator.cpp @@ -9,8 +9,8 @@ using namespace crepe; -Animator::Animator(uint32_t id, Sprite & ss, int row, int col, int col_animator) - : Component(id), +Animator::Animator(const Component::Data & data, Sprite & ss, int row, int col, int col_animator) + : Component(data), spritesheet(ss), row(row), col(col) { diff --git a/src/crepe/api/Animator.h b/src/crepe/api/Animator.h index def0240..f66dc1a 100644 --- a/src/crepe/api/Animator.h +++ b/src/crepe/api/Animator.h @@ -6,6 +6,7 @@ #include "Sprite.h" namespace crepe { + class AnimatorSystem; class SDLContext; @@ -35,7 +36,7 @@ public: * * This constructor sets up the Animator with the given parameters, and initializes the animation system. */ - Animator(uint32_t id, Sprite & spritesheet, int row, int col, + Animator(const Component::Data & data, Sprite & spritesheet, int row, int col, int col_animate); ~Animator(); // dbg_trace diff --git a/src/crepe/api/BehaviorScript.cpp b/src/crepe/api/BehaviorScript.cpp index e69de29..ce1cfde 100644 --- a/src/crepe/api/BehaviorScript.cpp +++ b/src/crepe/api/BehaviorScript.cpp @@ -0,0 +1,4 @@ +#include "BehaviorScript.h" + +using namespace crepe; + diff --git a/src/crepe/api/BehaviorScript.h b/src/crepe/api/BehaviorScript.h index 6b1fec7..8d21a72 100644 --- a/src/crepe/api/BehaviorScript.h +++ b/src/crepe/api/BehaviorScript.h @@ -12,19 +12,35 @@ class Script; class BehaviorScript : public Component { protected: - friend class crepe::ComponentManager; using Component::Component; + //! Only ComponentManager is allowed to instantiate BehaviorScript + friend class ComponentManager; public: virtual ~BehaviorScript() = default; public: + /** + * \brief Set the concrete script of this component + * + * \tparam T Concrete script type (derived from \c crepe::Script) + * + * \returns Reference to BehaviorScript component (`*this`) + */ template BehaviorScript & set_script(); protected: - friend class crepe::ScriptSystem; + //! ScriptSystem needs direct access to the script instance + friend class ScriptSystem; + //! Flag to indicate if script->init() has been called already + bool initialized = false; std::unique_ptr