Gestion de la musique dans le gestionnaire de ressources
This commit is contained in:
parent
b3ed814237
commit
f403d72da7
|
@ -2,12 +2,14 @@
|
|||
#define __PTF_RESOURCE_MANAGER_HPP__
|
||||
|
||||
#include <SFML/Graphics.hpp>
|
||||
#include <SFML/Audio.hpp>
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
class ResourceManager {
|
||||
private:
|
||||
std::map<std::string, sf::Texture> 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
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#include "resource_manager.hpp"
|
||||
#include "whereami.h"
|
||||
#include <memory>
|
||||
#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();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue