1#ifndef SDL3PP_SURFACE_H_
2#define SDL3PP_SURFACE_H_
4#include <SDL3/SDL_surface.h>
5#include <SDL3/SDL_version.h>
6#include "SDL3pp_blendmode.h"
7#include "SDL3pp_iostream.h"
8#include "SDL3pp_pixels.h"
9#include "SDL3pp_properties.h"
10#include "SDL3pp_rect.h"
11#include "SDL3pp_stdinc.h"
74 SDL_SURFACE_PREALLOCATED;
77 SDL_SURFACE_LOCK_NEEDED;
94#if SDL_VERSION_ATLEAST(3, 2, 10)
101 SDL_SCALEMODE_NEAREST;
104 SDL_SCALEMODE_LINEAR;
337 return SDL_SurfaceHasAlternateImages(
get());
361 reinterpret_cast<SurfaceRef*
>(SDL_GetSurfaceImages(
get(), &count));
444 bool HasRLE()
const {
return SDL_SurfaceHasRLE(
get()); }
490 CheckError(SDL_SetSurfaceColorKey(
get(), key.has_value(), key.value_or(0)));
742 if (
BlendMode blendMode; SDL_GetSurfaceBlendMode(
get(), &blendMode)) {
771 return SDL_SetSurfaceClipRect(
get(), rect);
944 CheckError(SDL_ClearSurface(
get(), color.r, color.g, color.b, color.a));
1072 CheckError(SDL_FillSurfaceRects(
get(), rects.data(), rects.size(), color));
1146 const SDL_Point& dstpos)
1148 Blit(src, srcrect,
Rect{dstpos, {}});
1248 const SDL_Rect& srcrect,
1249 const SDL_Rect& dstrect)
1251 CheckError(SDL_BlitSurfaceUnchecked(src.
get(), &srcrect,
get(), &dstrect));
1280 SDL_BlitSurfaceScaled(src.
get(), srcrect,
get(), dstrect, scaleMode));
1305 const SDL_Rect& srcrect,
1306 const SDL_Rect& dstrect,
1310 SDL_BlitSurfaceScaled(src.
get(), &srcrect,
get(), &dstrect, scaleMode));
1313#if SDL_VERSION_ATLEAST(3, 2, 4)
1339 SDL_StretchSurface(src.
get(), srcrect,
get(), dstrect, scaleMode));
1402 SDL_ScaleMode scaleMode,
1406 src.
get(), srcrect, scale, scaleMode,
get(), dstrect));
1454 SDL_SCALEMODE_NEAREST,
1498 SDL_ScaleMode scaleMode,
1536 return MapColor(color.r, color.g, color.b, color.a);
1570 return SDL_MapSurfaceRGB(
get(), r, g, b);
1603 Uint32
MapColor(Uint8 r, Uint8 g, Uint8 b, Uint8 a)
const
1605 return SDL_MapSurfaceRGBA(
get(), r, g, b, a);
1644 ReadPixel(p, &c->r, &c->g, &c->b, &c->a);
1662 return ReadPixel(p, &c->r, &c->g, &c->b, &c->a);
1695 CheckError(SDL_ReadSurfacePixel(
get(), p.x, p.y, r, g, b, a));
1725 CheckError(SDL_ReadSurfacePixelFloat(
get(), p.x, p.y, r, g, b, a));
1747 CheckError(SDL_WriteSurfacePixel(
get(), p.x, p.y, c.r, c.g, c.b, c.a));
1766 CheckError(SDL_WriteSurfacePixelFloat(
get(), p.x, p.y, c.r, c.g, c.b, c.a));
1813 static void reset(SDL_Surface* resource) { SDL_DestroySurface(resource); }
1877 return Surface(SDL_LoadBMP_IO(src,
false));
1900 return Surface(SDL_LoadBMP(file));
1958 CheckError(SDL_CreateSurfaceFrom(size.x, size.y, format, pixels, pitch)));
2084namespace prop::Surface {
2086constexpr auto SDR_WHITE_POINT_FLOAT = SDL_PROP_SURFACE_SDR_WHITE_POINT_FLOAT;
2088constexpr auto HDR_HEADROOM_FLOAT = SDL_PROP_SURFACE_HDR_HEADROOM_FLOAT;
2090constexpr auto TONEMAP_OPERATOR_STRING =
2091 SDL_PROP_SURFACE_TONEMAP_OPERATOR_STRING;
2093#if SDL_VERSION_ATLEAST(3, 2, 6)
2095constexpr auto HOTSPOT_X_NUMBER = SDL_PROP_SURFACE_HOTSPOT_X_NUMBER;
2097constexpr auto HOTSPOT_Y_NUMBER = SDL_PROP_SURFACE_HOTSPOT_Y_NUMBER;
2164 return Surface{SDL_ScaleSurface(
get(), width, height, scaleMode)};
2169 return Surface{SDL_ConvertSurface(
get(), format)};
2177 return Surface{SDL_ConvertSurfaceAndColorspace(
2178 get(), format, palette.
get(), colorspace, props.
get())};
2212 width, height, src_format, src, src_pitch, dst_format, dst, dst_pitch));
2258 CheckError(SDL_ConvertPixelsAndColorspace(width,
2262 src_properties.
get(),
2267 dst_properties.
get(),
Colorspace definitions.
Definition SDL3pp_pixels.h:1318
Base class for locks.
Definition SDL3pp_resource.h:408
Optional-like shim for references.
Definition SDL3pp_optionalRef.h:20
Base class for SDL memory allocated array wrap.
Definition SDL3pp_ownPtr.h:43
RESOURCE release()
Returns reference and reset this.
Definition SDL3pp_resource.h:178
reference & get()
Get reference.
Definition SDL3pp_resource.h:120
Implement shared ownership for a resource.
Definition SDL3pp_resource.h:283
Implement unique ownership for a resource.
Definition SDL3pp_resource.h:226
constexpr ResourceUnique(std::nullptr_t=nullptr)
Default constructor.
Definition SDL3pp_resource.h:231
void reset()
Resets the value, destroying the resource if not nullptr.
Definition SDL3pp_resource.h:265
A dumb pointer to resource.
Definition SDL3pp_resource.h:197
constexpr ResourceUnsafe()=default
Default constructor.
Implement weak ownership for a resource.
Definition SDL3pp_resource.h:328
A SDL managed resource.
Definition SDL3pp_resource.h:29
constexpr Resource(T resource={})
Constructs from the underlying resource.
Definition SDL3pp_resource.h:37
constexpr SDL_Surface * get() const
Return contained resource;.
Definition SDL3pp_resource.h:76
span-like for empty-derived structs
Definition SDL3pp_spanRef.h:24
Helpers to use C++ strings parameters.
Definition SDL3pp_strings.h:43
#define SDL_assert_paranoid(condition)
An assertion test that is performed only when built with paranoid settings.
Definition SDL3pp_assert.h:364
Uint32 BlendMode
A set of blend modes used in drawing operations.
Definition SDL3pp_blendmode.h:35
constexpr BlendMode BLENDMODE_INVALID
INVALID.
Definition SDL3pp_blendmode.h:73
constexpr void CheckError(bool result)
Check and throw if returned value from SDL is an error.
Definition SDL3pp_error.h:206
constexpr FlipMode FLIP_VERTICAL
flip vertically
Definition SDL3pp_surface.h:117
constexpr SurfaceFlags SURFACE_SIMD_ALIGNED
Surface uses pixel memory allocated with aligned_alloc()
Definition SDL3pp_surface.h:85
constexpr SurfaceFlags SURFACE_LOCK_NEEDED
Surface needs to be locked to access pixels.
Definition SDL3pp_surface.h:76
void ConvertPixels(int width, int height, PixelFormat src_format, const void *src, int src_pitch, PixelFormat dst_format, void *dst, int dst_pitch)
Copy a block of pixels of one format to another format.
Definition SDL3pp_surface.h:2202
void SaveBMP(SurfaceRef surface, IOStreamRef dst)
Save a surface to a seekable SDL data stream in BMP format.
Definition SDL3pp_surface.h:2125
Surface Convert(PixelFormat format) const
Copy an existing surface to a new surface of the specified format.
Definition SDL3pp_surface.h:2167
SurfaceLock Lock() &
Set up a surface for directly accessing the pixels.
Definition SDL3pp_surface.h:2103
constexpr FlipMode FLIP_NONE
Do not flip.
Definition SDL3pp_surface.h:113
constexpr SurfaceFlags SURFACE_PREALLOCATED
Surface uses preallocated pixel memory.
Definition SDL3pp_surface.h:73
ResourceShared< Surface > SurfaceShared
Handle to a shared surface.
Definition SDL3pp_surface.h:49
void ConvertPixelsAndColorspace(int width, int height, PixelFormat src_format, Colorspace src_colorspace, PropertiesRef src_properties, const void *src, int src_pitch, PixelFormat dst_format, Colorspace dst_colorspace, PropertiesRef dst_properties, void *dst, int dst_pitch)
Copy a block of pixels of one format and colorspace to another format and colorspace.
Definition SDL3pp_surface.h:2245
constexpr ScaleMode SCALEMODE_LINEAR
linear filtering
Definition SDL3pp_surface.h:103
Surface Duplicate() const
Creates a new surface identical to the existing surface.
Definition SDL3pp_surface.h:2155
constexpr ScaleMode SCALEMODE_NEAREST
nearest pixel sampling
Definition SDL3pp_surface.h:100
constexpr SurfaceFlags SURFACE_LOCKED
Surface is currently locked.
Definition SDL3pp_surface.h:79
SurfaceShared share()
Move this surface into a SurfaceShared.
Definition SDL3pp_surface.h:1981
SDL_FlipMode FlipMode
The flip mode.
Definition SDL3pp_surface.h:111
constexpr ScaleMode SCALEMODE_INVALID
INVALID.
Definition SDL3pp_surface.h:96
constexpr FlipMode FLIP_HORIZONTAL
flip horizontally
Definition SDL3pp_surface.h:115
Uint32 SurfaceFlags
The flags on an Surface.
Definition SDL3pp_surface.h:71
SDL_ScaleMode ScaleMode
The scaling mode.
Definition SDL3pp_surface.h:92
void PremultiplyAlpha(int width, int height, PixelFormat src_format, const void *src, int src_pitch, PixelFormat dst_format, void *dst, int dst_pitch, bool linear)
Premultiply the alpha on a block of pixels.
Definition SDL3pp_surface.h:2295
Surface Scale(int width, int height, ScaleMode scaleMode) const
Creates a new surface identical to the existing surface, scaled to the desired size.
Definition SDL3pp_surface.h:2160
the main namespace where all SDL3pp public functions and types live
Definition SDL3pp_assert.h:7
A structure that represents a color as RGBA components.
Definition SDL3pp_pixels.h:1638
The read/write operation structure.
Definition SDL3pp_iostream.h:123
A set of indexed colors representing a palette.
Definition SDL3pp_pixels.h:1971
The structure that defines a point (using integers).
Definition SDL3pp_rect.h:46
SDL properties ID.
Definition SDL3pp_properties.h:209
A rectangle, with the origin at the upper left (using integers).
Definition SDL3pp_rect.h:833
Locks a Surface.
Definition SDL3pp_surface.h:2014
~SurfaceLock()
Destructor.
Definition SDL3pp_surface.h:2063
SurfaceLock(SurfaceRef surface)
Set up a surface for directly accessing the pixels.
Definition SDL3pp_surface.h:2052
void reset()
Same as Unlock(), just for uniformity.
Definition SDL3pp_surface.h:2081
constexpr SurfaceLock(SurfaceLock &&other)
Move constructor.
Definition SDL3pp_surface.h:2023
constexpr SurfaceLock()=default
Creates an empty lock.
void Unlock()
Release a surface after directly accessing the pixels.
Definition SDL3pp_surface.h:2076
A collection of pixels used in software blitting.
Definition SDL3pp_surface.h:153
void WritePixel(const SDL_Point &p, SDL_Color c)
Writes a single pixel to a surface.
Definition SDL3pp_surface.h:1745
constexpr Point GetSize() const
Get the size in pixels.
Definition SDL3pp_surface.h:1782
void ReadPixel(const SDL_Point &p, Uint8 *r, Uint8 *g, Uint8 *b, Uint8 *a) const
Retrieves a single pixel from a surface.
Definition SDL3pp_surface.h:1689
bool HasColorKey() const
Returns whether the surface has a color key.
Definition SDL3pp_surface.h:516
void ReadPixel(const SDL_Point &p, SDL_Color *c) const
This function prioritizes correctness over speed: it is suitable for unit tests, but is not intended ...
Definition SDL3pp_surface.h:1642
void SetColorKey(Color key)
Set the color key (transparent pixel) in a surface.
Definition SDL3pp_surface.h:465
void FillRect(OptionalRef< const SDL_Rect > rect, Uint32 color)
Perform a fast fill of a rectangle with a specific color.
Definition SDL3pp_surface.h:1020
static void reset(SDL_Surface *resource)
Free a surface.
Definition SDL3pp_surface.h:1813
void Flip(FlipMode flip)
Flip a surface vertically or horizontally.
Definition SDL3pp_surface.h:814
void Blit9GridWithScale(SurfaceRef src, OptionalRef< const SDL_Rect > srcrect, int left_width, int right_width, int top_height, int bottom_height, float scale, SDL_ScaleMode scaleMode, OptionalRef< const SDL_Rect > dstrect)
Perform a scaled blit using the 9-grid algorithm to a destination surface, which may be of a differen...
Definition SDL3pp_surface.h:1491
void WritePixel(const SDL_Point &p, SDL_FColor c)
Writes a single pixel to a surface.
Definition SDL3pp_surface.h:1764
void RemoveAlternateImages()
Remove all alternate versions of a surface.
Definition SDL3pp_surface.h:380
void BlitScaled(SurfaceRef src, OptionalRef< const SDL_Rect > srcrect, OptionalRef< const SDL_Rect > dstrect, ScaleMode scaleMode)
Perform a scaled blit to a destination surface, which may be of a different format.
Definition SDL3pp_surface.h:1274
void ReadPixel(const SDL_Point &p, float *r, float *g, float *b, float *a) const
Retrieves a single pixel from a surface.
Definition SDL3pp_surface.h:1719
constexpr void * GetPixels() const
Get the pixels.
Definition SDL3pp_surface.h:1797
void Blit(SurfaceRef src, OptionalRef< const SDL_Rect > srcrect, OptionalRef< const SDL_Rect > dstrect)
Performs a fast blit from the source surface to the destination surface with clipping.
Definition SDL3pp_surface.h:1220
Uint32 MapColor(Uint8 r, Uint8 g, Uint8 b) const
Map an RGB triple to an opaque pixel value for a surface.
Definition SDL3pp_surface.h:1568
void Blit(SurfaceRef src, OptionalRef< const SDL_Rect > srcrect, const SDL_Point &dstpos)
Performs a fast blit from the source surface to the destination surface with clipping.
Definition SDL3pp_surface.h:1144
void GetColorMod(Uint8 *r, Uint8 *g, Uint8 *b) const
Get the additional color value multiplied into blit operations.
Definition SDL3pp_surface.h:631
constexpr bool MustLock() const
Evaluates to true if the surface needs to be locked before access.
Definition SDL3pp_surface.h:387
BlendMode GetBlendMode() const
Get the blend mode used for blit operations.
Definition SDL3pp_surface.h:740
PaletteRef CreatePalette()
Create a palette and associate it with a surface.
Definition SDL3pp_surface.h:256
void SetRLE(bool enabled)
Set the RLE acceleration hint for a surface.
Definition SDL3pp_surface.h:431
void FillRects(SpanRef< const SDL_Rect > rects, SDL_Color color)
Perform a fast fill of a set of rectangles with a specific color.
Definition SDL3pp_surface.h:1042
Color ReadPixel(const SDL_Point &p) const
This function prioritizes correctness over speed: it is suitable for unit tests, but is not intended ...
Definition SDL3pp_surface.h:1621
void BlitUncheckedScaled(SurfaceRef src, const SDL_Rect &srcrect, const SDL_Rect &dstrect, ScaleMode scaleMode)
Perform low-level surface scaled blitting only.
Definition SDL3pp_surface.h:1304
void Blit9Grid(SurfaceRef src, OptionalRef< const SDL_Rect > srcrect, int left_width, int right_width, int top_height, int bottom_height, OptionalRef< const SDL_Rect > dstrect)
Perform a scaled blit using the 9-grid algorithm to a destination surface, which may be of a differen...
Definition SDL3pp_surface.h:1439
constexpr PixelFormat GetFormat() const
Get the pixel format.
Definition SDL3pp_surface.h:1792
void BlitTiled(SurfaceRef src, OptionalRef< const SDL_Rect > srcrect, OptionalRef< const SDL_Rect > dstrect)
Perform a tiled blit to a destination surface, which may be of a different format.
Definition SDL3pp_surface.h:1366
Colorspace GetColorspace() const
Get the colorspace used by a surface.
Definition SDL3pp_surface.h:228
bool SetClipRect(OptionalRef< const SDL_Rect > rect)
Set the clipping rectangle for a surface.
Definition SDL3pp_surface.h:769
PropertiesRef GetProperties() const
Get the properties associated with a surface.
Definition SDL3pp_surface.h:187
bool HasAlternateImages() const
Return whether a surface has alternate versions available.
Definition SDL3pp_surface.h:335
Uint32 MapColor(SDL_Color color) const
Map an RGBA quadruple to a pixel value for a surface.
Definition SDL3pp_surface.h:1534
constexpr int GetHeight() const
Get the height in pixels.
Definition SDL3pp_surface.h:1777
Color GetColorKey() const
Get the color key (transparent pixel) for a surface.
Definition SDL3pp_surface.h:535
void SetColorMod(Uint8 r, Uint8 g, Uint8 b)
Set an additional color value multiplied into blit operations.
Definition SDL3pp_surface.h:611
void FillRect(OptionalRef< const SDL_Rect > rect, SDL_Color color)
Perform a fast fill of a rectangle with a specific color.
Definition SDL3pp_surface.h:993
void SetAlphaMod(Uint8 alpha)
Set an additional alpha value used in blit operations.
Definition SDL3pp_surface.h:654
PaletteRef GetPalette() const
Get the palette used by a surface.
Definition SDL3pp_surface.h:293
void PremultiplyAlpha(bool linear)
Premultiply the alpha in a surface.
Definition SDL3pp_surface.h:922
void ResetClipRect()
Disable the clipping rectangle for a surface.
Definition SDL3pp_surface.h:779
Uint8 GetAlphaMod() const
Get the additional alpha value used in blit operations.
Definition SDL3pp_surface.h:671
void SetMod(Color color)
Set an additional color and alpha value multiplied into blit operations.
Definition SDL3pp_surface.h:692
bool HasRLE() const
Returns whether the surface is RLE enabled.
Definition SDL3pp_surface.h:444
void ReadPixel(const SDL_Point &p, SDL_FColor *c) const
Retrieves a single pixel from a surface.
Definition SDL3pp_surface.h:1660
constexpr int GetPitch() const
Get pitch in bytes.
Definition SDL3pp_surface.h:1787
Color GetMod() const
Get the additional color and alpha value multiplied into blit operations.
Definition SDL3pp_surface.h:705
Rect GetClipRect() const
Get the clipping rectangle for a surface.
Definition SDL3pp_surface.h:797
void Stretch(SurfaceRef src, OptionalRef< SDL_Rect > srcrect, OptionalRef< SDL_Rect > dstrect, ScaleMode scaleMode)
Perform a stretched pixel copy from one surface to another.
Definition SDL3pp_surface.h:1333
void AddAlternateImage(SurfaceRef image)
Add an alternate version of a surface.
Definition SDL3pp_surface.h:317
void GetColorKey(Color *key) const
Get the color key (transparent pixel) for a surface.
Definition SDL3pp_surface.h:560
void Clear(SDL_FColor color)
Clear a surface with a specific color, with floating point precision.
Definition SDL3pp_surface.h:942
void SetPalette(PaletteRef palette)
Set the palette used by a surface.
Definition SDL3pp_surface.h:276
void Fill(SDL_Color color)
Perform a fast fill of a rectangle with a specific color.
Definition SDL3pp_surface.h:957
void BlitUnchecked(SurfaceRef src, const SDL_Rect &srcrect, const SDL_Rect &dstrect)
Perform low-level surface blitting only.
Definition SDL3pp_surface.h:1247
OwnArray< SurfaceRef > GetImages() const
Get an array including all versions of a surface.
Definition SDL3pp_surface.h:357
void BlitTiledWithScale(SurfaceRef src, OptionalRef< const SDL_Rect > srcrect, float scale, SDL_ScaleMode scaleMode, OptionalRef< const SDL_Rect > dstrect)
Perform a scaled and tiled blit to a destination surface, which may be of a different format.
Definition SDL3pp_surface.h:1399
void FillRects(SpanRef< const SDL_Rect > rects, Uint32 color)
Perform a fast fill of a set of rectangles with a specific color.
Definition SDL3pp_surface.h:1069
Uint32 MapColor(Uint8 r, Uint8 g, Uint8 b, Uint8 a) const
Map an RGBA quadruple to a pixel value for a surface.
Definition SDL3pp_surface.h:1603
void Fill(Uint32 color)
Perform a fast fill of a rectangle with a specific color.
Definition SDL3pp_surface.h:974
void SetBlendMode(BlendMode blendMode)
Set the blend mode used for blit operations.
Definition SDL3pp_surface.h:729
void SetColorspace(Colorspace colorspace)
Set the colorspace used by a surface.
Definition SDL3pp_surface.h:207
void GetColorKey(Uint32 *key) const
Get the color key (transparent pixel) for a surface.
Definition SDL3pp_surface.h:585
void SetColorKey(std::optional< Uint32 > key)
Set the color key (transparent pixel) in a surface.
Definition SDL3pp_surface.h:488
void ClearColorKey()
Unset the color key (transparent pixel) in a surface.
Definition SDL3pp_surface.h:502
constexpr int GetWidth() const
Get the width in pixels.
Definition SDL3pp_surface.h:1772
Unsafe Handle to surface.
Definition SDL3pp_surface.h:1996
constexpr SurfaceUnsafe(Surface &&other)
Constructs SurfaceUnsafe from Surface.
Definition SDL3pp_surface.h:2002
Handle to an owned surface.
Definition SDL3pp_surface.h:1824
static Surface LoadBMP(IOStreamRef src)
Load a BMP image from a seekable SDL data stream.
Definition SDL3pp_surface.h:1875
void Destroy()
Free a surface.
Definition SDL3pp_surface.h:1974
static Surface Create(const SDL_Point &size, PixelFormat format)
Allocate a new surface with a specific pixel format.
Definition SDL3pp_surface.h:1920
static Surface CreateFrom(const SDL_Point &size, PixelFormat format, void *pixels, int pitch)
Allocate a new surface with a specific pixel format and existing pixel data.
Definition SDL3pp_surface.h:1952
static Surface LoadBMP(StringParam file)
Load a BMP image from a file.
Definition SDL3pp_surface.h:1898
static Surface Load(StringParam file)
Load an image from a filesystem path into a software surface.
Definition SDL3pp_image.h:2217