Added initial implementation of the SDL Pixels category.

Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
BadQuanta
2026-04-26 17:45:26 +00:00
parent 85dbe33a77
commit 97c8847eb0
8 changed files with 239 additions and 11 deletions
+39
View File
@@ -0,0 +1,39 @@
#pragma once
/** @file Palette.hpp
* @brief HDK sdl video header only wrapper for SDL_Palette struct & related functions
*/
#include <SDL3/SDL_pixels.h>
#include <hdk/grid/SharedPtrWrapper.hpp>
namespace hdk::sdl {
/**
* @brief
* @note
*/
class Palette : public hdk::grid::SharedPtrWrapper<SDL_Palette> {
public:
/** Inherit constructors from SharedPtrWrapper */
using hdk::grid::SharedPtrWrapper<SDL_Palette>::SharedPtrWrapper;
/** @see https://wiki.libsdl.org/SDL3/SDL_CreatePalette
* @brief Create a palette with a specified number of colors.
* @param ncolors The number of colors in the palette.
* @return Palette The created palette. If the creation fails, returns an invalid palette.
*/
static Palette Create(int ncolors) {
return Palette(get_or_cache(SDL_CreatePalette(ncolors), SDL_DestroyPalette));
}
/** @see https://wiki.libsdl.org/SDL3/SDL_DestroyPalette
* @brief Destroy a palette and free its associated memory.
*/
void Destroy() const { SDL_DestroyPalette(*this); }
/** @see https://wiki.libsdl.org/SDL3/SDL_SetPaletteColors
* @brief Set a range of colors in a palette.
* @param colors An array of SDL_Color structures representing the colors to set in the palette.
* @param firstcolor The index of the first color to set in the palette.
* @param ncolors The number of colors to set in the palette.
* @return bool True on success, false on failure.
*/
bool SetColors(const SDL_Color* colors, int firstcolor, int ncolors) const {
return SDL_SetPaletteColors(*this, colors, firstcolor, ncolors);
}
};
}
+53
View File
@@ -0,0 +1,53 @@
#pragma once
/** @file PixelFormat.hpp
* @brief HDK sdl video header only wrapper for SDL_PixelFormat enum & related functions
*/
#include <SDL3/SDL_pixels.h>
#include <hdk/grid/PrimitiveWrapper.hpp>
/** PixelFormat makes working with SDL_PixelFormat easier. */
namespace hdk::sdl {
/// Forward declare to avoid circular dependency with PixelFormatDetails
class PixelFormatDetails;
/**
* @brief
*
*/
class PixelFormat : public hdk::grid::PrimitiveWrapper<SDL_PixelFormat> {
public:
/** Inherit constructors from PrimitiveWrapper */
using hdk::grid::PrimitiveWrapper<SDL_PixelFormat>::PrimitiveWrapper;
/** @see https://wiki.libsdl.org/SDL3/SDL_GetMasksForPixelFormat
* @brief Get the pixel format masks for a given pixel format.
* @param bpp Pointer to int to be filled in with the number of bytes per pixel.
* @param Rmask Pointer to Uint32 to be filled in with the red mask for the pixel format.
* @param Gmask Pointer to Uint32 to be filled in with the green mask for the pixel format.
* @param Bmask Pointer to Uint32 to be filled in with the blue mask for the pixel format.
* @param Amask Pointer to Uint32 to be filled in with the alpha mask for the pixel format.
* @return bool True on success
*/
bool GetMasks(int* bpp, Uint32* Rmask, Uint32* Gmask, Uint32* Bmask, Uint32* Amask) const {
return SDL_GetMasksForPixelFormat(*this, bpp, Rmask, Gmask, Bmask, Amask);
}
// Implemented in `../pixels.hpp` to avoid circular dependency
PixelFormatDetails GetDetails() const;
/** @see https://wiki.libsdl.org/SDL3/SDL_GetPixelFormatForMasks
* @brief Get the pixel format for a given set of pixel format masks.
* @param bpp The number of bytes per pixel for the pixel format.
* @param Rmask The red mask for the pixel format.
* @param Gmask The green mask for the pixel format.
* @param Bmask The blue mask for the pixel format.
* @param Amask The alpha mask for the pixel format.
* @return The pixel format corresponding to the given masks, or ::SDL_PIXELFORMAT_UNKNOWN
*/
static SDL_PixelFormat GetForMasks(int bpp, Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask) {
return SDL_GetPixelFormatForMasks(bpp, Rmask, Gmask, Bmask, Amask);
}
/** @see https://wiki.libsdl.org/SDL3/SDL_GetPixelFormatName
* @brief Get the name of a pixel format.
* @return const char* The name of the pixel format, or "Unknown" if the pixel format is not recognized.
*/
const char* GetName() const { return SDL_GetPixelFormatName(*this); }
};
}
@@ -0,0 +1,72 @@
#pragma once
/** @file Palette.hpp
* @brief HDK sdl video header only wrapper pointers to const SDL_PixelFormatDetails & related functions
* @note Does note use smartpointers because these are SDL owned constant structs.
*/
#include <SDL3/SDL_pixels.h>
#include <hdk/grid/PrimitiveWrapper.hpp>
/** PixelFormatDetails makes getting the details of a pixel format easier. */
namespace hdk::sdl {
/**
* @brief
*
*/
class PixelFormatDetails : public hdk::grid::PrimitiveWrapper<const SDL_PixelFormatDetails*> {
public:
/** Inherit constructors from PrimitiveWrapper */
using hdk::grid::PrimitiveWrapper<const SDL_PixelFormatDetails*>::PrimitiveWrapper;
/*** @see https://wiki.libsdl.org/SDL3/SDL_GetPixelFormatDetails
* @returns an PixelFormatDetails instance
*/
static PixelFormatDetails Get(SDL_PixelFormat format) {
return PixelFormatDetails(SDL_GetPixelFormatDetails(format));
}
/** @see https://wiki.libsdl.org/SDL3/SDL_GetRGB
* @brief Get the RGB values for a pixel value
* @param pixel The pixel value to query
* @param palette for indexed formats the palette to use, or NULL for non-indexed formats
* @param r pointer to Uint8 to store the red component
* @param g pointer to Uint8 to store the green component
* @param b pointer to Uint8 to store the blue component
* @param a pointer to Uint8 to store the alpha component
*/
void GetRGB(Uint32 pixel, SDL_Palette* palette, Uint8* r, Uint8* g, Uint8* b, Uint8* a) const {
SDL_GetRGB(pixel, *this, palette, r, g, b, a);
}
/** @see https://wiki.libsdl.org/SDL3/SDL_GetRGBA
* @brief Get the RGBA values for a pixel value
* @param pixel The pixel value to query
* @param palette for indexed formats the palette to use, or NULL for non-indexed formats
* @param r pointer to Uint8 to store the red component
* @param g pointer to Uint8 to store the green component
* @param b pointer to Uint8 to store the blue component
* @param a pointer to Uint8 to store the alpha component
*/
void GetRGBA(Uint32 pixel, SDL_Palette* palette, Uint8* r, Uint8* g, Uint8* b, Uint8* a) const {
SDL_GetRGBA(pixel, *this, palette, r, g, b, a);
}
/** @see https://wiki.libsdl.org/SDL3/SDL_MapRGB
* @brief Map RGB values to a pixel value for the pixel format
* @param palette for indexed formats the palette to use, or NULL for non-indexed formats
* @param r The red component of the color
* @param g The green component of the color
* @param b The blue component of the color
* @return The pixel value corresponding to the given RGB values for the pixel format
*/
Uint32 MapRGB(SDL_Palette* palette, Uint8 r, Uint8 g, Uint8 b) const {
return SDL_MapRGB(*this, palette, r, g, b);
}
/** @see https://wiki.libsdl.org/SDL3/SDL_MapRGBA
* @brief Map RGBA values to a pixel value for the pixel format
* @param palette for indexed formats the palette to use, or NULL for non-indexed formats
* @param r The red component of the color
* @param g The green component of the color
* @param b The blue component of the color
* @param a The alpha component of the color
* @return The pixel value corresponding to the given RGBA values for the pixel format
*/
Uint32 MapRGBA(SDL_Palette* palette, Uint8 r, Uint8 g, Uint8 b, Uint8 a) const {
return SDL_MapRGBA(*this, palette, r, g, b, a);
}
};
}