bwidgets/inc/basic_widgets/core/texture.hpp

82 lines
2.4 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 <memory>
2021-08-23 00:00:57 +02:00
#include <SDL_pixels.h>
#include <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>
#include <basic_widgets/core/type/deleter.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-23 23:50:14 +02:00
// Wrap most of the texture functions of SDL 2D rendering API.
class Texture final
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;
};
const Attr _attributes;
const std::unique_ptr<SDL_Texture, Deleter> _data;
2021-07-16 17:18:13 +02:00
public:
2021-08-14 16:44:47 +02:00
explicit Texture(SDL_Texture*);
Texture(const Renderer&, SDL_PixelFormatEnum, SDL_TextureAccess, int, int);
Texture(const 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&&) = 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(const SDL_Rect*, const void*, int) -> Texture*;
2021-07-16 17:18:13 +02:00
[[nodiscard]] const auto& attributes() const noexcept
{
return _attributes;
}
2021-07-16 17:18:13 +02:00
auto update(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
[[nodiscard]] static auto attributes(SDL_Texture* t)
{
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
return attr;
}
2021-07-16 17:18:13 +02:00
};
}
#endif