@@ -46,6 +46,18 @@ static const char pg_default_errors[] = "backslashreplace";
46
46
47
47
static PyObject * os_module = NULL ;
48
48
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
49
61
static Sint64
50
62
_pg_rw_size (SDL_RWops * );
51
63
static Sint64
@@ -56,6 +68,7 @@ static size_t
56
68
_pg_rw_write (SDL_RWops * , const void * , size_t , size_t );
57
69
static int
58
70
_pg_rw_close (SDL_RWops * );
71
+ #endif
59
72
60
73
/* Converter function used by PyArg_ParseTupleAndKeywords with the "O&" format.
61
74
*
@@ -291,13 +304,31 @@ pg_EncodeFilePath(PyObject *obj, PyObject *eclass)
291
304
static int
292
305
pgRWops_IsFileObject (SDL_RWops * rw )
293
306
{
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
294
316
return rw -> close == _pg_rw_close ;
317
+ #endif
295
318
}
296
319
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
297
326
static Sint64
298
327
_pg_rw_size (SDL_RWops * context )
299
328
{
300
329
pgRWHelper * helper = (pgRWHelper * )context -> hidden .unknown .data1 ;
330
+ #endif
331
+
301
332
PyObject * pos = NULL ;
302
333
PyObject * tmp = NULL ;
303
334
Sint64 size ;
@@ -361,10 +392,19 @@ _pg_rw_size(SDL_RWops *context)
361
392
return retval ;
362
393
}
363
394
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
364
403
static size_t
365
404
_pg_rw_write (SDL_RWops * context , const void * ptr , size_t size , size_t num )
366
405
{
367
406
pgRWHelper * helper = (pgRWHelper * )context -> hidden .unknown .data1 ;
407
+ #endif
368
408
PyObject * result ;
369
409
size_t retval ;
370
410
@@ -382,17 +422,28 @@ _pg_rw_write(SDL_RWops *context, const void *ptr, size_t size, size_t num)
382
422
}
383
423
384
424
Py_DECREF (result );
425
+ #if SDL_VERSION_ATLEAST (3 , 0 , 0 )
426
+ retval = size ;
427
+ #else
385
428
retval = num ;
429
+ #endif
386
430
387
431
end :
388
432
PyGILState_Release (state );
389
433
return retval ;
390
434
}
391
435
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
392
442
static int
393
443
_pg_rw_close (SDL_RWops * context )
394
444
{
395
445
pgRWHelper * helper = (pgRWHelper * )context -> hidden .unknown .data1 ;
446
+ #endif
396
447
PyObject * result ;
397
448
int retval = 0 ;
398
449
PyGILState_STATE state = PyGILState_Ensure ();
@@ -414,7 +465,9 @@ _pg_rw_close(SDL_RWops *context)
414
465
415
466
PyMem_Free (helper );
416
467
PyGILState_Release (state );
468
+ #if !SDL_VERSION_ATLEAST (3 , 0 , 0 )
417
469
SDL_FreeRW (context );
470
+ #endif
418
471
return retval ;
419
472
}
420
473
@@ -437,6 +490,37 @@ pgRWops_FromFileObject(PyObject *obj)
437
490
return NULL ;
438
491
}
439
492
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
440
524
rw = SDL_AllocRW ();
441
525
if (rw == NULL ) {
442
526
PyMem_Free (helper );
@@ -450,14 +534,22 @@ pgRWops_FromFileObject(PyObject *obj)
450
534
rw -> read = _pg_rw_read ;
451
535
rw -> write = _pg_rw_write ;
452
536
rw -> close = _pg_rw_close ;
537
+ #endif
453
538
454
539
return rw ;
455
540
}
456
541
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
457
548
static Sint64
458
549
_pg_rw_seek (SDL_RWops * context , Sint64 offset , int whence )
459
550
{
460
551
pgRWHelper * helper = (pgRWHelper * )context -> hidden .unknown .data1 ;
552
+ #endif
461
553
PyObject * result ;
462
554
Sint64 retval ;
463
555
@@ -498,10 +590,18 @@ _pg_rw_seek(SDL_RWops *context, Sint64 offset, int whence)
498
590
return retval ;
499
591
}
500
592
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
501
600
static size_t
502
601
_pg_rw_read (SDL_RWops * context , void * ptr , size_t size , size_t maxnum )
503
602
{
504
603
pgRWHelper * helper = (pgRWHelper * )context -> hidden .unknown .data1 ;
604
+ #endif
505
605
PyObject * result ;
506
606
Py_ssize_t retval ;
507
607
@@ -527,7 +627,9 @@ _pg_rw_read(SDL_RWops *context, void *ptr, size_t size, size_t maxnum)
527
627
retval = PyBytes_GET_SIZE (result );
528
628
if (retval ) {
529
629
memcpy (ptr , PyBytes_AsString (result ), retval );
630
+ #if !SDL_VERSION_ATLEAST (3 , 0 , 0 )
530
631
retval /= size ;
632
+ #endif
531
633
}
532
634
533
635
Py_DECREF (result );
0 commit comments