project-kyoku/include/tools/resourceholder.h

43 lines
760 B
C++

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