Skip to content

Commit 32453be

Browse files
committed
Get surface.c compiling in SDL3
1 parent 39c61e4 commit 32453be

File tree

2 files changed

+36
-14
lines changed

2 files changed

+36
-14
lines changed

src_c/meson.build

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,6 @@ rwobject = py.extension_module(
8484
subdir: pg,
8585
)
8686

87-
# TODO: support SDL3
88-
if sdl_api != 3
8987
simd_blitters_avx2 = static_library(
9088
'simd_blitters_avx2',
9189
'simd_blitters_avx2.c',
@@ -132,7 +130,6 @@ surface = py.extension_module(
132130
install: true,
133131
subdir: pg,
134132
)
135-
endif
136133

137134
surflock = py.extension_module(
138135
'surflock',

src_c/surface.c

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1546,7 +1546,12 @@ surf_set_alpha(pgSurfaceObject *self, PyObject *args)
15461546
bool success =
15471547
PG_SetSurfaceRLE(surf, (flags & PGS_RLEACCEL) ? SDL_TRUE : SDL_FALSE);
15481548
/* HACK HACK HACK */
1549-
if ((surf->flags & SDL_RLEACCEL) && (!(flags & PGS_RLEACCEL))) {
1549+
#if SDL_VERSION_ATLEAST(3, 0, 0)
1550+
if (SDL_SurfaceHasRLE(surf) && (!(flags & PGS_RLEACCEL)))
1551+
#else
1552+
if ((surf->flags & SDL_RLEACCEL) && (!(flags & PGS_RLEACCEL)))
1553+
#endif
1554+
{
15501555
/* hack to strip SDL_RLEACCEL flag off surface immediately when
15511556
it is not requested */
15521557
sdlrect.x = 0;
@@ -3016,9 +3021,15 @@ surf_get_flags(PyObject *self, PyObject *_null)
30163021
if (PG_SurfaceHasRLE(surf)) {
30173022
flags |= PGS_RLEACCELOK;
30183023
}
3024+
#if SDL_VERSION_ATLEAST(3, 0, 0)
3025+
if (SDL_SurfaceHasRLE(surf)) {
3026+
flags |= PGS_RLEACCEL;
3027+
}
3028+
#else
30193029
if ((sdl_flags & SDL_RLEACCEL)) {
30203030
flags |= PGS_RLEACCEL;
30213031
}
3032+
#endif
30223033
if (is_window_surf) {
30233034
if (window_flags & PG_WINDOW_FULLSCREEN_INCLUSIVE) {
30243035
flags |= PGS_FULLSCREEN;
@@ -4370,8 +4381,8 @@ surf_get_pixels_address(PyObject *self, PyObject *closure)
43704381
}
43714382

43724383
static int
4373-
surface_do_overlap(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst,
4374-
SDL_Rect *dstrect)
4384+
surface_do_overlap(SDL_Surface *src, SDL_Rect *srcrect, SDL_Rect *srcclip,
4385+
SDL_Surface *dst, SDL_Rect *dstrect)
43754386
{
43764387
Uint8 *srcpixels;
43774388
Uint8 *dstpixels;
@@ -4380,7 +4391,6 @@ surface_do_overlap(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst,
43804391
int x, y;
43814392
int w = srcrect->w, h = srcrect->h;
43824393
int maxw, maxh;
4383-
SDL_Rect *clip = &dst->clip_rect;
43844394
int span;
43854395
int dstoffset;
43864396

@@ -4406,23 +4416,23 @@ surface_do_overlap(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst,
44064416
}
44074417

44084418
/* clip the destination rectangle against the clip rectangle */
4409-
x = clip->x - dstx;
4419+
x = srcclip->x - dstx;
44104420
if (x > 0) {
44114421
w -= x;
44124422
dstx += x;
44134423
srcx += x;
44144424
}
4415-
x = dstx + w - clip->x - clip->w;
4425+
x = dstx + w - srcclip->x - srcclip->w;
44164426
if (x > 0) {
44174427
w -= x;
44184428
}
4419-
y = clip->y - dsty;
4429+
y = srcclip->y - dsty;
44204430
if (y > 0) {
44214431
h -= y;
44224432
dsty += y;
44234433
srcy += y;
44244434
}
4425-
y = dsty + h - clip->y - clip->h;
4435+
y = dsty + h - srcclip->y - srcclip->h;
44264436
if (y > 0) {
44274437
h -= y;
44284438
}
@@ -4460,8 +4470,15 @@ pgSurface_Blit(pgSurfaceObject *dstobj, pgSurfaceObject *srcobj,
44604470
SDL_Surface *dst = pgSurface_AsSurface(dstobj);
44614471
SDL_Surface *subsurface = NULL;
44624472
int result, suboffsetx = 0, suboffsety = 0;
4463-
SDL_Rect orig_clip, sub_clip;
4473+
SDL_Rect orig_clip, sub_clip, srcclip;
4474+
#if !SDL_VERSION_ATLEAST(3, 0, 0)
44644475
Uint8 alpha;
4476+
#endif
4477+
4478+
if (!PG_GetSurfaceClipRect(src, &srcclip)) {
4479+
PyErr_SetString(pgExc_SDLError, SDL_GetError());
4480+
return 0;
4481+
}
44654482

44664483
/* passthrough blits to the real surface */
44674484
if (((pgSurfaceObject *)dstobj)->subsurface) {
@@ -4506,11 +4523,13 @@ pgSurface_Blit(pgSurfaceObject *dstobj, pgSurfaceObject *srcobj,
45064523
owner is locked.
45074524
*/
45084525
dst->pixels == src->pixels && srcrect != NULL &&
4509-
surface_do_overlap(src, srcrect, dst, dstrect))) {
4526+
surface_do_overlap(src, srcrect, &srcclip, dst, dstrect))) {
45104527
/* Py_BEGIN_ALLOW_THREADS */
45114528
result = pygame_Blit(src, srcrect, dst, dstrect, blend_flags);
45124529
/* Py_END_ALLOW_THREADS */
45134530
}
4531+
// TODO SDL3: port the below bit of code. Skipping for initial surface port.
4532+
#if !SDL_VERSION_ATLEAST(3, 0, 0)
45144533
/* can't blit alpha to 8bit, crashes SDL */
45154534
else if (PG_SURF_BytesPerPixel(dst) == 1 &&
45164535
(SDL_ISPIXELFORMAT_ALPHA(PG_SURF_FORMATENUM(src)) ||
@@ -4554,14 +4573,20 @@ pgSurface_Blit(pgSurfaceObject *dstobj, pgSurfaceObject *srcobj,
45544573
}
45554574
/* Py_END_ALLOW_THREADS */
45564575
}
4576+
#endif
45574577
else if (blend_flags != PYGAME_BLEND_ALPHA_SDL2 &&
45584578
!(pg_EnvShouldBlendAlphaSDL2()) && !SDL_HasColorKey(src) &&
45594579
(PG_SURF_BytesPerPixel(dst) == 4 ||
45604580
PG_SURF_BytesPerPixel(dst) == 2) &&
45614581
_PgSurface_SrcAlpha(src) &&
45624582
(SDL_ISPIXELFORMAT_ALPHA(PG_SURF_FORMATENUM(src))) &&
45634583
!PG_SurfaceHasRLE(src) && !PG_SurfaceHasRLE(dst) &&
4564-
!(src->flags & SDL_RLEACCEL) && !(dst->flags & SDL_RLEACCEL)) {
4584+
#if SDL_VERSION_ATLEAST(3, 0, 0)
4585+
1
4586+
#else
4587+
!(src->flags & SDL_RLEACCEL) && !(dst->flags & SDL_RLEACCEL)
4588+
#endif
4589+
) {
45654590
/* If we have a 32bit source surface with per pixel alpha
45664591
and no RLE we'll use pygame_Blit so we can mimic how SDL1
45674592
behaved */

0 commit comments

Comments
 (0)