Skip to content

Instantly share code, notes, and snippets.

@clooth
Created September 21, 2013 14:55
Show Gist options
  • Save clooth/6651368 to your computer and use it in GitHub Desktop.
Save clooth/6651368 to your computer and use it in GitHub Desktop.
//
// TextureManager.h
// Grand
//
// Created by Nico Hämäläinen on 9/21/13.
// Copyright (c) 2013 Nico Hämäläinen. All rights reserved.
//
#ifndef __Grand__TextureManager__
#define __Grand__TextureManager__
#include <SFML/Graphics/Texture.hpp>
#include <boost/filesystem.hpp>
#include <map>
#include <memory>
namespace Grand
{
class TextureManager
{
public:
TextureManager();
const sf::Texture* get_texture(const boost::filesystem::path& texture_path);
private:
std::map<boost::filesystem::path, std::unique_ptr<sf::Texture>> texture_map;
bool is_texture_loaded(const boost::filesystem::path& texture_path) const;
void add_texture(const boost::filesystem::path& texture_path);
};
}
#endif
//
// TextureManager.cpp
// Grand
//
// Created by Nico Hämäläinen on 9/21/13.
// Copyright (c) 2013 Nico Hämäläinen. All rights reserved.
//
#include "TextureManager.hpp"
Grand::TextureManager::TextureManager()
{
}
const sf::Texture* Grand::TextureManager::get_texture(const boost::filesystem::path &texture_path)
{
if (!is_texture_loaded(texture_path))
{
add_texture(texture_path);
}
return texture_map[texture_path].get();
}
bool Grand::TextureManager::is_texture_loaded(const boost::filesystem::path &texture_path) const
{
auto found = texture_map.find(texture_path);
return !(found == texture_map.end());
}
void Grand::TextureManager::add_texture(const boost::filesystem::path &texture_path)
{
texture_map[texture_path] = std::unique_ptr<sf::Texture>(new sf::Texture());
texture_map[texture_path]->loadFromFile(texture_path.string());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment