2021-07-18 18:26:12 +02:00
|
|
|
#include <basic_widgets/core/renderer.hpp>
|
|
|
|
#include <basic_widgets/core/texture.hpp>
|
|
|
|
|
|
|
|
using namespace bwidgets;
|
|
|
|
|
2021-08-11 15:52:07 +02:00
|
|
|
Texture::Texture(SDL_Texture* t)
|
2021-08-26 18:59:52 +02:00
|
|
|
: _attributes(attributes(t)), _data {ptr_or_throw<SDLError>(t)}
|
2021-08-21 20:59:39 +02:00
|
|
|
{}
|
2021-07-18 18:26:12 +02:00
|
|
|
|
2021-08-21 20:59:39 +02:00
|
|
|
Texture::Texture(const Renderer& r, const SDL_PixelFormatEnum f,
|
|
|
|
const SDL_TextureAccess a, int w, int h)
|
2021-08-26 18:59:52 +02:00
|
|
|
: Texture {SDL_CreateTexture(r._data.get(), f, a, w, h)}
|
2021-07-29 16:06:03 +02:00
|
|
|
{}
|
2021-07-18 18:26:12 +02:00
|
|
|
|
2021-08-21 20:59:39 +02:00
|
|
|
Texture::Texture(const Renderer& r, SDL_Surface* const s)
|
2021-08-26 18:59:52 +02:00
|
|
|
: Texture {SDL_CreateTextureFromSurface(r._data.get(), s)}
|
2021-08-21 20:59:39 +02:00
|
|
|
{}
|
2021-07-18 18:26:12 +02:00
|
|
|
|
2021-08-06 14:22:56 +02:00
|
|
|
Texture::~Texture() noexcept
|
2021-07-18 18:26:12 +02:00
|
|
|
{
|
2021-08-10 23:48:19 +02:00
|
|
|
// Idk why but freeing the format in ~Attr gives memory errors (valgrind)
|
|
|
|
SDL_FreeFormat(_attributes.format);
|
2021-07-18 18:26:12 +02:00
|
|
|
}
|
|
|
|
|
2021-08-06 14:22:56 +02:00
|
|
|
auto Texture::alpha_mode() -> uint8_t
|
2021-07-18 18:26:12 +02:00
|
|
|
{
|
2021-07-30 20:41:47 +02:00
|
|
|
uint8_t mode = 0;
|
2021-08-26 18:59:52 +02:00
|
|
|
success_or_throw<SDLError>(SDL_GetTextureAlphaMod(_data.get(), &mode));
|
2021-07-18 18:26:12 +02:00
|
|
|
|
|
|
|
return mode;
|
|
|
|
}
|
|
|
|
|
2021-08-08 14:30:56 +02:00
|
|
|
auto Texture::alpha_mode(const uint8_t m) -> Texture*
|
2021-07-18 18:26:12 +02:00
|
|
|
{
|
2021-08-26 18:59:52 +02:00
|
|
|
success_or_throw<SDLError>(SDL_SetTextureAlphaMod(_data.get(), m));
|
2021-07-18 18:26:12 +02:00
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2021-08-06 14:22:56 +02:00
|
|
|
auto Texture::blend_mode() -> SDL_BlendMode
|
2021-07-18 18:26:12 +02:00
|
|
|
{
|
2021-07-30 20:41:47 +02:00
|
|
|
SDL_BlendMode mode {};
|
2021-08-26 18:59:52 +02:00
|
|
|
success_or_throw<SDLError>(SDL_GetTextureBlendMode(_data.get(), &mode));
|
2021-07-18 18:26:12 +02:00
|
|
|
|
|
|
|
return mode;
|
|
|
|
}
|
|
|
|
|
2021-08-08 14:30:56 +02:00
|
|
|
auto Texture::blend_mode(const SDL_BlendMode m) -> Texture*
|
2021-07-18 18:26:12 +02:00
|
|
|
{
|
2021-08-26 18:59:52 +02:00
|
|
|
success_or_throw<SDLError>(SDL_SetTextureBlendMode(_data.get(), m));
|
2021-07-18 18:26:12 +02:00
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2021-08-06 14:22:56 +02:00
|
|
|
auto Texture::color_mode() -> Color
|
2021-07-18 18:26:12 +02:00
|
|
|
{
|
2021-08-06 14:22:56 +02:00
|
|
|
Color mode;
|
2021-08-13 22:00:42 +02:00
|
|
|
success_or_throw<SDLError>(
|
2021-08-26 18:59:52 +02:00
|
|
|
SDL_GetTextureColorMod(_data.get(), &mode().r, &mode().g, &mode().b));
|
2021-07-18 18:26:12 +02:00
|
|
|
|
|
|
|
return mode;
|
|
|
|
}
|
|
|
|
|
2021-08-14 16:37:02 +02:00
|
|
|
auto Texture::color_mode(Color m) -> Texture*
|
2021-07-18 18:26:12 +02:00
|
|
|
{
|
2021-08-26 18:59:52 +02:00
|
|
|
success_or_throw<SDLError>(SDL_SetTextureColorMod(_data.get(), m().r, m().g, m().b));
|
2021-07-18 18:26:12 +02:00
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2021-08-06 14:22:56 +02:00
|
|
|
auto Texture::scale_mode() -> SDL_ScaleMode
|
2021-07-18 18:26:12 +02:00
|
|
|
{
|
2021-07-30 20:41:47 +02:00
|
|
|
SDL_ScaleMode mode {};
|
2021-08-26 18:59:52 +02:00
|
|
|
success_or_throw<SDLError>(SDL_GetTextureScaleMode(_data.get(), &mode));
|
2021-07-18 18:26:12 +02:00
|
|
|
|
|
|
|
return mode;
|
|
|
|
}
|
|
|
|
|
2021-08-08 14:30:56 +02:00
|
|
|
auto Texture::scale_mode(const SDL_ScaleMode m) -> Texture*
|
2021-07-18 18:26:12 +02:00
|
|
|
{
|
2021-08-26 18:59:52 +02:00
|
|
|
success_or_throw<SDLError>(SDL_SetTextureScaleMode(_data.get(), m));
|
2021-07-18 18:26:12 +02:00
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2021-08-21 20:59:39 +02:00
|
|
|
auto Texture::update(const SDL_Rect* const r, const void* const pixels, const int pitch)
|
|
|
|
-> Texture*
|
2021-07-18 18:26:12 +02:00
|
|
|
{
|
2021-08-26 18:59:52 +02:00
|
|
|
success_or_throw<SDLError>(SDL_UpdateTexture(_data.get(), r, pixels, pitch));
|
2021-07-18 18:26:12 +02:00
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|