bwidgets/inc/basic_widgets/core/texture.hpp

81 lines
2.5 KiB
C++
Raw Normal View History

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>
#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>
#include <basic_widgets/core/type/opaque_struct.hpp>
#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
{
uint32_t format_raw;
SDL_PixelFormat* format;
SDL_TextureAccess access;
int w, h;
} _attributes {};
2021-07-16 17:18:13 +02:00
public:
Texture(SDL_Texture*);
Texture(Renderer*, SDL_PixelFormatEnum, SDL_TextureAccess,
2021-08-10 23:48:19 +02:00
int, int);
Texture(Renderer*, SDL_Surface*);
2021-08-06 14:22:56 +02:00
Texture(const Texture&) = delete;
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;
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
{
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)
{
SDL_Rect rect = r;
update(&rect, pix, pitch);
2021-07-16 17:18:13 +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)
{
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
return attr;
}
2021-07-16 17:18:13 +02:00
};
}
#endif