project-kyoku/include/tools/resourceholder.h

43 lines
738 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;
}
Resource& get(Id id) const
{
const auto found = _resources.find(id);
return *found->second;
}
private:
std::map<Id, std::unique_ptr<Resource>> _resources;
};
namespace Fonts
{
enum class Id
{
GUI
};
}
using FontHolder = ResourceHolder<sf::Font, Fonts::Id>;