From 431b0bd7c6c502b42bb5be5488371d8c475e7024 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Thu, 14 Nov 2024 14:06:18 +0100 Subject: move some shit around --- src/crepe/Asset.cpp | 50 ------------------------------ src/crepe/Asset.h | 43 -------------------------- src/crepe/CMakeLists.txt | 2 -- src/crepe/api/Asset.cpp | 50 ++++++++++++++++++++++++++++++ src/crepe/api/Asset.h | 43 ++++++++++++++++++++++++++ src/crepe/api/AssetManager.cpp | 17 ---------- src/crepe/api/AssetManager.h | 65 --------------------------------------- src/crepe/api/AssetManager.hpp | 24 --------------- src/crepe/api/AudioSource.h | 5 ++- src/crepe/api/CMakeLists.txt | 8 +++-- src/crepe/api/ResourceManager.cpp | 17 ++++++++++ src/crepe/api/ResourceManager.h | 65 +++++++++++++++++++++++++++++++++++++++ src/crepe/api/ResourceManager.hpp | 24 +++++++++++++++ src/crepe/facade/Sound.cpp | 2 +- src/test/AssetTest.cpp | 2 +- 15 files changed, 208 insertions(+), 209 deletions(-) delete mode 100644 src/crepe/Asset.cpp delete mode 100644 src/crepe/Asset.h create mode 100644 src/crepe/api/Asset.cpp create mode 100644 src/crepe/api/Asset.h delete mode 100644 src/crepe/api/AssetManager.cpp delete mode 100644 src/crepe/api/AssetManager.h delete mode 100644 src/crepe/api/AssetManager.hpp create mode 100644 src/crepe/api/ResourceManager.cpp create mode 100644 src/crepe/api/ResourceManager.h create mode 100644 src/crepe/api/ResourceManager.hpp diff --git a/src/crepe/Asset.cpp b/src/crepe/Asset.cpp deleted file mode 100644 index 8692c6c..0000000 --- a/src/crepe/Asset.cpp +++ /dev/null @@ -1,50 +0,0 @@ -#include -#include -#include - -#include "Asset.h" -#include "api/Config.h" - -using namespace crepe; -using namespace std; - -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; } - -string Asset::find_asset(const string & src) const { - auto & cfg = Config::get_instance(); - auto & 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; - - // absolute paths do not need to be resolved, only canonicalized - filesystem::path path = src; - 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 (!root.has_parent_path()) - throw runtime_error(format("Asset: Cannot find root pattern ({})", root_pattern)); - root = root.parent_path(); - } - - // join path to root (base directory) and canonicalize - return filesystem::canonical(root / path); -} - -string Asset::whereami() const noexcept { - string path; - size_t path_length = wai_getExecutablePath(NULL, 0, NULL); - path.resize(path_length + 1); // wai writes null byte - wai_getExecutablePath(path.data(), path_length, NULL); - path.resize(path_length); - return path; -} - diff --git a/src/crepe/Asset.h b/src/crepe/Asset.h deleted file mode 100644 index f6e6782..0000000 --- a/src/crepe/Asset.h +++ /dev/null @@ -1,43 +0,0 @@ -#pragma once - -#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; - -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 diff --git a/src/crepe/CMakeLists.txt b/src/crepe/CMakeLists.txt index 52a781e..05f86d7 100644 --- a/src/crepe/CMakeLists.txt +++ b/src/crepe/CMakeLists.txt @@ -1,5 +1,4 @@ target_sources(crepe PUBLIC - Asset.cpp Particle.cpp ComponentManager.cpp Component.cpp @@ -8,7 +7,6 @@ target_sources(crepe PUBLIC ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES - Asset.h ComponentManager.h ComponentManager.hpp Component.h diff --git a/src/crepe/api/Asset.cpp b/src/crepe/api/Asset.cpp new file mode 100644 index 0000000..8692c6c --- /dev/null +++ b/src/crepe/api/Asset.cpp @@ -0,0 +1,50 @@ +#include +#include +#include + +#include "Asset.h" +#include "api/Config.h" + +using namespace crepe; +using namespace std; + +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; } + +string Asset::find_asset(const string & src) const { + auto & cfg = Config::get_instance(); + auto & 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; + + // absolute paths do not need to be resolved, only canonicalized + filesystem::path path = src; + 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 (!root.has_parent_path()) + throw runtime_error(format("Asset: Cannot find root pattern ({})", root_pattern)); + root = root.parent_path(); + } + + // join path to root (base directory) and canonicalize + return filesystem::canonical(root / path); +} + +string Asset::whereami() const noexcept { + string path; + size_t path_length = wai_getExecutablePath(NULL, 0, NULL); + path.resize(path_length + 1); // wai writes null byte + wai_getExecutablePath(path.data(), path_length, NULL); + path.resize(path_length); + return path; +} + diff --git a/src/crepe/api/Asset.h b/src/crepe/api/Asset.h new file mode 100644 index 0000000..f6e6782 --- /dev/null +++ b/src/crepe/api/Asset.h @@ -0,0 +1,43 @@ +#pragma once + +#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; + +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 diff --git a/src/crepe/api/AssetManager.cpp b/src/crepe/api/AssetManager.cpp deleted file mode 100644 index 3925758..0000000 --- a/src/crepe/api/AssetManager.cpp +++ /dev/null @@ -1,17 +0,0 @@ -#include "util/Log.h" - -#include "AssetManager.h" - -using namespace crepe; - -AssetManager & AssetManager::get_instance() { - static AssetManager instance; - return instance; -} - -AssetManager::~AssetManager() { - dbg_trace(); - this->asset_cache.clear(); -} - -AssetManager::AssetManager() { dbg_trace(); } diff --git a/src/crepe/api/AssetManager.h b/src/crepe/api/AssetManager.h deleted file mode 100644 index 86a9902..0000000 --- a/src/crepe/api/AssetManager.h +++ /dev/null @@ -1,65 +0,0 @@ -#pragma once - -#include -#include -#include -#include - -namespace crepe { - -/** - * \brief The AssetManager is responsible for storing and managing assets over - * multiple scenes. - * - * The AssetManager ensures that assets are loaded once and can be accessed - * across different scenes. It caches assets to avoid reloading them every time - * a scene is loaded. Assets are retained in memory until the AssetManager is - * destroyed, at which point the cached assets are cleared. - */ -class AssetManager { - -private: - //! A cache that holds all the assets, accessible by their file path, over multiple scenes. - std::unordered_map asset_cache; - -private: - AssetManager(); - virtual ~AssetManager(); - -public: - AssetManager(const AssetManager &) = delete; - AssetManager(AssetManager &&) = delete; - AssetManager & operator=(const AssetManager &) = delete; - AssetManager & operator=(AssetManager &&) = delete; - - /** - * \brief Retrieves the singleton instance of the AssetManager. - * - * \return A reference to the single instance of the AssetManager. - */ - static AssetManager & get_instance(); - -public: - /** - * \brief Caches an asset by loading it from the given file path. - * - * \param file_path The path to the asset file to load. - * \param reload If true, the asset will be reloaded from the file, even if - * it is already cached. - * \tparam T The type of asset to cache (e.g., texture, sound, etc.). - * - * \return A shared pointer to the cached asset. - * - * This template function caches the asset at the given file path. If the - * asset is already cached and `reload` is false, the existing cached version - * will be returned. Otherwise, the asset will be reloaded and added to the - * cache. - */ - template - std::shared_ptr cache(const std::string & file_path, - bool reload = false); -}; - -} // namespace crepe - -#include "AssetManager.hpp" diff --git a/src/crepe/api/AssetManager.hpp b/src/crepe/api/AssetManager.hpp deleted file mode 100644 index 977b4e1..0000000 --- a/src/crepe/api/AssetManager.hpp +++ /dev/null @@ -1,24 +0,0 @@ -#pragma once - -#include "AssetManager.h" - -namespace crepe { - -template -std::shared_ptr AssetManager::cache(const std::string & file_path, - bool reload) { - auto it = asset_cache.find(file_path); - - if (!reload && it != asset_cache.end()) { - return std::any_cast>(it->second); - } - - std::shared_ptr new_asset - = std::make_shared(file_path.c_str()); - - asset_cache[file_path] = new_asset; - - return new_asset; -} - -} // namespace crepe diff --git a/src/crepe/api/AudioSource.h b/src/crepe/api/AudioSource.h index 0748267..8a78927 100644 --- a/src/crepe/api/AudioSource.h +++ b/src/crepe/api/AudioSource.h @@ -1,11 +1,10 @@ #pragma once -#include - -#include "../Asset.h" #include "../Component.h" #include "../types.h" +#include "Asset.h" + namespace crepe { //! Audio source component diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt index 93a1fac..70f1527 100644 --- a/src/crepe/api/CMakeLists.txt +++ b/src/crepe/api/CMakeLists.txt @@ -8,7 +8,7 @@ target_sources(crepe PUBLIC Transform.cpp Color.cpp Texture.cpp - AssetManager.cpp + ResourceManager.cpp Sprite.cpp SaveManager.cpp Config.cpp @@ -20,6 +20,7 @@ target_sources(crepe PUBLIC Animator.cpp LoopManager.cpp LoopTimer.cpp + Asset.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES @@ -35,8 +36,8 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES Vector2.h Color.h Texture.h - AssetManager.h - AssetManager.hpp + ResourceManager.h + ResourceManager.hpp SaveManager.h Scene.h Metadata.h @@ -46,4 +47,5 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES Animator.h LoopManager.h LoopTimer.h + Asset.h ) diff --git a/src/crepe/api/ResourceManager.cpp b/src/crepe/api/ResourceManager.cpp new file mode 100644 index 0000000..470e511 --- /dev/null +++ b/src/crepe/api/ResourceManager.cpp @@ -0,0 +1,17 @@ +#include "util/Log.h" + +#include "ResourceManager.h" + +using namespace crepe; + +ResourceManager & ResourceManager::get_instance() { + static ResourceManager instance; + return instance; +} + +ResourceManager::~ResourceManager() { + dbg_trace(); + this->asset_cache.clear(); +} + +ResourceManager::ResourceManager() { dbg_trace(); } diff --git a/src/crepe/api/ResourceManager.h b/src/crepe/api/ResourceManager.h new file mode 100644 index 0000000..7a45493 --- /dev/null +++ b/src/crepe/api/ResourceManager.h @@ -0,0 +1,65 @@ +#pragma once + +#include +#include +#include +#include + +namespace crepe { + +/** + * \brief The ResourceManager is responsible for storing and managing assets over + * multiple scenes. + * + * The ResourceManager ensures that assets are loaded once and can be accessed + * across different scenes. It caches assets to avoid reloading them every time + * a scene is loaded. Assets are retained in memory until the ResourceManager is + * destroyed, at which point the cached assets are cleared. + */ +class ResourceManager { + +private: + //! A cache that holds all the assets, accessible by their file path, over multiple scenes. + std::unordered_map asset_cache; + +private: + ResourceManager(); + virtual ~ResourceManager(); + +public: + ResourceManager(const ResourceManager &) = delete; + ResourceManager(ResourceManager &&) = delete; + ResourceManager & operator=(const ResourceManager &) = delete; + ResourceManager & operator=(ResourceManager &&) = delete; + + /** + * \brief Retrieves the singleton instance of the ResourceManager. + * + * \return A reference to the single instance of the ResourceManager. + */ + static ResourceManager & get_instance(); + +public: + /** + * \brief Caches an asset by loading it from the given file path. + * + * \param file_path The path to the asset file to load. + * \param reload If true, the asset will be reloaded from the file, even if + * it is already cached. + * \tparam T The type of asset to cache (e.g., texture, sound, etc.). + * + * \return A shared pointer to the cached asset. + * + * This template function caches the asset at the given file path. If the + * asset is already cached and `reload` is false, the existing cached version + * will be returned. Otherwise, the asset will be reloaded and added to the + * cache. + */ + template + std::shared_ptr cache(const std::string & file_path, + bool reload = false); +}; + +} // namespace crepe + +#include "ResourceManager.hpp" diff --git a/src/crepe/api/ResourceManager.hpp b/src/crepe/api/ResourceManager.hpp new file mode 100644 index 0000000..9cd4bcb --- /dev/null +++ b/src/crepe/api/ResourceManager.hpp @@ -0,0 +1,24 @@ +#pragma once + +#include "ResourceManager.h" + +namespace crepe { + +template +std::shared_ptr ResourceManager::cache(const std::string & file_path, + bool reload) { + auto it = asset_cache.find(file_path); + + if (!reload && it != asset_cache.end()) { + return std::any_cast>(it->second); + } + + std::shared_ptr new_asset + = std::make_shared(file_path.c_str()); + + asset_cache[file_path] = new_asset; + + return new_asset; +} + +} // namespace crepe diff --git a/src/crepe/facade/Sound.cpp b/src/crepe/facade/Sound.cpp index b7bfeab..726f11f 100644 --- a/src/crepe/facade/Sound.cpp +++ b/src/crepe/facade/Sound.cpp @@ -1,6 +1,6 @@ #include -#include "../Asset.h" +#include "../api/Asset.h" #include "../util/Log.h" #include "Sound.h" diff --git a/src/test/AssetTest.cpp b/src/test/AssetTest.cpp index c3ff158..324a3f1 100644 --- a/src/test/AssetTest.cpp +++ b/src/test/AssetTest.cpp @@ -1,7 +1,7 @@ #include "api/Config.h" #include -#include +#include using namespace std; using namespace crepe; -- cgit v1.2.3