diff options
Diffstat (limited to 'src/crepe/api')
-rw-r--r-- | src/crepe/api/Asset.cpp | 50 | ||||
-rw-r--r-- | src/crepe/api/Asset.h | 43 | ||||
-rw-r--r-- | src/crepe/api/AssetManager.cpp | 17 | ||||
-rw-r--r-- | src/crepe/api/AudioSource.h | 5 | ||||
-rw-r--r-- | src/crepe/api/CMakeLists.txt | 8 | ||||
-rw-r--r-- | src/crepe/api/ResourceManager.cpp | 17 | ||||
-rw-r--r-- | src/crepe/api/ResourceManager.h (renamed from src/crepe/api/AssetManager.h) | 28 | ||||
-rw-r--r-- | src/crepe/api/ResourceManager.hpp (renamed from src/crepe/api/AssetManager.hpp) | 4 |
8 files changed, 133 insertions, 39 deletions
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 <filesystem> +#include <stdexcept> +#include <whereami.h> + +#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 <string> + +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/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 <memory> - -#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/AssetManager.h b/src/crepe/api/ResourceManager.h index 86a9902..7a45493 100644 --- a/src/crepe/api/AssetManager.h +++ b/src/crepe/api/ResourceManager.h @@ -8,36 +8,36 @@ namespace crepe { /** - * \brief The AssetManager is responsible for storing and managing assets over + * \brief The ResourceManager is responsible for storing and managing assets over * multiple scenes. * - * The AssetManager ensures that assets are loaded once and can be accessed + * 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 AssetManager is + * a scene is loaded. Assets are retained in memory until the ResourceManager is * destroyed, at which point the cached assets are cleared. */ -class AssetManager { +class ResourceManager { private: //! A cache that holds all the assets, accessible by their file path, over multiple scenes. std::unordered_map<std::string, std::any> asset_cache; private: - AssetManager(); - virtual ~AssetManager(); + ResourceManager(); + virtual ~ResourceManager(); public: - AssetManager(const AssetManager &) = delete; - AssetManager(AssetManager &&) = delete; - AssetManager & operator=(const AssetManager &) = delete; - AssetManager & operator=(AssetManager &&) = delete; + ResourceManager(const ResourceManager &) = delete; + ResourceManager(ResourceManager &&) = delete; + ResourceManager & operator=(const ResourceManager &) = delete; + ResourceManager & operator=(ResourceManager &&) = delete; /** - * \brief Retrieves the singleton instance of the AssetManager. + * \brief Retrieves the singleton instance of the ResourceManager. * - * \return A reference to the single instance of the AssetManager. + * \return A reference to the single instance of the ResourceManager. */ - static AssetManager & get_instance(); + static ResourceManager & get_instance(); public: /** @@ -62,4 +62,4 @@ public: } // namespace crepe -#include "AssetManager.hpp" +#include "ResourceManager.hpp" diff --git a/src/crepe/api/AssetManager.hpp b/src/crepe/api/ResourceManager.hpp index 977b4e1..9cd4bcb 100644 --- a/src/crepe/api/AssetManager.hpp +++ b/src/crepe/api/ResourceManager.hpp @@ -1,11 +1,11 @@ #pragma once -#include "AssetManager.h" +#include "ResourceManager.h" namespace crepe { template <typename asset> -std::shared_ptr<asset> AssetManager::cache(const std::string & file_path, +std::shared_ptr<asset> ResourceManager::cache(const std::string & file_path, bool reload) { auto it = asset_cache.find(file_path); |