2021-08-06 14:22:56 +02:00
|
|
|
#ifndef BWIDGETS_TEXTURE_HPP
|
|
|
|
#define BWIDGETS_TEXTURE_HPP
|
2021-07-16 17:18:13 +02:00
|
|
|
|
|
|
|
#include <SDL2/SDL_pixels.h>
|
2021-07-29 16:06:03 +02:00
|
|
|
#include <SDL2/SDL_render.h>
|
2021-07-16 17:18:13 +02:00
|
|
|
|
2021-08-13 22:00:42 +02:00
|
|
|
#include <basic_widgets/core/error_helper.hpp>
|
2021-08-06 14:22:56 +02:00
|
|
|
#include <basic_widgets/core/type/color.hpp>
|
2021-07-18 18:26:12 +02:00
|
|
|
#include <basic_widgets/core/type/opaque_struct.hpp>
|
2021-07-16 17:18:13 +02:00
|
|
|
|
2021-08-06 14:22:56 +02:00
|
|
|
namespace bwidgets
|
2021-07-16 17:18:13 +02:00
|
|
|
{
|
2021-08-06 14:22:56 +02:00
|
|
|
class Renderer;
|
2021-07-16 17:18:13 +02:00
|
|
|
|
2021-08-11 15:52:07 +02:00
|
|
|
class Texture final : OpaqueStruct<SDL_Texture>::Wrapper
|
2021-07-16 17:18:13 +02:00
|
|
|
{
|
2021-08-11 15:52:07 +02:00
|
|
|
friend Renderer;
|
|
|
|
|
2021-08-10 23:48:19 +02:00
|
|
|
struct Attr final
|
2021-07-29 16:06:03 +02:00
|
|
|
{
|
|
|
|
uint32_t format_raw;
|
|
|
|
SDL_PixelFormat* format;
|
|
|
|
SDL_TextureAccess access;
|
|
|
|
int w, h;
|
2021-07-30 20:41:47 +02:00
|
|
|
} _attributes {};
|
2021-07-16 17:18:13 +02:00
|
|
|
|
2021-07-29 16:06:03 +02:00
|
|
|
public:
|
2021-08-14 16:44:47 +02:00
|
|
|
explicit Texture(SDL_Texture*);
|
2021-08-19 15:00:44 +02:00
|
|
|
Texture(Renderer&, SDL_PixelFormatEnum, SDL_TextureAccess, int, int);
|
|
|
|
Texture(Renderer&, SDL_Surface*);
|
2021-08-06 14:22:56 +02:00
|
|
|
Texture(const Texture&) = delete;
|
2021-08-12 15:29:25 +02:00
|
|
|
Texture(Texture&&) = delete;
|
2021-08-11 15:52:07 +02:00
|
|
|
~Texture() noexcept;
|
2021-07-16 17:18:13 +02:00
|
|
|
|
2021-08-06 14:22:56 +02:00
|
|
|
auto operator=(const Texture&) = delete;
|
2021-08-12 23:50:22 +02:00
|
|
|
auto operator=(Texture&&) = delete;
|
2021-08-06 14:22:56 +02:00
|
|
|
|
|
|
|
[[nodiscard]] auto alpha_mode() -> uint8_t;
|
|
|
|
auto alpha_mode(uint8_t) -> Texture*;
|
|
|
|
[[nodiscard]] auto blend_mode() -> SDL_BlendMode;
|
|
|
|
auto blend_mode(SDL_BlendMode) -> Texture*;
|
|
|
|
[[nodiscard]] auto color_mode() -> Color;
|
2021-08-14 16:37:02 +02:00
|
|
|
auto color_mode(Color) -> Texture*;
|
2021-08-06 14:22:56 +02:00
|
|
|
[[nodiscard]] auto scale_mode() -> SDL_ScaleMode;
|
|
|
|
auto scale_mode(SDL_ScaleMode) -> Texture*;
|
|
|
|
auto update(SDL_Rect*, const void*, int) -> Texture*;
|
2021-07-16 17:18:13 +02:00
|
|
|
|
2021-08-14 09:17:51 +02:00
|
|
|
[[nodiscard]] const auto& attributes() const noexcept
|
2021-07-29 16:06:03 +02:00
|
|
|
{
|
|
|
|
return _attributes;
|
|
|
|
}
|
2021-07-16 17:18:13 +02:00
|
|
|
|
2021-08-19 15:00:44 +02:00
|
|
|
auto update(SDL_Rect&& r, const void* pix, int pitch)
|
2021-07-29 16:06:03 +02:00
|
|
|
{
|
|
|
|
SDL_Rect rect = r;
|
|
|
|
update(&rect, pix, pitch);
|
2021-07-16 17:18:13 +02:00
|
|
|
|
2021-07-29 16:06:03 +02:00
|
|
|
return this;
|
|
|
|
}
|
2021-07-16 17:18:13 +02:00
|
|
|
|
2021-08-14 09:17:51 +02:00
|
|
|
[[nodiscard]] static auto attributes(SDL_Texture* t)
|
2021-07-29 16:06:03 +02:00
|
|
|
{
|
|
|
|
Attr attr {};
|
2021-08-13 22:00:42 +02:00
|
|
|
success_or_throw<SDLError>(
|
2021-08-12 16:10:49 +02:00
|
|
|
SDL_QueryTexture(t, &attr.format_raw,
|
|
|
|
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-cstyle-cast)
|
2021-08-13 22:00:42 +02:00
|
|
|
(int*)&attr.access, &attr.w, &attr.h));
|
|
|
|
attr.format = ptr_or_throw<SDLError>(SDL_AllocFormat(attr.format_raw));
|
2021-07-16 17:18:13 +02:00
|
|
|
|
2021-07-29 16:06:03 +02:00
|
|
|
return attr;
|
|
|
|
}
|
2021-07-16 17:18:13 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|