37 lines
782 B
C++
37 lines
782 B
C++
|
#include "texture.hpp"
|
||
|
#include "sdlerror.hpp"
|
||
|
#include "sdlmain.hpp"
|
||
|
#include <SDL2/SDL.h>
|
||
|
#include <stdexcept>
|
||
|
|
||
|
namespace cloonel {
|
||
|
Texture::Texture (const std::string& parPath, SDLMain* parMain, bool parLoadNow) :
|
||
|
m_path(parPath),
|
||
|
m_texture(nullptr),
|
||
|
m_sdlmain(parMain)
|
||
|
{
|
||
|
if (parLoadNow)
|
||
|
Reload();
|
||
|
}
|
||
|
|
||
|
Texture::~Texture() noexcept {
|
||
|
Destroy();
|
||
|
}
|
||
|
|
||
|
void Texture::Destroy() noexcept {
|
||
|
if (m_texture) {
|
||
|
SDL_DestroyTexture(m_texture);
|
||
|
m_texture = nullptr;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void Texture::Reload() {
|
||
|
Destroy();
|
||
|
SDL_Surface* surf = SDL_LoadBMP(m_path.c_str());
|
||
|
if (nullptr == surf)
|
||
|
throw std::runtime_error(GetFullErrorMessage(__PRETTY_FUNCTION__, m_path));
|
||
|
|
||
|
m_texture = SDL_CreateTextureFromSurface(m_sdlmain->GetRenderer(), surf);
|
||
|
}
|
||
|
} //namespace cloonel
|