2021-06-16 19:11:00 +02:00
|
|
|
#pragma once
|
2022-10-18 04:59:51 +02:00
|
|
|
|
|
|
|
#include <map>
|
2021-06-16 19:11:00 +02:00
|
|
|
#include <memory>
|
|
|
|
#include <stack>
|
2022-10-18 04:59:51 +02:00
|
|
|
|
2021-12-29 15:59:18 +01:00
|
|
|
namespace kku
|
|
|
|
{
|
|
|
|
|
2022-10-18 04:59:51 +02:00
|
|
|
template <typename Type, class SpriteFactory, class Sprite,
|
|
|
|
typename = std::enable_if_t<std::is_enum<Type>::value>>
|
2021-06-16 19:11:00 +02:00
|
|
|
class SpriteContainer
|
|
|
|
{
|
2022-10-18 04:59:51 +02:00
|
|
|
public:
|
|
|
|
explicit SpriteContainer(std::initializer_list<Type> &&types,
|
|
|
|
const std::shared_ptr<SpriteFactory> &factory,
|
|
|
|
std::size_t reserve_size = 20)
|
|
|
|
: _sprite_factory(factory), _poll_reserve_size(reserve_size)
|
2021-06-16 19:11:00 +02:00
|
|
|
{
|
2022-10-18 04:59:51 +02:00
|
|
|
for (const Type &type : types)
|
2021-06-16 19:11:00 +02:00
|
|
|
reallocatePoll(type);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline std::shared_ptr<Sprite> getSprite(Type type)
|
|
|
|
{
|
2022-10-18 04:59:51 +02:00
|
|
|
SpritePoll &poll = _sprite_dispatcher.at(type);
|
2021-06-16 19:11:00 +02:00
|
|
|
|
|
|
|
if (poll.empty())
|
|
|
|
reallocatePoll(type);
|
|
|
|
|
|
|
|
std::shared_ptr<Sprite> sprite = poll.top();
|
|
|
|
poll.pop();
|
|
|
|
|
|
|
|
return sprite;
|
|
|
|
}
|
|
|
|
|
2022-10-18 04:59:51 +02:00
|
|
|
inline void resetSprite(const std::shared_ptr<Sprite> &sprite,
|
|
|
|
Type action) noexcept
|
2021-06-16 19:11:00 +02:00
|
|
|
{
|
2022-05-26 04:13:13 +02:00
|
|
|
sprite->reset();
|
2021-06-16 19:11:00 +02:00
|
|
|
_sprite_dispatcher[action].push(sprite);
|
|
|
|
}
|
|
|
|
|
2021-08-09 20:59:40 +02:00
|
|
|
~SpriteContainer() = default;
|
2021-06-16 19:11:00 +02:00
|
|
|
|
2022-10-18 04:59:51 +02:00
|
|
|
private:
|
2021-06-16 19:11:00 +02:00
|
|
|
inline void reallocatePoll(Type sprite_type)
|
|
|
|
{
|
|
|
|
SpritePoll &poll = _sprite_dispatcher[sprite_type];
|
|
|
|
for (std::size_t i = 0; i < _poll_reserve_size; ++i)
|
|
|
|
{
|
2022-02-06 02:33:09 +01:00
|
|
|
poll.push(_sprite_factory->createSprite(sprite_type));
|
2021-06-16 19:11:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
using SpritePoll = std::stack<std::shared_ptr<Sprite>>;
|
|
|
|
|
|
|
|
std::map<Type, SpritePoll> _sprite_dispatcher;
|
2021-12-28 19:04:50 +01:00
|
|
|
const std::shared_ptr<SpriteFactory> _sprite_factory;
|
2021-06-16 19:11:00 +02:00
|
|
|
|
|
|
|
std::size_t _poll_reserve_size;
|
|
|
|
};
|
2021-12-29 15:59:18 +01:00
|
|
|
|
2022-10-18 04:59:51 +02:00
|
|
|
} // namespace kku
|