Skip to content

Commit 6e70144

Browse files
committed
Fixes for rwobject (now iostream) SDL3
1 parent a95e809 commit 6e70144

File tree

5 files changed

+137
-3
lines changed

5 files changed

+137
-3
lines changed

src_c/font.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,15 +1187,19 @@ font_init(PyFontObject *self, PyObject *args, PyObject *kwds)
11871187
if (fontsize <= 1)
11881188
fontsize = 1;
11891189

1190-
if (rw->size(rw) <= 0) {
1190+
if (SDL_RWsize(rw) <= 0) {
11911191
PyErr_Format(PyExc_ValueError,
11921192
"Font file object has an invalid file size: %lld",
1193-
rw->size(rw));
1193+
SDL_RWsize(rw));
11941194
goto error;
11951195
}
11961196

11971197
Py_BEGIN_ALLOW_THREADS;
1198+
#if SDL_VERSION_ATLEAST(3, 0, 0)
1199+
font = TTF_OpenFontIO(rw, 1, fontsize);
1200+
#else
11981201
font = TTF_OpenFontRW(rw, 1, fontsize);
1202+
#endif
11991203
Py_END_ALLOW_THREADS;
12001204

12011205
Py_DECREF(obj);

src_c/imageext.c

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,18 @@ image_load_ext(PyObject *self, PyObject *arg, PyObject *kwarg)
117117
SDL_UnlockMutex(_pg_img_mutex);
118118
*/
119119

120+
#if SDL_VERSION_ATLEAST(3, 0, 0)
121+
surf = IMG_LoadTyped_IO(rw, 1, type);
122+
#else
120123
surf = IMG_LoadTyped_RW(rw, 1, type);
124+
#endif
121125
Py_END_ALLOW_THREADS;
122-
#else /* ~WITH_THREAD */
126+
#else /* ~WITH_THREAD */
127+
#if SDL_VERSION_ATLEAST(3, 0, 0)
128+
surf = IMG_LoadTyped_IO(rw, 1, type);
129+
#else
123130
surf = IMG_LoadTyped_RW(rw, 1, type);
131+
#endif
124132
#endif /* ~WITH_THREAD */
125133

126134
if (ext) {
@@ -166,7 +174,11 @@ imageext_load_sized_svg(PyObject *self, PyObject *arg, PyObject *kwargs)
166174
}
167175

168176
Py_BEGIN_ALLOW_THREADS;
177+
#if SDL_VERSION_ATLEAST(3, 0, 0)
178+
surf = IMG_LoadSizedSVG_IO(rw, width, height);
179+
#else
169180
surf = IMG_LoadSizedSVG_RW(rw, width, height);
181+
#endif
170182
SDL_RWclose(rw);
171183
Py_END_ALLOW_THREADS;
172184
if (surf == NULL) {
@@ -231,7 +243,11 @@ image_save_ext(PyObject *self, PyObject *arg, PyObject *kwarg)
231243
char *ext = iext_find_extension(name);
232244
if (!strcasecmp(ext, "jpeg") || !strcasecmp(ext, "jpg")) {
233245
if (rw != NULL) {
246+
#if SDL_VERSION_ATLEAST(3, 0, 0)
247+
result = IMG_SaveJPG_IO(surf, rw, 0, JPEG_QUALITY);
248+
#else
234249
result = IMG_SaveJPG_RW(surf, rw, 0, JPEG_QUALITY);
250+
#endif
235251
}
236252
else {
237253
result = IMG_SaveJPG(surf, name, JPEG_QUALITY);
@@ -240,7 +256,11 @@ image_save_ext(PyObject *self, PyObject *arg, PyObject *kwarg)
240256
else if (!strcasecmp(ext, "png")) {
241257
/*Py_BEGIN_ALLOW_THREADS; */
242258
if (rw != NULL) {
259+
#if SDL_VERSION_ATLEAST(3, 0, 0)
260+
result = IMG_SavePNG_IO(surf, rw, 0);
261+
#else
243262
result = IMG_SavePNG_RW(surf, rw, 0);
263+
#endif
244264
}
245265
else {
246266
result = IMG_SavePNG(surf, name);

src_c/mixer.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1869,7 +1869,11 @@ sound_init(PyObject *self, PyObject *arg, PyObject *kwarg)
18691869
return -1;
18701870
}
18711871
Py_BEGIN_ALLOW_THREADS;
1872+
#if SDL_VERSION_ATLEAST(3, 0, 0)
1873+
chunk = Mix_LoadWAV_IO(rw, 1);
1874+
#else
18721875
chunk = Mix_LoadWAV_RW(rw, 1);
1876+
#endif
18731877
Py_END_ALLOW_THREADS;
18741878
if (chunk == NULL) {
18751879
PyErr_SetString(pgExc_SDLError, SDL_GetError());

src_c/music.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,11 @@ _load_music(PyObject *obj, char *namehint)
388388
}
389389

390390
Py_BEGIN_ALLOW_THREADS;
391+
#if SDL_VERSION_ATLEAST(3, 0, 0)
392+
new_music = Mix_LoadMUSType_IO(rw, _get_type_from_hint(type), SDL_TRUE);
393+
#else
391394
new_music = Mix_LoadMUSType_RW(rw, _get_type_from_hint(type), SDL_TRUE);
395+
#endif
392396
Py_END_ALLOW_THREADS;
393397

394398
if (ext) {

src_c/rwobject.c

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,18 @@ static const char pg_default_errors[] = "backslashreplace";
4646

4747
static PyObject *os_module = NULL;
4848

49+
#if SDL_VERSION_ATLEAST(3, 0, 0)
50+
static Sint64
51+
_pg_rw_size(void *);
52+
static Sint64
53+
_pg_rw_seek(void *, Sint64, SDL_IOWhence);
54+
static size_t
55+
_pg_rw_read(void *, void *, size_t, SDL_IOStatus *);
56+
static size_t
57+
_pg_rw_write(void *, const void *, size_t, SDL_IOStatus *);
58+
static int
59+
_pg_rw_close(void *);
60+
#else
4961
static Sint64
5062
_pg_rw_size(SDL_RWops *);
5163
static Sint64
@@ -56,6 +68,7 @@ static size_t
5668
_pg_rw_write(SDL_RWops *, const void *, size_t, size_t);
5769
static int
5870
_pg_rw_close(SDL_RWops *);
71+
#endif
5972

6073
/* Converter function used by PyArg_ParseTupleAndKeywords with the "O&" format.
6174
*
@@ -291,13 +304,31 @@ pg_EncodeFilePath(PyObject *obj, PyObject *eclass)
291304
static int
292305
pgRWops_IsFileObject(SDL_RWops *rw)
293306
{
307+
#if SDL_VERSION_ATLEAST(3, 0, 0)
308+
SDL_PropertiesID props = SDL_GetIOProperties(rw);
309+
if (!props) {
310+
// pgRWops_IsFileObject doesn't have any error checking facility
311+
// so when in doubt let's say it isn't a file object.
312+
return 0;
313+
}
314+
return SDL_GetBooleanProperty(props, "_pygame_is_file_object", 0);
315+
#else
294316
return rw->close == _pg_rw_close;
317+
#endif
295318
}
296319

320+
#if SDL_VERSION_ATLEAST(3, 0, 0)
321+
static Sint64
322+
_pg_rw_size(void *userdata)
323+
{
324+
pgRWHelper *helper = (pgRWHelper *)userdata;
325+
#else
297326
static Sint64
298327
_pg_rw_size(SDL_RWops *context)
299328
{
300329
pgRWHelper *helper = (pgRWHelper *)context->hidden.unknown.data1;
330+
#endif
331+
301332
PyObject *pos = NULL;
302333
PyObject *tmp = NULL;
303334
Sint64 size;
@@ -361,10 +392,19 @@ _pg_rw_size(SDL_RWops *context)
361392
return retval;
362393
}
363394

395+
#if SDL_VERSION_ATLEAST(3, 0, 0)
396+
static size_t
397+
_pg_rw_write(void *userdata, const void *ptr, size_t size,
398+
SDL_IOStatus *status)
399+
{
400+
pgRWHelper *helper = (pgRWHelper *)userdata;
401+
size_t num = 1;
402+
#else
364403
static size_t
365404
_pg_rw_write(SDL_RWops *context, const void *ptr, size_t size, size_t num)
366405
{
367406
pgRWHelper *helper = (pgRWHelper *)context->hidden.unknown.data1;
407+
#endif
368408
PyObject *result;
369409
size_t retval;
370410

@@ -382,17 +422,28 @@ _pg_rw_write(SDL_RWops *context, const void *ptr, size_t size, size_t num)
382422
}
383423

384424
Py_DECREF(result);
425+
#if SDL_VERSION_ATLEAST(3, 0, 0)
426+
retval = size;
427+
#else
385428
retval = num;
429+
#endif
386430

387431
end:
388432
PyGILState_Release(state);
389433
return retval;
390434
}
391435

436+
#if SDL_VERSION_ATLEAST(3, 0, 0)
437+
static int
438+
_pg_rw_close(void *userdata)
439+
{
440+
pgRWHelper *helper = (pgRWHelper *)userdata;
441+
#else
392442
static int
393443
_pg_rw_close(SDL_RWops *context)
394444
{
395445
pgRWHelper *helper = (pgRWHelper *)context->hidden.unknown.data1;
446+
#endif
396447
PyObject *result;
397448
int retval = 0;
398449
PyGILState_STATE state = PyGILState_Ensure();
@@ -414,7 +465,9 @@ _pg_rw_close(SDL_RWops *context)
414465

415466
PyMem_Free(helper);
416467
PyGILState_Release(state);
468+
#if !SDL_VERSION_ATLEAST(3, 0, 0)
417469
SDL_FreeRW(context);
470+
#endif
418471
return retval;
419472
}
420473

@@ -437,6 +490,37 @@ pgRWops_FromFileObject(PyObject *obj)
437490
return NULL;
438491
}
439492

493+
#if SDL_VERSION_ATLEAST(3, 0, 0)
494+
SDL_IOStreamInterface iface;
495+
iface.size = _pg_rw_size;
496+
iface.seek = _pg_rw_seek;
497+
iface.read = _pg_rw_read;
498+
iface.write = _pg_rw_write;
499+
iface.close = _pg_rw_close;
500+
501+
// TODO: These should raise SDLError probably?
502+
// rwobject.c hasn't required pygame.base before (the source of SDLError)
503+
// so omitting that for now.
504+
505+
rw = SDL_OpenIO(&iface, helper);
506+
if (rw == NULL) {
507+
iface.close(helper);
508+
PyMem_Free(helper);
509+
return (SDL_RWops *)RAISE(PyExc_IOError, SDL_GetError());
510+
}
511+
512+
SDL_PropertiesID props = SDL_GetIOProperties(rw);
513+
if (!props) {
514+
PyMem_Free(helper);
515+
return (SDL_RWops *)RAISE(PyExc_IOError, SDL_GetError());
516+
}
517+
518+
if (SDL_SetBooleanProperty(props, "_pygame_is_file_object", 1) < 0) {
519+
PyMem_Free(helper);
520+
return (SDL_RWops *)RAISE(PyExc_IOError, SDL_GetError());
521+
}
522+
523+
#else
440524
rw = SDL_AllocRW();
441525
if (rw == NULL) {
442526
PyMem_Free(helper);
@@ -450,14 +534,22 @@ pgRWops_FromFileObject(PyObject *obj)
450534
rw->read = _pg_rw_read;
451535
rw->write = _pg_rw_write;
452536
rw->close = _pg_rw_close;
537+
#endif
453538

454539
return rw;
455540
}
456541

542+
#if SDL_VERSION_ATLEAST(3, 0, 0)
543+
static Sint64
544+
_pg_rw_seek(void *userdata, Sint64 offset, SDL_IOWhence whence)
545+
{
546+
pgRWHelper *helper = (pgRWHelper *)userdata;
547+
#else
457548
static Sint64
458549
_pg_rw_seek(SDL_RWops *context, Sint64 offset, int whence)
459550
{
460551
pgRWHelper *helper = (pgRWHelper *)context->hidden.unknown.data1;
552+
#endif
461553
PyObject *result;
462554
Sint64 retval;
463555

@@ -498,10 +590,18 @@ _pg_rw_seek(SDL_RWops *context, Sint64 offset, int whence)
498590
return retval;
499591
}
500592

593+
#if SDL_VERSION_ATLEAST(3, 0, 0)
594+
static size_t
595+
_pg_rw_read(void *userdata, void *ptr, size_t size, SDL_IOStatus *status)
596+
{
597+
pgRWHelper *helper = (pgRWHelper *)userdata;
598+
size_t maxnum = 1;
599+
#else
501600
static size_t
502601
_pg_rw_read(SDL_RWops *context, void *ptr, size_t size, size_t maxnum)
503602
{
504603
pgRWHelper *helper = (pgRWHelper *)context->hidden.unknown.data1;
604+
#endif
505605
PyObject *result;
506606
Py_ssize_t retval;
507607

@@ -527,7 +627,9 @@ _pg_rw_read(SDL_RWops *context, void *ptr, size_t size, size_t maxnum)
527627
retval = PyBytes_GET_SIZE(result);
528628
if (retval) {
529629
memcpy(ptr, PyBytes_AsString(result), retval);
630+
#if !SDL_VERSION_ATLEAST(3, 0, 0)
530631
retval /= size;
632+
#endif
531633
}
532634

533635
Py_DECREF(result);

0 commit comments

Comments
 (0)