project-kyoku/include/tools/resourceholder.h

43 lines
760 B
C
Raw Normal View History

2021-08-21 08:29:58 -04:00
#pragma once
#include <memory>
#include <map>
namespace sf { class Texture; class Font; }
template <typename Resource, typename Id>
class ResourceHolder
{
public:
bool load(Id id, const std::string& filename)
{
auto resource = std::make_unique<Resource>();
if (!resource->loadFromFile(filename))
return false;
_resources[id] = std::move(resource);
return true;
}
2021-08-26 13:41:16 -04:00
const std::shared_ptr<Resource>& get(Id id) const
2021-08-21 08:29:58 -04:00
{
const auto found = _resources.find(id);
2021-08-26 13:41:16 -04:00
return found->second;
2021-08-21 08:29:58 -04:00
}
private:
2021-08-26 13:41:16 -04:00
std::map<Id, std::shared_ptr<Resource>> _resources;
2021-08-21 08:29:58 -04:00
};
namespace Fonts
{
enum class Id
{
GUI
};
}
using FontHolder = ResourceHolder<sf::Font, Fonts::Id>;