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
+45 -138
View File
@@ -1,199 +1,83 @@
#pragma once
/** @file Surface.hpp
* @brief Defines the Surface class for handling SDL surfaces.
*/
/// @file Surface.hpp
/// For complete documentation, see src/video/Surface.cpp
#include <SDL3/SDL.h>
#include <hdk/grid/SharedPtrWrapper.hpp>
/** Surfaces hold bitmap data **/
#include <list>
namespace hdk::sdl {
class Palette;
class Surface : public hdk::grid::SharedPtrWrapper<SDL_Surface> {
public:
/** Inherit constructors from SharedPtrWrapper */
friend class Renderer;
using hdk::grid::SharedPtrWrapper<SDL_Surface>::SharedPtrWrapper;
/** @see https://wiki.libsdl.org/SDL3/SDL_AddSurfaceAlternateImage
* @brief Add an alternate image to a surface.
* @param surface The surface to add the alternate image to.
* @return bool true on success
*/
bool AddAlternateImage(SDL_Surface* surface) const { return SDL_AddSurfaceAlternateImage(*this, surface); }
/** @see https://wiki.libsdl.org/SDL3/SDL_BlitSurface
* @brief Perform a fast blit from `this` surface to the destination surface.
* @param dst_surface The destination surface to blit on to.
* @param dst_rect A pointer to an SDL_Rect structure representing the area on the destination surface to blit
* onto, or NULL to blit onto the entire surface.
* @param src_rect A pointer to an SDL_Rect structure representing the area of the source surface to blit, or
* NULL to blit the entire surface.
* @return bool true on success
*/
bool BlitTo(
SDL_Surface* dst_surface, const SDL_Rect* dst_rect = nullptr, const SDL_Rect* src_rect = nullptr) const {
return SDL_BlitSurface(*this, src_rect, dst_surface, dst_rect);
}
/** @see https://wiki.libsdl.org/SDL3/SDL_BlitSurface
* @brief Perform a fast blit to `this` surface from a source surface.
* @param src_surface The source surface to blit from.
* @param src_rect A pointer to an SDL_Rect structure representing the area on the source surface to blit
* from, or NULL to blit the entire surface.
* @param dst_rect A pointer to an SDL_Rect structure representing the area of the destination surface to blit,
* or NULL to blit the entire surface.
* @return bool true on success
*/
bool BlitFrom(
SDL_Surface* src_surface, const SDL_Rect* src_rect = nullptr, const SDL_Rect* dst_rect = nullptr) const {
return SDL_BlitSurface(src_surface, src_rect, *this, dst_rect);
}
/** @see https://wiki.libsdl.org/SDL3/SDL_BlitSurface9Grid
* @brief Perform a blit with 9-grid scaling from `this` surface to the destination surface.
* @param dst_surface The destination surface to blit on to.
* @param dst_rect A pointer to an SDL_Rect structure representing the area on the destination
* @param left_width The width of the left columns of the 9-grid
* @param right_width The width of the right columns of the 9-grid
* @param top_height The height of the top rows of the 9-grid
* @param bottom_height The height of the bottom rows of the 9-grid
* @param scale The scale factor to apply to the center of the 9-grid
* @param scale_mode
* @param src_rect
* @return bool true on success
*/
bool Blit9GridTo(SDL_Surface* dst_surface, const SDL_Rect* dst_rect, int left_width, int right_width,
int top_height, int bottom_height, float scale, SDL_ScaleMode scale_mode,
const SDL_Rect* src_rect = nullptr) const {
return SDL_BlitSurface9Grid(*this, src_rect, left_width, right_width, top_height, bottom_height, scale,
scale_mode, dst_surface, dst_rect);
}
/** @see https://wiki.libsdl.org/SDL3/SDL_BlitSurface9Grid
* @brief Perform a blit with 9-grid scaling to `this` surface from a source surface.
* @param src_surface The source surface to blit from.
* @param src_rect
* @param left_width The width of the left columns of the 9-grid
* @param right_width The width of the right columns of the 9-grid
* @param top_height The height of the top rows of the 9-grid
* @param bottom_height The height of the bottom rows of the 9-grid
* @param scale The scale factor to apply to the center of the 9-grid
* @param scale_mode
* @param dst_rect A pointer to an SDL_Rect structure representing the area on the destination surface to blit, or
NULL to blit the entire surface.
* @return bool true on success
*/
bool Blit9GridFrom(SDL_Surface* src_surface, const SDL_Rect* src_rect, int left_width, int right_width,
int top_height, int bottom_height, float scale, SDL_ScaleMode scale_mode,
const SDL_Rect* dst_rect = nullptr) const {
return SDL_BlitSurface9Grid(src_surface, src_rect, left_width, right_width, top_height, bottom_height, scale,
scale_mode, *this, dst_rect);
}
/** @see https://wiki.libsdl.org/SDL3/SDL_BlitSurfaceScaled
* @brief Perform a scaled blit from `this` surface to the destination surface.
* @param dst_surface The destination surface to blit on to.
* @param dst_rect A pointer to an SDL_Rect structure representing the area on the destination
* @param scale_mode The scale mode to use for scaling the surface
* @param src_rect A pointer to an SDL_Rect structure representing the area of the source
* surface to blit, or NULL to blit the entire surface.
* @return bool true on success
*/
bool BlitScaledTo(SDL_Surface* dst_surface, const SDL_Rect* dst_rect, SDL_ScaleMode scale_mode,
const SDL_Rect* src_rect = nullptr) const {
return SDL_BlitSurfaceScaled(*this, src_rect, dst_surface, dst_rect, scale_mode);
}
/** @see https://wiki.libsdl.org/SDL3/SDL_BlitSurfaceScaled
* @brief Perform a scaled blit to `this` surface from a source surface.
* @param src_surface The source surface to blit from.
* @param src_rect A pointer to an SDL_Rect structure representing the area on the source
* @param scale_mode The scale mode to use for scaling the surface
* @param dst_rect A pointer to an SDL_Rect structure representing the area on the destination surface to blit, or
* NULL to blit the entire surface.
* @return bool true
*/
bool BlitScaledFrom(SDL_Surface* src_surface, const SDL_Rect* src_rect, SDL_ScaleMode scale_mode,
const SDL_Rect* dst_rect = nullptr) const {
return SDL_BlitSurfaceScaled(src_surface, src_rect, *this, dst_rect, scale_mode);
}
/** @see https://wiki.libsdl.org/SDL3/SDL_BlitSurfaceTiled
* @brief Perform a tiled blit from `this` surface to the destination surface.
* @param dst_surface The destination surface to blit on to.
* @param dst_rect A pointer to an SDL_Rect structure representing the area on the destination surface to blit
* onto, or NULL to blit onto the entire surface.
* @param src_rect A pointer to an SDL_Rect structure representing the area of the source surface to blit, or NULL
* to blit the entire surface.
* @return bool true on success
*/
bool BlitTiledTo(
SDL_Surface* dst_surface, const SDL_Rect* dst_rect = nullptr, const SDL_Rect* src_rect = nullptr) const {
return SDL_BlitSurfaceTiled(*this, src_rect, dst_surface, dst_rect);
}
/** @see https://wiki.libsdl.org/SDL3/SDL_BlitSurfaceTiled
* @brief Perform a tiled blit to `this` surface from a source surface.
* @param src_surface The source surface to blit from.
* @param src_rect A pointer to an SDL_Rect structure representing the area on the source surface to blit, or NULL
* to blit the entire surface.
* @param dst_rect A pointer to an SDL_Rect structure representing the area of the destination surface to blit, or
* NULL to blit the entire surface.
* @return bool true on success
*/
bool BlitTiledFrom(
SDL_Surface* src_surface, const SDL_Rect* src_rect = nullptr, const SDL_Rect* dst_rect = nullptr) const {
return SDL_BlitSurfaceTiled(src_surface, src_rect, *this, dst_rect);
}
/** @see https://wiki.libsdl.org/SDL3/SDL_BlitSurfaceTiledWithScale
* @brief Perform a tiled blit with scale from `this` surface to the destination surface.
* @param dst_surface The destination surface to blit on to.
* @param dst_rect A pointer to an SDL_Rect structure representing the area on the destination surface to blit
* onto, or NULL to blit onto the entire surface.
* @param scale The scale factor to apply to the source surface when blitting.
* @param scale_mode The scale mode to use for scaling the surface
* @param src_rect A pointer to an SDL_Rect structure representing the area of the source
*/
bool BlitTiledWithScaleTo(SDL_Surface* dst_surface, const SDL_Rect* dst_rect, float scale,
SDL_ScaleMode scale_mode, const SDL_Rect* src_rect = nullptr) const {
return SDL_BlitSurfaceTiledWithScale(*this, src_rect, scale, scale_mode, dst_surface, dst_rect);
}
/** @see https://wiki.libsdl.org/SDL3/SDL_BlitSurfaceTiledWithScale
* @brief Perform a tiled blit with scale to `this` surface from a source surface.
* @param src_surface The source surface to blit from.
* @param src_rect A pointer to an SDL_Rect structure representing the area of the source surface to blit, or NULL
* to blit the entire surface.
* @param scale The scale factor to apply to the source surface when blitting.
* @param scale_mode The scale mode to use for scaling the surface
* @param dst_rect A pointer to an SDL_Rect structure representing the area of the destination surface to blit, or
* NULL to blit the entire surface.
* @return bool true on success
*/
bool BlitTiledWithScaleFrom(SDL_Surface* src_surface, const SDL_Rect* src_rect, float scale,
SDL_ScaleMode scale_mode, const SDL_Rect* dst_rect = nullptr) const {
return SDL_BlitSurfaceTiledWithScale(src_surface, src_rect, scale, scale_mode, *this, dst_rect);
}
/** @todo https://wiki.libsdl.org/SDL3/SDL_BlitSurfaceUnchecked
*/
/** @todo https://wiki.libsdl.org/SDL3/SDL_BlitSurfaceUncheckedScaled
*/
/** @see https://wiki.libsdl.org/SDL3/SDL_ClearSurface
* @brief Clear the surface to the specified color.
* @param r
* @param g
* @param b
* @param a
*/
bool BlitUncheckedTo(SDL_Surface* dst_surface, const SDL_Rect* dst_rect = nullptr, const SDL_Rect* src_rect = nullptr) const {
return SDL_BlitSurfaceUnchecked(*this, src_rect, dst_surface, dst_rect);
}
bool BlitUncheckedFrom(SDL_Surface* src_surface, const SDL_Rect* src_rect = nullptr, const SDL_Rect* dst_rect = nullptr) const {
return SDL_BlitSurfaceUnchecked(src_surface, src_rect, *this, dst_rect);
}
bool BlitUncheckedScaledTo(SDL_Surface* dst_surface, const SDL_Rect* dst_rect, SDL_ScaleMode scale_mode,
const SDL_Rect* src_rect = nullptr) const {
return SDL_BlitSurfaceUncheckedScaled(*this, src_rect, dst_surface, dst_rect, scale_mode);
}
bool BlitUncheckedScaledFrom(SDL_Surface* src_surface, const SDL_Rect* src_rect, SDL_ScaleMode scale_mode,
const SDL_Rect* dst_rect = nullptr) const {
return SDL_BlitSurfaceUncheckedScaled(src_surface, src_rect, *this, dst_rect, scale_mode);
}
void Clear(float r, float g, float b, float a) const { SDL_ClearSurface(*this, r, g, b, a); }
/** @todo https://wiki.libsdl.org/SDL3/SDL_ConvertPixels
*/
/** @todo https://wiki.libsdl.org/SDL3/SDL_ConvertPixelsAndColorspace
*/
/** @see https://wiki.libsdl.org/SDL3/SDL_ConvertSurface
* @brief Convert surface to specified pixel format.
* @param format The pixel format to convert to.
* @return A new Surface instance with the converted surface, or nullptr on failure.
*/
Surface Convert(SDL_PixelFormat format) const {
return get_or_cache(SDL_ConvertSurface(*this, format), SDL_DestroySurface);
}
/** @see https://wiki.libsdl.org/SDL3/SDL_ConvertSurfaceAndColorspace
* @brief Convert surface to specified pixel format and colorspace.
* @param format
* @param palette (optional) The palette to use for the converted surface, or NULL to use the palette from the
* original surface.
* @param colorspace
* @param properties (optional) can be 0.
* @return A new Surface instance with the converted surface, or nullptr on failure.
*/
Surface ConvertAndColorspace(SDL_PixelFormat format, SDL_Palette* palette, SDL_Colorspace colorspace,
SDL_PropertiesID properties = 0) const {
return get_or_cache(
@@ -221,7 +105,11 @@ namespace hdk::sdl {
static Surface CreateFrom(int w, int h, SDL_PixelFormat format, void* pixels, int pitch) {
return get_or_cache(SDL_CreateSurfaceFrom(w, h, format, pixels, pitch), SDL_DestroySurface);
}
/** @todo https://wiki.libsdl.org/SDL3/SDL_CreateSurfacePalette */
/** @see https://wiki.libsdl.org/SDL3/SDL_CreateSurfacePalette
* @brief Create a new surface with a palette.
* @returns a new pallet on success or nullptr on failure
*/
Palette CreatePalette() const;
/** @see https://wiki.libsdl.org/SDL3/SDL_DestroySurface
*/
/** @see https://wiki.libsdl.org/SDL3/SDL_DuplicateSurface
@@ -289,8 +177,27 @@ namespace hdk::sdl {
* @return The colorspace of the surface.
*/
SDL_Colorspace GetColorspace() const { return SDL_GetSurfaceColorspace(*this); }
/** @todo https://wiki.libsdl.org/SDL3/SDL_GetSurfaceImages
/** @see https://wiki.libsdl.org/SDL3/SDL_GetSurfaceImages
* @brief Get the surface's alternative images.
* @param count pointer to an int variable to receive the number of alternative images
* @return An array of Surface instances representing the surface's alternative images, or nullptr
*/
SDL_Surface** GetImages(int* count) const { return SDL_GetSurfaceImages(*this, count); }
/**
* @brief Get the surface's alternative images as a list.
* @return A list of Surface instances representing the surface's alternative images.
*/
std::list<Surface> GetImages() const {
int count = 0;
SDL_Surface** images = GetImages(&count);
std::list<Surface> image_list;
for (int i = 0; i < count; ++i) {
image_list.emplace_back(get_or_cache(images[i], SDL_DestroySurface));
}
SDL_free(images);
return image_list;
}
/** @todo https://wiki.libsdl.org/SDL3/SDL_GetSurfacePalette
*/
/** @see https://wiki.libsdl.org/SDL3/SDL_GetSurfaceProperties