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 <cstdint>
|
2021-08-10 23:48:19 +02:00
|
|
|
#include <memory>
|
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-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-20 15:20:46 +02:00
|
|
|
#include <basic_widgets/core/type/sdl_error.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:
|
|
|
|
Texture(SDL_Texture*);
|
2021-08-12 15:55:47 +02:00
|
|
|
Texture(Renderer*, SDL_PixelFormatEnum, SDL_TextureAccess,
|
2021-08-10 23:48:19 +02:00
|
|
|
int, int);
|
2021-08-12 15:55:47 +02:00
|
|
|
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 15:29:25 +02:00
|
|
|
auto operator=(Texture&&) -> 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;
|
|
|
|
auto color_mode(const Color&) -> Texture*;
|
|
|
|
[[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-08 16:00:22 +02:00
|
|
|
[[nodiscard]] inline 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-06 14:22:56 +02:00
|
|
|
inline auto update(const 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-06 14:22:56 +02:00
|
|
|
[[nodiscard]] static inline auto attributes(SDL_Texture* t)
|
2021-07-29 16:06:03 +02:00
|
|
|
{
|
|
|
|
Attr attr {};
|
|
|
|
SDLError::success_or_throw(SDL_QueryTexture(t, &attr.format_raw,
|
|
|
|
(int*)&attr.access, &attr.w,
|
|
|
|
&attr.h),
|
|
|
|
__FILE__, __FUNCTION__, __LINE__);
|
|
|
|
attr.format = SDLError::ptr_or_throw(SDL_AllocFormat(attr.format_raw),
|
|
|
|
__FILE__, __FUNCTION__, __LINE__);
|
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
|