Skip to content

Commit 5729e4f

Browse files
committed
A few surface.c SDL3 fixes
1 parent e84b4be commit 5729e4f

File tree

1 file changed

+54
-12
lines changed

1 file changed

+54
-12
lines changed

src_c/surface.c

+54-12
Original file line numberDiff line numberDiff line change
@@ -1054,7 +1054,7 @@ surf_get_palette(PyObject *self, PyObject *_null)
10541054

10551055
SURF_INIT_CHECK(surf)
10561056

1057-
pal = surf->format->palette;
1057+
pal = PG_GetSurfacePalette(surf);
10581058

10591059
if (!pal)
10601060
return RAISE(pgExc_SDLError, "Surface has no palette to get\n");
@@ -1093,7 +1093,7 @@ surf_get_palette_at(PyObject *self, PyObject *args)
10931093
return NULL;
10941094
SURF_INIT_CHECK(surf)
10951095

1096-
pal = surf->format->palette;
1096+
pal = PG_GetSurfacePalette(surf);
10971097

10981098
if (!pal)
10991099
return RAISE(pgExc_SDLError, "Surface has no palette to set\n");
@@ -1132,7 +1132,7 @@ surf_set_palette(PyObject *self, PyObject *seq)
11321132
if (!PySequence_Check(seq))
11331133
return RAISE(PyExc_ValueError, "Argument must be a sequence type");
11341134

1135-
pal = surf->format->palette;
1135+
pal = PG_GetSurfacePalette(surf);
11361136

11371137
if (!SDL_ISPIXELFORMAT_INDEXED(PG_SURF_FORMATENUM(surf)))
11381138
return RAISE(pgExc_SDLError, "Surface colors are not indexed\n");
@@ -1192,7 +1192,7 @@ surf_set_palette_at(PyObject *self, PyObject *args)
11921192
if (!SDL_ISPIXELFORMAT_INDEXED(PG_SURF_FORMATENUM(surf)))
11931193
return RAISE(pgExc_SDLError, "Surface colors are not indexed\n");
11941194

1195-
pal = surf->format->palette;
1195+
pal = PG_GetSurfacePalette(surf);
11961196

11971197
if (!pal) {
11981198
return RAISE(pgExc_SDLError, "Surface is not palettized\n");
@@ -2485,7 +2485,7 @@ surf_scroll(PyObject *self, PyObject *args, PyObject *keywds)
24852485
int dx = 0, dy = 0, scroll_flag = PGS_SCROLL_DEFAULT;
24862486
int erase = 0, repeat = 0;
24872487
SDL_Surface *surf;
2488-
SDL_Rect *clip_rect, work_rect;
2488+
SDL_Rect work_rect;
24892489
int w = 0, h = 0, x = 0, y = 0;
24902490

24912491
static char *kwids[] = {"dx", "dy", "scroll_flag", NULL};
@@ -2517,11 +2517,15 @@ surf_scroll(PyObject *self, PyObject *args, PyObject *keywds)
25172517
}
25182518
}
25192519

2520-
clip_rect = &surf->clip_rect;
2520+
SDL_Rect clip_rect;
2521+
if (!PG_GetSurfaceClipRect(surf, &clip_rect)) {
2522+
return RAISE(pgExc_SDLError, SDL_GetError());
2523+
}
2524+
25212525
SDL_Rect surf_rect = {0, 0, surf->w, surf->h};
25222526

25232527
// In SDL3, SDL_IntersectRect is renamed to SDL_GetRectIntersection
2524-
if (!SDL_IntersectRect(clip_rect, &surf_rect, &work_rect)) {
2528+
if (!SDL_IntersectRect(&clip_rect, &surf_rect, &work_rect)) {
25252529
Py_RETURN_NONE;
25262530
}
25272531

@@ -2578,7 +2582,11 @@ static PyObject *
25782582
surf_get_flags(PyObject *self, PyObject *_null)
25792583
{
25802584
Uint32 sdl_flags = 0;
2585+
#if SDL_VERSION_ATLEAST(3, 0, 0)
2586+
SDL_WindowFlags window_flags = 0;
2587+
#else
25812588
Uint32 window_flags = 0;
2589+
#endif
25822590
Uint32 flags = 0;
25832591
int is_alpha;
25842592
int is_window_surf = 0;
@@ -3567,6 +3575,12 @@ _get_buffer_3D(PyObject *obj, Py_buffer *view_p, int flags)
35673575
"A 3D surface view is not contiguous");
35683576
return -1;
35693577
}
3578+
PG_PixelFormat *surface_format = PG_GetSurfaceFormat(surface);
3579+
if (surface_format == NULL) {
3580+
PyErr_SetString(pgExc_SDLError, SDL_GetError());
3581+
return -1;
3582+
}
3583+
35703584
if (_init_buffer(obj, view_p, flags)) {
35713585
return -1;
35723586
}
@@ -3582,7 +3596,7 @@ _get_buffer_3D(PyObject *obj, Py_buffer *view_p, int flags)
35823596
view_p->shape[2] = 3;
35833597
view_p->strides[0] = pixelsize;
35843598
view_p->strides[1] = surface->pitch;
3585-
switch (surface->format->Rmask) {
3599+
switch (surface_format->Rmask) {
35863600
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
35873601
case 0xffU:
35883602
view_p->strides[2] = 1;
@@ -3628,29 +3642,57 @@ _get_buffer_3D(PyObject *obj, Py_buffer *view_p, int flags)
36283642
static int
36293643
_get_buffer_red(PyObject *obj, Py_buffer *view_p, int flags)
36303644
{
3645+
PG_PixelFormat *surface_format =
3646+
PG_GetSurfaceFormat(pgSurface_AsSurface(obj));
3647+
if (surface_format == NULL) {
3648+
PyErr_SetString(pgExc_SDLError, SDL_GetError());
3649+
return -1;
3650+
}
3651+
36313652
return _get_buffer_colorplane(obj, view_p, flags, "red",
3632-
pgSurface_AsSurface(obj)->format->Rmask);
3653+
surface_format->Rmask);
36333654
}
36343655

36353656
static int
36363657
_get_buffer_green(PyObject *obj, Py_buffer *view_p, int flags)
36373658
{
3659+
PG_PixelFormat *surface_format =
3660+
PG_GetSurfaceFormat(pgSurface_AsSurface(obj));
3661+
if (surface_format == NULL) {
3662+
PyErr_SetString(pgExc_SDLError, SDL_GetError());
3663+
return -1;
3664+
}
3665+
36383666
return _get_buffer_colorplane(obj, view_p, flags, "green",
3639-
pgSurface_AsSurface(obj)->format->Gmask);
3667+
surface_format->Gmask);
36403668
}
36413669

36423670
static int
36433671
_get_buffer_blue(PyObject *obj, Py_buffer *view_p, int flags)
36443672
{
3673+
PG_PixelFormat *surface_format =
3674+
PG_GetSurfaceFormat(pgSurface_AsSurface(obj));
3675+
if (surface_format == NULL) {
3676+
PyErr_SetString(pgExc_SDLError, SDL_GetError());
3677+
return -1;
3678+
}
3679+
36453680
return _get_buffer_colorplane(obj, view_p, flags, "blue",
3646-
pgSurface_AsSurface(obj)->format->Bmask);
3681+
surface_format->Bmask);
36473682
}
36483683

36493684
static int
36503685
_get_buffer_alpha(PyObject *obj, Py_buffer *view_p, int flags)
36513686
{
3687+
PG_PixelFormat *surface_format =
3688+
PG_GetSurfaceFormat(pgSurface_AsSurface(obj));
3689+
if (surface_format == NULL) {
3690+
PyErr_SetString(pgExc_SDLError, SDL_GetError());
3691+
return -1;
3692+
}
3693+
36523694
return _get_buffer_colorplane(obj, view_p, flags, "alpha",
3653-
pgSurface_AsSurface(obj)->format->Amask);
3695+
surface_format->Amask);
36543696
}
36553697

36563698
static int

0 commit comments

Comments
 (0)