2016-03-22 19:03:19 +00:00
|
|
|
#include "resource_manager.hpp"
|
2016-04-10 10:27:06 +00:00
|
|
|
#include <iostream>
|
2016-04-11 01:50:08 +00:00
|
|
|
#include <algorithm>
|
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-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-10 10:27:06 +00:00
|
|
|
void ResourceManager::preload() {
|
|
|
|
if (preloaded) {
|
|
|
|
return;
|
|
|
|
}
|
2016-03-30 11:32:49 +00:00
|
|
|
|
2016-04-12 16:11:36 +00:00
|
|
|
boost::filesystem::path current = boost::filesystem::current_path();
|
|
|
|
boost::filesystem::directory_iterator end;
|
2016-04-10 10:27:06 +00:00
|
|
|
|
|
|
|
// on garde une référence aux chemins des différentes ressources
|
|
|
|
textures_path = current / "res/textures";
|
|
|
|
fonts_path = current / "res/fonts";
|
|
|
|
levels_path = current / "res/levels";
|
|
|
|
musics_path = current / "res/musics";
|
|
|
|
|
|
|
|
// préchargement de toutes les textures
|
2016-04-12 16:11:36 +00:00
|
|
|
for (boost::filesystem::directory_iterator it(textures_path); it != end; ++it) {
|
2016-04-10 10:27:06 +00:00
|
|
|
if (boost::filesystem::is_regular_file(it->path())) {
|
|
|
|
std::string full_path = boost::filesystem::canonical(it->path()).string();
|
|
|
|
std::string name = it->path().filename().string();
|
|
|
|
|
2016-04-19 15:51:42 +00:00
|
|
|
auto image = std::unique_ptr<sf::Image>(new sf::Image);
|
2016-04-10 10:27:06 +00:00
|
|
|
auto texture = std::unique_ptr<sf::Texture>(new sf::Texture);
|
2016-04-10 23:04:50 +00:00
|
|
|
texture->setSmooth(true);
|
2016-04-10 10:27:06 +00:00
|
|
|
|
2016-04-19 15:51:42 +00:00
|
|
|
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)) {
|
2016-04-10 10:27:06 +00:00
|
|
|
std::cerr << "ERREUR!" << std::endl;
|
|
|
|
} else {
|
|
|
|
std::cout << "OK!" << std::endl;
|
|
|
|
}
|
|
|
|
|
2016-04-19 15:51:42 +00:00
|
|
|
images[name] = std::move(image);
|
2016-04-10 10:27:06 +00:00
|
|
|
textures[name] = std::move(texture);
|
|
|
|
}
|
2016-03-22 19:03:19 +00:00
|
|
|
}
|
|
|
|
|
2016-04-10 10:27:06 +00:00
|
|
|
// préchargement de toutes les polices
|
2016-04-12 16:11:36 +00:00
|
|
|
for (boost::filesystem::directory_iterator it(fonts_path); it != end; ++it) {
|
2016-04-10 10:27:06 +00:00
|
|
|
if (boost::filesystem::is_regular_file(it->path())) {
|
|
|
|
std::string full_path = boost::filesystem::canonical(it->path()).string();
|
|
|
|
std::string name = it->path().filename().string();
|
|
|
|
|
|
|
|
auto font = std::unique_ptr<sf::Font>(new sf::Font);
|
|
|
|
std::cout << "Chargement de la police " << name << "... ";
|
2016-03-22 19:03:19 +00:00
|
|
|
|
2016-04-10 10:27:06 +00:00
|
|
|
if (!font->loadFromFile(full_path)) {
|
|
|
|
std::cerr << "ERREUR!" << std::endl;
|
|
|
|
} else {
|
|
|
|
std::cout << "OK!" << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
fonts[name] = std::move(font);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
preloaded = true;
|
|
|
|
}
|
|
|
|
|
2016-04-19 15:51:42 +00:00
|
|
|
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];
|
|
|
|
}
|
|
|
|
|
2016-04-10 10:27:06 +00:00
|
|
|
sf::Texture& ResourceManager::getTexture(std::string name) {
|
|
|
|
if (textures.count(name) == 0) {
|
2016-04-09 20:36:07 +00:00
|
|
|
throw std::runtime_error(
|
2016-04-19 15:51:42 +00:00
|
|
|
"Impossible de récupérer la texture inexistante : " + name
|
2016-04-09 20:36:07 +00:00
|
|
|
);
|
2016-03-22 19:03:19 +00:00
|
|
|
}
|
|
|
|
|
2016-04-10 03:10:27 +00:00
|
|
|
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) {
|
2016-04-10 10:27:06 +00:00
|
|
|
if (fonts.count(name) == 0) {
|
2016-04-09 20:36:07 +00:00
|
|
|
throw std::runtime_error(
|
2016-04-19 15:51:42 +00:00
|
|
|
"Impossible de récupérer la police inexistante : " + name
|
2016-04-09 20:36:07 +00:00
|
|
|
);
|
2016-04-05 15:37:43 +00:00
|
|
|
}
|
|
|
|
|
2016-04-10 03:10:27 +00:00
|
|
|
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) {
|
2016-04-11 01:50:08 +00:00
|
|
|
return (levels_path / name).string();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> ResourceManager::getLevelList() {
|
|
|
|
boost::filesystem::directory_iterator iter(levels_path);
|
|
|
|
std::vector<boost::filesystem::path> list;
|
|
|
|
std::vector<std::string> path_list;
|
|
|
|
|
|
|
|
// récupération de la liste de tous les niveaux
|
|
|
|
std::copy(
|
|
|
|
iter, boost::filesystem::directory_iterator(),
|
|
|
|
std::back_inserter(list)
|
|
|
|
);
|
|
|
|
|
|
|
|
// tri par ordre alphabétique
|
|
|
|
std::sort(list.begin(), list.end());
|
|
|
|
|
|
|
|
// conversion en chemins absolus
|
|
|
|
for (auto it = list.begin(); it != list.end(); it++) {
|
|
|
|
path_list.push_back((*it).string());
|
|
|
|
}
|
|
|
|
|
|
|
|
return path_list;
|
2016-03-30 11:32:49 +00:00
|
|
|
}
|
|
|
|
|
2016-04-09 20:36:07 +00:00
|
|
|
void ResourceManager::playMusic(std::string 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
|
|
|
|
if (current_music == name) {
|
|
|
|
if (!playing_state) {
|
|
|
|
playing_state = true;
|
|
|
|
music.play();
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-09 20:36:07 +00:00
|
|
|
// tente de charger la musique depuis le dossier "res/musics"
|
2016-04-10 10:27:06 +00:00
|
|
|
std::string full_path = boost::filesystem::canonical(musics_path / name).string();
|
|
|
|
std::cout << "Lecture de la musique " << name << "... ";
|
|
|
|
|
|
|
|
if (!music.openFromFile(full_path)) {
|
|
|
|
std::cerr << "ERREUR!" << std::endl;
|
|
|
|
} else {
|
|
|
|
std::cout << "OK!" << std::endl;
|
2016-04-09 20:36:07 +00:00
|
|
|
}
|
|
|
|
|
2016-04-12 13:48:06 +00:00
|
|
|
current_music = name;
|
|
|
|
playing_state = true;
|
2016-04-09 20:36:07 +00:00
|
|
|
music.play();
|
2016-03-30 11:32:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ResourceManager::stopMusic() {
|
2016-04-12 13:48:06 +00:00
|
|
|
// on n'arrête la musique que si elle ne l'est pas déjà
|
|
|
|
if (playing_state) {
|
|
|
|
playing_state = false;
|
|
|
|
music.stop();
|
|
|
|
}
|
2016-03-30 11:32:49 +00:00
|
|
|
}
|
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);
|
|
|
|
}
|