Files
2026-05-13 19:48:22 +00:00

252 lines
15 KiB
C++

#pragma once
/// @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>
#include <hdk/sdl/video/Surface.hpp>
namespace hdk::sdl {
class Window;
class Texture;
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);
using hdk::grid::SharedPtrWrapper<SDL_Renderer>::SharedPtrWrapper;
/// @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));
}
// SDL_CreateRendererWithProperties
// https://wiki.libsdl.org/SDL3/SDL_CreateSoftwareRenderer
static Renderer CreateSoftware(SDL_Surface* surface) {
return Renderer(get_or_cache(SDL_CreateSoftwareRenderer(surface), 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;
bool Flush() const { return SDL_FlushRenderer(*this); }
/// @todo SDL_GDKResumeRenderer
/// @todo SDL_GDKSuspendRenderer
bool GetOutputSize(int* w, int* h) const { return SDL_GetRenderOutputSize(*this, w, h); }
bool GetDefaultTextureScaleMode(SDL_ScaleMode* scaleMode) const {
return SDL_GetDefaultTextureScaleMode(*this, scaleMode);
}
/// @todo SDL_GetGPURendererDevice
static int GetNumRenderDrivers() { return SDL_GetNumRenderDrivers(); }
bool GetClipRect(SDL_Rect* rect) const { return SDL_GetRenderClipRect(*this, rect); }
bool GetColorScale(float* scale) const { return SDL_GetRenderColorScale(*this, scale); }
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).
* @param r A pointer to a Uint8 to be filled in with the red component of the drawing color. If the renderer is
* invalid, this will be set to 0.
* @param g A pointer to a Uint8 to be filled in with the green component of the drawing color. If the renderer is
* invalid, this will be set to 0.
* @param b A pointer to a Uint8 to be filled in with the blue component of the drawing color. If the renderer is
* invalid, this will be set to 0.
* @param a A pointer to a Uint8 to be filled in with the alpha component of the drawing color. If the renderer is
* invalid, this will be set to 0.
* @return bool True if the drawing color was successfully retrieved, false otherwise.
*/
bool GetDrawColor(Uint8* r, Uint8* g, Uint8* b, Uint8* a) const {
return SDL_GetRenderDrawColor(*this, r, g, b, a);
}
/** @see https://wiki.libsdl.org/SDL3/SDL_GetRenderDrawColorFloat
* @brief Get the color used for drawing operations (Fill and Line) as floats.
* @param r A pointer to a float to be filled in with the red component of the drawing color. If the renderer is
* invalid, this will be set to 0.0f.
* @param g A pointer to a float to be filled in with the green component of the drawing color. If the renderer is
* invalid, this will be set to 0.0f.
* @param b A pointer to a float to be filled in with the blue component of the drawing color. If the renderer is
* invalid, this will be set to 0.0f.
* @param a A pointer to a float to be filled in with the alpha component of the drawing color. If the renderer is
* invalid, this will be set to 0.0f.
* @return bool True if the drawing color was successfully retrieved, false otherwise.
*/
bool GetDrawColorFloat(float* r, float* g, float* b, float* a) const {
return SDL_GetRenderDrawColorFloat(*this, r, g, b, a);
}
/** @see https://wiki.libsdl.org/SDL3/SDL_GetRenderDriver
* @brief Get the name of a renderer driver specified by an index.
* @param index The index of display driver to query, starting at 0.
* @return const char* driver name string
*/
static const char* GetDriver(int index) { return SDL_GetRenderDriver(index); }
/** @see https://wiki.libsdl.org/SDL3/SDL_GetRenderer
* @brief Get the renderer associated with a window.
* @param window The window to query
* @return Renderer The renderer associated with the window
*/
static Renderer GetFromWindow(SDL_Window* window) {
/** we may not have created the renderer we get back, if not we will not own it. */
return Renderer(get_or_view(SDL_GetRenderer(window)));
}
/** @see https://wiki.libsdl.org/SDL3/SDL_GetRendererFromTexture
* @brief Get the renderer associated with a texture.
* @param texture The texture to query
* @return Renderer The renderer associated with the texture
*/
static Renderer GetFromTexture(SDL_Texture* texture) {
/** we may not have created the renderer we get back, if not we will not own it. */
return Renderer(get_or_view(SDL_GetRendererFromTexture(texture)));
}
/** @see https://wiki.libsdl.org/SDL3/SDL_GetRendererName
* @brief Get the name of a renderer.
* @return const char* The name of the renderer
*/
const char* GetName() const { return SDL_GetRendererName(*this); }
/** @see https://wiki.libsdl.org/SDL3/SDL_GetRendererProperties
* @brief Get the properties of a renderer.
* @returns Properties associated with the renderer.
*/
Properties GetProperties() const { return Properties(SDL_GetRendererProperties(*this)); }
/** @see https://wiki.libsdl.org/SDL_GetRenderLogicalPresentation
* @brief device independent resolution and presentation mode
* @param w pointer to store width
* @param h pointer to store height
* @param mode sdl enum value to store presentation mode
* @return bool true on success
*/
bool GetLogicalPresentation(int* w, int* h, SDL_RendererLogicalPresentation* mode) const {
return SDL_GetRenderLogicalPresentation(*this, w, h, mode);
}
/** @see https://wiki.libsdl.org/SDL3/SDL_GetRenderLogicalPresentationRect
* @brief Get the logical presentation rectangle of a renderer.
* @param rect pointer to store the logical presentation rectangle
* @return bool true on success
*/
bool GetLogicalPresentationRect(SDL_FRect* rect) const {
return SDL_GetRenderLogicalPresentationRect(*this, rect);
}
/**
* @todo https://wiki.libsdl.org/SDL3/SDL_GetRenderMetalCommandEncoder
* @todo https://wiki.libsdl.org/SDL3/SDL_GetRenderMetalLayer
*/
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 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); }
/// 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);
}
bool GetViewport(SDL_Rect* rect) const { return SDL_GetRenderViewport(*this, rect); }
bool GetVSync(int* vsync) const { return SDL_GetRenderVSync(*this, vsync); }
/**
@todo https://wiki.libsdl.org/SDL3/SDL_GetRenderWindow
**/
bool Clear() const { return SDL_RenderClear(*this); }
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);
}
bool CoordinatesToWindow(float rendererX, float rendererY, float* windowX, float* windowY) const {
return SDL_RenderCoordinatesToWindow(*this, rendererX, rendererY, windowX, windowY);
}
bool DebugText(float x, float y, const char* text) const { return SDL_RenderDebugText(*this, x, y, text); }
template <typename... Args> bool DebugTextFormat(float x, float y, const char* fmt, Args... args) const {
return SDL_RenderDebugTextFormat(*this, x, y, fmt, args...);
}
bool FillRect(const SDL_FRect* rect) const { return SDL_RenderFillRect(*this, rect); }
bool FillRects(const SDL_FRect* rects, int count) const { return SDL_RenderFillRects(*this, rects, count); }
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);
}
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);
}
bool RenderLine(float x1, float y1, float x2, float y2) const { return SDL_RenderLine(*this, x1, y1, x2, y2); }
bool RenderLines(const SDL_FPoint* points, int count) const { return SDL_RenderLines(*this, points, count); }
bool RenderPoint(float x, float y) const { return SDL_RenderPoint(*this, x, y); }
bool RenderPoints(const SDL_FPoint* points, int count) const { return SDL_RenderPoints(*this, points, count); }
bool Present() const { return SDL_RenderPresent(*this); }
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); }
bool RenderRect(const SDL_FRect&& rect) const { return SDL_RenderRect(*this, &rect); }
bool RenderRects(const SDL_FRect* rects, int count) const { return SDL_RenderRects(*this, rects, count); }
bool RenderTexture(SDL_Texture* texture, const SDL_FRect* src, const SDL_FRect* dst) const {
return SDL_RenderTexture(*this, texture, src, dst);
}
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);
}
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);
}
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);
}
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);
}
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);
}
bool SetDefaultTextureScaleMode(SDL_ScaleMode scaleMode) const {
return SDL_SetDefaultTextureScaleMode(*this, scaleMode);
}
/** @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
**/
bool SetClipRect(const SDL_Rect* rect) const { return SDL_SetRenderClipRect(*this, rect); }
bool SetRenderColorScale(float scale) const { return SDL_SetRenderColorScale(*this, scale); }
bool SetDrawBlendMode(SDL_BlendMode blendMode) const { return SDL_SetRenderDrawBlendMode(*this, blendMode); }
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);
}
bool SetLogicalPresentation(int w, int h, SDL_RendererLogicalPresentation mode) const {
return SDL_SetRenderLogicalPresentation(*this, w, h, mode);
}
bool SetScale(float scaleX, float scaleY) const { return SDL_SetRenderScale(*this, scaleX, scaleY); }
bool SetRenderTarget(SDL_Texture* texture) const { return SDL_SetRenderTarget(*this, texture); }
bool SetTextureAddressMode(SDL_TextureAddressMode u_mode, SDL_TextureAddressMode v_mode) const {
return SDL_SetRenderTextureAddressMode(*this, u_mode, v_mode);
}
bool ViewportSet() const { return SDL_RenderViewportSet(*this); }
bool SetViewport(const SDL_Rect* rect) const { return SDL_SetRenderViewport(*this, rect); }
bool SetViewport(const SDL_Rect&& rect) const { return SDL_SetRenderViewport(*this, &rect); }
bool SetVSync(int vsync) const { return SDL_SetRenderVSync(*this, vsync); }
/// Implemented in ../video.hpp to avoid circular dependency
Window GetWindow() const;
};
}