Permettre au gest. de ressources de renvoyer des images

This commit is contained in:
Mattéo Delabre 2016-04-19 17:51:42 +02:00
parent 18553ce385
commit f1c0d67b32
2 changed files with 35 additions and 8 deletions

View File

@ -20,14 +20,15 @@ private:
bool preloaded;
boost::filesystem::path textures_path;
std::unordered_map<std::string, std::unique_ptr<sf::Texture>> textures;
boost::filesystem::path fonts_path;
std::unordered_map<std::string, std::unique_ptr<sf::Font>> fonts;
boost::filesystem::path levels_path;
boost::filesystem::path musics_path;
std::unordered_map<std::string, std::unique_ptr<sf::Image>> images;
std::unordered_map<std::string, std::unique_ptr<sf::Texture>> textures;
std::unordered_map<std::string, std::unique_ptr<sf::Font>> fonts;
sf::Music music;
float music_volume;
bool playing_state;
std::string current_music;
@ -40,6 +41,11 @@ public:
*/
void preload();
/**
* Récupère une image préchargée
*/
sf::Image& getImage(std::string name);
/**
* Récupère une texture préchargée
*/

View File

@ -29,16 +29,27 @@ void ResourceManager::preload() {
std::string full_path = boost::filesystem::canonical(it->path()).string();
std::string name = it->path().filename().string();
auto image = std::unique_ptr<sf::Image>(new sf::Image);
auto texture = std::unique_ptr<sf::Texture>(new sf::Texture);
texture->setSmooth(true);
std::cout << "Chargement de la texture " << name << "... ";
if (!texture->loadFromFile(full_path)) {
std::cout << "Chargement de l'image " << name << "... ";
if (!image->loadFromFile(full_path)) {
std::cerr << "ERREUR!" << std::endl;
} else {
std::cout << "OK!" << std::endl;
}
std::cout << "Mise en mémoire de la texture " << name << "... ";
if (!texture->loadFromImage(*image)) {
std::cerr << "ERREUR!" << std::endl;
} else {
std::cout << "OK!" << std::endl;
}
images[name] = std::move(image);
textures[name] = std::move(texture);
}
}
@ -65,10 +76,20 @@ void ResourceManager::preload() {
preloaded = true;
}
sf::Image& ResourceManager::getImage(std::string name) {
if (images.count(name) == 0) {
throw std::runtime_error(
"Impossible de récupérer l'image inexistante : " + name
);
}
return *images[name];
}
sf::Texture& ResourceManager::getTexture(std::string name) {
if (textures.count(name) == 0) {
throw std::runtime_error(
"Impossible de charger la texture inexistante : " + name
"Impossible de récupérer la texture inexistante : " + name
);
}
@ -78,7 +99,7 @@ sf::Texture& ResourceManager::getTexture(std::string name) {
sf::Font& ResourceManager::getFont(std::string name) {
if (fonts.count(name) == 0) {
throw std::runtime_error(
"Impossible de charger la police inexistante : " + name
"Impossible de récupérer la police inexistante : " + name
);
}