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
+31 -341
View File
@@ -1,38 +1,27 @@
#pragma once
/** @file Renderer.hpp
* @brief HDK sdl video header only wrapper for SDL_Renderer struct & related functions
*/
/// @file Renderer.hpp
/// Header-only wrapper for SDL_Renderer struct & related functions.
/// For complete documentation, see src/render/Renderer.cpp
///
#include <SDL3/SDL.h>
#include <hdk/grid/SharedPtrWrapper.hpp>
#include <hdk/sdl/Properties.hpp>
/** Renderers are used to render 2D graphics in SDL */
#include <hdk/sdl/video/Surface.hpp>
namespace hdk::sdl {
class Window;
class Texture;
/**
* @brief Wraps the SDL_Renderer struct and related functions in a C++ class that uses shared_ptr for lifetime management.
*
*
*/
class Renderer : public hdk::grid::SharedPtrWrapper<SDL_Renderer> {
public:
friend class Window;
friend class Texture;
friend std::pair<Window, Renderer> CreateWindowAndRenderer(
const char* title, int width, int height, SDL_WindowFlags window_flags);
/** Inherit constructors from SharedPtrWrapper */
using hdk::grid::SharedPtrWrapper<SDL_Renderer>::SharedPtrWrapper;
/// @todo https://wiki.libsdl.org/SDL3/SDL_AddVulkanRenderSemaphores
/// @todo https://wiki.libsdl.org/SDL3/SDL_ConvertEventToRenderCoordinates
/// @todo https://wiki.libsdl.org/SDL3/SDL_CreateGPURenderer
/// @todo https://wiki.libsdl.org/SDL3/SDL_CreateGPURenderState
/** @see https://wiki.libsdl.org/SDL3/SDL_CreateRenderer
* @brief Create a renderer for a window.
* @param window The window to create the renderer for. If the window is invalid, this function will return an
* invalid renderer.
* @param driverName The name of the rendering driver to initialize. If NULL, the first is used.
* @return Renderer The created renderer. If the creation fails, returns an invalid renderer.
*/
/// @todo SDL_AddVulkanRenderSemaphores
/// @todo SDL_ConvertEventToRenderCoordinates
/// @todo SDL_CreateGPURenderer
/// @todo SDL_CreateGPURenderState
static Renderer Create(SDL_Window* window, const char* driverName) {
return Renderer(get_or_cache(SDL_CreateRenderer(window, driverName), SDL_DestroyRenderer));
}
@@ -41,62 +30,27 @@ namespace hdk::sdl {
static Renderer CreateSoftware(SDL_Surface* surface) {
return Renderer(get_or_cache(SDL_CreateSoftwareRenderer(surface), SDL_DestroyRenderer));
}
/// @todo https://wiki.libsdl.org/SDL3/SDL_CreateWindowAndRenderer
/// @todo https://wiki.libsdl.org/SDL3/SDL_DestroyGPURenderState
/// @todo https://wiki.libsdl.org/SDL3/SDL_DestroyRenderer
/// @todo SDL_CreateWindowAndRenderer
/// @todo SDL_DestroyGPURenderState
/// @todo SDL_DestroyRenderer
/// Implemented in `../render.hpp` to avoid circular dependency with Texture
Texture CreateTexture(SDL_PixelFormat format, SDL_TextureAccess access, int w, int h) const;
/// Implemented in `../render.hpp` to avoid circular dependency with Texture
Texture CreateTextureFromSurface(SDL_Surface* surface) const;
/// Implemented in `../render.hpp` to avoid circular dependency with Texture
Texture CreateTextureWithProperties(SDL_PropertiesID properties) const;
/** @see https://wiki.libsdl.org/SDL3/SDL_FlushRenderer
* @brief Flush the current rendering commands to the screen.
* @return bool True if the renderer was successfully flushed, false otherwise.
*/
bool Flush() const { return SDL_FlushRenderer(*this); }
/// @todo https://wiki.libsdl.org/SDL3/SDL_GDKResumeRenderer
/// @todo https://wiki.libsdl.org/SDL3/SDL_GDKSuspendRenderer
/** @see https://wiki.libsdl.org/SDL3/SDL_GetCurrentRenderOutputSize
* @brief Get the output size in pixels of the current render target.
* @param w A pointer to an int to be filled in with the width of the render output. If the renderer is invalid,
* this will be set to 0.
* @param h A pointer to an int to be filled in with the height of the render output. If the renderer is invalid,
* this will be set to 0.
* @return bool True if the output size was successfully retrieved, false otherwise.
*/
/// @todo SDL_GDKResumeRenderer
/// @todo SDL_GDKSuspendRenderer
bool GetOutputSize(int* w, int* h) const { return SDL_GetRenderOutputSize(*this, w, h); }
/** @see https://wiki.libsdl.org/SDL3/SDL_GetDefaultTextureScaleMode
* @brief Get the default texture scale mode for the renderer.
* @param scaleMode A pointer to an SDL_ScaleMode variable to be filled in with the default texture scale mode for
* the renderer. If the renderer is invalid, this will be set to SDL_ScaleMode_None.
* @return bool True if the default texture scale mode was successfully retrieved, false otherwise.
*/
bool GetDefaultTextureScaleMode(SDL_ScaleMode* scaleMode) const {
return SDL_GetDefaultTextureScaleMode(*this, scaleMode);
}
/// @todo https://wiki.libsdl.org/SDL3/SDL_GetGPURendererDevice
// https://wiki.libsdl.org/SDL3/SDL_GetNumRenderDrivers
/** @see https://wiki.libsdl.org/SDL3/SDL_GetRenderClipRect
* @brief Get the clip rectangle for the current rendering target.
* @param rect A pointer to an SDL_Rect structure to be filled in with the clip rectangle for the current
* rendering target. If the renderer is invalid, this will be set to {0, 0, 0, 0}.
* @return bool True if the clip rectangle was successfully retrieved, false otherwise.
*/
/// @todo SDL_GetGPURendererDevice
static int GetNumRenderDrivers() { return SDL_GetNumRenderDrivers(); }
bool GetClipRect(SDL_Rect* rect) const { return SDL_GetRenderClipRect(*this, rect); }
/** @see https://wiki.libsdl.org/SDL3/SDL_GetRenderColorScale
* @brief Get the color scale for the renderer.
* @param scale A pointer to a float to be filled in with the color scale for the renderer. If the renderer is
* invalid, this will be set to 0.0f.
* @return bool True if the color scale was successfully retrieved, false otherwise.
*/
bool GetColorScale(float* scale) const { return SDL_GetRenderColorScale(*this, scale); }
/** @see https://wiki.libsdl.org/SDL3/SDL_GetRenderDrawBlendMode
* @brief Get the blend mode used for drawing operations (Fill and Line).
* @param blendMode A pointer to an SDL_BlendMode variable to be filled in with the blend mode used for drawing
* operations. If the renderer is invalid, this will be set to SDL_BlendMode_None.
* @return bool True if the blend mode was successfully retrieved, false otherwise.
*/
bool GetDrawBlendMode(SDL_BlendMode* blendMode) const { return SDL_GetRenderDrawBlendMode(*this, blendMode); }
/* @see https://wiki.libsdl.org/SDL3/SDL_GetRenderDrawColor
* @brief Get the color used for drawing operations (Fill and Line).
@@ -187,373 +141,109 @@ namespace hdk::sdl {
bool GetSafeArea(SDL_Rect* rect) const { return SDL_GetRenderSafeArea(*this, rect); }
/** @see https://wiki.libsdl.org/SDL3/SDL_GetRenderScale
* @brief Get the scale of the current render target.
* @param scaleX A pointer to a float to be filled in with the horizontal scale factor.
* @param scaleX A pointer to a float to be filled in with the horizontal scale factor.
* @param scaleY A pointer to a float to be filled in with the vertical scale factor.
* @return bool True on success
*/
bool GetScale(float* scaleX, float* scaleY) const { return SDL_GetRenderScale(*this, scaleX, scaleY); }
/**
@todo https://wiki.libsdl.org/SDL3/SDL_GetRenderTarget
**/
/** @see https://wiki.libsdl.org/SDL3/SDL_GetRenderTextureAddressMode
* @brief Get the texture address mode for the current render target.
* @param u_mode A pointer to an SDL_TextureAddressMode variable
* @param v_mode A pointer to an SDL_TextureAddressMode variable
* @return bool True on success
*/
/// Implemented in `../render.hpp` to avoid circular dependency with Texture
Texture GetRenderTarget() const;
bool GetTextureAddressMode(SDL_TextureAddressMode* u_mode, SDL_TextureAddressMode* v_mode) const {
return SDL_GetRenderTextureAddressMode(*this, u_mode, v_mode);
}
/** @see https://wiki.libsdl.org/SDL3/SDL_GetRenderViewport
* @brief Get the viewport for the current render target.
* @param rect A pointer to an SDL_Rect structure to be filled in with the viewport
* @return bool True on success
*/
bool GetViewport(SDL_Rect* rect) const { return SDL_GetRenderViewport(*this, rect); }
/** @see https://wiki.libsdl.org/SDL3/SDL_GetRenderVSync
* @brief Get the vertical sync setting for the current renderer.
* @param vsync A pointer to an int to store the vertical sync setting
* @return bool True on success
*/
bool GetVSync(int* vsync) const { return SDL_GetRenderVSync(*this, vsync); }
/**
@todo https://wiki.libsdl.org/SDL3/SDL_GetRenderWindow
**/
/** @see https://wiki.libsdl.org/SDL3/SDL_RenderClear
* @brief Clear the current rendering target with the drawing color.
* @return bool True if the renderer was successfully cleared, false otherwise.
*/
bool Clear() const { return SDL_RenderClear(*this); }
//
/** @see https://wiki.libsdl.org/SDL3/SDL_RenderClipEnabled
* @brief Check whether clipping is enabled for the current render target.
* @return bool True if clipping is enabled, false otherwise.
*/
// https://wiki.libsdl.org/SDL3/SDL_RenderCoordinatesFromWindow
/** @see https://wiki.libsdl.org/SDL3/SDL_RenderCoordinatesFromWindow
* @brief Convert window coordinates to renderer coordinates.
* @param windowX The x coordinate in window space
* @param windowY The y coordinate in window space
* @param rendererX A pointer to a float to be filled in with the x coordinate
* @param rendererY A pointer to a float to be filled in with the y coordinate
* @return bool True if the coordinates were successfully converted, false otherwise.
*/
bool ClipEnabled() const { return SDL_RenderClipEnabled(*this); }
bool CoordinatesFromWindow(float windowX, float windowY, float* rendererX, float* rendererY) const {
return SDL_RenderCoordinatesFromWindow(*this, windowX, windowY, rendererX, rendererY);
}
/** @see https://wiki.libsdl.org/SDL3/SDL_RenderCoordinatesToWindow
* @brief Convert renderer coordinates to window coordinates.
* @param rendererX The x coordinate in renderer space
* @param rendererY The y coordinate in renderer space
* @param windowX A pointer to a float to be filled in with the x coordinate
* @param windowY A pointer to a float to be filled in with the y coordinate
* @return bool True if the coordinates were successfully converted, false otherwise.
*/
bool CoordinatesToWindow(float rendererX, float rendererY, float* windowX, float* windowY) const {
return SDL_RenderCoordinatesToWindow(*this, rendererX, rendererY, windowX, windowY);
}
/** @see https://wiki.libsdl.org/SDL3/SDL_RenderDebugText
* @brief Render debug text at a specific position in the current render target.
* @param x The x coordinate of the text position
* @param y The y coordinate of the text position
* @param text The text to render
* @return bool True if the text was successfully rendered, false otherwise.
*/
bool DebugText(float x, float y, const char* text) const { return SDL_RenderDebugText(*this, x, y, text); }
/** @see https://wiki.libsdl.org/SDL3/SDL_RenderDebugTextFormat
* @brief Render formatted debug text at a specific position in the current render target.
* @param x The x coordinate of the text position
* @param y The y coordinate of the text position
* @param fmt The formatted text to render (printf-style format string)
* @param args The arguments for the formatted text
* @return bool True if the text was successfully rendered, false otherwise.
*/
template <typename... Args> bool DebugTextFormat(float x, float y, const char* fmt, Args... args) const {
return SDL_RenderDebugTextFormat(*this, x, y, fmt, args...);
}
/** @see https://wiki.libsdl.org/SDL3/SDL_RenderFillRect
* @brief fill rectangle with current draw color
* @param rect pointer to rectangle to fill, or NULL to fill the entire render target
* @return bool true on success
*/
bool FillRect(const SDL_FRect* rect) const { return SDL_RenderFillRect(*this, rect); }
/** @see https://wiki.libsdl.org/SDL3/SDL_RenderFillRects
* @brief fill multiple rectangles with current draw color
* @param rects pointer to array of rectangles to fill
* @param count number of rectangles in the array
* @return bool true on success
*/
bool FillRects(const SDL_FRect* rects, int count) const { return SDL_RenderFillRects(*this, rects, count); }
/** @see https://wiki.libsdl.org/SDL3/SDL_RenderGeometry
* @brief Render geometry with the current render target
* @param texture The texture to use for rendering
* @param vertices Pointer to an array of vertices
* @param numVertices Number of vertices in the array
* @param indices Pointer to an array of indices
* @param numIndices Number of indices in the array
* @return bool True on success, false on failure
*/
bool RenderGeometry(
SDL_Texture* texture, const SDL_Vertex* vertices, int numVertices, const int* indices, int numIndices) const {
return SDL_RenderGeometry(*this, texture, vertices, numVertices, indices, numIndices);
}
/** @see https://wiki.libsdl.org/SDL3/SDL_RenderGeometryRaw
* @brief render a list of triangles with optional texture and index data
* @param texture The texture to use for rendering, or NULL for no texture
* @param xy Pointer to an array of 2D vertex positions (x, y) in normalized coordinates (0.0 to 1.0)
* @param xy_stride The byte stride between consecutive vertex positions in the xy array
* @param color Pointer to an array of vertex colors
* @param color_stride The byte stride between consecutive vertex colors in the color array
* @param uv Pointer to an array of texture coordinates (u, v) in normalized coordinates (0.0 to 1.0), or NULL if
* not using textures
* @param uv_stride The byte stride between consecutive texture coordinates in the uv array
* @param numVertices The number of vertices in the xy, color, and uv arrays
* @param indices Pointer to an array of vertex indices that define the triangles to render, or NULL to render the
* vertices in sequential order
* @param numIndices The number of indices
* @param sizeIndices The byte size of each index in the indices array (1, 2, or 4 bytes)
* @return bool True on success
*/
bool RenderGeometryRaw(SDL_Texture* texture, const float* xy, int xy_stride, const SDL_FColor* color,
int color_stride, const float* uv, int uv_stride, int numVertices, const void* indices, int numIndices,
int sizeIndices) const {
return SDL_RenderGeometryRaw(*this, texture, xy, xy_stride, color, color_stride, uv, uv_stride, numVertices,
indices, numIndices, sizeIndices);
}
/** @see https://wiki.libsdl.org/SDL3/SDL_RenderLine
* @brief render line with current draw color
* @param x1
* @param y1
* @param x2
* @param y2
* @return bool true on success
*/
bool RenderLine(float x1, float y1, float x2, float y2) const { return SDL_RenderLine(*this, x1, y1, x2, y2); }
/** @see https://wiki.libsdl.org/SDL3/SDL_RenderLines
* @brief render multiple lines with current draw color
* @param points
* @param count number of points in the array
* @return bool true on success
*/
bool RenderLines(const SDL_FPoint* points, int count) const { return SDL_RenderLines(*this, points, count); }
/** @see https://wiki.libsdl.org/SDL3/SDL_RenderPoint
* @brief render point with current draw color
* @param x
* @param y
* @return bool true on success
*/
bool RenderPoint(float x, float y) const { return SDL_RenderPoint(*this, x, y); }
/** @see https://wiki.libsdl.org/SDL3/SDL_RenderPoints
* @brief render multiple points with current draw color
* @param points
* @param count number of points in the array
* @return bool true on success
*/
bool RenderPoints(const SDL_FPoint* points, int count) const { return SDL_RenderPoints(*this, points, count); }
/** @see https://wiki.libsdl.org/SDL3/SDL_RenderPresent
* @brief Update the screen with any rendering performed since the previous call.
* @return bool True if the screen was successfully updated, false otherwise.
*/
bool Present() const { return SDL_RenderPresent(*this); }
/** @todo https://wiki.libsdl.org/SDL3/SDL_RenderReadPixels */
/** @see https://wiki.libsdl.org/SDL3/SDL_RenderRect
* @brief render rectangle outline with current draw color
* @param rect pointer to rectangle to outline, or NULL to outline the entire render target
* @return bool true on success
*/
Surface ReadPixels(const SDL_Rect* rect) const {
return Surface(Surface::get_or_cache(SDL_RenderReadPixels(*this, rect), SDL_DestroySurface));
}
bool RenderRect(const SDL_FRect* rect) const { return SDL_RenderRect(*this, rect); }
/** @see https://wiki.libsdl.org/SDL3/SDL_RenderRects
* @brief render multiple rectangle outlines with current draw color
* @param rects pointer to array of rectangles to outline
* @param count number of rectangles in the array
* @return bool true on success
*/
bool RenderRects(const SDL_FRect* rects, int count) const { return SDL_RenderRects(*this, rects, count); }
/** @see https://wiki.libsdl.org/SDL3/SDL_RenderTexture
* @brief Render texture
* @param texture
* @param src
* @param dst
* @return bool true on success
*/
bool RenderTexture(SDL_Texture* texture, const SDL_FRect* src, const SDL_FRect* dst) const {
return SDL_RenderTexture(*this, texture, src, dst);
}
/** @see https://wiki.libsdl.org/SDL3/SDL_RenderTexture9Grid
* @brief Render a texture with 9-grid scaling
* @param texture
* @param src
* @param left_width
* @param right_width
* @param top_height
* @param bottom_height
* @param scale
* @param dst
* @return bool true on success
*/
bool RenderTexture9Grid(SDL_Texture* texture, const SDL_FRect* src, float left_width, float right_width,
float top_height, float bottom_height, float scale, const SDL_FRect* dst) const {
return SDL_RenderTexture9Grid(
*this, texture, src, left_width, right_width, top_height, bottom_height, scale, dst);
}
/** @see https://wiki.libsdl.org/SDL3/SDL_RenderTexture9GridTiled
* @brief Render a texture with 9-grid scaling and tiling
* @param texture
* @param src
* @param left_width
* @param right_width
* @param top_height
* @param bottom_height
* @param scale
* @param dst
* @param tile_scale
* @return bool true on success
*/
bool RenderTexture9GridTiled(SDL_Texture* texture, const SDL_FRect* src, float left_width, float right_width,
float top_height, float bottom_height, float scale, const SDL_FRect* dst, float tile_scale) const {
return SDL_RenderTexture9GridTiled(
*this, texture, src, left_width, right_width, top_height, bottom_height, scale, dst, tile_scale);
}
/** @see https://wiki.libsdl.org/SDL3/SDL_RenderTextureAffine
* @brief Render a texture with affine transformations
* @param texture
* @param src
* @param origin
* @param right
* @param down
* @return bool true on success
*/
bool RenderTextureAffine(SDL_Texture* texture, const SDL_FRect* src, const SDL_FPoint* origin,
const SDL_FPoint* right, const SDL_FPoint* down) const {
return SDL_RenderTextureAffine(*this, texture, src, origin, right, down);
}
/** @see https://wiki.libsdl.org/SDL3/SDL_RenderTextureRotated
* @brief Render a texture with rotation and flipping
* @param texture
* @param srcrect
* @param dstrect
* @param angle
* @param center
* @param flip
* @return bool true on success
*/
bool RenderTextureRotated(SDL_Texture* texture, const SDL_FRect* srcrect, const SDL_FRect* dstrect, double angle,
const SDL_FPoint* center, SDL_FlipMode flip) const {
return SDL_RenderTextureRotated(*this, texture, srcrect, dstrect, angle, center, flip);
}
/** @see https://wiki.libsdl.org/SDL3/SDL_RenderTextureTiled
* @brief Render a texture with tiling
* @param texture
* @param srcrect
* @param scale
* @param dstrect
* @param tile_scale
* @return bool true on success
*/
bool RenderTextureTiled(
SDL_Texture* texture, const SDL_FRect* srcrect, float scale, const SDL_FRect* dstrect, float tile_scale) const {
return SDL_RenderTextureTiled(*this, texture, srcrect, scale, dstrect);
}
/** @see https://wiki.libsdl.org/SDL3/SDL_SetDefaultTextureScaleMode
* @brief Set the default texture scale mode for the renderer.
* @param scaleMode The default texture scale mode for the renderer.
* @return bool True if the default texture scale mode was successfully set, false otherwise.
*/
bool SetDefaultTextureScaleMode(SDL_ScaleMode scaleMode) const {
return SDL_SetDefaultTextureScaleMode(*this, scaleMode);
}
/** @todo https://wiki.libsdl.org/SDL3/SDL_SetGPURenderState
/** @todo SDL_SetGPURenderState
// https://wiki.libsdl.org/SDL3/SDL_SetGPURenderStateFragmentUniforms
// https://wiki.libsdl.org/SDL3/SDL_SetGPURenderStateSamplerBindings
// https://wiki.libsdl.org/SDL3/SDL_SetGPURenderStateStorageBuffers
// https://wiki.libsdl.org/SDL3/SDL_SetGPURenderStateStorageTextures
**/
/** @see https://wiki.libsdl.org/SDL3/SDL_SetRenderClipRect
* @brief Set the clip rectangle for the current rendering target.
* @param rect
* @return bool true on success
*/
bool SetClipRect(const SDL_Rect* rect) const { return SDL_SetRenderClipRect(*this, rect); }
/** @see https://wiki.libsdl.org/SDL3/SDL_SetRenderColorScale
* @brief Set the color scale for the renderer.
* @param scale The color scale for the renderer.
* @return bool true on success
*/
bool SetRenderColorScale(float scale) const { return SDL_SetRenderColorScale(*this, scale); }
/** @see https://wiki.libsdl.org/SDL3/SDL_SetRenderDrawBlendMode
* @brief Set the blend mode used for drawing operations (Fill and Line).
* @param blendMode The blend mode used for drawing operations.
* @return bool true on success
*/
bool SetDrawBlendMode(SDL_BlendMode blendMode) const { return SDL_SetRenderDrawBlendMode(*this, blendMode); }
/** @see https://wiki.libsdl.org/SDL3/SDL_SetRenderDrawColor
* @brief Set the color used for drawing operations (Fill and Line).
* @param r The red component of the drawing color
* @param g The green component of the drawing color
* @param b The blue component of the drawing color
* @param a The alpha component of the drawing color
* @return bool true on success
*/
bool SetDrawColor(Uint8 r, Uint8 g, Uint8 b, Uint8 a) const {
return SDL_SetRenderDrawColor(*this, r, g, b, a);
}
/** @see https://wiki.libsdl.org/SDL3/SDL_SetRenderDrawColorFloat
* @brief Set the color used for drawing operations (Fill and Line) as floats.
* @param r The red component of the drawing color
* @param g The green component of the drawing color
* @param b The blue component of the drawing color
* @param a The alpha component of the drawing color
* @return bool true on success
*/
bool SetDrawColor(Uint8 r, Uint8 g, Uint8 b, Uint8 a) const { return SDL_SetRenderDrawColor(*this, r, g, b, a); }
bool SetDrawColorFloat(float r, float g, float b, float a) const {
return SDL_SetRenderDrawColorFloat(*this, r, g, b, a);
}
/** @see https://wiki.libsdl.org/SDL3/SDL_SetRenderLogicalPresentation
* @brief Set the logical presentation for the renderer.
* @param w The width of the logical presentation
* @param h The height of the logical presentation
* @param mode The presentation mode for the logical presentation
* @return bool true on success
*/
bool SetLogicalPresentation(int w, int h, SDL_RendererLogicalPresentation mode) const {
return SDL_SetRenderLogicalPresentation(*this, w, h, mode);
}
/** @see https://wiki.libsdl.org/SDL3/SDL_SetRenderScale
* @brief Set the scale for the current render target.
* @param scaleX The horizontal scale factor
* @param scaleY The vertical scale factor
* @return bool true on success
*/
bool SetScale(float scaleX, float scaleY) const { return SDL_SetRenderScale(*this, scaleX, scaleY); }
/** @see https://wiki.libsdl.org/SDL3/SDL_SetRenderTarget
* @brief Set the render target to a texture or the default render target.
* @param texture The texture to set as the render target, or NULL to set the default render target
* @return bool true on success
*/
bool SetRenderTarget(SDL_Texture* texture) const { return SDL_SetRenderTarget(*this, texture); }
/** @see https://wiki.libsdl.org/SDL3/SDL_SetRenderTextureAddressMode
* @brief Set the texture address mode for the current render target.
* @param u_mode The texture address mode for the horizontal direction
* @param v_mode The texture address mode for the vertical direction
* @return bool true on success
*/
bool SetTextureAddressMode(SDL_TextureAddressMode u_mode, SDL_TextureAddressMode v_mode) const {
return SDL_SetRenderTextureAddressMode(*this, u_mode, v_mode);
}
/** @see https://wiki.libsdl.org/SDL3/SDL_SetRenderViewport
* @brief Set the viewport for the current render target.
* @param rect A pointer to an SDL_Rect structure representing the viewport, or NULL to use the entire render
* target.
* @return bool True if the viewport was successfully set, false otherwise.
*/
bool ViewportSet() const { return SDL_RenderViewportSet(*this); }
bool SetViewport(const SDL_Rect* rect) const { return SDL_SetRenderViewport(*this, rect); }
/** @see https://wiki.libsdl.org/SDL3/SDL_SetRenderVSync
* @brief Set the vertical sync setting for the current renderer.
* @param vsync The vertical sync setting to use (0 for no vsync, 1 for vsync, -1 for adaptive vsync)
* @return bool True on success, false on failure
*/
bool SetVSync(int vsync) const { return SDL_SetRenderVSync(*this, vsync); }
/// Forward declare: implemented in ../video.hpp to avoid circular dependency
bool SetVSync(int vsync) const { return SDL_SetRenderVSync(*this, vsync); }
/// Implemented in ../video.hpp to avoid circular dependency
Window GetWindow() const;
};
}
+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);
}