bwidgets/inc/basic_widgets/core/texture.hpp

75 lines
2.2 KiB
C++
Raw Normal View History

#ifndef BWIDGETS_TEXTURE_HPP_
#define BWIDGETS_TEXTURE_HPP_
2021-07-16 17:18:13 +02:00
#include <cstdint>
#include <SDL2/SDL_render.h>
#include <SDL2/SDL_pixels.h>
#include <basic_widgets/core/type/opaque_struct.hpp>
#include <basic_widgets/core/type/sdl_error.hpp>
2021-07-16 17:18:13 +02:00
namespace bwidgets::core
2021-07-16 17:18:13 +02:00
{
struct Renderer;
2021-07-16 17:18:13 +02:00
struct Texture final : OpaqueStruct<SDL_Texture>
2021-07-16 17:18:13 +02:00
{
struct Attr
{
uint32_t format_raw;
SDL_PixelFormat* format;
SDL_TextureAccess access;
int w, h;
} _attributes;
public:
Texture(SDL_Texture*);
Texture(Renderer* r,
SDL_PixelFormatEnum f,
SDL_TextureAccess a,
int w, int h);
Texture(Renderer*, SDL_Surface*);
virtual ~Texture() noexcept override;
2021-07-16 17:18:13 +02:00
uint8_t alpha_mode();
Texture* alpha_mode(uint8_t);
2021-07-16 17:18:13 +02:00
SDL_BlendMode blend_mode();
Texture* blend_mode(SDL_BlendMode);
2021-07-16 17:18:13 +02:00
SDL_Color color_mode();
Texture* color_mode(const SDL_Color&);
2021-07-16 17:18:13 +02:00
SDL_ScaleMode scale_mode();
Texture* scale_mode(SDL_ScaleMode);
Texture* update(SDL_Rect*, const void*, int);
2021-07-16 17:18:13 +02:00
inline const Attr& attributes() {
2021-07-16 17:18:13 +02:00
return _attributes;
}
inline Texture* update(const SDL_Rect& r, const void* pix, int pitch) {
2021-07-16 17:18:13 +02:00
SDL_Rect rect = r;
update(&rect, pix, pitch);
return this;
2021-07-16 17:18:13 +02:00
}
static inline const Attr attributes(SDL_Texture* t) {
2021-07-16 17:18:13 +02:00
Attr attr {};
SDLError::success_or_throw(
2021-07-16 17:18:13 +02:00
SDL_QueryTexture(t,
&attr.format_raw, (int*)&attr.access, &attr.w, &attr.h),
__FILE__, __FUNCTION__, __LINE__
);
attr.format = SDLError::ptr_or_throw(
2021-07-16 17:18:13 +02:00
SDL_AllocFormat(attr.format_raw),
__FILE__, __FUNCTION__, __LINE__
);
return attr;
}
};
}
#endif