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.

56 lines
1.1 KiB
C

3 years ago
#ifndef RESOURCEHOLDER_H
#define RESOURCEHOLDER_H
#include <assert.h>
#include <string>
#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)
{
std::unique_ptr<Resource> resource(new Resource());
if (!resource->loadFromFile(filename))
return false;
map_resources[id] = std::move(resource);
return true;
}
Resource& get(Id id) const
{
const auto found = map_resources.find(id);
assert(found != map_resources.end());
return *found->second;
}
private:
std::map<Id, std::unique_ptr<Resource>> map_resources;
};
// /////////////////////////////////////////////////////////// //
namespace Textures
{
enum class Id
{
Player,
Weakling,
Shotguner,
Background
};
}
using TextureHolder = ResourceHolder<sf::Texture, Textures::Id>;
using FontHolder = ResourceHolder<sf::Font, Textures::Id>;
#endif // RESOURCEHOLDER_H