Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions src_c/_pygame.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,17 @@

#define PG_CreateSurface SDL_CreateSurface
#define PG_CreateSurfaceFrom SDL_CreateSurfaceFrom
#define PG_ConvertSurface SDL_ConvertSurface
#define PG_ConvertSurfaceFormat SDL_ConvertSurface

/* Convert surface using palette and format of dst */
static inline SDL_Surface *
PG_ConvertSurface(SDL_Surface *surface, SDL_Surface *dst)
{
return SDL_ConvertSurfaceAndColorspace(surface, dst->format,
SDL_GetSurfacePalette(dst),
SDL_GetSurfaceColorspace(dst), 0);
}

#define PG_PixelFormatEnum SDL_PixelFormat

#define PG_SurfaceHasRLE SDL_SurfaceHasRLE
Expand Down Expand Up @@ -144,11 +152,23 @@ PG_SURF_BitsPerPixel(SDL_Surface *surf)

#define PG_PixelFormat const SDL_PixelFormatDetails

static inline SDL_Palette *
PG_GetSurfacePalette(SDL_Surface *surf)
{
SDL_Palette *ret = SDL_GetSurfacePalette(surf);
if (!ret && SDL_ISPIXELFORMAT_INDEXED(surf->format)) {
/* Palette doesn't exist but is expected, so create it.
* SDL will associate the newly created palette with the surface */
ret = SDL_CreateSurfacePalette(surf);
}
Comment on lines +159 to +163
Copy link
Member

@Starbuck5 Starbuck5 Sep 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is how we should be doing this. If our surfaces are missing palettes we deal with that when the surface is not created, not randomly at any point as a side effect of a getter function. And/or make the consumers okay with index formats without palettes, since that's a thing in SDL3 and therefore we will want to support it anyway.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this being part of the getter makes sense considering that the getter is responsible for returning a palette, and we must ensure it doesn't return NULL when it should actually return a palette. But sure, I suppose it being part of PG_CreateSurface could also work (but then we'd also have to think about all the other ways a surface is created, which means more places to modify)

return ret;
}

static inline bool
PG_GetSurfaceDetails(SDL_Surface *surf, PG_PixelFormat **format_p,
SDL_Palette **palette_p)
{
*palette_p = SDL_GetSurfacePalette(surf);
*palette_p = PG_GetSurfacePalette(surf);
*format_p = SDL_GetPixelFormatDetails(surf->format);
return *format_p != NULL;
}
Expand All @@ -159,7 +179,6 @@ PG_GetSurfaceFormat(SDL_Surface *surf)
return SDL_GetPixelFormatDetails(surf->format);
}

#define PG_GetSurfacePalette SDL_GetSurfacePalette
#define PG_SetPaletteColors SDL_SetPaletteColors
#define PG_SetSurfacePalette SDL_SetSurfacePalette
#define PG_SetSurfaceColorKey SDL_SetSurfaceColorKey
Expand Down Expand Up @@ -215,10 +234,15 @@ PG_GetSurfaceFormat(SDL_Surface *surf)
SDL_CreateRGBSurfaceWithFormat(0, width, height, 0, format)
#define PG_CreateSurfaceFrom(width, height, format, pixels, pitch) \
SDL_CreateRGBSurfaceWithFormatFrom(pixels, width, height, 0, pitch, format)
#define PG_ConvertSurface(src, fmt) SDL_ConvertSurface(src, fmt, 0)
#define PG_ConvertSurfaceFormat(src, pixel_format) \
SDL_ConvertSurfaceFormat(src, pixel_format, 0)

static inline SDL_Surface *
PG_ConvertSurface(SDL_Surface *surface, SDL_Surface *dst)
{
return SDL_ConvertSurface(surface, dst->format, 0);
}

#define PG_PixelFormatEnum SDL_PixelFormatEnum

#define PG_SoftStretchNearest(src, srcrect, dst, dstrect) \
Expand Down
2 changes: 1 addition & 1 deletion src_c/draw.c
Original file line number Diff line number Diff line change
Expand Up @@ -1334,7 +1334,7 @@ flood_fill(PyObject *self, PyObject *arg, PyObject *kwargs)
if (pgSurface_Check(colorobj)) {
pat_surfobj = ((pgSurfaceObject *)colorobj);

pattern = PG_ConvertSurface(pat_surfobj->surf, surf->format);
pattern = PG_ConvertSurface(pat_surfobj->surf, surf);

if (pattern == NULL) {
return RAISE(PyExc_RuntimeError, "error converting pattern surf");
Expand Down
2 changes: 1 addition & 1 deletion src_c/pixelarray_methods.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ _make_surface(pgPixelArrayObject *array, PyObject *args)
}

/* Ensure the new surface has the same format as the original */
new_surf = PG_ConvertSurface(temp_surf, surf->format);
new_surf = PG_ConvertSurface(temp_surf, surf);
if (temp_surf != surf) {
SDL_FreeSurface(temp_surf);
}
Expand Down
10 changes: 5 additions & 5 deletions src_c/surface.c
Original file line number Diff line number Diff line change
Expand Up @@ -1620,7 +1620,7 @@ surf_copy(pgSurfaceObject *self, PyObject *_null)
SURF_INIT_CHECK(surf)

pgSurface_Prep(self);
newsurf = PG_ConvertSurface(surf, surf->format);
newsurf = PG_ConvertSurface(surf, surf);
pgSurface_Unprep(self);

final = surf_subtype_new(Py_TYPE(self), newsurf, 1);
Expand Down Expand Up @@ -1680,7 +1680,7 @@ surf_convert(pgSurfaceObject *self, PyObject *args)
if (argobject) {
if (pgSurface_Check(argobject)) {
src = pgSurface_AsSurface(argobject);
newsurf = PG_ConvertSurface(surf, src->format);
newsurf = PG_ConvertSurface(surf, src);
}
else {
/* will be updated later, initialize to make static analyzer happy
Expand Down Expand Up @@ -1834,7 +1834,7 @@ surf_convert(pgSurfaceObject *self, PyObject *args)
if (argobject) {
if (pgSurface_Check(argobject)) {
src = pgSurface_AsSurface(argobject);
newsurf = PG_ConvertSurface(surf, src->format);
newsurf = PG_ConvertSurface(surf, src);
}
else {
/* will be updated later, initialize to make static analyzer happy
Expand Down Expand Up @@ -1951,7 +1951,7 @@ surf_convert(pgSurfaceObject *self, PyObject *args)
SDL_SetPixelFormatPalette(format, palette);
}
}
newsurf = PG_ConvertSurface(surf, format);
newsurf = SDL_ConvertSurface(surf, format, 0);
SDL_SetSurfaceBlendMode(newsurf, SDL_BLENDMODE_NONE);
SDL_FreeFormat(format);
SDL_FreePalette(palette);
Expand Down Expand Up @@ -3753,7 +3753,7 @@ surf_premul_alpha(pgSurfaceObject *self, PyObject *_null)

pgSurface_Prep(self);
// Make a copy of the surface first
newsurf = PG_ConvertSurface(surf, surf->format);
newsurf = PG_ConvertSurface(surf, surf);

if ((surf->w > 0 && surf->h > 0)) {
// If the surface has no pixels we don't need to premul
Expand Down
Loading