You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
43 lines
760 B
C
43 lines
760 B
C
3 years ago
|
#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;
|
||
|
}
|
||
|
|
||
3 years ago
|
const std::shared_ptr<Resource>& get(Id id) const
|
||
3 years ago
|
{
|
||
|
const auto found = _resources.find(id);
|
||
3 years ago
|
return found->second;
|
||
3 years ago
|
}
|
||
|
|
||
|
private:
|
||
3 years ago
|
std::map<Id, std::shared_ptr<Resource>> _resources;
|
||
3 years ago
|
};
|
||
|
|
||
|
namespace Fonts
|
||
|
{
|
||
|
enum class Id
|
||
|
{
|
||
|
GUI
|
||
|
};
|
||
|
}
|
||
|
|
||
|
using FontHolder = ResourceHolder<sf::Font, Fonts::Id>;
|
||
|
|