2016-03-22 19:03:19 +00:00
|
|
|
#include "resource_manager.hpp"
|
2016-04-10 10:27:06 +00:00
|
|
|
#include <iostream>
|
2016-04-20 14:33:21 +00:00
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
namespace fs = boost::filesystem;
|
|
|
|
|
|
|
|
ResourceManager::get() {
|
|
|
|
static ResourceManager manager;
|
|
|
|
return manager;
|
|
|
|
}
|
2016-03-22 19:03:19 +00:00
|
|
|
|
2016-04-12 13:48:06 +00:00
|
|
|
ResourceManager::ResourceManager() : preloaded(false),
|
|
|
|
music_volume(20), playing_state(false), current_music("") {
|
2016-04-20 14:33:21 +00:00
|
|
|
|
|
|
|
// mise en mémoire des chemins vers les dossiers de ressources
|
|
|
|
fs::path res_path = fs::current_path() / "res";
|
|
|
|
|
|
|
|
textures_path = res_path / "textures";
|
|
|
|
fonts_path = res_path / "fonts";
|
|
|
|
levels_path = res_path / "levels";
|
|
|
|
musics_path = res_path / "musics";
|
|
|
|
|
2016-04-09 20:36:07 +00:00
|
|
|
// 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-20 14:33:21 +00:00
|
|
|
std::vector<fs::path> ResourceManager::getFiles(fs::path dir) const {
|
|
|
|
fs::recursive_directory_iterator dir(path), end;
|
|
|
|
std::vector<fs::path> result;
|
2016-04-10 10:27:06 +00:00
|
|
|
|
2016-04-20 14:33:21 +00:00
|
|
|
// on boucle sur tous les fichiers du dossier
|
|
|
|
// et de ses sous-dossiers et on les garde en mémoire
|
|
|
|
while (dir != end) {
|
|
|
|
if (fs::is_regular_file(dir->path())) {
|
|
|
|
result.push_back(dir->path());
|
|
|
|
}
|
2016-04-10 10:27:06 +00:00
|
|
|
|
2016-04-20 14:33:21 +00:00
|
|
|
++dir;
|
|
|
|
}
|
2016-04-10 10:27:06 +00:00
|
|
|
|
2016-04-20 14:33:21 +00:00
|
|
|
return result;
|
|
|
|
}
|
2016-04-10 10:27:06 +00:00
|
|
|
|
2016-04-20 14:33:21 +00:00
|
|
|
const fs::path& getTexturesPath() const {
|
|
|
|
return textures_path;
|
|
|
|
}
|
2016-04-19 15:51:42 +00:00
|
|
|
|
2016-04-20 14:33:21 +00:00
|
|
|
const fs::path& getFontsPath() const {
|
|
|
|
return fonts_path;
|
|
|
|
}
|
2016-04-19 15:51:42 +00:00
|
|
|
|
2016-04-20 14:33:21 +00:00
|
|
|
const fs::path& getLevelsPath() const {
|
|
|
|
return levels_path;
|
|
|
|
}
|
2016-04-19 15:51:42 +00:00
|
|
|
|
2016-04-20 14:33:21 +00:00
|
|
|
const fs::path& getMusicsPath() const {
|
|
|
|
return musics_path;
|
|
|
|
}
|
2016-04-10 10:27:06 +00:00
|
|
|
|
2016-04-20 14:33:21 +00:00
|
|
|
std::shared_ptr<sf::Image> ResourceManager::getImage(std::string name) {
|
|
|
|
// si l'image a déjà été chargée, on retourne la
|
|
|
|
// version en cache mémoire
|
|
|
|
if (images_cache.count(name) > 0) {
|
|
|
|
return images_cache[name];
|
2016-03-22 19:03:19 +00:00
|
|
|
}
|
|
|
|
|
2016-04-20 14:33:21 +00:00
|
|
|
fs::path image_path = textures_path / name;
|
|
|
|
std::string full_path = fs::canonical(image_path).string();
|
2016-04-10 10:27:06 +00:00
|
|
|
|
2016-04-20 14:33:21 +00:00
|
|
|
// on tente de charger l'image depuis son emplacement
|
|
|
|
auto image = std::shared_ptr<sf::Image>(new sf::Image);
|
|
|
|
std::cout << "Chargement de l'image " << name << " : ";
|
2016-04-10 10:27:06 +00:00
|
|
|
|
2016-04-20 14:33:21 +00:00
|
|
|
if (image->loadFromFile(full_path)) {
|
|
|
|
std::cout << "OK!" << std::endl;
|
|
|
|
} else {
|
|
|
|
std::cerr << "ERR!" << std::endl;
|
2016-04-19 15:51:42 +00:00
|
|
|
}
|
|
|
|
|
2016-04-20 14:33:21 +00:00
|
|
|
// on met en cache l'image pour les requêtes suivantes
|
|
|
|
// puis on la renvoie
|
|
|
|
images_cache[name] = std::move(image);
|
|
|
|
return images_cache[name];
|
2016-04-19 15:51:42 +00:00
|
|
|
}
|
|
|
|
|
2016-04-19 22:37:44 +00:00
|
|
|
std::shared_ptr<sf::Texture> ResourceManager::getTexture(std::string name) {
|
2016-04-20 14:33:21 +00:00
|
|
|
// si la texture est déjà dans le GPU, on renvoie son pointeur
|
|
|
|
if (textures_cache.count(name) > 0) {
|
|
|
|
return textures_cache[name];
|
2016-03-22 19:03:19 +00:00
|
|
|
}
|
|
|
|
|
2016-04-20 14:33:21 +00:00
|
|
|
// on récupère l'image depuis le disque
|
|
|
|
std::shared_ptr<sf::Image> image = getImage(name);
|
2016-03-30 11:32:49 +00:00
|
|
|
|
2016-04-20 14:33:21 +00:00
|
|
|
// on transfère l'image vers le GPU
|
|
|
|
auto texture = std::shared_ptr<sf::Texture>(new sf::Texture);
|
|
|
|
std::cout << "Création de la texture " << name << " : ";
|
2016-04-05 15:37:43 +00:00
|
|
|
|
2016-04-20 14:33:21 +00:00
|
|
|
if (texture->loadFromImage(*image)) {
|
|
|
|
std::cout << "OK!" << std::endl;
|
|
|
|
} else {
|
|
|
|
std::cerr << "ERR!" << std::endl;
|
|
|
|
}
|
2016-04-05 15:37:43 +00:00
|
|
|
|
2016-04-20 14:33:21 +00:00
|
|
|
// on met en cache la texture pour les requêtes suivantes
|
|
|
|
// puis on la renvoie
|
|
|
|
textures_cache[name] = std::move(texture);
|
|
|
|
return textures_cache[name];
|
2016-04-11 01:50:08 +00:00
|
|
|
}
|
|
|
|
|
2016-04-20 14:33:21 +00:00
|
|
|
std::shared_ptr<sf::Font> ResourceManager::getFont(std::string name) {
|
|
|
|
// on ne maintient pas de cache pour les polices, car ceci
|
|
|
|
// est géré par la librairie du GUI (SFGUI). On tente de
|
|
|
|
// charger la police depuis le disque
|
|
|
|
fs::path font_path = fonts_path / name;
|
|
|
|
std::string full_path = fs::canonical(font_path).string();
|
2016-04-11 01:50:08 +00:00
|
|
|
|
2016-04-20 14:33:21 +00:00
|
|
|
auto font = std::shared_ptr<sf::Font>(new sf::Font);
|
|
|
|
std::cout << "Chargement de la police " << name << ": ";
|
2016-04-11 01:50:08 +00:00
|
|
|
|
2016-04-20 14:33:21 +00:00
|
|
|
if (font->loadFromFile(full_path)) {
|
|
|
|
std::cout << "OK!" << std::endl;
|
|
|
|
} else {
|
|
|
|
std::cerr << "ERR!" << std::endl;
|
2016-04-11 01:50:08 +00:00
|
|
|
}
|
|
|
|
|
2016-04-20 14:33:21 +00:00
|
|
|
return font;
|
2016-03-30 11:32:49 +00:00
|
|
|
}
|
|
|
|
|
2016-04-09 20:36:07 +00:00
|
|
|
void ResourceManager::playMusic(std::string name) {
|
2016-04-20 14:33:21 +00:00
|
|
|
fs::path music_path = musics_path / name;
|
|
|
|
|
2016-04-12 13:48:06 +00:00
|
|
|
// si la musique est déjà chargée, on la relance si elle
|
|
|
|
// est en pause, sinon on ne fait rien
|
2016-04-20 14:33:21 +00:00
|
|
|
if (current_music_path == music_path) {
|
|
|
|
if (!is_playing) {
|
|
|
|
is_playing = true;
|
2016-04-12 13:48:06 +00:00
|
|
|
music.play();
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-09 20:36:07 +00:00
|
|
|
// tente de charger la musique depuis le dossier "res/musics"
|
2016-04-20 14:33:21 +00:00
|
|
|
std::string full_path = fs::canonical(music_path).string();
|
2016-04-10 10:27:06 +00:00
|
|
|
std::cout << "Lecture de la musique " << name << "... ";
|
|
|
|
|
2016-04-20 14:33:21 +00:00
|
|
|
if (music.openFromFile(full_path)) {
|
2016-04-10 10:27:06 +00:00
|
|
|
std::cout << "OK!" << std::endl;
|
2016-04-20 14:33:21 +00:00
|
|
|
} else {
|
|
|
|
std::cerr << "ERR!" << std::endl;
|
2016-04-09 20:36:07 +00:00
|
|
|
}
|
|
|
|
|
2016-04-20 14:33:21 +00:00
|
|
|
current_music_path = music_path;
|
|
|
|
is_playing = true;
|
2016-04-09 20:36:07 +00:00
|
|
|
music.play();
|
2016-03-30 11:32:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ResourceManager::stopMusic() {
|
2016-04-20 14:33:21 +00:00
|
|
|
is_playing = false;
|
|
|
|
music.stop();
|
2016-03-30 11:32:49 +00:00
|
|
|
}
|
2016-04-09 20:36:07 +00:00
|
|
|
|
2016-04-20 14:33:21 +00:00
|
|
|
float ResourceManager::getMusicVolume() const {
|
2016-04-09 20:36:07 +00:00
|
|
|
return music_volume;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResourceManager::setMusicVolume(float set_music_volume) {
|
|
|
|
music_volume = set_music_volume;
|
|
|
|
music.setVolume(music_volume);
|
|
|
|
}
|