2016-03-22 19:03:19 +00:00
|
|
|
#include "resource_manager.hpp"
|
2016-03-24 08:11:17 +00:00
|
|
|
#include "whereami.h"
|
2016-03-24 16:20:13 +00:00
|
|
|
#include <memory>
|
2016-03-22 19:03:19 +00:00
|
|
|
|
2016-03-30 18:40:59 +00:00
|
|
|
ResourceManager::ResourceManager() {
|
|
|
|
music.setLoop(true);
|
|
|
|
}
|
|
|
|
|
2016-03-22 19:03:19 +00:00
|
|
|
ResourceManager::~ResourceManager() {
|
|
|
|
textures.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-24 08:11:17 +00:00
|
|
|
* Récupère le chemin actuel de l'exécutable sous la forme
|
|
|
|
* d'une chaîne de caractères grâce à la librairie whereami
|
2016-03-22 19:03:19 +00:00
|
|
|
*/
|
|
|
|
std::string getCurrentDirectory() {
|
2016-03-24 08:11:17 +00:00
|
|
|
int length = wai_getExecutablePath(NULL, 0, NULL), dirname_length;
|
2016-03-31 08:20:09 +00:00
|
|
|
std::unique_ptr<char[]> buffer = std::unique_ptr<char[]>(new char[length + 1]);
|
2016-03-24 13:47:09 +00:00
|
|
|
wai_getExecutablePath(buffer.get(), length, &dirname_length);
|
2016-03-22 19:03:19 +00:00
|
|
|
|
2016-04-01 20:33:38 +00:00
|
|
|
if (length == 0) {
|
2016-03-24 08:11:17 +00:00
|
|
|
throw std::runtime_error("Impossible de déterminer le chemin actuel");
|
2016-03-22 19:03:19 +00:00
|
|
|
}
|
|
|
|
|
2016-03-24 13:47:09 +00:00
|
|
|
buffer.get()[length] = '\0';
|
|
|
|
return std::string(buffer.get()).substr(0, dirname_length);
|
2016-03-22 19:03:19 +00:00
|
|
|
}
|
|
|
|
|
2016-03-30 11:32:49 +00:00
|
|
|
/**
|
|
|
|
* Récupère le chemin absolu vers la ressource dont
|
|
|
|
* le nom est passé en argument
|
|
|
|
*/
|
|
|
|
inline std::string getResourcePath(std::string name) {
|
2016-03-30 18:37:45 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
return getCurrentDirectory() + "\\res\\" + name;
|
|
|
|
#else
|
|
|
|
return getCurrentDirectory() + "/res/" + name;
|
|
|
|
#endif
|
2016-03-30 11:32:49 +00:00
|
|
|
}
|
|
|
|
|
2016-03-22 19:03:19 +00:00
|
|
|
sf::Texture& ResourceManager::getTexture(std::string name) {
|
|
|
|
// si la texture est déjà chargée, on l'utilise directement
|
|
|
|
if (textures.count(name) > 0) {
|
|
|
|
return textures[name];
|
|
|
|
}
|
|
|
|
|
|
|
|
sf::Texture texture;
|
|
|
|
|
|
|
|
// tente de charger la texture dans le chemin "CWD/res/name"
|
2016-03-30 11:32:49 +00:00
|
|
|
if (!texture.loadFromFile(getResourcePath(name))) {
|
2016-03-24 19:11:25 +00:00
|
|
|
throw std::runtime_error("Impossible de charger l'image : " + name);
|
2016-03-22 19:03:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
textures[name] = texture;
|
|
|
|
return textures[name];
|
|
|
|
}
|
2016-03-30 11:32:49 +00:00
|
|
|
|
2016-04-05 15:37:43 +00:00
|
|
|
sf::Font& ResourceManager::getFont(std::string name) {
|
|
|
|
// si la police est déjà chargée, on l'utilise directement
|
|
|
|
if (fonts.count(name) > 0) {
|
|
|
|
return fonts[name];
|
|
|
|
}
|
|
|
|
|
|
|
|
sf::Font font;
|
|
|
|
|
|
|
|
// tente de charger la police dans le chemin "CWD/res/name"
|
|
|
|
if (!font.loadFromFile(getResourcePath(name))) {
|
|
|
|
throw std::runtime_error("Impossible de charger la police : " + name);
|
|
|
|
}
|
|
|
|
|
|
|
|
fonts[name] = font;
|
|
|
|
return fonts[name];
|
|
|
|
}
|
|
|
|
|
2016-03-30 11:32:49 +00:00
|
|
|
void ResourceManager::setMusic(std::string name) {
|
|
|
|
if (!music.openFromFile(getResourcePath(name))) {
|
|
|
|
throw std::runtime_error("Impossible de charger la musique : " + name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResourceManager::playMusic() {
|
|
|
|
music.play();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResourceManager::pauseMusic() {
|
|
|
|
music.pause();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResourceManager::stopMusic() {
|
|
|
|
music.stop();
|
|
|
|
}
|