Skip to content

Commit 7569c95

Browse files
committed
SDL3: surface+pixelarray+font+draw: runtime fixes
1 parent 8923e2a commit 7569c95

File tree

4 files changed

+35
-11
lines changed

4 files changed

+35
-11
lines changed

src_c/_pygame.h

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,17 @@
7777

7878
#define PG_CreateSurface SDL_CreateSurface
7979
#define PG_CreateSurfaceFrom SDL_CreateSurfaceFrom
80-
#define PG_ConvertSurface SDL_ConvertSurface
8180
#define PG_ConvertSurfaceFormat SDL_ConvertSurface
8281

82+
/* Convert surface using palette and format of dst */
83+
static inline SDL_Surface *
84+
PG_ConvertSurface(SDL_Surface *surface, SDL_Surface *dst)
85+
{
86+
return SDL_ConvertSurfaceAndColorspace(surface, dst->format,
87+
SDL_GetSurfacePalette(dst),
88+
SDL_GetSurfaceColorspace(dst), 0);
89+
}
90+
8391
#define PG_PixelFormatEnum SDL_PixelFormat
8492

8593
#define PG_SurfaceHasRLE SDL_SurfaceHasRLE
@@ -144,11 +152,23 @@ PG_SURF_BitsPerPixel(SDL_Surface *surf)
144152

145153
#define PG_PixelFormat const SDL_PixelFormatDetails
146154

155+
static inline SDL_Palette *
156+
PG_GetSurfacePalette(SDL_Surface *surf)
157+
{
158+
SDL_Palette *ret = SDL_GetSurfacePalette(surf);
159+
if (!ret && SDL_ISPIXELFORMAT_INDEXED(surf->format)) {
160+
/* Palette doesn't exist but is expected, so create it.
161+
* SDL will associate the newly created palette with the surface */
162+
ret = SDL_CreateSurfacePalette(surf);
163+
}
164+
return ret;
165+
}
166+
147167
static inline bool
148168
PG_GetSurfaceDetails(SDL_Surface *surf, PG_PixelFormat **format_p,
149169
SDL_Palette **palette_p)
150170
{
151-
*palette_p = SDL_GetSurfacePalette(surf);
171+
*palette_p = PG_GetSurfacePalette(surf);
152172
*format_p = SDL_GetPixelFormatDetails(surf->format);
153173
return *format_p != NULL;
154174
}
@@ -159,7 +179,6 @@ PG_GetSurfaceFormat(SDL_Surface *surf)
159179
return SDL_GetPixelFormatDetails(surf->format);
160180
}
161181

162-
#define PG_GetSurfacePalette SDL_GetSurfacePalette
163182
#define PG_SetPaletteColors SDL_SetPaletteColors
164183
#define PG_SetSurfacePalette SDL_SetSurfacePalette
165184
#define PG_SetSurfaceColorKey SDL_SetSurfaceColorKey
@@ -215,10 +234,15 @@ PG_GetSurfaceFormat(SDL_Surface *surf)
215234
SDL_CreateRGBSurfaceWithFormat(0, width, height, 0, format)
216235
#define PG_CreateSurfaceFrom(width, height, format, pixels, pitch) \
217236
SDL_CreateRGBSurfaceWithFormatFrom(pixels, width, height, 0, pitch, format)
218-
#define PG_ConvertSurface(src, fmt) SDL_ConvertSurface(src, fmt, 0)
219237
#define PG_ConvertSurfaceFormat(src, pixel_format) \
220238
SDL_ConvertSurfaceFormat(src, pixel_format, 0)
221239

240+
static inline SDL_Surface *
241+
PG_ConvertSurface(SDL_Surface *surface, SDL_Surface *dst)
242+
{
243+
return SDL_ConvertSurface(surface, dst->format, 0);
244+
}
245+
222246
#define PG_PixelFormatEnum SDL_PixelFormatEnum
223247

224248
#define PG_SoftStretchNearest(src, srcrect, dst, dstrect) \

src_c/draw.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1334,7 +1334,7 @@ flood_fill(PyObject *self, PyObject *arg, PyObject *kwargs)
13341334
if (pgSurface_Check(colorobj)) {
13351335
pat_surfobj = ((pgSurfaceObject *)colorobj);
13361336

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

13391339
if (pattern == NULL) {
13401340
return RAISE(PyExc_RuntimeError, "error converting pattern surf");

src_c/pixelarray_methods.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ _make_surface(pgPixelArrayObject *array, PyObject *args)
137137
}
138138

139139
/* Ensure the new surface has the same format as the original */
140-
new_surf = PG_ConvertSurface(temp_surf, surf->format);
140+
new_surf = PG_ConvertSurface(temp_surf, surf);
141141
if (temp_surf != surf) {
142142
SDL_FreeSurface(temp_surf);
143143
}

src_c/surface.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1620,7 +1620,7 @@ surf_copy(pgSurfaceObject *self, PyObject *_null)
16201620
SURF_INIT_CHECK(surf)
16211621

16221622
pgSurface_Prep(self);
1623-
newsurf = PG_ConvertSurface(surf, surf->format);
1623+
newsurf = PG_ConvertSurface(surf, surf);
16241624
pgSurface_Unprep(self);
16251625

16261626
final = surf_subtype_new(Py_TYPE(self), newsurf, 1);
@@ -1680,7 +1680,7 @@ surf_convert(pgSurfaceObject *self, PyObject *args)
16801680
if (argobject) {
16811681
if (pgSurface_Check(argobject)) {
16821682
src = pgSurface_AsSurface(argobject);
1683-
newsurf = PG_ConvertSurface(surf, src->format);
1683+
newsurf = PG_ConvertSurface(surf, src);
16841684
}
16851685
else {
16861686
/* will be updated later, initialize to make static analyzer happy
@@ -1834,7 +1834,7 @@ surf_convert(pgSurfaceObject *self, PyObject *args)
18341834
if (argobject) {
18351835
if (pgSurface_Check(argobject)) {
18361836
src = pgSurface_AsSurface(argobject);
1837-
newsurf = PG_ConvertSurface(surf, src->format);
1837+
newsurf = PG_ConvertSurface(surf, src);
18381838
}
18391839
else {
18401840
/* will be updated later, initialize to make static analyzer happy
@@ -1951,7 +1951,7 @@ surf_convert(pgSurfaceObject *self, PyObject *args)
19511951
SDL_SetPixelFormatPalette(format, palette);
19521952
}
19531953
}
1954-
newsurf = PG_ConvertSurface(surf, format);
1954+
newsurf = SDL_ConvertSurface(surf, format, 0);
19551955
SDL_SetSurfaceBlendMode(newsurf, SDL_BLENDMODE_NONE);
19561956
SDL_FreeFormat(format);
19571957
SDL_FreePalette(palette);
@@ -3753,7 +3753,7 @@ surf_premul_alpha(pgSurfaceObject *self, PyObject *_null)
37533753

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

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

0 commit comments

Comments
 (0)