2014-02-08 23:11:26 +01:00
|
|
|
#include "texture.hpp"
|
|
|
|
#include "sdlerror.hpp"
|
|
|
|
#include "sdlmain.hpp"
|
|
|
|
#include <SDL2/SDL.h>
|
|
|
|
#include <stdexcept>
|
2014-02-09 00:32:11 +01:00
|
|
|
#include <cassert>
|
2014-02-08 23:11:26 +01:00
|
|
|
|
|
|
|
namespace cloonel {
|
2014-02-09 00:32:11 +01:00
|
|
|
///------------------------------------------------------------------------
|
|
|
|
///------------------------------------------------------------------------
|
2014-02-08 23:11:26 +01:00
|
|
|
Texture::Texture (const std::string& parPath, SDLMain* parMain, bool parLoadNow) :
|
|
|
|
m_path(parPath),
|
|
|
|
m_texture(nullptr),
|
|
|
|
m_sdlmain(parMain)
|
|
|
|
{
|
|
|
|
if (parLoadNow)
|
|
|
|
Reload();
|
|
|
|
}
|
|
|
|
|
2014-02-09 00:32:11 +01:00
|
|
|
///------------------------------------------------------------------------
|
|
|
|
///------------------------------------------------------------------------
|
2014-02-08 23:11:26 +01:00
|
|
|
Texture::~Texture() noexcept {
|
|
|
|
Destroy();
|
|
|
|
}
|
|
|
|
|
2014-02-09 00:32:11 +01:00
|
|
|
///------------------------------------------------------------------------
|
|
|
|
///------------------------------------------------------------------------
|
2014-02-08 23:11:26 +01:00
|
|
|
void Texture::Destroy() noexcept {
|
|
|
|
if (m_texture) {
|
|
|
|
SDL_DestroyTexture(m_texture);
|
|
|
|
m_texture = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-09 00:32:11 +01:00
|
|
|
///------------------------------------------------------------------------
|
|
|
|
///------------------------------------------------------------------------
|
2014-02-08 23:11:26 +01:00
|
|
|
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);
|
2014-02-09 00:32:11 +01:00
|
|
|
SDL_FreeSurface(surf);
|
2014-02-09 01:19:40 +01:00
|
|
|
if (m_texture)
|
|
|
|
SDL_QueryTexture(m_texture, nullptr, nullptr, &m_width, &m_height);
|
2014-02-09 00:32:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
///------------------------------------------------------------------------
|
|
|
|
///------------------------------------------------------------------------
|
2014-02-09 01:19:40 +01:00
|
|
|
void Texture::Render (int parX, int parY) {
|
2014-02-09 00:32:11 +01:00
|
|
|
assert(IsLoaded());
|
2014-02-09 01:19:40 +01:00
|
|
|
const SDL_Rect dest = { parX, parY, m_width, m_height };
|
|
|
|
SDL_RenderCopy(m_sdlmain->GetRenderer(), m_texture, nullptr, &dest);
|
2014-02-08 23:11:26 +01:00
|
|
|
}
|
|
|
|
} //namespace cloonel
|