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"
56 SDL_SURFACE_PREALLOCATED;
59 SDL_SURFACE_LOCK_NEEDED;
76#if SDL_VERSION_ATLEAST(3, 2, 10)
86 SDL_SCALEMODE_NEAREST;
219 SDL_CreateSurfaceFrom(size.x, size.y, format, pixels, pitch)))
381 return SDL_SurfaceHasAlternateImages(
get());
406 reinterpret_cast<SurfaceRef*
>(SDL_GetSurfaceImages(
get(), &count));
480 bool HasRLE()
const {
return SDL_SurfaceHasRLE(
get()); }
526 CheckError(SDL_SetSurfaceColorKey(
get(), key.has_value(), key.value_or(0)));
771 if (
BlendMode blendMode; SDL_GetSurfaceBlendMode(
get(), &blendMode)) {
797 return SDL_SetSurfaceClipRect(
get(), rect);
819 if (
Rect r; SDL_GetSurfaceClipRect(
get(), &r))
return r;
965 CheckError(SDL_ClearSurface(
get(), color.r, color.g, color.b, color.a));
1096 CheckError(SDL_FillSurfaceRects(
get(), rects.data(), rects.size(), color));
1170 const SDL_Point& dstpos)
1172 Blit(src, srcrect,
Rect{dstpos, {}});
1275 const SDL_Rect& srcrect,
1276 const SDL_Rect& dstrect)
1278 CheckError(SDL_BlitSurfaceUnchecked(src.
get(), &srcrect,
get(), &dstrect));
1307 SDL_BlitSurfaceScaled(src.
get(), srcrect,
get(), dstrect, scaleMode));
1332 const SDL_Rect& srcrect,
1333 const SDL_Rect& dstrect,
1337 SDL_BlitSurfaceScaled(src.
get(), &srcrect,
get(), &dstrect, scaleMode));
1340#if SDL_VERSION_ATLEAST(3, 2, 4)
1361 const SDL_Rect& srcrect,
1362 const SDL_Rect& dstrect,
1366 SDL_StretchSurface(src.
get(), &srcrect,
get(), &dstrect, scaleMode));
1429 SDL_ScaleMode scaleMode,
1433 src.
get(), srcrect, scale, scaleMode,
get(), dstrect));
1481 SDL_SCALEMODE_NEAREST,
1525 SDL_ScaleMode scaleMode,
1563 return MapColor(color.r, color.g, color.b, color.a);
1591 return SDL_MapSurfaceRGB(
get(), r, g, b);
1618 Uint32
MapColor(Uint8 r, Uint8 g, Uint8 b, Uint8 a)
const
1620 return SDL_MapSurfaceRGBA(
get(), r, g, b, a);
1659 ReadPixel(p, &c->r, &c->g, &c->b, &c->a);
1677 return ReadPixel(p, &c->r, &c->g, &c->b, &c->a);
1710 CheckError(SDL_ReadSurfacePixel(
get(), p.x, p.y, r, g, b, a));
1740 CheckError(SDL_ReadSurfacePixelFloat(
get(), p.x, p.y, r, g, b, a));
1762 CheckError(SDL_WriteSurfacePixel(
get(), p.x, p.y, c.r, c.g, c.b, c.a));
1781 CheckError(SDL_WriteSurfacePixelFloat(
get(), p.x, p.y, c.r, c.g, c.b, c.a));
1858 void reset(SDL_Surface* newResource = {})
1860 SDL_DestroySurface(
release(newResource));
1879 constexpr explicit Surface(SDL_Surface* resource = {})
1884 constexpr Surface(
const Surface& other) =
delete;
1919 : surface(std::move(surface))
1921 if (!SDL_LockSurface(this->surface.
get())) this->surface.
reset();
1936 : surface(other.surface.release())
1949 std::swap(surface, other.surface);
1956 constexpr operator bool()
const {
return bool(surface); }
1985namespace prop::Surface {
1987constexpr auto SDR_WHITE_POINT_FLOAT = SDL_PROP_SURFACE_SDR_WHITE_POINT_FLOAT;
1989constexpr auto HDR_HEADROOM_FLOAT = SDL_PROP_SURFACE_HDR_HEADROOM_FLOAT;
1991constexpr auto TONEMAP_OPERATOR_STRING =
1992 SDL_PROP_SURFACE_TONEMAP_OPERATOR_STRING;
1994constexpr auto HOTSPOT_X_NUMBER = SDL_PROP_SURFACE_HOTSPOT_X_NUMBER;
1996constexpr auto HOTSPOT_Y_NUMBER = SDL_PROP_SURFACE_HOTSPOT_Y_NUMBER;
2015 return Surface{SDL_LoadBMP_IO(src.
get(),
false)};
2090 return Surface{SDL_ScaleSurface(
get(), width, height, scaleMode)};
2095 return Surface{SDL_ConvertSurface(
get(), format)};
2103 return Surface{SDL_ConvertSurfaceAndColorspace(
2104 get(), format, palette.
get(), colorspace, props.
get())};
2138 width, height, src_format, src, src_pitch, dst_format, dst, dst_pitch));
2184 CheckError(SDL_ConvertPixelsAndColorspace(width,
2188 src_properties.
get(),
2193 dst_properties.
get(),
2248#pragma endregion impl
Colorspace definitions.
Definition SDL3pp_pixels.h:1301
Optional-like shim for references.
Definition SDL3pp_optionalRef.h:20
Base class for SDL memory allocated array wrap.
Definition SDL3pp_ownPtr.h:43
A SDL managed resource.
Definition SDL3pp_resource.h:17
constexpr SDL_Surface * release(SDL_Surface * newResource={})
Return contained resource and empties or replace value.
Definition SDL3pp_resource.h:60
constexpr Resource(T resource={})
Constructs the underlying resource.
Definition SDL3pp_resource.h:22
constexpr SDL_Surface * get() const
Return contained resource;.
Definition SDL3pp_resource.h:57
span-like for empty-derived structs
Definition SDL3pp_spanRef.h:24
Helpers to use C++ strings parameters.
Definition SDL3pp_strings.h:43
Locks a Surface for access to its pixels.
Definition SDL3pp_surface.h:1912
void * GetPixels() const
Get the pixels.
Definition SDL3pp_surface.h:1970
~SurfaceLock()
destructor
Definition SDL3pp_surface.h:1944
int GetPitch() const
Get pitch (the number of bytes between the start of one row the next)
Definition SDL3pp_surface.h:1975
SurfaceLock & operator=(SurfaceLock other)
Assignment operator.
Definition SDL3pp_surface.h:1947
SurfaceLock(const SurfaceLock &other)=delete
Copy ctor.
void Unlock()
Release the locked surface after directly accessing the pixels.
Definition SDL3pp_surface.h:1965
PixelFormat GetFormat() const
Get the pixel format.
Definition SDL3pp_surface.h:1980
SurfaceLock(SurfaceLock &&other)
Move ctor.
Definition SDL3pp_surface.h:1935
#define SDL_assert_paranoid(condition)
An assertion test that is performed only when built with paranoid settings.
Definition SDL3pp_assert.h:374
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:75
constexpr void CheckError(bool result)
Check and throw if returned value from SDL is an error.
Definition SDL3pp_error.h:206
Surface Convert(PixelFormat format) const
Copy an existing surface to a new surface of the specified format.
Definition SDL3pp_surface.h:2093
constexpr FlipMode FLIP_VERTICAL
flip vertically
Definition SDL3pp_surface.h:102
constexpr SurfaceFlags SURFACE_SIMD_ALIGNED
Surface uses pixel memory allocated with aligned_alloc()
Definition SDL3pp_surface.h:67
constexpr SurfaceFlags SURFACE_LOCK_NEEDED
Surface needs to be locked to access pixels.
Definition SDL3pp_surface.h:58
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:2128
constexpr FlipMode FLIP_NONE
Do not flip.
Definition SDL3pp_surface.h:98
constexpr SurfaceFlags SURFACE_PREALLOCATED
Surface uses preallocated pixel memory.
Definition SDL3pp_surface.h:55
Surface Duplicate() const
Creates a new surface identical to the existing surface.
Definition SDL3pp_surface.h:2081
constexpr ScaleMode SCALEMODE_LINEAR
linear filtering
Definition SDL3pp_surface.h:88
constexpr ScaleMode SCALEMODE_NEAREST
nearest pixel sampling
Definition SDL3pp_surface.h:85
constexpr SurfaceFlags SURFACE_LOCKED
Surface is currently locked.
Definition SDL3pp_surface.h:61
SDL_FlipMode FlipMode
The flip mode.
Definition SDL3pp_surface.h:96
constexpr ScaleMode SCALEMODE_INVALID
Definition SDL3pp_surface.h:81
void SaveBMP(SurfaceBase &surface, IOStreamBase &dst)
Save a surface to a seekable SDL data stream in BMP format.
Definition SDL3pp_surface.h:2052
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:2086
constexpr FlipMode FLIP_HORIZONTAL
flip horizontally
Definition SDL3pp_surface.h:100
void ConvertPixelsAndColorspace(int width, int height, PixelFormat src_format, Colorspace src_colorspace, PropertiesBase &src_properties, const void *src, int src_pitch, PixelFormat dst_format, Colorspace dst_colorspace, PropertiesBase &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:2171
Uint32 SurfaceFlags
The flags on an SurfaceBase.
Definition SDL3pp_surface.h:53
SDL_ScaleMode ScaleMode
The scaling mode.
Definition SDL3pp_surface.h:74
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:2221
Surface LoadBMP(IOStreamBase &src)
Load a BMP image from a seekable SDL data stream.
Definition SDL3pp_surface.h:2013
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:1621
The read/write operation structure.
Definition SDL3pp_iostream.h:107
A set of indexed colors representing a palette.
Definition SDL3pp_pixels.h:1954
Handle to a non owned palette.
Definition SDL3pp_pixels.h:2030
The structure that defines a point (using integers)
Definition SDL3pp_rect.h:41
Wrap properties id.
Definition SDL3pp_properties.h:203
Handle to a non owned properties.
Definition SDL3pp_properties.h:693
A rectangle, with the origin at the upper left (using integers).
Definition SDL3pp_rect.h:817
A collection of pixels used in software blitting.
Definition SDL3pp_surface.h:138
Point GetSize() const
Get the size in pixels.
Definition SDL3pp_surface.h:1797
void ClearColorKey()
Unset the color key (transparent pixel) in a surface.
Definition SDL3pp_surface.h:538
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:1618
void SetColorKey(Color key)
Set the color key (transparent pixel) in a surface.
Definition SDL3pp_surface.h:501
SurfaceBase(StringParam file)
Load an image from a filesystem path into a software surface.
Definition SDL3pp_image.h:2189
Colorspace GetColorspace() const
Get the colorspace used by a surface.
Definition SDL3pp_surface.h:283
void BlitScaled(const SurfaceBase &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:1301
Uint8 GetAlphaMod() const
Get the additional alpha value used in blit operations.
Definition SDL3pp_surface.h:700
int GetHeight() const
Get the height in pixels.
Definition SDL3pp_surface.h:1792
void RemoveAlternateImages()
Remove all alternate versions of a surface.
Definition SDL3pp_surface.h:422
Color GetMod() const
Get the additional color and alpha value multiplied into blit operations.
Definition SDL3pp_surface.h:734
void Blit(const SurfaceBase &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:1168
PaletteRef GetPalette() const
Get the palette used by a surface.
Definition SDL3pp_surface.h:338
void Clear(SDL_FColor color)
Clear a surface with a specific color, with floating point precision.
Definition SDL3pp_surface.h:963
void Fill(SDL_Color color)
Perform a fast fill of a rectangle with a specific color.
Definition SDL3pp_surface.h:978
void Fill(Uint32 color)
Perform a fast fill of a rectangle with a specific color.
Definition SDL3pp_surface.h:995
void Blit9Grid(const SurfaceBase &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:1466
void SetRLE(bool enabled)
Set the RLE acceleration hint for a surface.
Definition SDL3pp_surface.h:473
bool HasColorKey() const
Returns whether the surface has a color key.
Definition SDL3pp_surface.h:545
PixelFormat GetFormat() const
Get the pixel format.
Definition SDL3pp_surface.h:1802
PropertiesRef GetProperties() const
Get the properties associated with a surface.
Definition SDL3pp_surface.h:248
bool HasRLE() const
Returns whether the surface is RLE enabled.
Definition SDL3pp_surface.h:480
void AddAlternateImage(SurfaceBase &image)
Add an alternate version of a surface.
Definition SDL3pp_surface.h:363
void GetColorMod(Uint8 *r, Uint8 *g, Uint8 *b) const
Get the additional color value multiplied into blit operations.
Definition SDL3pp_surface.h:660
void SetMod(Color color)
Set an additional color and alpha value multiplied into blit operations.
Definition SDL3pp_surface.h:721
Uint32 MapColor(SDL_Color color) const
Map an RGBA quadruple to a pixel value for a surface.
Definition SDL3pp_surface.h:1561
void ReadPixel(const SDL_Point &p, SDL_FColor *c) const
Retrieves a single pixel from a surface.
Definition SDL3pp_surface.h:1675
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:1093
void BlitTiledWithScale(const SurfaceBase &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:1426
OwnArray< SurfaceRef > GetImages() const
Get an array including all versions of a surface.
Definition SDL3pp_surface.h:402
void FillRect(const SDL_Rect &rect, Uint32 color)
Perform a fast fill of a rectangle with a specific color.
Definition SDL3pp_surface.h:1044
void BlitUncheckedScaled(const SurfaceBase &src, const SDL_Rect &srcrect, const SDL_Rect &dstrect, ScaleMode scaleMode)
Perform low-level surface scaled blitting only.
Definition SDL3pp_surface.h:1331
Rect GetClipRect() const
Get the clipping rectangle for a surface.
Definition SDL3pp_surface.h:817
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:1734
void FillRect(const SDL_Rect &rect, SDL_Color color)
Perform a fast fill of a rectangle with a specific color.
Definition SDL3pp_surface.h:1017
void SetBlendMode(BlendMode blendMode)
Set the blend mode used for blit operations.
Definition SDL3pp_surface.h:758
SurfaceBase(const SDL_Point &size, PixelFormat format)
Allocate a new surface with a specific pixel format.
Definition SDL3pp_surface.h:185
void PremultiplyAlpha(bool linear)
Premultiply the alpha in a surface.
Definition SDL3pp_surface.h:941
void SetColorKey(std::optional< Uint32 > key)
Set the color key (transparent pixel) in a surface.
Definition SDL3pp_surface.h:524
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:1636
void BlitTiled(const SurfaceBase &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:1393
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:1066
void WritePixel(const SDL_Point &p, SDL_FColor c)
Writes a single pixel to a surface.
Definition SDL3pp_surface.h:1779
bool HasAlternateImages() const
Return whether a surface has alternate versions available.
Definition SDL3pp_surface.h:379
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:1704
Color GetColorKey() const
Get the color key (transparent pixel) for a surface.
Definition SDL3pp_surface.h:564
void SetPalette(PaletteBase &palette)
Set the palette used by a surface.
Definition SDL3pp_surface.h:327
bool SetClipRect(OptionalRef< const SDL_Rect > rect)
Set the clipping rectangle for a surface.
Definition SDL3pp_surface.h:795
void GetColorKey(Color *key) const
Get the color key (transparent pixel) for a surface.
Definition SDL3pp_surface.h:589
void GetColorKey(Uint32 *key) const
Get the color key (transparent pixel) for a surface.
Definition SDL3pp_surface.h:614
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:1589
BlendMode GetBlendMode() const
Get the blend mode used for blit operations.
Definition SDL3pp_surface.h:769
constexpr bool MustLock() const
Evaluates to true if the surface needs to be locked before access.
Definition SDL3pp_surface.h:429
void SetColorspace(Colorspace colorspace)
Set the colorspace used by a surface.
Definition SDL3pp_surface.h:268
void SetAlphaMod(Uint8 alpha)
Set an additional alpha value used in blit operations.
Definition SDL3pp_surface.h:683
void BlitUnchecked(const SurfaceBase &src, const SDL_Rect &srcrect, const SDL_Rect &dstrect)
Perform low-level surface blitting only.
Definition SDL3pp_surface.h:1274
SurfaceBase(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:214
void SetColorMod(Uint8 r, Uint8 g, Uint8 b)
Set an additional color value multiplied into blit operations.
Definition SDL3pp_surface.h:640
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:1657
void Blit(const SurfaceBase &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:1247
void Blit9GridWithScale(const SurfaceBase &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:1518
int GetWidth() const
Get the width in pixels.
Definition SDL3pp_surface.h:1787
void Flip(FlipMode flip)
Flip a surface vertically or horizontally.
Definition SDL3pp_surface.h:833
void Stretch(const SurfaceBase &src, const SDL_Rect &srcrect, const SDL_Rect &dstrect, ScaleMode scaleMode)
Perform a stretched pixel copy from one surface to another.
Definition SDL3pp_surface.h:1360
PaletteRef CreatePalette()
Create a palette and associate it with a surface.
Definition SDL3pp_surface.h:310
SurfaceLock Lock() &
Set up a surface for directly accessing the pixels.
Definition SDL3pp_surface.h:2246
void WritePixel(const SDL_Point &p, SDL_Color c)
Writes a single pixel to a surface.
Definition SDL3pp_surface.h:1760
void ResetClipRect()
Disable the clipping rectangle for a surface.
Definition SDL3pp_surface.h:805
Handle to a non owned surface.
Definition SDL3pp_surface.h:1814
constexpr SurfaceRef(SurfaceRef &&other)
Move constructor.
Definition SDL3pp_surface.h:1828
constexpr ~SurfaceRef()=default
Default constructor.
void reset(SDL_Surface *newResource={})
Free a surface.
Definition SDL3pp_surface.h:1858
constexpr SurfaceRef(const SurfaceRef &other)
Copy constructor.
Definition SDL3pp_surface.h:1820
SurfaceRef & operator=(SurfaceRef other)
Assignment operator.
Definition SDL3pp_surface.h:1841
Handle to an owned surface.
Definition SDL3pp_surface.h:1873
Surface & operator=(Surface other)
Assignment operator.
Definition SDL3pp_surface.h:1899
~Surface()
Frees up resource when object goes out of scope.
Definition SDL3pp_surface.h:1894
constexpr Surface(Surface &&other)=default
Move constructor.
constexpr Surface(SDL_Surface *resource={})
Constructs from the underlying resource.
Definition SDL3pp_surface.h:1879