Skip to content

Port over more SDL functions to SDL3 #3451

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 31, 2025
Merged
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
14 changes: 14 additions & 0 deletions src_c/_pygame.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ PG_GetSurfaceFormat(SDL_Surface *surf)
#define PG_GetSurfaceBlendMode SDL_GetSurfaceBlendMode
#define PG_GetSurfaceAlphaMod SDL_GetSurfaceAlphaMod
#define PG_SetSurfaceAlphaMod SDL_SetSurfaceAlphaMod
#define PG_FillSurfaceRect SDL_FillSurfaceRect
#define PG_LockSurface SDL_LockSurface

#define PG_GetRGBA SDL_GetRGBA
#define PG_GetRGB SDL_GetRGB
Expand Down Expand Up @@ -269,6 +271,18 @@ PG_SetSurfaceAlphaMod(SDL_Surface *surface, Uint8 alpha)
return SDL_SetSurfaceAlphaMod(surface, alpha) == 0;
}

static inline bool
PG_FillSurfaceRect(SDL_Surface *dst, const SDL_Rect *rect, Uint32 color)
{
return SDL_FillRect(dst, rect, color) == 0;
}

static inline bool
PG_LockSurface(SDL_Surface *surface)
{
return SDL_LockSurface(surface) == 0;
}

// NOTE:
// palette is part of the format in SDL2, so these functions below have it
// as a separate parameter to be consistent with the SDL3 signature.
Expand Down
4 changes: 2 additions & 2 deletions src_c/alphablit.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ SoftBlitPyGame(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst,
/* Lock the destination if it's in hardware */
dst_locked = 0;
if (SDL_MUSTLOCK(dst)) {
if (SDL_LockSurface(dst) < 0) {
if (!PG_LockSurface(dst)) {
okay = 0;
}
else {
Expand All @@ -90,7 +90,7 @@ SoftBlitPyGame(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst,
/* Lock the source if it's in hardware */
src_locked = 0;
if (SDL_MUSTLOCK(src)) {
if (SDL_LockSurface(src) < 0) {
if (!PG_LockSurface(src)) {
okay = 0;
}
else {
Expand Down
5 changes: 2 additions & 3 deletions src_c/draw.c
Original file line number Diff line number Diff line change
Expand Up @@ -1178,7 +1178,6 @@ rect(PyObject *self, PyObject *args, PyObject *kwargs)
int top_left_radius = -1, top_right_radius = -1, bottom_left_radius = -1,
bottom_right_radius = -1;
SDL_Rect sdlrect;
int result;
SDL_Rect clipped;
int drawn_area[4] = {INT_MAX, INT_MAX, INT_MIN,
INT_MIN}; /* Used to store bounding box values */
Expand Down Expand Up @@ -1248,10 +1247,10 @@ rect(PyObject *self, PyObject *args, PyObject *kwargs)
else {
pgSurface_Prep(surfobj);
pgSurface_Lock(surfobj);
result = SDL_FillRect(surf, &clipped, color);
bool success = PG_FillSurfaceRect(surf, &clipped, color);
pgSurface_Unlock(surfobj);
pgSurface_Unprep(surfobj);
if (result != 0) {
if (!success) {
return RAISE(pgExc_SDLError, SDL_GetError());
}
}
Expand Down
4 changes: 2 additions & 2 deletions src_c/freetype/ft_render.c
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ _PGFT_Render_ExistingSurface(FreeTypeInstance *ft, pgFontObject *fontobj,
Layout *font_text;

if (SDL_MUSTLOCK(surface)) {
if (SDL_LockSurface(surface) == -1) {
if (!PG_LockSurface(surface)) {
SDL_FreeSurface(surface);
PyErr_SetString(pgExc_SDLError, SDL_GetError());
return -1;
Expand Down Expand Up @@ -445,7 +445,7 @@ _PGFT_Render_NewSurface(FreeTypeInstance *ft, pgFontObject *fontobj,
}

if (SDL_MUSTLOCK(surface)) {
if (SDL_LockSurface(surface) == -1) {
if (!PG_LockSurface(surface)) {
PyErr_SetString(pgExc_SDLError, SDL_GetError());
SDL_FreeSurface(surface);
return 0;
Expand Down
4 changes: 2 additions & 2 deletions src_c/surface.c
Original file line number Diff line number Diff line change
Expand Up @@ -2198,7 +2198,7 @@ surf_fill(pgSurfaceObject *self, PyObject *args, PyObject *keywds)
else {
pgSurface_Prep(self);
pgSurface_Lock((pgSurfaceObject *)self);
result = SDL_FillRect(surf, &sdlrect, color);
result = PG_FillSurfaceRect(surf, &sdlrect, color) - 1;
pgSurface_Unlock((pgSurfaceObject *)self);
pgSurface_Unprep(self);
}
Expand Down Expand Up @@ -2938,7 +2938,7 @@ surf_scroll(PyObject *self, PyObject *args, PyObject *keywds)
if (!repeat) {
if (dx >= w || dx <= -w || dy >= h || dy <= -h) {
if (erase) {
if (SDL_FillRect(surf, NULL, 0) == -1) {
if (!PG_FillSurfaceRect(surf, NULL, 0)) {
PyErr_SetString(pgExc_SDLError, SDL_GetError());
return NULL;
}
Expand Down
2 changes: 1 addition & 1 deletion src_c/surface_fill.c
Original file line number Diff line number Diff line change
Expand Up @@ -927,7 +927,7 @@ surface_fill_blend(SDL_Surface *surface, SDL_Rect *rect, Uint32 color,

/* Lock the surface, if needed */
if (SDL_MUSTLOCK(surface)) {
if (SDL_LockSurface(surface) < 0) {
if (!PG_LockSurface(surface)) {
return -1;
}
locked = 1;
Expand Down
7 changes: 1 addition & 6 deletions src_c/surflock.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,7 @@ pgSurface_LockBy(pgSurfaceObject *surfobj, PyObject *lockobj)
if (surf->subsurface != NULL) {
pgSurface_Prep(surfobj);
}
#if SDL_VERSION_ATLEAST(3, 0, 0)
if (!SDL_LockSurface(surf->surf))
#else
if (SDL_LockSurface(surf->surf) == -1)
#endif
{
if (!PG_LockSurface(surf->surf)) {
PyErr_SetString(PyExc_RuntimeError, "error locking surface");
return 0;
}
Expand Down
18 changes: 4 additions & 14 deletions src_c/transform.c
Original file line number Diff line number Diff line change
Expand Up @@ -2420,20 +2420,10 @@ solid_overlay(pgSurfaceObject *srcobj, Uint32 color, pgSurfaceObject *dstobj,
int src_lock = SDL_MUSTLOCK(src);
int dst_lock = src != newsurf && SDL_MUSTLOCK(newsurf);

#if SDL_VERSION_ATLEAST(3, 0, 0)
if (src_lock && !SDL_LockSurface(src))
#else
if (src_lock && SDL_LockSurface(src) < 0)
#endif
{
if (src_lock && !PG_LockSurface(src)) {
return NULL;
}
#if SDL_VERSION_ATLEAST(3, 0, 0)
if (dst_lock && !SDL_LockSurface(newsurf))
#else
if (dst_lock && SDL_LockSurface(newsurf) < 0)
#endif
{
if (dst_lock && !PG_LockSurface(newsurf)) {
if (src_lock) {
SDL_UnlockSurface(src);
}
Expand Down Expand Up @@ -2696,13 +2686,13 @@ modify_hsl(SDL_Surface *surf, PG_PixelFormat *fmt, SDL_Surface *dst,
{
int surf_locked = 0;
if (SDL_MUSTLOCK(surf)) {
if (SDL_LockSurface(surf) == 0) {
if (PG_LockSurface(surf)) {
surf_locked = 1;
}
}
int dst_locked = 0;
if (SDL_MUSTLOCK(dst)) {
if (SDL_LockSurface(dst) == 0) {
if (PG_LockSurface(dst)) {
dst_locked = 1;
}
}
Expand Down
Loading