diff --git a/include/resource_manager.hpp b/include/resource_manager.hpp index 05cd04a..05acae7 100644 --- a/include/resource_manager.hpp +++ b/include/resource_manager.hpp @@ -2,12 +2,14 @@ #define __PTF_RESOURCE_MANAGER_HPP__ #include +#include #include #include class ResourceManager { private: std::map textures; + sf::Music music; public: ~ResourceManager(); @@ -18,6 +20,27 @@ public: * depuis son emplacement */ sf::Texture& getTexture(std::string name); + + /** + * Change la musique en lecture de fond + * Doit être utilisé pour la lecture en continu + */ + void setMusic(std::string name); + + /** + * Démarre la musique de fond + */ + void playMusic(); + + /** + * Met en pause la musique de fond + */ + void pauseMusic(); + + /** + * Arrête la musique de fond + */ + void stopMusic(); }; #endif diff --git a/src/resource_manager.cpp b/src/resource_manager.cpp index b0cebb7..55cd5cd 100644 --- a/src/resource_manager.cpp +++ b/src/resource_manager.cpp @@ -1,6 +1,7 @@ #include "resource_manager.hpp" #include "whereami.h" #include +#define RESOURCE_PATH #ifdef _WIN32 #define FILE_SEP '\\' @@ -29,6 +30,14 @@ std::string getCurrentDirectory() { return std::string(buffer.get()).substr(0, dirname_length); } +/** + * Récupère le chemin absolu vers la ressource dont + * le nom est passé en argument + */ +inline std::string getResourcePath(std::string name) { + return getCurrentDirectory() + FILE_SEP + "res" + FILE_SEP + name; +} + sf::Texture& ResourceManager::getTexture(std::string name) { // si la texture est déjà chargée, on l'utilise directement if (textures.count(name) > 0) { @@ -36,13 +45,32 @@ sf::Texture& ResourceManager::getTexture(std::string name) { } sf::Texture texture; - std::string path = getCurrentDirectory() + FILE_SEP + "res" + FILE_SEP + name; // tente de charger la texture dans le chemin "CWD/res/name" - if (!texture.loadFromFile(path)) { + if (!texture.loadFromFile(getResourcePath(name))) { throw std::runtime_error("Impossible de charger l'image : " + name); } textures[name] = texture; return textures[name]; } + +void ResourceManager::setMusic(std::string name) { + std::string path = getCurrentDirectory() + + 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(); +}