From 13da6cf16b4e87f1d11a3f58b1ace86dadb6df78 Mon Sep 17 00:00:00 2001 From: David Delassus Date: Wed, 29 May 2024 01:02:57 +0200 Subject: [PATCH] :recycle: add `resource_type` typename requirement on asset trait --- README.md | 52 ++++++++++++++++++++++++++++++++++- include/trollworks/assets.hpp | 6 +++- 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 1de93ba..baf7c41 100644 --- a/README.md +++ b/README.md @@ -246,8 +246,10 @@ resource cache from EnTT, for more information consult struct my_asset { // ... + using resource_type = my_asset; + struct loader_type { - using result_type = std::shared_ptr; + using result_type = std::shared_ptr; result_type operator()(/* ... */) const { // ... @@ -258,6 +260,54 @@ struct my_asset { auto& cache = tw::asset_manager::cache(); ``` +> **NB:** The `resource_type` type name may seem redundant, but it is there for +> assets that loads the same type of resources, consider the following example: + +```cpp +struct spritesheet { + // ... +}; + +struct aseprite_sheet { + using resource_type = spritesheet; + + struct loader_type { + using result_type = std::shared_ptr; + + result_type operator()(/* ... */) const { + // ... + } + }; +}; + +struct texturepacker_sheet { + using resource_type = spritesheet; + + struct loader_type { + using result_type = std::shared_ptr; + + result_type operator()(/* ... */) const { + // ... + } + }; +}; +``` + +Both `aseprite_sheet` and `texturepacker_sheet` assets will return a +`spritesheet` resource: + +```cpp +auto [it, loaded] = tw::asset_manager::cache().load(/* ... */); +auto [id, sheet] = *it; +// sheet is entt::resource +``` + +```cpp +auto [it, loaded] = tw::asset_manager::cache().load(/* ... */); +auto [id, sheet] = *it; +// sheet is entt::resource +``` + ### Messaging The message bus is simply a singleton returning an `entt::dispatcher`. For more diff --git a/include/trollworks/assets.hpp b/include/trollworks/assets.hpp index ef6600c..f0a998e 100644 --- a/include/trollworks/assets.hpp +++ b/include/trollworks/assets.hpp @@ -8,13 +8,17 @@ namespace tw { template concept asset_trait = requires(T& asset) { + typename T::resource_type; typename T::loader_type; }; template class asset_manager { public: - using cache_type = entt::resource_cache; + using cache_type = entt::resource_cache< + typename A::resource_type, + typename A::loader_type + >; static cache_type& cache() { if (!entt::locator::has_value()) {