Skip to content

Commit 5728de9

Browse files
authored
Merge pull request #3344 from Starbuck5/surface-sdl3-a-bit-more
A few surface.c SDL3 fixes
2 parents 7b05323 + 5729e4f commit 5728de9

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;
@@ -3575,6 +3583,12 @@ _get_buffer_3D(PyObject *obj, Py_buffer *view_p, int flags)
35753583
"A 3D surface view is not contiguous");
35763584
return -1;
35773585
}
3586+
PG_PixelFormat *surface_format = PG_GetSurfaceFormat(surface);
3587+
if (surface_format == NULL) {
3588+
PyErr_SetString(pgExc_SDLError, SDL_GetError());
3589+
return -1;
3590+
}
3591+
35783592
if (_init_buffer(obj, view_p, flags)) {
35793593
return -1;
35803594
}
@@ -3590,7 +3604,7 @@ _get_buffer_3D(PyObject *obj, Py_buffer *view_p, int flags)
35903604
view_p->shape[2] = 3;
35913605
view_p->strides[0] = pixelsize;
35923606
view_p->strides[1] = surface->pitch;
3593-
switch (surface->format->Rmask) {
3607+
switch (surface_format->Rmask) {
35943608
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
35953609
case 0xffU:
35963610
view_p->strides[2] = 1;
@@ -3636,29 +3650,57 @@ _get_buffer_3D(PyObject *obj, Py_buffer *view_p, int flags)
36363650
static int
36373651
_get_buffer_red(PyObject *obj, Py_buffer *view_p, int flags)
36383652
{
3653+
PG_PixelFormat *surface_format =
3654+
PG_GetSurfaceFormat(pgSurface_AsSurface(obj));
3655+
if (surface_format == NULL) {
3656+
PyErr_SetString(pgExc_SDLError, SDL_GetError());
3657+
return -1;
3658+
}
3659+
36393660
return _get_buffer_colorplane(obj, view_p, flags, "red",
3640-
pgSurface_AsSurface(obj)->format->Rmask);
3661+
surface_format->Rmask);
36413662
}
36423663

36433664
static int
36443665
_get_buffer_green(PyObject *obj, Py_buffer *view_p, int flags)
36453666
{
3667+
PG_PixelFormat *surface_format =
3668+
PG_GetSurfaceFormat(pgSurface_AsSurface(obj));
3669+
if (surface_format == NULL) {
3670+
PyErr_SetString(pgExc_SDLError, SDL_GetError());
3671+
return -1;
3672+
}
3673+
36463674
return _get_buffer_colorplane(obj, view_p, flags, "green",
3647-
pgSurface_AsSurface(obj)->format->Gmask);
3675+
surface_format->Gmask);
36483676
}
36493677

36503678
static int
36513679
_get_buffer_blue(PyObject *obj, Py_buffer *view_p, int flags)
36523680
{
3681+
PG_PixelFormat *surface_format =
3682+
PG_GetSurfaceFormat(pgSurface_AsSurface(obj));
3683+
if (surface_format == NULL) {
3684+
PyErr_SetString(pgExc_SDLError, SDL_GetError());
3685+
return -1;
3686+
}
3687+
36533688
return _get_buffer_colorplane(obj, view_p, flags, "blue",
3654-
pgSurface_AsSurface(obj)->format->Bmask);
3689+
surface_format->Bmask);
36553690
}
36563691

36573692
static int
36583693
_get_buffer_alpha(PyObject *obj, Py_buffer *view_p, int flags)
36593694
{
3695+
PG_PixelFormat *surface_format =
3696+
PG_GetSurfaceFormat(pgSurface_AsSurface(obj));
3697+
if (surface_format == NULL) {
3698+
PyErr_SetString(pgExc_SDLError, SDL_GetError());
3699+
return -1;
3700+
}
3701+
36603702
return _get_buffer_colorplane(obj, view_p, flags, "alpha",
3661-
pgSurface_AsSurface(obj)->format->Amask);
3703+
surface_format->Amask);
36623704
}
36633705

36643706
static int

0 commit comments

Comments
 (0)