2016-03-22 19:03:19 +00:00
|
|
|
#include "resource_manager.hpp"
|
2016-03-24 08:11:17 +00:00
|
|
|
#include "whereami.h"
|
2016-04-09 20:36:07 +00:00
|
|
|
#include <cstring>
|
2016-03-22 19:03:19 +00:00
|
|
|
|
2016-04-09 20:36:07 +00:00
|
|
|
// définition du séparateur de fichiers en fonction
|
|
|
|
// du type de système
|
|
|
|
#ifdef _WIN32
|
|
|
|
const std::string SEP = "\\";
|
|
|
|
#else
|
|
|
|
const std::string SEP = "/";
|
|
|
|
#endif
|
2016-03-22 19:03:19 +00:00
|
|
|
|
2016-04-09 20:36:07 +00:00
|
|
|
ResourceManager::ResourceManager() : music_volume(5) {
|
|
|
|
// on récupère le chemin actuel de l'exécutable pour pouvoir accéder
|
|
|
|
// au dossier des ressources qui est situé dans le même dossier
|
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';
|
2016-04-09 20:36:07 +00:00
|
|
|
std::string base_dir = std::string(buffer.get()).substr(0, dirname_length);
|
|
|
|
resources_dir = base_dir + SEP + "res" + SEP;
|
|
|
|
|
|
|
|
// initialisation de la musique en bouclage et au volume par défaut
|
|
|
|
music.setLoop(true);
|
|
|
|
music.setVolume(music_volume);
|
2016-03-22 19:03:19 +00:00
|
|
|
}
|
|
|
|
|
2016-04-09 20:36:07 +00:00
|
|
|
ResourceManager::~ResourceManager() {
|
|
|
|
textures.clear();
|
|
|
|
fonts.clear();
|
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) {
|
2016-04-10 03:10:27 +00:00
|
|
|
return *textures[name];
|
2016-03-22 19:03:19 +00:00
|
|
|
}
|
|
|
|
|
2016-04-10 03:10:27 +00:00
|
|
|
auto texture = std::unique_ptr<sf::Texture>(new sf::Texture);
|
2016-03-22 19:03:19 +00:00
|
|
|
|
2016-04-09 20:36:07 +00:00
|
|
|
// tente de charger la texture dans le chemin "res/textures/name.png"
|
2016-04-10 03:10:27 +00:00
|
|
|
if (!texture->loadFromFile(resources_dir + SEP + "textures" + SEP + name)) {
|
2016-04-09 20:36:07 +00:00
|
|
|
throw std::runtime_error(
|
|
|
|
"Impossible de charger l'image \"" + name + "\""
|
|
|
|
);
|
2016-03-22 19:03:19 +00:00
|
|
|
}
|
|
|
|
|
2016-04-10 03:10:27 +00:00
|
|
|
textures[name] = std::move(texture);
|
|
|
|
return *textures[name];
|
2016-03-22 19:03:19 +00:00
|
|
|
}
|
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) {
|
2016-04-10 03:10:27 +00:00
|
|
|
return *fonts[name];
|
2016-04-05 15:37:43 +00:00
|
|
|
}
|
|
|
|
|
2016-04-10 03:10:27 +00:00
|
|
|
auto font = std::unique_ptr<sf::Font>(new sf::Font);
|
2016-04-05 15:37:43 +00:00
|
|
|
|
2016-04-09 20:36:07 +00:00
|
|
|
// tente de charger la police depuis le dossier "res/fonts"
|
2016-04-10 03:10:27 +00:00
|
|
|
if (!font->loadFromFile(resources_dir + SEP + "fonts" + SEP + name)) {
|
2016-04-09 20:36:07 +00:00
|
|
|
throw std::runtime_error(
|
|
|
|
"Impossible de charger la police \"" + name + "\""
|
|
|
|
);
|
2016-04-05 15:37:43 +00:00
|
|
|
}
|
|
|
|
|
2016-04-10 03:10:27 +00:00
|
|
|
fonts[name] = std::move(font);
|
|
|
|
return *fonts[name];
|
2016-04-05 15:37:43 +00:00
|
|
|
}
|
|
|
|
|
2016-04-10 07:41:26 +00:00
|
|
|
std::string ResourceManager::getLevelPath(std::string name) {
|
|
|
|
return resources_dir + SEP + "levels" + SEP + name;
|
2016-03-30 11:32:49 +00:00
|
|
|
}
|
|
|
|
|
2016-04-09 20:36:07 +00:00
|
|
|
void ResourceManager::playMusic(std::string name) {
|
|
|
|
// tente de charger la musique depuis le dossier "res/musics"
|
|
|
|
if (!music.openFromFile(resources_dir + SEP + "musics" + SEP + name)) {
|
|
|
|
throw std::runtime_error("Impossible de charger la musique : " + name);
|
|
|
|
}
|
|
|
|
|
|
|
|
music.play();
|
2016-03-30 11:32:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ResourceManager::stopMusic() {
|
|
|
|
music.stop();
|
|
|
|
}
|
2016-04-09 20:36:07 +00:00
|
|
|
|
|
|
|
float ResourceManager::getMusicVolume() {
|
|
|
|
return music_volume;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResourceManager::setMusicVolume(float set_music_volume) {
|
|
|
|
music_volume = set_music_volume;
|
|
|
|
music.setVolume(music_volume);
|
|
|
|
}
|