From 8d78727d6e7badca16ba7a1328643928a0039569 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Mon, 18 Nov 2024 18:02:09 +0100 Subject: move utilities from loek/audio --- src/crepe/api/Asset.h | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 src/crepe/api/Asset.h (limited to 'src/crepe/api/Asset.h') diff --git a/src/crepe/api/Asset.h b/src/crepe/api/Asset.h new file mode 100644 index 0000000..05dccba --- /dev/null +++ b/src/crepe/api/Asset.h @@ -0,0 +1,60 @@ +#pragma once + +#include +#include + +namespace crepe { + +/** + * \brief Asset location helper + * + * This class is used to locate game asset files, and should *always* be used + * instead of reading file paths directly. + */ +class Asset { +public: + /** + * \param src Unique identifier to asset + */ + Asset(const std::string & src); + /** + * \param src Unique identifier to asset + */ + Asset(const char * src); + +public: + /** + * \brief Get the path to this asset + * \return path to this asset + */ + const std::string & get_path() const noexcept; + + /** + * \brief Comparison operator + * \param other Possibly different instance of \c Asset to test equality against + * \return True if \c this and \c other are equal + */ + bool operator == (const Asset & other) const noexcept; + +private: + //! path to asset + const std::string src; + +private: + std::string find_asset(const std::string & src) const; + /** + * \returns The path to the current executable + */ + std::string whereami() const noexcept; +}; + +} // namespace crepe + +namespace std { + +template<> struct hash { + size_t operator()(const crepe::Asset & asset) const noexcept; +}; + +} + -- cgit v1.2.3 From 9770b548c5619821d7b6ea7a017df2b5a6898d0a Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Mon, 18 Nov 2024 18:10:31 +0100 Subject: add doxygen comments --- src/crepe/api/Asset.h | 10 +++++++++- src/crepe/util/OptionalRef.h | 37 +++++++++++++++++++++++++++++++++++-- 2 files changed, 44 insertions(+), 3 deletions(-) (limited to 'src/crepe/api/Asset.h') diff --git a/src/crepe/api/Asset.h b/src/crepe/api/Asset.h index 05dccba..685dd3a 100644 --- a/src/crepe/api/Asset.h +++ b/src/crepe/api/Asset.h @@ -1,7 +1,6 @@ #pragma once #include -#include namespace crepe { @@ -52,7 +51,16 @@ private: namespace std { +//! Hash helper struct template<> struct hash { + /** + * \brief Hash operator for crepe::Asset + * + * This function hashes a crepe::Asset instance, allowing it to be used as a key in an \c + * std::unordered_map. + * + * \returns Hash value + */ size_t operator()(const crepe::Asset & asset) const noexcept; }; diff --git a/src/crepe/util/OptionalRef.h b/src/crepe/util/OptionalRef.h index 1ad3a6d..8417a25 100644 --- a/src/crepe/util/OptionalRef.h +++ b/src/crepe/util/OptionalRef.h @@ -12,18 +12,51 @@ namespace crepe { template class OptionalRef { public: + //! Initialize empty (nonexistant) reference OptionalRef() = default; - OptionalRef(T &); - OptionalRef & operator=(T &); + //! Initialize reference with value + OptionalRef(T & ref); + /** + * \brief Assign new reference + * + * \param ref Reference to assign + * + * \return Reference to this (required for operator) + */ + OptionalRef & operator=(T & ref); + /** + * \brief Check if this reference is not empty + * + * \returns `true` if reference is set, or `false` if it is not + */ explicit operator bool() const noexcept; + /** + * \brief Assign new reference + * + * \param ref Reference to assign + */ void set(T &) noexcept; + /** + * \brief Retrieve this reference + * + * \returns Internal reference if it is set + * + * \throws std::runtime_error if this function is called while the reference it not set + */ T & get() const; + /** + * \brief Make this reference empty + */ void clear() noexcept; + //! Copy constructor OptionalRef(const OptionalRef &); + //! Move constructor OptionalRef(OptionalRef &&); + //! Copy assignment OptionalRef & operator=(const OptionalRef &); + //! Move assignment OptionalRef & operator=(OptionalRef &&); private: -- cgit v1.2.3 From 02845c3d25130e9473604cb2eeee42a7a7a8eadf Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 20 Nov 2024 14:01:31 +0100 Subject: `make format` --- src/crepe/api/Asset.cpp | 15 +++++---------- src/crepe/api/Asset.h | 8 ++++---- src/crepe/types.h | 4 ++-- src/crepe/util/OptionalRef.h | 13 ++++++------- src/crepe/util/OptionalRef.hpp | 3 +-- src/example/rendering.cpp | 5 ++--- src/test/AssetTest.cpp | 13 +++---------- src/test/OptionalRefTest.cpp | 1 - 8 files changed, 23 insertions(+), 39 deletions(-) (limited to 'src/crepe/api/Asset.h') diff --git a/src/crepe/api/Asset.cpp b/src/crepe/api/Asset.cpp index 5271cf7..3fe3ceb 100644 --- a/src/crepe/api/Asset.cpp +++ b/src/crepe/api/Asset.cpp @@ -8,8 +8,8 @@ using namespace crepe; using namespace std; -Asset::Asset(const string & src) : src(find_asset(src)) { } -Asset::Asset(const char * src) : src(find_asset(src)) { } +Asset::Asset(const string & src) : src(find_asset(src)) {} +Asset::Asset(const char * src) : src(find_asset(src)) {} const string & Asset::get_path() const noexcept { return this->src; } @@ -22,14 +22,12 @@ string Asset::find_asset(const string & src) const { // absolute paths do not need to be resolved, only canonicalized filesystem::path path = src; - if (path.is_absolute()) - return filesystem::canonical(path); + if (path.is_absolute()) return filesystem::canonical(path); // find directory matching root_pattern filesystem::path root = this->whereami(); while (1) { - if (filesystem::exists(root / root_pattern)) - break; + if (filesystem::exists(root / root_pattern)) break; if (!root.has_parent_path()) throw runtime_error(format("Asset: Cannot find root pattern ({})", root_pattern)); root = root.parent_path(); @@ -48,11 +46,8 @@ string Asset::whereami() const noexcept { return path; } -bool Asset::operator==(const Asset & other) const noexcept { - return this->src == other.src; -} +bool Asset::operator==(const Asset & other) const noexcept { return this->src == other.src; } size_t std::hash::operator()(const Asset & asset) const noexcept { return std::hash{}(asset.get_path()); }; - diff --git a/src/crepe/api/Asset.h b/src/crepe/api/Asset.h index 685dd3a..77596ac 100644 --- a/src/crepe/api/Asset.h +++ b/src/crepe/api/Asset.h @@ -33,7 +33,7 @@ public: * \param other Possibly different instance of \c Asset to test equality against * \return True if \c this and \c other are equal */ - bool operator == (const Asset & other) const noexcept; + bool operator==(const Asset & other) const noexcept; private: //! path to asset @@ -52,7 +52,8 @@ private: namespace std { //! Hash helper struct -template<> struct hash { +template <> +struct hash { /** * \brief Hash operator for crepe::Asset * @@ -64,5 +65,4 @@ template<> struct hash { size_t operator()(const crepe::Asset & asset) const noexcept; }; -} - +} // namespace std diff --git a/src/crepe/types.h b/src/crepe/types.h index 86730cc..914c76c 100644 --- a/src/crepe/types.h +++ b/src/crepe/types.h @@ -1,8 +1,8 @@ #pragma once #include -#include #include +#include namespace crepe { @@ -13,4 +13,4 @@ typedef uint32_t game_object_id_t; template using RefVector = std::vector>; -} +} // namespace crepe diff --git a/src/crepe/util/OptionalRef.h b/src/crepe/util/OptionalRef.h index 8417a25..0a94ae5 100644 --- a/src/crepe/util/OptionalRef.h +++ b/src/crepe/util/OptionalRef.h @@ -23,7 +23,7 @@ public: * * \return Reference to this (required for operator) */ - OptionalRef & operator=(T & ref); + OptionalRef & operator=(T & ref); /** * \brief Check if this reference is not empty * @@ -51,13 +51,13 @@ public: void clear() noexcept; //! Copy constructor - OptionalRef(const OptionalRef &); + OptionalRef(const OptionalRef &); //! Move constructor - OptionalRef(OptionalRef &&); + OptionalRef(OptionalRef &&); //! Copy assignment - OptionalRef & operator=(const OptionalRef &); + OptionalRef & operator=(const OptionalRef &); //! Move assignment - OptionalRef & operator=(OptionalRef &&); + OptionalRef & operator=(OptionalRef &&); private: /** @@ -68,7 +68,6 @@ private: T * ref = nullptr; }; -} +} // namespace crepe #include "OptionalRef.hpp" - diff --git a/src/crepe/util/OptionalRef.hpp b/src/crepe/util/OptionalRef.hpp index 7b201b0..ee41f61 100644 --- a/src/crepe/util/OptionalRef.hpp +++ b/src/crepe/util/OptionalRef.hpp @@ -63,5 +63,4 @@ OptionalRef::operator bool() const noexcept { return this->ref != nullptr; } -} - +} // namespace crepe diff --git a/src/example/rendering.cpp b/src/example/rendering.cpp index 522ec0f..01794f8 100644 --- a/src/example/rendering.cpp +++ b/src/example/rendering.cpp @@ -30,9 +30,8 @@ int main() { // Normal adding components { Color color(0, 0, 0, 0); - Sprite & sprite - = obj.add_component(make_shared("asset/texture/img.png"), - color, FlipSettings{false, false}); + Sprite & sprite = obj.add_component( + make_shared("asset/texture/img.png"), color, FlipSettings{false, false}); sprite.sorting_in_layer = 2; sprite.order_in_layer = 1; obj.add_component(Color::RED); diff --git a/src/test/AssetTest.cpp b/src/test/AssetTest.cpp index 563a253..8aa7629 100644 --- a/src/test/AssetTest.cpp +++ b/src/test/AssetTest.cpp @@ -10,18 +10,12 @@ using namespace testing; class AssetTest : public Test { public: Config & cfg = Config::get_instance(); - void SetUp() override { - this->cfg.asset.root_pattern = ".crepe-root"; - } + void SetUp() override { this->cfg.asset.root_pattern = ".crepe-root"; } }; -TEST_F(AssetTest, Existant) { - ASSERT_NO_THROW(Asset{"asset/texture/img.png"}); -} +TEST_F(AssetTest, Existant) { ASSERT_NO_THROW(Asset{"asset/texture/img.png"}); } -TEST_F(AssetTest, Nonexistant) { - ASSERT_ANY_THROW(Asset{"asset/nonexistant"}); -} +TEST_F(AssetTest, Nonexistant) { ASSERT_ANY_THROW(Asset{"asset/nonexistant"}); } TEST_F(AssetTest, Rootless) { cfg.asset.root_pattern.clear(); @@ -30,4 +24,3 @@ TEST_F(AssetTest, Rootless) { Asset asset{arbitrary}; ASSERT_EQ(arbitrary, asset.get_path()); } - diff --git a/src/test/OptionalRefTest.cpp b/src/test/OptionalRefTest.cpp index 219ccca..2072d56 100644 --- a/src/test/OptionalRefTest.cpp +++ b/src/test/OptionalRefTest.cpp @@ -35,4 +35,3 @@ TEST(OptionalRefTest, Implicit) { EXPECT_TRUE(ref); ASSERT_NO_THROW(ref.get()); } - -- cgit v1.2.3 From 22a7e9f3c40b4b6eb68a5343e4870e76c4bfcf63 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 20 Nov 2024 14:24:08 +0100 Subject: process feedback on #39 --- readme.md | 7 +++++++ src/crepe/ComponentManager.h | 5 +++-- src/crepe/ComponentManager.hpp | 9 ++++----- src/crepe/api/Asset.cpp | 5 +++-- src/crepe/api/Asset.h | 16 ++++++++++++++++ src/crepe/system/System.h | 2 -- src/crepe/util/OptionalRef.h | 11 +---------- src/crepe/util/OptionalRef.hpp | 24 ------------------------ 8 files changed, 34 insertions(+), 45 deletions(-) (limited to 'src/crepe/api/Asset.h') diff --git a/readme.md b/readme.md index c83853d..d309b30 100644 --- a/readme.md +++ b/readme.md @@ -32,6 +32,7 @@ This project uses the following libraries |`SoLoud`|(latest git `master` version)| |Google Test (`GTest`)|1.15.2| |Berkeley DB (`libdb`)|5.3.21| +|Where Am I?|(latest git `master` version) > [!NOTE] > Most of these libraries are likely available from your package manager if you @@ -49,6 +50,11 @@ $ git submodule update --init --recursive --depth 1 Then, follow these steps for each library you want to install: +> [!IMPORTANT] +> A dollar sign prompt (`$`) indicates commands to be run as a regular user, +> while a hashtag (`#`) is used to denote commands that must be run with +> privileges (e.g. as root or using `sudo`). + 1. Change into the library folder (run **one** of these): ``` $ cd lib/googletest @@ -56,6 +62,7 @@ Then, follow these steps for each library you want to install: $ cd lib/soloud/contrib $ cd lib/sdl_image $ cd lib/sdl_ttf + $ cd lib/whereami ``` 2. Use CMake to configure the build, run the build and install (run **all** of these): diff --git a/src/crepe/ComponentManager.h b/src/crepe/ComponentManager.h index 2107453..0956d1e 100644 --- a/src/crepe/ComponentManager.h +++ b/src/crepe/ComponentManager.h @@ -8,6 +8,7 @@ #include "api/Vector2.h" #include "Component.h" +#include "types.h" namespace crepe { @@ -112,7 +113,7 @@ public: * \return A vector of all components of the specific type and id */ template - std::vector> get_components_by_id(game_object_id_t id) const; + RefVector get_components_by_id(game_object_id_t id) const; /** * \brief Get all components of a specific type * @@ -122,7 +123,7 @@ public: * \return A vector of all components of the specific type */ template - std::vector> get_components_by_type() const; + RefVector get_components_by_type() const; private: /** diff --git a/src/crepe/ComponentManager.hpp b/src/crepe/ComponentManager.hpp index be99cac..4d5eaf4 100644 --- a/src/crepe/ComponentManager.hpp +++ b/src/crepe/ComponentManager.hpp @@ -81,15 +81,14 @@ void ComponentManager::delete_components() { } template -std::vector> -ComponentManager::get_components_by_id(game_object_id_t id) const { +RefVector ComponentManager::get_components_by_id(game_object_id_t id) const { using namespace std; // Determine the type of T (this is used as the key of the unordered_map<>) type_index type = typeid(T); // Create an empty vector<> - vector> component_vector; + RefVector component_vector; if (this->components.find(type) == this->components.end()) return component_vector; @@ -114,14 +113,14 @@ ComponentManager::get_components_by_id(game_object_id_t id) const { } template -std::vector> ComponentManager::get_components_by_type() const { +RefVector ComponentManager::get_components_by_type() const { using namespace std; // Determine the type of T (this is used as the key of the unordered_map<>) type_index type = typeid(T); // Create an empty vector<> - vector> component_vector; + RefVector component_vector; // Find the type (in the unordered_map<>) if (this->components.find(type) == this->components.end()) return component_vector; diff --git a/src/crepe/api/Asset.cpp b/src/crepe/api/Asset.cpp index 3fe3ceb..e148367 100644 --- a/src/crepe/api/Asset.cpp +++ b/src/crepe/api/Asset.cpp @@ -2,9 +2,10 @@ #include #include -#include "Asset.h" #include "api/Config.h" +#include "Asset.h" + using namespace crepe; using namespace std; @@ -15,7 +16,7 @@ const string & Asset::get_path() const noexcept { return this->src; } string Asset::find_asset(const string & src) const { auto & cfg = Config::get_instance(); - auto & root_pattern = cfg.asset.root_pattern; + string & root_pattern = cfg.asset.root_pattern; // if root_pattern is empty, find_asset must return all paths as-is if (root_pattern.empty()) return src; diff --git a/src/crepe/api/Asset.h b/src/crepe/api/Asset.h index 77596ac..bfd0ac7 100644 --- a/src/crepe/api/Asset.h +++ b/src/crepe/api/Asset.h @@ -40,6 +40,22 @@ private: const std::string src; private: + /** + * \brief Locate asset path, or throw exception if it cannot be found + * + * This function resolves asset locations relative to crepe::Config::root_pattern if it is + * set and \p src is a relative path. If \p src is an absolute path, it is canonicalized. + * This function only returns if the file can be found. + * + * \param src Arbitrary path to resource file + * + * \returns \p src if crepe::Config::root_pattern is empty + * \returns Canonical path to \p src + * + * \throws std::runtime_error if root_pattern cannot be found + * \throws std::filesystem::filesystem_error if the resolved path does not exist + * \throws std::filesystem::filesystem_error if the path cannot be canonicalized + */ std::string find_asset(const std::string & src) const; /** * \returns The path to the current executable diff --git a/src/crepe/system/System.h b/src/crepe/system/System.h index 36f7edc..28ea20e 100644 --- a/src/crepe/system/System.h +++ b/src/crepe/system/System.h @@ -1,7 +1,5 @@ #pragma once -#include "../ComponentManager.h" - namespace crepe { class ComponentManager; diff --git a/src/crepe/util/OptionalRef.h b/src/crepe/util/OptionalRef.h index 0a94ae5..57f9635 100644 --- a/src/crepe/util/OptionalRef.h +++ b/src/crepe/util/OptionalRef.h @@ -36,7 +36,7 @@ public: * * \param ref Reference to assign */ - void set(T &) noexcept; + void set(T & ref) noexcept; /** * \brief Retrieve this reference * @@ -50,15 +50,6 @@ public: */ void clear() noexcept; - //! Copy constructor - OptionalRef(const OptionalRef &); - //! Move constructor - OptionalRef(OptionalRef &&); - //! Copy assignment - OptionalRef & operator=(const OptionalRef &); - //! Move assignment - OptionalRef & operator=(OptionalRef &&); - private: /** * \brief Reference to the value of type \c T diff --git a/src/crepe/util/OptionalRef.hpp b/src/crepe/util/OptionalRef.hpp index ee41f61..71e2a39 100644 --- a/src/crepe/util/OptionalRef.hpp +++ b/src/crepe/util/OptionalRef.hpp @@ -11,30 +11,6 @@ OptionalRef::OptionalRef(T & ref) { this->set(ref); } -template -OptionalRef::OptionalRef(const OptionalRef & other) { - this->ref = other.ref; -} - -template -OptionalRef::OptionalRef(OptionalRef && other) { - this->ref = other.ref; - other.clear(); -} - -template -OptionalRef & OptionalRef::operator=(const OptionalRef & other) { - this->ref = other.ref; - return *this; -} - -template -OptionalRef & OptionalRef::operator=(OptionalRef && other) { - this->ref = other.ref; - other.clear(); - return *this; -} - template T & OptionalRef::get() const { if (this->ref == nullptr) -- cgit v1.2.3