Skip to content

Commit 23ff10a

Browse files
committed
No errors in surface.c
1 parent 5d79edd commit 23ff10a

File tree

2 files changed

+67
-26
lines changed

2 files changed

+67
-26
lines changed

src_c/_pygame.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585
SDL_GetPixelFormatDetails(PG_SurfaceFormatEnum(surf))
8686
#define PG_GetSurfacePalette SDL_GetSurfacePalette
8787
#define PG_SurfaceMapRGBA(surf, r, g, b, a) \
88-
SDL_MapSurfaceRGBA(surface, r, g, b, a)
88+
SDL_MapSurfaceRGBA(surf, r, g, b, a)
8989
#define PG_GetSurfaceClipRect(surf, rect) SDL_GetSurfaceClipRect(surf, rect)
9090

9191
#define PG_SurfaceHasRLE SDL_SurfaceHasRLE
@@ -171,7 +171,7 @@ PG_FillRect(SDL_Surface *dst, const SDL_Rect *rect, Uint32 color)
171171
#define PG_GetSurfaceFormat(surf) (surf)->format
172172
#define PG_GetSurfacePalette(surf) (surf)->format->palette
173173
#define PG_SurfaceMapRGBA(surf, r, g, b, a) \
174-
SDL_MapRGBA(PG_GetSurfaceFormat(surface), r, g, b, a)
174+
SDL_MapRGBA(PG_GetSurfaceFormat(surf), r, g, b, a)
175175
#define PG_GetSurfaceClipRect(surf, rect) \
176176
{ \
177177
rect = &surf->clip_rect; \

src_c/surface.c

+65-24
Original file line numberDiff line numberDiff line change
@@ -507,8 +507,6 @@ surface_init(pgSurfaceObject *self, PyObject *args, PyObject *kwds)
507507
return -1;
508508
}
509509

510-
default_format.palette = NULL;
511-
512510
surface_cleanup(self);
513511

514512
if (depth && masks) { /* all info supplied, most errorchecking
@@ -923,11 +921,7 @@ surf_map_rgb(PyObject *self, PyObject *args)
923921

924922
SURF_INIT_CHECK(surf)
925923

926-
#if SDL_VERSION_ATLEAST(3, 0, 0)
927-
color = SDL_MapSurfaceRGBA(surf, rgba[0], rgba[1], rgba[2], rgba[3]);
928-
#else
929-
color = SDL_MapRGBA(surf->format, rgba[0], rgba[1], rgba[2], rgba[3]);
930-
#endif
924+
color = PG_SurfaceMapRGBA(surf, rgba[0], rgba[1], rgba[2], rgba[3]);
931925
return PyLong_FromLong(color);
932926
}
933927

@@ -1314,6 +1308,9 @@ surf_set_alpha(pgSurfaceObject *self, PyObject *args)
13141308
pgSurface_Prep(self);
13151309
result =
13161310
SDL_SetSurfaceRLE(surf, (flags & PGS_RLEACCEL) ? SDL_TRUE : SDL_FALSE);
1311+
1312+
#if !SDL_VERSION_ATLEAST(3, 0, 0)
1313+
// TODO: is it fine to ignore this part for SDL3?
13171314
/* HACK HACK HACK */
13181315
if ((surf->flags & SDL_RLEACCEL) && (!(flags & PGS_RLEACCEL))) {
13191316
/* hack to strip SDL_RLEACCEL flag off surface immediately when
@@ -1328,6 +1325,7 @@ surf_set_alpha(pgSurfaceObject *self, PyObject *args)
13281325
SDL_LowerBlit(surf, &sdlrect, surface, &sdlrect);
13291326
SDL_FreeSurface(surface);
13301327
}
1328+
#endif
13311329
/* HACK HACK HACK */
13321330
if (result == 0)
13331331
result = SDL_SetSurfaceAlphaMod(surf, alpha);
@@ -1533,29 +1531,52 @@ surf_convert(pgSurfaceObject *self, PyObject *args)
15331531
format.BytesPerPixel = (bpp + 7) / 8;
15341532
#endif
15351533
if (PG_FORMAT_BitsPerPixel((&format)) > 8)
1536-
/* Allow a 8 bit source surface with an empty palette to be
1537-
* converted to a format without a palette (pygame-ce issue
1538-
* #146). If the target format has a non-NULL palette pointer
1539-
* then SDL_ConvertSurface checks that the palette is not
1540-
* empty-- that at least one entry is not black.
1541-
*/
1534+
/* Allow an 8 bit source surface with an empty palette to be
1535+
* converted to a format without a palette (pygame-ce issue
1536+
* #146). If the target format has a non-NULL palette pointer
1537+
* then SDL_ConvertSurface checks that the palette is not
1538+
* empty-- that at least one entry is not black.
1539+
*/
1540+
#if SDL_VERSION_ATLEAST(3, 0, 0)
1541+
SDL_FreePalette(palette);
1542+
palette = NULL;
1543+
#else
15421544
format.palette = NULL;
1545+
#endif
15431546
if (SDL_ISPIXELFORMAT_INDEXED(SDL_MasksToPixelFormatEnum(
15441547
PG_FORMAT_BitsPerPixel((&format)), format.Rmask,
15451548
format.Gmask, format.Bmask, format.Amask))) {
1546-
if (SDL_ISPIXELFORMAT_INDEXED(surf->format->format)) {
1547-
SDL_SetPixelFormatPalette(&format, surf->format->palette);
1549+
if (SDL_ISPIXELFORMAT_INDEXED(PG_SurfaceFormatEnum(surf))) {
1550+
#if SDL_VERSION_ATLEAST(3, 0, 0)
1551+
if (palette)
1552+
SDL_FreePalette(palette);
1553+
palette = PG_GetSurfacePalette(surf);
1554+
#else
1555+
SDL_SetPixelFormatPalette(&format,
1556+
PG_GetSurfacePalette(surf));
1557+
#endif
15481558
}
15491559
else {
15501560
/* Give the surface something other than an all white
15511561
* palette.
15521562
*/
15531563
SDL_SetPaletteColors(palette, default_palette_colors, 0,
15541564
default_palette_size);
1565+
#if !SDL_VERSION_ATLEAST(3, 0, 0)
15551566
SDL_SetPixelFormatPalette(&format, palette);
1567+
#endif
15561568
}
15571569
}
1570+
#if SDL_VERSION_ATLEAST(3, 0, 0)
1571+
PG_PixelFormatEnum format_enum = SDL_GetPixelFormatForMasks(
1572+
format.bits_per_pixel, format.Rmask, format.Gmask,
1573+
format.Bmask, format.Amask);
1574+
newsurf = SDL_ConvertSurfaceAndColorspace(
1575+
surf, format_enum, palette, SDL_GetSurfaceColorspace(surf),
1576+
SDL_GetSurfaceProperties(surf));
1577+
#else
15581578
newsurf = PG_ConvertSurface(surf, &format);
1579+
#endif
15591580
SDL_SetSurfaceBlendMode(newsurf, SDL_BLENDMODE_NONE);
15601581
SDL_FreePalette(palette);
15611582
}
@@ -1571,7 +1592,7 @@ surf_convert(pgSurfaceObject *self, PyObject *args)
15711592
}
15721593

15731594
if (has_colorkey) {
1574-
colorkey = SDL_MapRGBA(newsurf->format, key_r, key_g, key_b, key_a);
1595+
colorkey = PG_SurfaceMapRGBA(newsurf, key_r, key_g, key_b, key_a);
15751596
if (SDL_SetColorKey(newsurf, SDL_TRUE, colorkey) != 0) {
15761597
PyErr_SetString(pgExc_SDLError, SDL_GetError());
15771598
SDL_FreeSurface(newsurf);
@@ -2540,7 +2561,12 @@ static int
25402561
_PgSurface_SrcAlpha(SDL_Surface *surf)
25412562
{
25422563
SDL_BlendMode mode;
2543-
if (SDL_GetSurfaceBlendMode(surf, &mode) < 0) {
2564+
#if SDL_VERSION_ATLEAST(3, 0, 0)
2565+
if (!SDL_GetSurfaceBlendMode(surf, &mode))
2566+
#else
2567+
if (SDL_GetSurfaceBlendMode(surf, &mode) < 0)
2568+
#endif
2569+
{
25442570
PyErr_SetString(pgExc_SDLError, SDL_GetError());
25452571
return -1;
25462572
}
@@ -2579,7 +2605,11 @@ surf_get_flags(PyObject *self, PyObject *_null)
25792605
flags |= PGS_PREALLOC;
25802606
if (PG_SurfaceHasRLE(surf))
25812607
flags |= PGS_RLEACCELOK;
2608+
#if SDL_VERSION_ATLEAST(3, 0, 0) // TODO: is there a better solution?
2609+
if (PG_SurfaceHasRLE(surf))
2610+
#else
25822611
if ((sdl_flags & SDL_RLEACCEL))
2612+
#endif
25832613
flags |= PGS_RLEACCEL;
25842614
if (is_window_surf) {
25852615
if (window_flags & PG_WINDOW_FULLSCREEN_INCLUSIVE)
@@ -2790,10 +2820,11 @@ surf_subsurface(PyObject *self, PyObject *args)
27902820
return RAISE(pgExc_SDLError, SDL_GetError());
27912821

27922822
/* copy the colormap if we need it */
2793-
if (SDL_ISPIXELFORMAT_INDEXED(surf->format->format) &&
2794-
surf->format->palette) {
2795-
SDL_Color *colors = surf->format->palette->colors;
2796-
int ncolors = surf->format->palette->ncolors;
2823+
if (SDL_ISPIXELFORMAT_INDEXED(PG_SurfaceFormatEnum(surf)) &&
2824+
PG_GetSurfacePalette(surf)) {
2825+
SDL_Palette *surf_pallete = PG_GetSurfacePalette(surf);
2826+
SDL_Color *colors = surf_pallete->colors;
2827+
int ncolors = surf_pallete->ncolors;
27972828
SDL_Palette *pal = SDL_AllocPalette(ncolors);
27982829

27992830
if (!pal) {
@@ -4049,11 +4080,11 @@ pgSurface_Blit(pgSurfaceObject *dstobj, pgSurfaceObject *srcobj,
40494080
PG_PixelFormat *fmt = PG_GetSurfaceFormat(src);
40504081
PG_PixelFormatMut newfmt;
40514082

4052-
newfmt.palette = 0; /* Set NULL (or SDL gets confused) */
40534083
#if SDL_VERSION_ATLEAST(3, 0, 0)
40544084
newfmt.bits_per_pixel = fmt->bits_per_pixel;
40554085
newfmt.bytes_per_pixel = fmt->bytes_per_pixel;
40564086
#else
4087+
newfmt.palette = 0; /* Set NULL (or SDL gets confused) */
40574088
newfmt.BitsPerPixel = fmt->BitsPerPixel;
40584089
newfmt.BytesPerPixel = fmt->BytesPerPixel;
40594090
#endif
@@ -4076,7 +4107,14 @@ pgSurface_Blit(pgSurfaceObject *dstobj, pgSurfaceObject *srcobj,
40764107
newfmt.Gloss = fmt->Gloss;
40774108
newfmt.Bloss = fmt->Bloss;
40784109
#endif
4110+
#if SDL_VERSION_ATLEAST(3, 0, 0)
4111+
PG_PixelFormatEnum format_enum = SDL_GetPixelFormatForMasks(
4112+
newfmt.bits_per_pixel, newfmt.Rmask, newfmt.Gmask,
4113+
newfmt.Bmask, newfmt.Amask);
4114+
src = SDL_ConvertSurface(src, format_enum);
4115+
#else
40794116
src = PG_ConvertSurface(src, &newfmt);
4117+
#endif
40804118
if (src) {
40814119
result = SDL_BlitSurface(src, srcrect, dst, dstrect);
40824120
SDL_FreeSurface(src);
@@ -4093,8 +4131,11 @@ pgSurface_Blit(pgSurfaceObject *dstobj, pgSurfaceObject *srcobj,
40934131
PG_SURF_BytesPerPixel(dst) == 2) &&
40944132
_PgSurface_SrcAlpha(src) &&
40954133
(SDL_ISPIXELFORMAT_ALPHA(PG_SurfaceFormatEnum(src))) &&
4096-
!PG_SurfaceHasRLE(src) && !PG_SurfaceHasRLE(dst) &&
4097-
!(src->flags & SDL_RLEACCEL) && !(dst->flags & SDL_RLEACCEL)) {
4134+
!PG_SurfaceHasRLE(src) && !PG_SurfaceHasRLE(dst)
4135+
#if !SDL_VERSION_ATLEAST(3, 0, 0)
4136+
&& !(src->flags & SDL_RLEACCEL) && !(dst->flags & SDL_RLEACCEL)
4137+
#endif
4138+
) {
40984139
/* If we have a 32bit source surface with per pixel alpha
40994140
and no RLE we'll use pygame_Blit so we can mimic how SDL1
41004141
behaved */

0 commit comments

Comments
 (0)