Pixels, Surfaces, Renderer, oh my...

This commit is contained in:
BadQuanta
2026-05-11 10:28:21 +00:00
parent 97c8847eb0
commit 28e9c4ba18
35 changed files with 4578 additions and 1210 deletions
+5 -164
View File
@@ -1,11 +1,10 @@
#pragma once
/** @file Texture.hpp
* @brief HDK sdl video header only wrapper for SDL_Texture struct & related functions
*/
/// @file Texture.hpp
/// For complete documentation, see src/render/Texture.cpp
#include <SDL3/SDL.h>
#include <hdk/grid/SharedPtrWrapper.hpp>
#include <hdk/sdl/Properties.hpp>
/** Textures are used to store pixel data that can be rendered by a renderer. */
#include <hdk/sdl/pixels/Palette.hpp>
namespace hdk::sdl {
/// Forward declare to avoid circular dependency with Renderer
class Renderer;
@@ -13,226 +12,68 @@ namespace hdk::sdl {
class Texture : public hdk::grid::SharedPtrWrapper<SDL_Texture> {
public:
friend class Renderer;
/** Inherit constructors from SharedPtrWrapper */
using hdk::grid::SharedPtrWrapper<SDL_Texture>::SharedPtrWrapper;
/** @see https://wiki.libsdl.org/SDL3/SDL_CreateTexture
* @brief Create a texture for a rendering context.
* @param renderer The rendering context to create the texture for.
* @param format The pixel format of the texture
* @param access The access pattern for the texture
* @param w The width of the texture in pixels
* @param h The height of the texture in pixels
* @return Texture The created texture. If the creation fails, returns an invalid texture.
*/
static Texture Create(SDL_Renderer* renderer, SDL_PixelFormat format, SDL_TextureAccess access, int w, int h) {
return Texture(get_or_cache(SDL_CreateTexture(renderer, format, access, w, h), SDL_DestroyTexture));
}
/** @see https://wiki.libsdl.org/SDL3/SDL_CreateTextureFromSurface
* @brief Create a texture from an existing surface.
* @param renderer The rendering context to create the texture for.
* @param surface The surface to create the texture from.
* @return Texture The created texture. If the creation fails, returns an invalid texture.
*/
static Texture CreateFromSurface(SDL_Renderer* renderer, SDL_Surface* surface) {
return Texture(get_or_cache(SDL_CreateTextureFromSurface(renderer, surface), SDL_DestroyTexture));
}
/** @see https://wiki.libsdl.org/SDL3/SDL_CreateTextureWithProperties
* @brief Create a texture for a rendering context with specific properties.
* @param renderer The rendering context to create the texture for.
* @param properties The properties to create the texture with.
* @return Texture The created texture. If the creation fails, returns an invalid texture.
*/
static Texture CreateWithProperties(SDL_Renderer* renderer, SDL_PropertiesID properties) {
return Texture(get_or_cache(SDL_CreateTextureWithProperties(renderer, properties), SDL_DestroyTexture));
}
// https://wiki.libsdl.org/SDL3/SDL_DestroyTexture
/** @see https://wiki.libsdl.org/SDL3/SDL_GetTextureAlphaMod
* @brief Get the alpha modulation value for a texture.
* @param alpha A pointer to an int
* @return bool true on success
*/
bool GetAlphaMod(Uint8* alpha) const { return SDL_GetTextureAlphaMod(*this, alpha); }
Uint8 GetAlphaMod() const {
Uint8 alpha;
SDL_GetTextureAlphaMod(*this, &alpha);
return alpha;
}
/** @see https://wiki.libsdl.org/SDL3/SDL_GetTextureAlphaModFloat
* @brief Get the alpha modulation value for a texture as a float.
* @param alpha A pointer to a float
* @return bool true on success
*/
bool GetAlphaModFloat(float* alpha) const { return SDL_GetTextureAlphaModFloat(*this, alpha); }
float GetAlphaModFloat() const {
float alpha;
SDL_GetTextureAlphaModFloat(*this, &alpha);
return alpha;
}
/** @see https://wiki.libsdl.org/SDL3/SDL_GetTextureBlendMode
* @brief Get the blend mode for a texture.
* @param blendMode A pointer to an SDL_BlendMode variable to be filled in
* @return bool true on success
*/
bool GetBlendMode(SDL_BlendMode* blendMode) const { return SDL_GetTextureBlendMode(*this, blendMode); }
SDL_BlendMode GetBlendMode() const {
SDL_BlendMode blendMode;
SDL_GetTextureBlendMode(*this, &blendMode);
return blendMode;
}
/** @see https://wiki.libsdl.org/SDL3/SDL_GetTextureColorMod
* @brief Get the color modulation values for a texture.
* @param r A pointer to a Uint8 to be filled in with the red color
* @param g A pointer to a Uint8 to be filled in with the green color
* @param b A pointer to a Uint8 to be filled in with the blue color
* @return bool true on success
*/
bool GetColorMod(Uint8* r, Uint8* g, Uint8* b) const { return SDL_GetTextureColorMod(*this, r, g, b); }
/** @see https://wiki.libsdl.org/SDL3/SDL_GetTextureColorModFloat
* @brief Get the color modulation values for a texture as floats.
* @param r A pointer to a float to be filled in with the red color
* @param g A pointer to a float to be filled in with the green color
* @param b A pointer to a float to be filled in with the blue color
* @return bool true on success
*/
bool GetColorModFloat(float* r, float* g, float* b) const { return SDL_GetTextureColorModFloat(*this, r, g, b); }
/** @todo https://wiki.libsdl.org/SDL3/SDL_GetTexturePalette **/
/** https://wiki.libsdl.org/SDL3/SDL_GetTextureProperties
* @brief Get the properties of a texture.
* @returns Properties associated with the texture.
*/
Palette GetPalette() const { return Palette(Palette::get_or_cache(SDL_GetTexturePalette(*this), SDL_DestroyPalette)); }
Properties GetProperties() const { return Properties(SDL_GetTextureProperties(*this)); }
/// Implemented in `../render.hpp` to avoid circular dependency with Renderer
Renderer GetRenderer() const;
/** @see https://wiki.libsdl.org/SDL3/SDL_GetTextureScaleMode
* @brief Get the scale mode for a texture.
* @param scaleMode A pointer to an SDL_ScaleMode variable to be filled in
* @return bool true on success
*/
bool GetScaleMode(SDL_ScaleMode* scaleMode) const { return SDL_GetTextureScaleMode(*this, scaleMode); }
SDL_ScaleMode GetScaleMode() const {
SDL_ScaleMode scaleMode;
SDL_GetTextureScaleMode(*this, &scaleMode);
return scaleMode;
}
/** @see https://wiki.libsdl.org/SDL3/SDL_GetTextureSize
* @brief Get the size of a texture.
* @param w A pointer to a float to be filled in with the width
* @param h A pointer to a float to be filled in with the height
* @return bool true on success
*/
bool GetSize(float* w, float* h) const { return SDL_GetTextureSize(*this, w, h); }
/** @see https://wiki.libsdl.org/SDL3/SDL_LockTexture
* @brief Lock a portion of the texture for write-only pixel access.
* @param rect A pointer to an SDL_Rect structure representing the area to lock, or NULL to lock the entire
* texture.
* @param pixels A pointer to a void* to be filled in with the locked pixels. You can cast this to the appropriate
* type based on the texture's pixel format.
* @param pitch A pointer to an int to be filled in with the pitch of the locked pixels (the length of a row of
* pixels in bytes).
* @return bool true on success
*/
bool Lock(const SDL_Rect* rect, void** pixels, int* pitch) const {
return SDL_LockTexture(*this, rect, pixels, pitch);
}
/** @see https://wiki.libsdl.org/SDL3/SDL_LockTextureToSurface
* @brief Lock a portion of the texture for write-only pixel access and create an SDL_Surface that can be used to
* access the pixels.
* @param rect A pointer to an SDL_Rect structure representing the area to lock, or NULL to lock the entire
* texture.
* @param surface A pointer to an SDL_Surface* to be filled in with the created surface. You must free this
* surface with SDL_FreeSurface when you are done with it.
* @return bool true on success
*/
bool LockToSurface(const SDL_Rect* rect, SDL_Surface** surface) const {
return SDL_LockTextureToSurface(*this, rect, surface);
}
/** @see https://wiki.libsdl.org/SDL3/SDL_SetTextureAlphaMod
* @brief Set the alpha modulation value for a texture.
* @param alpha The alpha modulation value to set
* @return bool true on success
*/
bool SetAlphaMod(Uint8 alpha) const { return SDL_SetTextureAlphaMod(*this, alpha); }
/** @see https://wiki.libsdl.org/SDL3/SDL_SetTextureAlphaModFloat
* @brief Set the alpha modulation value for a texture as a float.
* @param alpha The alpha modulation value to set
* @return bool true on success
*/
bool SetAlphaModFloat(float alpha) const { return SDL_SetTextureAlphaModFloat(*this, alpha); }
/** @see https://wiki.libsdl.org/SDL3/SDL_SetTextureBlendMode
* @brief Set the blend mode for a texture.
* @param blendMode The blend mode to set
* @return bool true on success
*/
bool SetBlendMode(SDL_BlendMode blendMode) const { return SDL_SetTextureBlendMode(*this, blendMode); }
/** @see https://wiki.libsdl.org/SDL3/SDL_SetTextureColorMod
* @brief Set the color modulation values for a texture.
* @param r The red color modulation value to set
* @param g The green color modulation value to set
* @param b The blue color modulation value to set
* @return bool true on success
*/
bool SetColorMod(Uint8 r, Uint8 g, Uint8 b) const { return SDL_SetTextureColorMod(*this, r, g, b); }
/** @see https://wiki.libsdl.org/SDL3/SDL_SetTextureColorModFloat
* @brief Set the color modulation values for a texture as floats.
* @param r The red color modulation value to set
* @param g The green color modulation value to set
* @param b The blue color modulation value to set
* @return bool true on success
*/
bool SetColorModFloat(float r, float g, float b) const { return SDL_SetTextureColorModFloat(*this, r, g, b); }
/** @todo https://wiki.libsdl.org/SDL3/SDL_SetTexturePalette **/
/** @see https://wiki.libsdl.org/SDL3/SDL_SetTextureScaleMode
* @brief Set the scale mode for a texture.
* @param scaleMode The scale mode to set
* @return bool true on success
*/
bool SetPalette(SDL_Palette* palette) const { return SDL_SetTexturePalette(*this, palette); }
bool SetScaleMode(SDL_ScaleMode scaleMode) const { return SDL_SetTextureScaleMode(*this, scaleMode); }
/** @see https://wiki.libsdl.org/SDL3/SDL_UnlockTexture
* @brief Unlock a texture, enabling the changes made to the texture's pixels to be rendered.
*/
void Unlock() const { SDL_UnlockTexture(*this); }
/** @see https://wiki.libsdl.org/SDL3/SDL_UpdateNVTexture
* @brief Update a rectangle within a texture with new pixel data in NV12 or NV21 format.
* @param rect A pointer to an SDL_Rect structure representing the area to update, or NULL to update the entire
* texture.
* @param Yplane A pointer to the pixel data for the Y plane of the texture. The format of this data should be a
* contiguous array of bytes, where each byte represents the intensity of the Y component for a pixel.
* @param Ypitch The pitch of the Y plane pixel data (the length of a row of pixels in bytes).
* @param UVplane A pointer to the pixel data for the interleaved U and V planes of the texture. The format of
* this data should be a contiguous array of bytes, where each pair of bytes represents the U and V components for
* a pixel. The U and V values should be interleaved, meaning that the first byte represents the U component and
* the second byte represents the V component for a pixel.
* @param UVpitch The pitch of the interleaved U and V plane pixel data (the length of a row of pixels in bytes).
* @return bool true on success
*/
bool UpdateNV(const SDL_Rect* rect, const Uint8* Yplane, int Ypitch, const Uint8* UVplane, int UVpitch) const {
return SDL_UpdateNVTexture(*this, rect, Yplane, Ypitch, UVplane, UVpitch);
}
/** @see https://wiki.libsdl.org/SDL3/SDL_UpdateTexture
* @brief Update a rectangle within a texture with new pixel data.
* @param rect A pointer to an SDL_Rect structure representing the area to update, or NULL to update the entire
* texture.
* @param pixels A pointer to the pixel data to update the texture with. The format of this data should match the
* pixel format of the texture.
* @param pitch The pitch of the pixel data (the length of a row of pixels in bytes).
* @return bool true on success
*/
bool Update(const SDL_Rect* rect, const void* pixels, int pitch) const {
return SDL_UpdateTexture(*this, rect, pixels, pitch);
}
/** @see https://wiki.libsdl.org/SDL3/SDL_UpdateYUVTexture
* @brief Update a rectangle within a texture
* @param rect A pointer to an SDL_Rect structure representing the area to update, or NULL to update the entire
* texture.
* @param Yplane A pointer to the pixel data for the Y plane of the texture.
* @param Ypitch The pitch of the Y plane pixel data (the length of a row of pixels in bytes).
* @param Uplane A pointer to the pixel data for the U plane of the texture.
* @param Upitch The pitch of the U plane pixel data (the length of a row of pixels in bytes).
* @param Vplane A pointer to the pixel data for the V plane of the texture.
* @param Vpitch The pitch of the V plane pixel data (the length of a row of pixels in bytes).
* @return bool true on success
*/
bool UpdateYUV(const SDL_Rect* rect, const Uint8* Yplane, int Ypitch, const Uint8* Uplane, int Upitch, const Uint8* Vplane, int Vpitch) const {
return SDL_UpdateYUVTexture(*this, rect, Yplane, Ypitch, Uplane, Upitch, Vplane, Vpitch);
}