Skip to content

Commit eeec672

Browse files
committed
Fix rwops and some image stuff on SDL3
1 parent b7f0c25 commit eeec672

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

src_c/image.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,14 @@ image_save(PyObject *self, PyObject *arg, PyObject *kwarg)
197197
SDL_RWops *rw = pgRWops_FromFileObject(obj);
198198
if (rw != NULL) {
199199
if (!strcasecmp(ext, "bmp")) {
200+
#if SDL_VERSION_ATLEAST(3, 0, 0)
201+
result = (SDL_SaveBMP_IO(surf, rw, 0) ? 0 : -1);
202+
#else
200203
/* The SDL documentation didn't specify which negative
201204
* number is returned upon error. We want to be sure that
202205
* result is either 0 or -1: */
203206
result = (SDL_SaveBMP_RW(surf, rw, 0) == 0 ? 0 : -1);
207+
#endif
204208
}
205209
else {
206210
result = SaveTGA_RW(surf, rw, 1);
@@ -213,10 +217,14 @@ image_save(PyObject *self, PyObject *arg, PyObject *kwarg)
213217
else {
214218
if (!strcasecmp(ext, "bmp")) {
215219
Py_BEGIN_ALLOW_THREADS;
220+
#if SDL_VERSION_ATLEAST(3, 0, 0)
221+
result = (SDL_SaveBMP(surf, name) ? 0 : -1);
222+
#else
216223
/* The SDL documentation didn't specify which negative number
217224
* is returned upon error. We want to be sure that result is
218225
* either 0 or -1: */
219226
result = (SDL_SaveBMP(surf, name) == 0 ? 0 : -1);
227+
#endif
220228
Py_END_ALLOW_THREADS;
221229
}
222230
else {

src_c/imageext.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,26 +333,34 @@ image_save_ext(PyObject *self, PyObject *arg, PyObject *kwarg)
333333
if (!strcasecmp(ext, "jpeg") || !strcasecmp(ext, "jpg")) {
334334
if (rw != NULL) {
335335
#if SDL_VERSION_ATLEAST(3, 0, 0)
336-
result = IMG_SaveJPG_IO(surf, rw, 0, JPEG_QUALITY);
336+
result = IMG_SaveJPG_IO(surf, rw, 0, JPEG_QUALITY) ? 0 : -1;
337337
#else
338338
result = IMG_SaveJPG_RW(surf, rw, 0, JPEG_QUALITY);
339339
#endif
340340
}
341341
else {
342+
#if SDL_VERSION_ATLEAST(3, 0, 0)
343+
result = IMG_SaveJPG(surf, name, JPEG_QUALITY) ? 0 : -1;
344+
#else
342345
result = IMG_SaveJPG(surf, name, JPEG_QUALITY);
346+
#endif
343347
}
344348
}
345349
else if (!strcasecmp(ext, "png")) {
346350
/*Py_BEGIN_ALLOW_THREADS; */
347351
if (rw != NULL) {
348352
#if SDL_VERSION_ATLEAST(3, 0, 0)
349-
result = IMG_SavePNG_IO(surf, rw, 0);
353+
result = IMG_SavePNG_IO(surf, rw, 0) ? 0 : -1;
350354
#else
351355
result = IMG_SavePNG_RW(surf, rw, 0);
352356
#endif
353357
}
354358
else {
359+
#if SDL_VERSION_ATLEAST(3, 0, 0)
360+
result = IMG_SavePNG(surf, name) ? 0 : -1;
361+
#else
355362
result = IMG_SavePNG(surf, name);
363+
#endif
356364
}
357365
/*Py_END_ALLOW_THREADS; */
358366
}

src_c/rwobject.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,7 @@ pgRWops_FromFileObject(PyObject *obj)
500500

501501
#if SDL_VERSION_ATLEAST(3, 0, 0)
502502
SDL_IOStreamInterface iface;
503+
SDL_INIT_INTERFACE(&iface);
503504
iface.size = _pg_rw_size;
504505
iface.seek = _pg_rw_seek;
505506
iface.read = _pg_rw_read;
@@ -513,24 +514,26 @@ pgRWops_FromFileObject(PyObject *obj)
513514
rw = SDL_OpenIO(&iface, helper);
514515
if (rw == NULL) {
515516
iface.close(helper);
516-
PyMem_Free(helper);
517517
return (SDL_RWops *)RAISE(PyExc_IOError, SDL_GetError());
518518
}
519519

520520
SDL_PropertiesID props = SDL_GetIOProperties(rw);
521521
if (!props) {
522-
PyMem_Free(helper);
522+
iface.close(helper);
523523
return (SDL_RWops *)RAISE(PyExc_IOError, SDL_GetError());
524524
}
525525

526526
if (!SDL_SetBooleanProperty(props, "_pygame_is_file_object", 1)) {
527-
PyMem_Free(helper);
527+
iface.close(helper);
528528
return (SDL_RWops *)RAISE(PyExc_IOError, SDL_GetError());
529529
}
530530

531531
#else
532532
rw = SDL_AllocRW();
533533
if (rw == NULL) {
534+
/* TODO: here member attributes of helper are getting leaked and close
535+
* isn't being called. Refactor to use _pg_rw_close logic on helper
536+
* here */
534537
PyMem_Free(helper);
535538
return (SDL_RWops *)PyErr_NoMemory();
536539
}

0 commit comments

Comments
 (0)