Skip to content

Commit ec2bdd0

Browse files
committed
Pixelarray SDL3
1 parent de6a74d commit ec2bdd0

File tree

3 files changed

+135
-78
lines changed

3 files changed

+135
-78
lines changed

src_c/meson.build

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,6 @@ bufferproxy = py.extension_module(
249249
subdir: pg,
250250
)
251251

252-
# TODO: support SDL3
253-
if sdl_api != 3
254252
pixelarray = py.extension_module(
255253
'pixelarray',
256254
'pixelarray.c',
@@ -259,7 +257,6 @@ pixelarray = py.extension_module(
259257
install: true,
260258
subdir: pg,
261259
)
262-
endif
263260

264261
math = py.extension_module(
265262
'math',

src_c/pixelarray.c

Lines changed: 61 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "pygame.h"
2424

2525
#include "pgcompat.h"
26+
#include <stddef.h>
2627

2728
#include "doc/pixelarray_doc.h"
2829

@@ -971,6 +972,13 @@ _array_assign_array(pgPixelArrayObject *array, Py_ssize_t low, Py_ssize_t high,
971972
return -1;
972973
}
973974

975+
PG_PixelFormat *surf_format = PG_GetSurfaceFormat(surf);
976+
PG_PixelFormat *val_surf_format = PG_GetSurfaceFormat(val_surf);
977+
if (surf_format == NULL || val_surf_format == NULL) {
978+
PyErr_SetString(pgExc_SDLError, SDL_GetError());
979+
return -1;
980+
}
981+
974982
/* If we reassign the same array, we need to copy the pixels
975983
* first. */
976984
if (SURFACE_EQUALS(array, val)) {
@@ -1021,20 +1029,23 @@ _array_assign_array(pgPixelArrayObject *array, Py_ssize_t low, Py_ssize_t high,
10211029
}
10221030
break;
10231031
case 3: {
1032+
// Note:
1033+
// Why is the 24 bit case pixelformat aware but none of the rest are?
1034+
// - Starbuck, jan. 2025
10241035
#if (SDL_BYTEORDER == SDL_LIL_ENDIAN)
1025-
Uint32 Roffset = surf->format->Rshift >> 3;
1026-
Uint32 Goffset = surf->format->Gshift >> 3;
1027-
Uint32 Boffset = surf->format->Bshift >> 3;
1028-
Uint32 vRoffset = val_surf->format->Rshift >> 3;
1029-
Uint32 vGoffset = val_surf->format->Gshift >> 3;
1030-
Uint32 vBoffset = val_surf->format->Bshift >> 3;
1036+
Uint32 Roffset = surf_format->Rshift >> 3;
1037+
Uint32 Goffset = surf_format->Gshift >> 3;
1038+
Uint32 Boffset = surf_format->Bshift >> 3;
1039+
Uint32 vRoffset = val_surf_format->Rshift >> 3;
1040+
Uint32 vGoffset = val_surf_format->Gshift >> 3;
1041+
Uint32 vBoffset = val_surf_format->Bshift >> 3;
10311042
#else
1032-
Uint32 Roffset = 2 - (surf->format->Rshift >> 3);
1033-
Uint32 Goffset = 2 - (surf->format->Gshift >> 3);
1034-
Uint32 Boffset = 2 - (surf->format->Bshift >> 3);
1035-
Uint32 vRoffset = 2 - (val_surf->format->Rshift >> 3);
1036-
Uint32 vGoffset = 2 - (val_surf->format->Gshift >> 3);
1037-
Uint32 vBoffset = 2 - (val_surf->format->Bshift >> 3);
1043+
Uint32 Roffset = 2 - (surf_format->Rshift >> 3);
1044+
Uint32 Goffset = 2 - (surf_format->Gshift >> 3);
1045+
Uint32 Boffset = 2 - (surf_format->Bshift >> 3);
1046+
Uint32 vRoffset = 2 - (val_surf_format->Rshift >> 3);
1047+
Uint32 vGoffset = 2 - (val_surf_format->Gshift >> 3);
1048+
Uint32 vBoffset = 2 - (val_surf_format->Bshift >> 3);
10381049
#endif
10391050
for (y = 0; y < dim1; ++y) {
10401051
pixel_p = pixelrow;
@@ -1076,7 +1087,6 @@ _array_assign_sequence(pgPixelArrayObject *array, Py_ssize_t low,
10761087
Py_ssize_t high, PyObject *val)
10771088
{
10781089
SDL_Surface *surf = pgSurface_AsSurface(array->surface);
1079-
SDL_PixelFormat *format;
10801090
Py_ssize_t dim0 = ABS(high - low);
10811091
Py_ssize_t dim1 = array->shape[1];
10821092
Py_ssize_t stride0 = high >= low ? array->strides[0] : -array->strides[0];
@@ -1097,8 +1107,13 @@ _array_assign_sequence(pgPixelArrayObject *array, Py_ssize_t low,
10971107
return -1;
10981108
}
10991109

1100-
format = surf->format;
1101-
bpp = PG_FORMAT_BytesPerPixel(format);
1110+
PG_PixelFormat *surf_format = PG_GetSurfaceFormat(surf);
1111+
if (surf_format == NULL) {
1112+
PyErr_SetString(pgExc_SDLError, SDL_GetError());
1113+
return -1;
1114+
}
1115+
1116+
bpp = PG_FORMAT_BytesPerPixel(surf_format);
11021117

11031118
if (!dim1) {
11041119
dim1 = 1;
@@ -1150,13 +1165,13 @@ _array_assign_sequence(pgPixelArrayObject *array, Py_ssize_t low,
11501165
break;
11511166
case 3: {
11521167
#if (SDL_BYTEORDER == SDL_LIL_ENDIAN)
1153-
Uint32 Roffset = surf->format->Rshift >> 3;
1154-
Uint32 Goffset = surf->format->Gshift >> 3;
1155-
Uint32 Boffset = surf->format->Bshift >> 3;
1168+
Uint32 Roffset = surf_format->Rshift >> 3;
1169+
Uint32 Goffset = surf_format->Gshift >> 3;
1170+
Uint32 Boffset = surf_format->Bshift >> 3;
11561171
#else
1157-
Uint32 Roffset = 2 - (surf->format->Rshift >> 3);
1158-
Uint32 Goffset = 2 - (surf->format->Gshift >> 3);
1159-
Uint32 Boffset = 2 - (surf->format->Bshift >> 3);
1172+
Uint32 Roffset = 2 - (surf_format->Rshift >> 3);
1173+
Uint32 Goffset = 2 - (surf_format->Gshift >> 3);
1174+
Uint32 Boffset = 2 - (surf_format->Bshift >> 3);
11601175
#endif
11611176
for (y = 0; y < dim1; ++y) {
11621177
pixel_p = pixelrow;
@@ -1206,7 +1221,13 @@ _array_assign_slice(pgPixelArrayObject *array, Py_ssize_t low, Py_ssize_t high,
12061221
Py_ssize_t x;
12071222
Py_ssize_t y;
12081223

1209-
bpp = PG_SURF_BytesPerPixel(surf);
1224+
PG_PixelFormat *surf_format = PG_GetSurfaceFormat(surf);
1225+
if (surf_format == NULL) {
1226+
PyErr_SetString(pgExc_SDLError, SDL_GetError());
1227+
return -1;
1228+
}
1229+
1230+
bpp = PG_FORMAT_BytesPerPixel(surf_format);
12101231

12111232
if (!dim1) {
12121233
dim1 = 1;
@@ -1241,13 +1262,13 @@ _array_assign_slice(pgPixelArrayObject *array, Py_ssize_t low, Py_ssize_t high,
12411262
} break;
12421263
case 3: {
12431264
#if (SDL_BYTEORDER == SDL_LIL_ENDIAN)
1244-
Uint32 Roffset = surf->format->Rshift >> 3;
1245-
Uint32 Goffset = surf->format->Gshift >> 3;
1246-
Uint32 Boffset = surf->format->Bshift >> 3;
1265+
Uint32 Roffset = surf_format->Rshift >> 3;
1266+
Uint32 Goffset = surf_format->Gshift >> 3;
1267+
Uint32 Boffset = surf_format->Bshift >> 3;
12471268
#else
1248-
Uint32 Roffset = 2 - (surf->format->Rshift >> 3);
1249-
Uint32 Goffset = 2 - (surf->format->Gshift >> 3);
1250-
Uint32 Boffset = 2 - (surf->format->Bshift >> 3);
1269+
Uint32 Roffset = 2 - (surf_format->Rshift >> 3);
1270+
Uint32 Goffset = 2 - (surf_format->Gshift >> 3);
1271+
Uint32 Boffset = 2 - (surf_format->Bshift >> 3);
12511272
#endif
12521273
Uint8 r = (Uint8)(color >> 16);
12531274
Uint8 g = (Uint8)(color >> 8);
@@ -1352,6 +1373,12 @@ _pxarray_ass_item(pgPixelArrayObject *array, Py_ssize_t index, PyObject *value)
13521373
dim1 = 1;
13531374
}
13541375

1376+
PG_PixelFormat *surf_format = PG_GetSurfaceFormat(surf);
1377+
if (surf_format == NULL) {
1378+
PyErr_SetString(pgExc_SDLError, SDL_GetError());
1379+
return -1;
1380+
}
1381+
13551382
Py_BEGIN_ALLOW_THREADS;
13561383
/* Single value assignment. */
13571384
switch (bpp) {
@@ -1369,13 +1396,13 @@ _pxarray_ass_item(pgPixelArrayObject *array, Py_ssize_t index, PyObject *value)
13691396
break;
13701397
case 3: {
13711398
#if (SDL_BYTEORDER == SDL_LIL_ENDIAN)
1372-
Uint32 Roffset = surf->format->Rshift >> 3;
1373-
Uint32 Goffset = surf->format->Gshift >> 3;
1374-
Uint32 Boffset = surf->format->Bshift >> 3;
1399+
Uint32 Roffset = surf_format->Rshift >> 3;
1400+
Uint32 Goffset = surf_format->Gshift >> 3;
1401+
Uint32 Boffset = surf_format->Bshift >> 3;
13751402
#else
1376-
Uint32 Roffset = 2 - (surf->format->Rshift >> 3);
1377-
Uint32 Goffset = 2 - (surf->format->Gshift >> 3);
1378-
Uint32 Boffset = 2 - (surf->format->Bshift >> 3);
1403+
Uint32 Roffset = 2 - (surf_format->Rshift >> 3);
1404+
Uint32 Goffset = 2 - (surf_format->Gshift >> 3);
1405+
Uint32 Boffset = 2 - (surf_format->Bshift >> 3);
13791406
#endif
13801407
for (y = 0; y < dim1; ++y) {
13811408
pixel_p[Roffset] = (Uint8)(color >> 16);

0 commit comments

Comments
 (0)