pygame-ce
pygame-ce copied to clipboard
Restructure and port surface init and convert to SDL3
Surface init--
# Before, each codepath would output bpp and masks, which was then used to construct a format enum.
depth + masks -> bpp and masks
depth -> bpp + default masks
surface -> format -> bpp and masks
default window surface -> format -> bpp and masks
# Now, each codepath outputs a format enum itself.
depth + masks -> format enum
depth -> default format enum
surface -> format enum
default window surface -> format enum
Why change this now? It's not just that it's nicer, one of the old code paths used an SDL_PixelFormat as a writeable thing, and since that no longer exists the code could not be directly ported.
Surface convert--
Surface.convert had a similar thing but even more dubious. The code memcpy(&format, surf->format, sizeof(format)); does not seem particularly safe! These objects contain internal refcounts, internal references.
It used a bunch of the same default masks, so just like surface init I translated them all into pixel format enums. This eliminates some logic around building formats up, although I did still have to build a format struct in the SDL2 code path to convert to a surface with a palette. In SDL3 I had access to a function that just took a pixelformat enum and a palette, like all the SDL3 things, so I used that instead.