@@ -507,8 +507,6 @@ surface_init(pgSurfaceObject *self, PyObject *args, PyObject *kwds)
507
507
return -1 ;
508
508
}
509
509
510
- default_format .palette = NULL ;
511
-
512
510
surface_cleanup (self );
513
511
514
512
if (depth && masks ) { /* all info supplied, most errorchecking
@@ -923,11 +921,7 @@ surf_map_rgb(PyObject *self, PyObject *args)
923
921
924
922
SURF_INIT_CHECK (surf )
925
923
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 ]);
931
925
return PyLong_FromLong (color );
932
926
}
933
927
@@ -1314,6 +1308,9 @@ surf_set_alpha(pgSurfaceObject *self, PyObject *args)
1314
1308
pgSurface_Prep (self );
1315
1309
result =
1316
1310
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?
1317
1314
/* HACK HACK HACK */
1318
1315
if ((surf -> flags & SDL_RLEACCEL ) && (!(flags & PGS_RLEACCEL ))) {
1319
1316
/* hack to strip SDL_RLEACCEL flag off surface immediately when
@@ -1328,6 +1325,7 @@ surf_set_alpha(pgSurfaceObject *self, PyObject *args)
1328
1325
SDL_LowerBlit (surf , & sdlrect , surface , & sdlrect );
1329
1326
SDL_FreeSurface (surface );
1330
1327
}
1328
+ #endif
1331
1329
/* HACK HACK HACK */
1332
1330
if (result == 0 )
1333
1331
result = SDL_SetSurfaceAlphaMod (surf , alpha );
@@ -1533,29 +1531,52 @@ surf_convert(pgSurfaceObject *self, PyObject *args)
1533
1531
format .BytesPerPixel = (bpp + 7 ) / 8 ;
1534
1532
#endif
1535
1533
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
1542
1544
format .palette = NULL ;
1545
+ #endif
1543
1546
if (SDL_ISPIXELFORMAT_INDEXED (SDL_MasksToPixelFormatEnum (
1544
1547
PG_FORMAT_BitsPerPixel ((& format )), format .Rmask ,
1545
1548
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
1548
1558
}
1549
1559
else {
1550
1560
/* Give the surface something other than an all white
1551
1561
* palette.
1552
1562
*/
1553
1563
SDL_SetPaletteColors (palette , default_palette_colors , 0 ,
1554
1564
default_palette_size );
1565
+ #if !SDL_VERSION_ATLEAST (3 , 0 , 0 )
1555
1566
SDL_SetPixelFormatPalette (& format , palette );
1567
+ #endif
1556
1568
}
1557
1569
}
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
1558
1578
newsurf = PG_ConvertSurface (surf , & format );
1579
+ #endif
1559
1580
SDL_SetSurfaceBlendMode (newsurf , SDL_BLENDMODE_NONE );
1560
1581
SDL_FreePalette (palette );
1561
1582
}
@@ -1571,7 +1592,7 @@ surf_convert(pgSurfaceObject *self, PyObject *args)
1571
1592
}
1572
1593
1573
1594
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 );
1575
1596
if (SDL_SetColorKey (newsurf , SDL_TRUE , colorkey ) != 0 ) {
1576
1597
PyErr_SetString (pgExc_SDLError , SDL_GetError ());
1577
1598
SDL_FreeSurface (newsurf );
@@ -2540,7 +2561,12 @@ static int
2540
2561
_PgSurface_SrcAlpha (SDL_Surface * surf )
2541
2562
{
2542
2563
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
+ {
2544
2570
PyErr_SetString (pgExc_SDLError , SDL_GetError ());
2545
2571
return -1 ;
2546
2572
}
@@ -2579,7 +2605,11 @@ surf_get_flags(PyObject *self, PyObject *_null)
2579
2605
flags |= PGS_PREALLOC ;
2580
2606
if (PG_SurfaceHasRLE (surf ))
2581
2607
flags |= PGS_RLEACCELOK ;
2608
+ #if SDL_VERSION_ATLEAST (3 , 0 , 0 ) // TODO: is there a better solution?
2609
+ if (PG_SurfaceHasRLE (surf ))
2610
+ #else
2582
2611
if ((sdl_flags & SDL_RLEACCEL ))
2612
+ #endif
2583
2613
flags |= PGS_RLEACCEL ;
2584
2614
if (is_window_surf ) {
2585
2615
if (window_flags & PG_WINDOW_FULLSCREEN_INCLUSIVE )
@@ -2790,10 +2820,11 @@ surf_subsurface(PyObject *self, PyObject *args)
2790
2820
return RAISE (pgExc_SDLError , SDL_GetError ());
2791
2821
2792
2822
/* 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 ;
2797
2828
SDL_Palette * pal = SDL_AllocPalette (ncolors );
2798
2829
2799
2830
if (!pal ) {
@@ -4049,11 +4080,11 @@ pgSurface_Blit(pgSurfaceObject *dstobj, pgSurfaceObject *srcobj,
4049
4080
PG_PixelFormat * fmt = PG_GetSurfaceFormat (src );
4050
4081
PG_PixelFormatMut newfmt ;
4051
4082
4052
- newfmt .palette = 0 ; /* Set NULL (or SDL gets confused) */
4053
4083
#if SDL_VERSION_ATLEAST (3 , 0 , 0 )
4054
4084
newfmt .bits_per_pixel = fmt -> bits_per_pixel ;
4055
4085
newfmt .bytes_per_pixel = fmt -> bytes_per_pixel ;
4056
4086
#else
4087
+ newfmt .palette = 0 ; /* Set NULL (or SDL gets confused) */
4057
4088
newfmt .BitsPerPixel = fmt -> BitsPerPixel ;
4058
4089
newfmt .BytesPerPixel = fmt -> BytesPerPixel ;
4059
4090
#endif
@@ -4076,7 +4107,14 @@ pgSurface_Blit(pgSurfaceObject *dstobj, pgSurfaceObject *srcobj,
4076
4107
newfmt .Gloss = fmt -> Gloss ;
4077
4108
newfmt .Bloss = fmt -> Bloss ;
4078
4109
#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
4079
4116
src = PG_ConvertSurface (src , & newfmt );
4117
+ #endif
4080
4118
if (src ) {
4081
4119
result = SDL_BlitSurface (src , srcrect , dst , dstrect );
4082
4120
SDL_FreeSurface (src );
@@ -4093,8 +4131,11 @@ pgSurface_Blit(pgSurfaceObject *dstobj, pgSurfaceObject *srcobj,
4093
4131
PG_SURF_BytesPerPixel (dst ) == 2 ) &&
4094
4132
_PgSurface_SrcAlpha (src ) &&
4095
4133
(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
+ ) {
4098
4139
/* If we have a 32bit source surface with per pixel alpha
4099
4140
and no RLE we'll use pygame_Blit so we can mimic how SDL1
4100
4141
behaved */
0 commit comments