Skip to content

Commit f4fb43e

Browse files
committed
Use SDL_AllocFormat instead of creating it manually
According to the docs, `SDL_PixelFormat` is a read-only structure. Creating it manually leaves out some important fields like `format` and `next` pointer to be undefined. Signed-off-by: Marcin Serwin <marcin@serwin.dev>
1 parent a966d54 commit f4fb43e

File tree

1 file changed

+6
-22
lines changed

1 file changed

+6
-22
lines changed

src_c/surface.c

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4540,29 +4540,13 @@ pgSurface_Blit(pgSurfaceObject *dstobj, pgSurfaceObject *srcobj,
45404540
}
45414541
else {
45424542
SDL_PixelFormat *fmt = src->format;
4543-
SDL_PixelFormat newfmt;
4543+
SDL_PixelFormat *newfmt =
4544+
SDL_AllocFormat(SDL_MasksToPixelFormatEnum(
4545+
fmt->BitsPerPixel, fmt->Rmask, fmt->Gmask, fmt->Bmask, 0));
45444546

4545-
newfmt.palette = 0; /* Set NULL (or SDL gets confused) */
4546-
#if SDL_VERSION_ATLEAST(3, 0, 0)
4547-
newfmt.bits_per_pixel = fmt->bits_per_pixel;
4548-
newfmt.bytes_per_pixel = fmt->bytes_per_pixel;
4549-
#else
4550-
newfmt.BitsPerPixel = fmt->BitsPerPixel;
4551-
newfmt.BytesPerPixel = fmt->BytesPerPixel;
4552-
#endif
4553-
newfmt.Amask = 0;
4554-
newfmt.Rmask = fmt->Rmask;
4555-
newfmt.Gmask = fmt->Gmask;
4556-
newfmt.Bmask = fmt->Bmask;
4557-
newfmt.Ashift = 0;
4558-
newfmt.Rshift = fmt->Rshift;
4559-
newfmt.Gshift = fmt->Gshift;
4560-
newfmt.Bshift = fmt->Bshift;
4561-
newfmt.Aloss = 0;
4562-
newfmt.Rloss = fmt->Rloss;
4563-
newfmt.Gloss = fmt->Gloss;
4564-
newfmt.Bloss = fmt->Bloss;
4565-
src = PG_ConvertSurface(src, &newfmt);
4547+
src = PG_ConvertSurface(src, newfmt);
4548+
4549+
SDL_FreeFormat(newfmt);
45664550
if (src) {
45674551
#if SDL_VERSION_ATLEAST(3, 0, 0)
45684552
result = SDL_BlitSurface(src, srcrect, dst, dstrect) ? 0 : -1;

0 commit comments

Comments
 (0)