Le gestionnaire de ressources renvoie des pointeurs

This commit is contained in:
Mattéo Delabre 2016-04-20 00:37:44 +02:00
parent ff24f202ce
commit 66081fa104
12 changed files with 33 additions and 37 deletions

View File

@ -24,9 +24,9 @@ private:
boost::filesystem::path levels_path;
boost::filesystem::path musics_path;
std::unordered_map<std::string, std::unique_ptr<sf::Image>> images;
std::unordered_map<std::string, std::unique_ptr<sf::Texture>> textures;
std::unordered_map<std::string, std::unique_ptr<sf::Font>> fonts;
std::unordered_map<std::string, std::shared_ptr<sf::Image>> images;
std::unordered_map<std::string, std::shared_ptr<sf::Texture>> textures;
std::unordered_map<std::string, std::shared_ptr<sf::Font>> fonts;
sf::Music music;
float music_volume;
@ -44,17 +44,17 @@ public:
/**
* Récupère une image préchargée
*/
sf::Image& getImage(std::string name);
std::shared_ptr<sf::Image> getImage(std::string name);
/**
* Récupère une texture préchargée
*/
sf::Texture& getTexture(std::string name);
std::shared_ptr<sf::Texture> getTexture(std::string name);
/**
* Récupère une police préchargée
*/
sf::Font& getFont(std::string name);
std::shared_ptr<sf::Font> getFont(std::string name);
/**
* Récupère le chemin vers le fichier du niveau portant le

View File

@ -47,8 +47,8 @@ void Block::prepareDraw(ResourceManager& resources) {
texture_name = "block.tga";
}
sprite.setTexture(resources.getTexture(texture_name));
select_sprite.setTexture(resources.getTexture("block_select.tga"));
sprite.setTexture(*resources.getTexture(texture_name));
select_sprite.setTexture(*resources.getTexture("block_select.tga"));
}
void Block::draw(Level& level) {

View File

@ -14,7 +14,7 @@ Object::Ptr FinishBlock::clone() const {
void FinishBlock::prepareDraw(ResourceManager& resources) {
Block::prepareDraw(resources);
sprite.setOrigin(sf::Vector2f(23, 41));
sprite.setTexture(resources.getTexture("finish_block.tga"), true);
sprite.setTexture(*resources.getTexture("finish_block.tga"), true);
}
void FinishBlock::activate(Game& game, Object::Ptr object) {

View File

@ -45,7 +45,7 @@ void GravityBlock::draw(Level& level) {
icon_sprite.setColor(sf::Color(255, 255, 255, opacity));
// on dessine l'icône
icon_sprite.setTexture(level.getResourceManager().getTexture(
icon_sprite.setTexture(*level.getResourceManager().getTexture(
texture_name + ".tga"
));

View File

@ -14,7 +14,7 @@ Object::Ptr KillBlock::clone() const {
void KillBlock::prepareDraw(ResourceManager& resources) {
Block::prepareDraw(resources);
sprite.setTexture(resources.getTexture("kill_block.tga"));
sprite.setTexture(*resources.getTexture("kill_block.tga"));
}
void KillBlock::activate(Game& game, Object::Ptr object) {

View File

@ -288,10 +288,10 @@ void Level::draw() {
// on dessine le fond s'il y en a un
if (background != "") {
sf::Texture& bg_texture = getResourceManager().getTexture(background);
sf::Vector2f bg_size = (sf::Vector2f) bg_texture.getSize();
auto bg_texture = getResourceManager().getTexture(background);
sf::Vector2f bg_size = (sf::Vector2f) bg_texture->getSize();
background_sprite.setTexture(bg_texture);
background_sprite.setTexture(*bg_texture);
// on regarde la position du coin où il faut commencer à
// dessiner le fond, et la zone sur laquelle il faut le dessiner

View File

@ -27,7 +27,7 @@ namespace {
}
Menu::Menu(Manager& manager) : State(manager) {
background.setTexture(getResourceManager().getTexture("bg_menu.tga"));
background.setTexture(*getResourceManager().getTexture("bg_menu.tga"));
loadMainMenu();
}
@ -104,7 +104,7 @@ void Menu::frame() {
// affichage du menu
sf::RenderWindow& window = getWindow();
sf::Vector2f size = (sf::Vector2f) window.getSize();
sf::Font font = getResourceManager().getFont("raleway.ttf");
std::shared_ptr<sf::Font> font = getResourceManager().getFont("raleway.ttf");
// on s'assure d'être dans la vue par défaut (pas de zoom, 0x0 en haut gauche)
getManager().useGUIView();
@ -137,7 +137,7 @@ void Menu::frame() {
labels.clear();
for (unsigned int i = 0; i < choices.size(); i++) {
sf::Text label(choices[i], font, font_size);
sf::Text label(choices[i], *font, font_size);
sf::FloatRect text_size = label.getLocalBounds();
sf::Vector2f base_position(
@ -173,10 +173,6 @@ void Menu::loadMainMenu() {
actions.clear();
selection = 0;
sf::Texture& texture = getResourceManager().getTexture("bg_menu.tga");
texture.setSmooth(true);
background.setTexture(texture);
choices.push_back(sf::String(L"Jouer"));
actions.push_back(std::bind(&Menu::loadLevelMenu, this));

View File

@ -70,12 +70,12 @@ void Player::draw(Level& level) {
texture_name = "player_2.tga";
}
sprite.setTexture(&level.getResourceManager().getTexture(texture_name));
sprite.setTexture(level.getResourceManager().getTexture(texture_name).get());
sprite.setRadius(getRadius());
sprite.setOrigin(sf::Vector2f(getRadius(), getRadius()));
sprite.setPosition(getPosition());
shadow_sprite.setTexture(&level.getResourceManager().getTexture("player_shadow.tga"));
shadow_sprite.setTexture(level.getResourceManager().getTexture("player_shadow.tga").get());
shadow_sprite.setRadius(getRadius() - 1);
shadow_sprite.setOrigin(sf::Vector2f(getRadius() - 1, getRadius() - 1));
shadow_sprite.setPosition(getPosition());

View File

@ -29,8 +29,8 @@ void ResourceManager::preload() {
std::string full_path = boost::filesystem::canonical(it->path()).string();
std::string name = it->path().filename().string();
auto image = std::unique_ptr<sf::Image>(new sf::Image);
auto texture = std::unique_ptr<sf::Texture>(new sf::Texture);
auto image = std::shared_ptr<sf::Image>(new sf::Image);
auto texture = std::shared_ptr<sf::Texture>(new sf::Texture);
texture->setSmooth(true);
std::cout << "Chargement de l'image " << name << "... ";
@ -60,7 +60,7 @@ void ResourceManager::preload() {
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);
auto font = std::shared_ptr<sf::Font>(new sf::Font);
std::cout << "Chargement de la police " << name << "... ";
if (!font->loadFromFile(full_path)) {
@ -76,34 +76,34 @@ void ResourceManager::preload() {
preloaded = true;
}
sf::Image& ResourceManager::getImage(std::string name) {
std::shared_ptr<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];
return images[name];
}
sf::Texture& ResourceManager::getTexture(std::string name) {
std::shared_ptr<sf::Texture> ResourceManager::getTexture(std::string name) {
if (textures.count(name) == 0) {
throw std::runtime_error(
"Impossible de récupérer la texture inexistante : " + name
);
}
return *textures[name];
return textures[name];
}
sf::Font& ResourceManager::getFont(std::string name) {
std::shared_ptr<sf::Font> ResourceManager::getFont(std::string name) {
if (fonts.count(name) == 0) {
throw std::runtime_error(
"Impossible de récupérer la police inexistante : " + name
);
}
return *fonts[name];
return fonts[name];
}
std::string ResourceManager::getLevelPath(std::string name) {

View File

@ -2,7 +2,7 @@
#include "manager.hpp"
Rules::Rules(Manager& manager) : State(manager) {
background.setTexture(getResourceManager().getTexture("bg_rules.png"));
background.setTexture(*getResourceManager().getTexture("bg_rules.png"));
getWindow().setFramerateLimit(Manager::FPS);
}

View File

@ -24,7 +24,7 @@ void SwitchBlock::draw(Level& level) {
icon_sprite.setColor(sf::Color(255, 255, 255, opacity));
// on dessine l'icône
icon_sprite.setTexture(level.getResourceManager().getTexture(
icon_sprite.setTexture(*level.getResourceManager().getTexture(
"switch_block.tga"
));

View File

@ -8,16 +8,16 @@ WidgetTimer::WidgetTimer(Manager& manager, bool can_change, std::function<void(i
timer_down(manager, std::bind(&WidgetTimer::subtractTime, this), sf::Vector2f(30, 16), WidgetButton::ARROW_DOWN) {
// initialisation des formes
timer_seconds_text.setFont(manager.getResourceManager().getFont("monoid.ttf"));
timer_seconds_text.setFont(*manager.getResourceManager().getFont("monoid.ttf"));
timer_seconds_text.setCharacterSize(18);
timer_seconds_text.setColor(sf::Color::Black);
timer_sep_text.setString(":");
timer_sep_text.setFont(manager.getResourceManager().getFont("monoid.ttf"));
timer_sep_text.setFont(*manager.getResourceManager().getFont("monoid.ttf"));
timer_sep_text.setCharacterSize(18);
timer_sep_text.setColor(sf::Color::Black);
timer_minutes_text.setFont(manager.getResourceManager().getFont("monoid.ttf"));
timer_minutes_text.setFont(*manager.getResourceManager().getFont("monoid.ttf"));
timer_minutes_text.setCharacterSize(18);
timer_minutes_text.setColor(sf::Color::Black);
}