Skip to content

Commit

Permalink
♻️ add resource_type typename requirement on asset trait
Browse files Browse the repository at this point in the history
  • Loading branch information
linkdd committed May 28, 2024
1 parent c0dbbbf commit 13da6cf
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
52 changes: 51 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<my_asset>;
using result_type = std::shared_ptr<resource_type>;
result_type operator()(/* ... */) const {
// ...
Expand All @@ -258,6 +260,54 @@ struct my_asset {
auto& cache = tw::asset_manager<my_asset>::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<resource_type>;

result_type operator()(/* ... */) const {
// ...
}
};
};

struct texturepacker_sheet {
using resource_type = spritesheet;

struct loader_type {
using result_type = std::shared_ptr<resource_type>;

result_type operator()(/* ... */) const {
// ...
}
};
};
```

Both `aseprite_sheet` and `texturepacker_sheet` assets will return a
`spritesheet` resource:

```cpp
auto [it, loaded] = tw::asset_manager<aseprite_sheet>::cache().load(/* ... */);
auto [id, sheet] = *it;
// sheet is entt::resource<spritesheet>
```

```cpp
auto [it, loaded] = tw::asset_manager<texturepacker_sheet>::cache().load(/* ... */);
auto [id, sheet] = *it;
// sheet is entt::resource<spritesheet>
```

### Messaging

The message bus is simply a singleton returning an `entt::dispatcher`. For more
Expand Down
6 changes: 5 additions & 1 deletion include/trollworks/assets.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@
namespace tw {
template <typename T>
concept asset_trait = requires(T& asset) {
typename T::resource_type;
typename T::loader_type;
};

template <asset_trait A>
class asset_manager {
public:
using cache_type = entt::resource_cache<A, typename A::loader_type>;
using cache_type = entt::resource_cache<
typename A::resource_type,
typename A::loader_type
>;

static cache_type& cache() {
if (!entt::locator<cache_type>::has_value()) {
Expand Down

0 comments on commit 13da6cf

Please # to comment.