64 lines
1.9 KiB
C++
64 lines
1.9 KiB
C++
/********************\
|
|
| Copyright 2024, |
|
|
| Ulysse Cura |
|
|
\********************/
|
|
|
|
///////////////////////////////////////////////
|
|
// //
|
|
// Definition de la classe TextureManager. //
|
|
// //
|
|
///////////////////////////////////////////////
|
|
|
|
#include <iostream>
|
|
#include <SDL2/SDL_image.h>
|
|
#include <stdexcept>
|
|
#include "Game.hpp"
|
|
#include "TextureManager.hpp"
|
|
|
|
using std::string, std::cerr, std::runtime_error;
|
|
|
|
SDL_Texture *TextureManager::LoadTexture(const string &path)
|
|
{
|
|
auto it = m_textures.find(path);
|
|
|
|
if (it != m_textures.end())
|
|
return it->second.get();
|
|
|
|
SDL_Surface *tmpSurface = IMG_Load(path.c_str());
|
|
if (!tmpSurface) {
|
|
cerr << "Erreur IMG_Load : " << IMG_GetError() << '\n';
|
|
throw runtime_error("Impossible de charger la texture depuis la memoire.\n");
|
|
}
|
|
|
|
SDL_Texture *texture = SDL_CreateTextureFromSurface(Game::renderer, tmpSurface);
|
|
SDL_FreeSurface(tmpSurface);
|
|
|
|
if (!texture) {
|
|
cerr << "Erreur SDL_CreateTextureFromSurface : " << SDL_GetError() << '\n';
|
|
throw runtime_error("Impossible de convertir la surface en texture.\n");
|
|
}
|
|
|
|
m_textures.emplace(path, texture_ptr(texture, &SDL_DestroyTexture));
|
|
|
|
it = m_textures.find(path);
|
|
|
|
if(it != m_textures.end())
|
|
return it->second.get();
|
|
|
|
|
|
throw runtime_error("Impossible de trouver la texture dans l'unordered_map.\n");
|
|
}
|
|
|
|
void TextureManager::Draw(SDL_Texture *texture, SDL_Rect srcR, SDL_Rect dstR, bool flip)
|
|
{
|
|
SDL_RendererFlip flipRender {SDL_FLIP_NONE};
|
|
|
|
if(flip) flipRender = SDL_FLIP_HORIZONTAL;
|
|
|
|
if(SDL_RenderCopyEx(Game::renderer, texture, &srcR, &dstR, 0, NULL, flipRender) != 0)
|
|
{
|
|
cerr << "Erreur SDL_RenderCopy : " << SDL_GetError() << '\n';
|
|
throw runtime_error("Impossible de dessiner la texture.\n");
|
|
}
|
|
}
|