From 3306bf0e01463a23996b240070a681ca91d75bf6 Mon Sep 17 00:00:00 2001 From: Ankith Date: Tue, 16 Dec 2025 17:27:26 +0530 Subject: [PATCH 1/3] SDL3: skip tests of unported stuff, run on CI --- .github/workflows/build-sdl3.yml | 90 +++++++++----------------------- test/controller_tags.py | 8 +++ test/gfxdraw_tags.py | 8 +++ test/joystick_tags.py | 8 +++ test/mixer_music_tags.py | 8 +++ test/mixer_tags.py | 8 +++ test/render_tags.py | 8 +++ test/sndarray_tags.py | 6 ++- test/touch_tags.py | 8 +++ test/video_tags.py | 8 +++ test/window_test.py | 4 ++ 11 files changed, 97 insertions(+), 67 deletions(-) create mode 100644 test/controller_tags.py create mode 100644 test/gfxdraw_tags.py create mode 100644 test/joystick_tags.py create mode 100644 test/mixer_music_tags.py create mode 100644 test/mixer_tags.py create mode 100644 test/render_tags.py create mode 100644 test/touch_tags.py create mode 100644 test/video_tags.py diff --git a/.github/workflows/build-sdl3.yml b/.github/workflows/build-sdl3.yml index 946e6dec26..f79894a26b 100644 --- a/.github/workflows/build-sdl3.yml +++ b/.github/workflows/build-sdl3.yml @@ -42,7 +42,12 @@ jobs: strategy: fail-fast: false # if a particular matrix build fails, don't skip the rest matrix: - os: [ubuntu-24.04, windows-latest, macos-14] + os: [ubuntu-latest, windows-latest, macos-15] + + # Use ubuntu 25.10 docker image instead of the ubuntu-latest which is older + # at the time of writing of this comment, and therefore doesn't have SDL3 + # and friends. + container: ${{ matrix.os == 'ubuntu-latest' && 'ubuntu:25.10' || '' }} env: # Pip now forces us to either make a venv or set this flag, so we will do @@ -53,76 +58,31 @@ jobs: steps: - uses: actions/checkout@v6.0.1 - - uses: actions/setup-python@v6 + + # windows runner has python 3.9 as default (at the time of writing of this) + # which we don't support, so we need to setup a newer version ourselves here + - name: Install pygame deps (windows) + if: matrix.os == 'windows-latest' + uses: actions/setup-python@v6 with: python-version: '3.14' - - name: Install pygame deps (linux) - if: matrix.os == 'ubuntu-24.04' - run: | - sudo apt-get update --fix-missing - sudo apt-get install libfreetype6-dev libportmidi-dev python3-dev - - name: Install pygame deps (mac) - if: matrix.os == 'macos-14' - run: brew install freetype portmidi - - # taken from https://wiki.libsdl.org/SDL3/README-linux#build-dependencies - - name: Install SDL3 deps (linux) - if: matrix.os == 'ubuntu-24.04' - run: > - sudo apt-get install build-essential git make - pkg-config cmake ninja-build gnome-desktop-testing libasound2-dev libpulse-dev - libaudio-dev libfribidi-dev libjack-dev libsndio-dev libx11-dev libxext-dev - libxrandr-dev libxcursor-dev libxfixes-dev libxi-dev libxss-dev libxtst-dev - libxkbcommon-dev libdrm-dev libgbm-dev libgl1-mesa-dev libgles2-mesa-dev - libegl1-mesa-dev libdbus-1-dev libibus-1.0-dev libudev-dev - libpipewire-0.3-dev libwayland-dev libdecor-0-dev liburing-dev + if: matrix.os == 'macos-15' + run: brew install sdl3 sdl3_image sdl3_ttf freetype portmidi - # taken from https://wiki.libsdl.org/SDL3/Installation - - name: Install SDL3 - if: matrix.os != 'windows-latest' - run: | - git clone https://github.com/libsdl-org/SDL - cd SDL - mkdir build - cd build - cmake -DCMAKE_BUILD_TYPE=Release .. - cmake --build . --config Release --parallel - sudo cmake --install . --config Release - - - name: Install SDL3_image - if: matrix.os != 'windows-latest' - run: | - git clone https://github.com/libsdl-org/SDL_image - cd SDL_image - mkdir build - cd build - cmake -DCMAKE_BUILD_TYPE=Release .. - cmake --build . --config Release --parallel - sudo cmake --install . --config Release - - - name: Install SDL3_ttf - if: matrix.os != 'windows-latest' + - name: Install pygame deps (linux) + if: matrix.os == 'ubuntu-latest' run: | - git clone https://github.com/libsdl-org/SDL_ttf - cd SDL_ttf - mkdir build - cd build - cmake -DCMAKE_BUILD_TYPE=Release .. - cmake --build . --config Release --parallel - sudo cmake --install . --config Release + apt-get update + apt-get install -y python3-dev python3-pip \ + libsdl3-dev libsdl3-image-dev libsdl3-ttf-dev libfreetype6-dev libportmidi-dev - - name: Build with SDL3 + - name: Build pygame-ce with SDL3 run: python3 dev.py build --sdl3 - # eventually we need to run all tests, but for now test that importing pygame - # works - - name: Test import works - run: python3 -c 'import pygame' - - # - name: Run tests - # env: - # SDL_VIDEODRIVER: "dummy" - # SDL_AUDIODRIVER: "disk" - # run: python3 -m pygame.tests -v --exclude opengl,music,timing --time_out 300 + - name: Run tests + env: + SDL_VIDEODRIVER: "dummy" + SDL_AUDIODRIVER: "disk" + run: python3 -m pygame.tests -v --exclude opengl,music,timing --time_out 300 diff --git a/test/controller_tags.py b/test/controller_tags.py new file mode 100644 index 0000000000..7048bc8392 --- /dev/null +++ b/test/controller_tags.py @@ -0,0 +1,8 @@ +import pygame + +__tags__ = ["sdl3_skip"] + +exclude = pygame.get_sdl_version() >= (3, 0, 0) + +if exclude: + __tags__.extend(("ignore", "subprocess_ignore")) diff --git a/test/gfxdraw_tags.py b/test/gfxdraw_tags.py new file mode 100644 index 0000000000..7048bc8392 --- /dev/null +++ b/test/gfxdraw_tags.py @@ -0,0 +1,8 @@ +import pygame + +__tags__ = ["sdl3_skip"] + +exclude = pygame.get_sdl_version() >= (3, 0, 0) + +if exclude: + __tags__.extend(("ignore", "subprocess_ignore")) diff --git a/test/joystick_tags.py b/test/joystick_tags.py new file mode 100644 index 0000000000..7048bc8392 --- /dev/null +++ b/test/joystick_tags.py @@ -0,0 +1,8 @@ +import pygame + +__tags__ = ["sdl3_skip"] + +exclude = pygame.get_sdl_version() >= (3, 0, 0) + +if exclude: + __tags__.extend(("ignore", "subprocess_ignore")) diff --git a/test/mixer_music_tags.py b/test/mixer_music_tags.py new file mode 100644 index 0000000000..7048bc8392 --- /dev/null +++ b/test/mixer_music_tags.py @@ -0,0 +1,8 @@ +import pygame + +__tags__ = ["sdl3_skip"] + +exclude = pygame.get_sdl_version() >= (3, 0, 0) + +if exclude: + __tags__.extend(("ignore", "subprocess_ignore")) diff --git a/test/mixer_tags.py b/test/mixer_tags.py new file mode 100644 index 0000000000..7048bc8392 --- /dev/null +++ b/test/mixer_tags.py @@ -0,0 +1,8 @@ +import pygame + +__tags__ = ["sdl3_skip"] + +exclude = pygame.get_sdl_version() >= (3, 0, 0) + +if exclude: + __tags__.extend(("ignore", "subprocess_ignore")) diff --git a/test/render_tags.py b/test/render_tags.py new file mode 100644 index 0000000000..7048bc8392 --- /dev/null +++ b/test/render_tags.py @@ -0,0 +1,8 @@ +import pygame + +__tags__ = ["sdl3_skip"] + +exclude = pygame.get_sdl_version() >= (3, 0, 0) + +if exclude: + __tags__.extend(("ignore", "subprocess_ignore")) diff --git a/test/sndarray_tags.py b/test/sndarray_tags.py index 5e24b93580..9c5b71ee9e 100644 --- a/test/sndarray_tags.py +++ b/test/sndarray_tags.py @@ -1,6 +1,8 @@ -__tags__ = ["array"] +import pygame -exclude = False +__tags__ = ["array", "sdl3_skip"] + +exclude = pygame.get_sdl_version() >= (3, 0, 0) try: import numpy diff --git a/test/touch_tags.py b/test/touch_tags.py new file mode 100644 index 0000000000..7048bc8392 --- /dev/null +++ b/test/touch_tags.py @@ -0,0 +1,8 @@ +import pygame + +__tags__ = ["sdl3_skip"] + +exclude = pygame.get_sdl_version() >= (3, 0, 0) + +if exclude: + __tags__.extend(("ignore", "subprocess_ignore")) diff --git a/test/video_tags.py b/test/video_tags.py new file mode 100644 index 0000000000..7048bc8392 --- /dev/null +++ b/test/video_tags.py @@ -0,0 +1,8 @@ +import pygame + +__tags__ = ["sdl3_skip"] + +exclude = pygame.get_sdl_version() >= (3, 0, 0) + +if exclude: + __tags__.extend(("ignore", "subprocess_ignore")) diff --git a/test/window_test.py b/test/window_test.py index b8dd1f0056..62cd094f56 100644 --- a/test/window_test.py +++ b/test/window_test.py @@ -117,6 +117,10 @@ def test_always_on_top_set(self): SDL < (2, 0, 18), "requires SDL 2.0.18+", ) + @unittest.skipIf( + os.environ.get("SDL_VIDEODRIVER") == pygame.NULL_VIDEODRIVER, + "requires the SDL_VIDEODRIVER to be a non-null value", + ) def test_mouse_rect(self): self.win.mouse_rect = None self.assertIsNone(self.win.mouse_rect) From aa8cc4a1fc2c46fd515a2c60e41b4d6dfc2b35b0 Mon Sep 17 00:00:00 2001 From: Ankith Date: Tue, 16 Dec 2025 15:17:16 +0530 Subject: [PATCH 2/3] SDL3: display+window: runtime fixes --- src_c/display.c | 108 +++++++++++++++++++++++++++++++++++++++++------- src_c/window.c | 23 +++++++++++ 2 files changed, 115 insertions(+), 16 deletions(-) diff --git a/src_c/display.c b/src_c/display.c index b2c380abcb..ef4438e398 100644 --- a/src_c/display.c +++ b/src_c/display.c @@ -99,6 +99,14 @@ _display_state_cleanup(_DisplayState *state) #endif } +/* TODO: Check if doing SDL_PIXELFORMAT_XRGB8888 in SDL2 codepath is fine. + * For now do it only on SDL3 codepath to fix SCALED issues */ +#if SDL_VERSION_ATLEAST(3, 0, 0) +#define PG_TEXTURE_FMT SDL_PIXELFORMAT_XRGB8888 +#else +#define PG_TEXTURE_FMT SDL_PIXELFORMAT_ARGB8888 +#endif + // prevent this code block from being linked twice // (this code block is copied by window.c) #ifndef BUILD_STATIC @@ -464,7 +472,12 @@ pg_GetVideoInfo(pg_VideoInfo *info) } else { #if SDL_VERSION_ATLEAST(3, 0, 0) - if ((mode_ptr = SDL_GetCurrentDisplayMode(0))) { + SDL_DisplayID primary_display = SDL_GetPrimaryDisplay(); + if (primary_display == 0) { + PyErr_SetString(pgExc_SDLError, SDL_GetError()); + return (pg_VideoInfo *)NULL; + } + if ((mode_ptr = SDL_GetCurrentDisplayMode(primary_display))) { info->current_w = mode_ptr->w; info->current_h = mode_ptr->h; formatenum = mode_ptr->format; @@ -942,9 +955,8 @@ pg_ResizeEventWatch(void *userdata, SDL_Event *event) SDL_DestroyTexture(pg_texture); - pg_texture = - SDL_CreateTexture(pg_renderer, SDL_PIXELFORMAT_ARGB8888, - SDL_TEXTUREACCESS_STREAMING, w, h); + pg_texture = SDL_CreateTexture(pg_renderer, PG_TEXTURE_FMT, + SDL_TEXTUREACCESS_STREAMING, w, h); } return 0; } @@ -1166,6 +1178,7 @@ PG_SetWindowFullscreen(SDL_Window *window, bool fullscreen, } } + SDL_SyncWindow(window); ret = true; end: SDL_free(modes); @@ -1261,6 +1274,16 @@ pg_set_mode(PyObject *self, PyObject *arg, PyObject *kwds) } } +#if SDL_VERSION_ATLEAST(3, 0, 0) + /* In SDL2, display == 0 meant primary display, so compat code for it */ + if (display == 0) { + display = SDL_GetPrimaryDisplay(); + if (display == 0) { + return RAISE(pgExc_SDLError, SDL_GetError()); + } + } +#endif + if ((vsync == -1) && ((flags & PGS_OPENGL) == 0)) { return RAISE(PyExc_ValueError, "requested adaptive vsync without OpenGL"); @@ -1751,9 +1774,9 @@ pg_set_mode(PyObject *self, PyObject *arg, PyObject *kwds) } #endif - pg_texture = SDL_CreateTexture( - pg_renderer, SDL_PIXELFORMAT_ARGB8888, - SDL_TEXTUREACCESS_STREAMING, w, h); + pg_texture = + SDL_CreateTexture(pg_renderer, PG_TEXTURE_FMT, + SDL_TEXTUREACCESS_STREAMING, w, h); } surf = PG_CreateSurface(w, h, SDL_PIXELFORMAT_XRGB8888); newownedsurf = surf; @@ -1875,6 +1898,9 @@ pg_set_mode(PyObject *self, PyObject *arg, PyObject *kwds) } } } +#if SDL_VERSION_ATLEAST(3, 0, 0) + SDL_SyncWindow(win); +#endif /*return the window's surface (screen)*/ Py_INCREF(surface); @@ -1973,6 +1999,9 @@ pg_set_window_position(PyObject *self, PyObject *arg) if (win) { /* Will raise errors with SDL 3, deal with it during the porting */ SDL_SetWindowPosition(win, x, y); +#if SDL_VERSION_ATLEAST(3, 0, 0) + SDL_SyncWindow(win); +#endif } Py_RETURN_NONE; @@ -1999,7 +2028,16 @@ pg_mode_ok(PyObject *self, PyObject *args, PyObject *kwds) &display_index)) { return NULL; } -#if !SDL_VERSION_ATLEAST(3, 0, 0) + +#if SDL_VERSION_ATLEAST(3, 0, 0) + /* In SDL2, display == 0 meant primary display, so compat code for it */ + if (display_index == 0) { + display_index = SDL_GetPrimaryDisplay(); + if (display_index == 0) { + return RAISE(pgExc_SDLError, SDL_GetError()); + } + } +#else /* Display ID is not bounded by number of displays in SDL3 */ if (display_index < 0 || display_index >= SDL_GetNumVideoDisplays()) { return RAISE(PyExc_ValueError, @@ -2026,6 +2064,15 @@ pg_mode_ok(PyObject *self, PyObject *args, PyObject *kwds) } #if SDL_VERSION_ATLEAST(3, 0, 0) + /* Compat with SDL2 behaviour */ + const SDL_DisplayMode *curmode; + if (!(curmode = SDL_GetCurrentDisplayMode(display_index))) { + return RAISE(pgExc_SDLError, SDL_GetError()); + } + if (curmode->w == desired.w && curmode->h == desired.h) { + return PyLong_FromLong(SDL_BITSPERPIXEL(curmode->format)); + } + if (!SDL_GetClosestFullscreenDisplayMode(display_index, desired.w, desired.h, desired.refresh_rate, 1, &closest)) { @@ -2065,7 +2112,15 @@ pg_list_modes(PyObject *self, PyObject *args, PyObject *kwds) return NULL; } -#if !SDL_VERSION_ATLEAST(3, 0, 0) +#if SDL_VERSION_ATLEAST(3, 0, 0) + /* In SDL2, display == 0 meant primary display, so compat code for it */ + if (display_index == 0) { + display_index = SDL_GetPrimaryDisplay(); + if (display_index == 0) { + return RAISE(pgExc_SDLError, SDL_GetError()); + } + } +#else /* Display ID is not bounded by number of displays in SDL3 */ if (display_index < 0 || display_index >= SDL_GetNumVideoDisplays()) { return RAISE(PyExc_ValueError, @@ -2077,19 +2132,31 @@ pg_list_modes(PyObject *self, PyObject *args, PyObject *kwds) #pragma PG_WARN(Ignoring flags) #if SDL_VERSION_ATLEAST(3, 0, 0) + const SDL_DisplayMode *curmode; + if (!(curmode = SDL_GetCurrentDisplayMode(display_index))) { + return RAISE(pgExc_SDLError, SDL_GetError()); + } if (bpp == 0) { - const SDL_DisplayMode *curmode; - if (!(curmode = SDL_GetCurrentDisplayMode(display_index))) { - return RAISE(pgExc_SDLError, SDL_GetError()); - } bpp = SDL_BITSPERPIXEL(curmode->format); } SDL_DisplayMode **modes = SDL_GetFullscreenDisplayModes(display_index, &nummodes); - if (nummodes < 0) { + if (!modes || nummodes < 0) { return RAISE(pgExc_SDLError, SDL_GetError()); } + + /* SDL3 can return empty list here but SDL2 didn't. In that case, use + * curmode. */ + if (nummodes == 0) { + SDL_free(modes); + modes = SDL_malloc(sizeof(SDL_DisplayMode **)); + if (!modes) { + return PyErr_NoMemory(); + } + modes[0] = (SDL_DisplayMode *)curmode; + nummodes = 1; + } #else if (bpp == 0) { SDL_DisplayMode curmode; @@ -2726,6 +2793,9 @@ pg_iconify(PyObject *self, PyObject *_null) return RAISE(pgExc_SDLError, "No open window"); } SDL_MinimizeWindow(win); +#if SDL_VERSION_ATLEAST(3, 0, 0) + SDL_SyncWindow(win); +#endif return PyBool_FromLong(1); } @@ -3219,7 +3289,7 @@ pg_toggle_fullscreen(PyObject *self, PyObject *_null) SDL_CreateRenderer(win, -1, SDL_RENDERER_SOFTWARE); #endif pg_texture = - SDL_CreateTexture(pg_renderer, SDL_PIXELFORMAT_ARGB8888, + SDL_CreateTexture(pg_renderer, PG_TEXTURE_FMT, SDL_TEXTUREACCESS_STREAMING, w, h); } #if SDL_VERSION_ATLEAST(3, 0, 0) @@ -3372,7 +3442,7 @@ pg_toggle_fullscreen(PyObject *self, PyObject *_null) SDL_CreateRenderer(win, -1, SDL_RENDERER_SOFTWARE); #endif pg_texture = - SDL_CreateTexture(pg_renderer, SDL_PIXELFORMAT_ARGB8888, + SDL_CreateTexture(pg_renderer, PG_TEXTURE_FMT, SDL_TEXTUREACCESS_STREAMING, w, h); } @@ -3453,6 +3523,9 @@ pg_toggle_fullscreen(PyObject *self, PyObject *_null) } } } +#if SDL_VERSION_ATLEAST(3, 0, 0) + SDL_SyncWindow(win); +#endif return PyLong_FromLong(1); } @@ -3531,6 +3604,9 @@ pg_display_resize_event(PyObject *self, PyObject *event) /* do not do anything that would invalidate a display surface! */ return PyLong_FromLong(-1); } +#if SDL_VERSION_ATLEAST(3, 0, 0) + SDL_SyncWindow(win); +#endif Py_RETURN_FALSE; } diff --git a/src_c/window.c b/src_c/window.c index a928a215c0..07e88a1d66 100644 --- a/src_c/window.c +++ b/src_c/window.c @@ -305,6 +305,7 @@ window_set_windowed(pgWindowObject *self, PyObject *_null) if (!SDL_SetWindowFullscreen(self->_win, 0)) { return RAISE(pgExc_SDLError, SDL_GetError()); } + SDL_SyncWindow(self->_win); #else if (SDL_SetWindowFullscreen(self->_win, 0)) { return RAISE(pgExc_SDLError, SDL_GetError()); @@ -345,6 +346,7 @@ pg_window_set_fullscreen(SDL_Window *window, int desktop) goto end; } + SDL_SyncWindow(window); ret = 1; end: SDL_free(modes); @@ -429,6 +431,9 @@ static PyObject * window_restore(pgWindowObject *self, PyObject *_null) { SDL_RestoreWindow(self->_win); +#if SDL_VERSION_ATLEAST(3, 0, 0) + SDL_SyncWindow(self->_win); +#endif Py_RETURN_NONE; } @@ -436,6 +441,9 @@ static PyObject * window_maximize(pgWindowObject *self, PyObject *_null) { SDL_MaximizeWindow(self->_win); +#if SDL_VERSION_ATLEAST(3, 0, 0) + SDL_SyncWindow(self->_win); +#endif Py_RETURN_NONE; } @@ -443,6 +451,9 @@ static PyObject * window_minimize(pgWindowObject *self, PyObject *_null) { SDL_MinimizeWindow(self->_win); +#if SDL_VERSION_ATLEAST(3, 0, 0) + SDL_SyncWindow(self->_win); +#endif Py_RETURN_NONE; } @@ -745,6 +756,9 @@ window_set_size(pgWindowObject *self, PyObject *arg, void *v) } SDL_SetWindowSize(self->_win, w, h); +#if SDL_VERSION_ATLEAST(3, 0, 0) + SDL_SyncWindow(self->_win); +#endif if (self->surf) { /* Ensure that the underlying surf is immediately updated, instead of * relying on the event callback */ @@ -797,6 +811,9 @@ window_set_minimum_size(pgWindowObject *self, PyObject *arg, void *v) } SDL_SetWindowMinimumSize(self->_win, w, h); +#if SDL_VERSION_ATLEAST(3, 0, 0) + SDL_SyncWindow(self->_win); +#endif return 0; } @@ -837,6 +854,9 @@ window_set_maximum_size(pgWindowObject *self, PyObject *arg, void *v) } SDL_SetWindowMaximumSize(self->_win, w, h); +#if SDL_VERSION_ATLEAST(3, 0, 0) + SDL_SyncWindow(self->_win); +#endif return 0; } @@ -868,6 +888,9 @@ window_set_position(pgWindowObject *self, PyObject *arg, void *v) } SDL_SetWindowPosition(self->_win, x, y); +#if SDL_VERSION_ATLEAST(3, 0, 0) + SDL_SyncWindow(self->_win); +#endif return 0; } From 422d7904bea14bf16ea05c47f22df6ef74c49a0d Mon Sep 17 00:00:00 2001 From: Ankith Date: Tue, 16 Dec 2025 15:18:24 +0530 Subject: [PATCH 3/3] SDL3: surface+pixelarray+font+draw: runtime fixes --- src_c/_pygame.h | 32 ++++++++++++++++++++++++++++---- src_c/draw.c | 2 +- src_c/pixelarray_methods.c | 2 +- src_c/surface.c | 10 +++++----- 4 files changed, 35 insertions(+), 11 deletions(-) diff --git a/src_c/_pygame.h b/src_c/_pygame.h index 2c701c0edb..bce92dac3b 100644 --- a/src_c/_pygame.h +++ b/src_c/_pygame.h @@ -77,9 +77,17 @@ #define PG_CreateSurface SDL_CreateSurface #define PG_CreateSurfaceFrom SDL_CreateSurfaceFrom -#define PG_ConvertSurface SDL_ConvertSurface #define PG_ConvertSurfaceFormat SDL_ConvertSurface +/* Convert surface using palette and format of dst */ +static inline SDL_Surface * +PG_ConvertSurface(SDL_Surface *surface, SDL_Surface *dst) +{ + return SDL_ConvertSurfaceAndColorspace(surface, dst->format, + SDL_GetSurfacePalette(dst), + SDL_GetSurfaceColorspace(dst), 0); +} + #define PG_PixelFormatEnum SDL_PixelFormat #define PG_SurfaceHasRLE SDL_SurfaceHasRLE @@ -144,11 +152,23 @@ PG_SURF_BitsPerPixel(SDL_Surface *surf) #define PG_PixelFormat const SDL_PixelFormatDetails +static inline SDL_Palette * +PG_GetSurfacePalette(SDL_Surface *surf) +{ + SDL_Palette *ret = SDL_GetSurfacePalette(surf); + if (!ret && SDL_ISPIXELFORMAT_INDEXED(surf->format)) { + /* Palette doesn't exist but is expected, so create it. + * SDL will associate the newly created palette with the surface */ + ret = SDL_CreateSurfacePalette(surf); + } + return ret; +} + static inline bool PG_GetSurfaceDetails(SDL_Surface *surf, PG_PixelFormat **format_p, SDL_Palette **palette_p) { - *palette_p = SDL_GetSurfacePalette(surf); + *palette_p = PG_GetSurfacePalette(surf); *format_p = SDL_GetPixelFormatDetails(surf->format); return *format_p != NULL; } @@ -159,7 +179,6 @@ PG_GetSurfaceFormat(SDL_Surface *surf) return SDL_GetPixelFormatDetails(surf->format); } -#define PG_GetSurfacePalette SDL_GetSurfacePalette #define PG_SetPaletteColors SDL_SetPaletteColors #define PG_SetSurfacePalette SDL_SetSurfacePalette #define PG_SetSurfaceColorKey SDL_SetSurfaceColorKey @@ -215,10 +234,15 @@ PG_GetSurfaceFormat(SDL_Surface *surf) SDL_CreateRGBSurfaceWithFormat(0, width, height, 0, format) #define PG_CreateSurfaceFrom(width, height, format, pixels, pitch) \ SDL_CreateRGBSurfaceWithFormatFrom(pixels, width, height, 0, pitch, format) -#define PG_ConvertSurface(src, fmt) SDL_ConvertSurface(src, fmt, 0) #define PG_ConvertSurfaceFormat(src, pixel_format) \ SDL_ConvertSurfaceFormat(src, pixel_format, 0) +static inline SDL_Surface * +PG_ConvertSurface(SDL_Surface *surface, SDL_Surface *dst) +{ + return SDL_ConvertSurface(surface, dst->format, 0); +} + #define PG_PixelFormatEnum SDL_PixelFormatEnum #define PG_SoftStretchNearest(src, srcrect, dst, dstrect) \ diff --git a/src_c/draw.c b/src_c/draw.c index 7684d19493..5c682bcc4c 100644 --- a/src_c/draw.c +++ b/src_c/draw.c @@ -1334,7 +1334,7 @@ flood_fill(PyObject *self, PyObject *arg, PyObject *kwargs) if (pgSurface_Check(colorobj)) { pat_surfobj = ((pgSurfaceObject *)colorobj); - pattern = PG_ConvertSurface(pat_surfobj->surf, surf->format); + pattern = PG_ConvertSurface(pat_surfobj->surf, surf); if (pattern == NULL) { return RAISE(PyExc_RuntimeError, "error converting pattern surf"); diff --git a/src_c/pixelarray_methods.c b/src_c/pixelarray_methods.c index 3663ab30ae..56ba27025e 100644 --- a/src_c/pixelarray_methods.c +++ b/src_c/pixelarray_methods.c @@ -137,7 +137,7 @@ _make_surface(pgPixelArrayObject *array, PyObject *args) } /* Ensure the new surface has the same format as the original */ - new_surf = PG_ConvertSurface(temp_surf, surf->format); + new_surf = PG_ConvertSurface(temp_surf, surf); if (temp_surf != surf) { SDL_FreeSurface(temp_surf); } diff --git a/src_c/surface.c b/src_c/surface.c index 59bb6b9a9f..d108a3bee5 100644 --- a/src_c/surface.c +++ b/src_c/surface.c @@ -1620,7 +1620,7 @@ surf_copy(pgSurfaceObject *self, PyObject *_null) SURF_INIT_CHECK(surf) pgSurface_Prep(self); - newsurf = PG_ConvertSurface(surf, surf->format); + newsurf = PG_ConvertSurface(surf, surf); pgSurface_Unprep(self); final = surf_subtype_new(Py_TYPE(self), newsurf, 1); @@ -1680,7 +1680,7 @@ surf_convert(pgSurfaceObject *self, PyObject *args) if (argobject) { if (pgSurface_Check(argobject)) { src = pgSurface_AsSurface(argobject); - newsurf = PG_ConvertSurface(surf, src->format); + newsurf = PG_ConvertSurface(surf, src); } else { /* will be updated later, initialize to make static analyzer happy @@ -1834,7 +1834,7 @@ surf_convert(pgSurfaceObject *self, PyObject *args) if (argobject) { if (pgSurface_Check(argobject)) { src = pgSurface_AsSurface(argobject); - newsurf = PG_ConvertSurface(surf, src->format); + newsurf = PG_ConvertSurface(surf, src); } else { /* will be updated later, initialize to make static analyzer happy @@ -1951,7 +1951,7 @@ surf_convert(pgSurfaceObject *self, PyObject *args) SDL_SetPixelFormatPalette(format, palette); } } - newsurf = PG_ConvertSurface(surf, format); + newsurf = SDL_ConvertSurface(surf, format, 0); SDL_SetSurfaceBlendMode(newsurf, SDL_BLENDMODE_NONE); SDL_FreeFormat(format); SDL_FreePalette(palette); @@ -3753,7 +3753,7 @@ surf_premul_alpha(pgSurfaceObject *self, PyObject *_null) pgSurface_Prep(self); // Make a copy of the surface first - newsurf = PG_ConvertSurface(surf, surf->format); + newsurf = PG_ConvertSurface(surf, surf); if ((surf->w > 0 && surf->h > 0)) { // If the surface has no pixels we don't need to premul