sdl2-compat: update after SDL_rwops read/write function change in SDL3

From 8c6f0539b15a00040d0625ec952e6a3b71a6bb77 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Mon, 7 Aug 2023 21:45:33 -0700
Subject: [PATCH] update after SDL_rwops read/write function change in SDL3

---
 src/sdl2_compat.c | 56 +++++++++++------------------------------------
 src/sdl3_syms.h   | 18 ++-------------
 2 files changed, 15 insertions(+), 59 deletions(-)

diff --git a/src/sdl2_compat.c b/src/sdl2_compat.c
index 5d78b5a..26b4fe0 100644
--- a/src/sdl2_compat.c
+++ b/src/sdl2_compat.c
@@ -1812,43 +1812,15 @@ RWops3to2_seek(SDL2_RWops *rwops2, Sint64 offset, int whence)
 static size_t SDLCALL
 RWops3to2_read(SDL2_RWops *rwops2, void *ptr, size_t size, size_t maxnum)
 {
-    Sint64 br;
-    size_t nmemb;
-    if (!size || !maxnum) {
-        return 0;
-    }
-    br = SDL3_RWread(rwops2->hidden.sdl3.rwops, ptr, ((Sint64) size) * ((Sint64) maxnum));
-    if (br <= 0) {
-        return 0;
-    }
-    nmemb = (size_t) br / size;
-    #if 0
-    if ((size_t) br % size) { /* last member partially read? */
-        nmemb++;
-    }
-    #endif
-    return nmemb;
+    size_t count = SDL3_RWread(rwops2->hidden.sdl3.rwops, ptr, (size * maxnum)) / size;
+    return count;
 }
 
 static size_t SDLCALL
 RWops3to2_write(SDL2_RWops *rwops2, const void *ptr, size_t size, size_t maxnum)
 {
-    Sint64 bw;
-    size_t nmemb;
-    if (!size || !maxnum) {
-        return 0;
-    }
-    bw = SDL3_RWwrite(rwops2->hidden.sdl3.rwops, ptr, ((Sint64) size) * ((Sint64) maxnum));
-    if (bw <= 0) {
-        return 0;
-    }
-    nmemb = (size_t) bw / size;
-    #if 0
-    if ((size_t) bw % size) { /* last member partially written? */
-        nmemb++;
-    }
-    #endif
-    return nmemb;
+    size_t count = SDL3_RWwrite(rwops2->hidden.sdl3.rwops, ptr, (size * maxnum)) / size;
+    return count;
 }
 
 static int SDLCALL
@@ -2113,25 +2085,23 @@ RWops2to3_seek(struct SDL_RWops *rwops3, Sint64 offset, int whence)
     return SDL_RWseek((SDL2_RWops *) rwops3->hidden.unknown.data1, offset, whence);
 }
 
-static Sint64 SDLCALL
-RWops2to3_read(struct SDL_RWops *rwops3, void *ptr, Sint64 size)
+static size_t SDLCALL
+RWops2to3_read(struct SDL_RWops *rwops3, void *ptr, size_t size)
 {
-    const size_t br = SDL_RWread((SDL2_RWops *) rwops3->hidden.unknown.data1, ptr, 1, (size_t) size);
-    return (Sint64) br;
+    return SDL_RWread((SDL2_RWops *) rwops3->hidden.unknown.data1, ptr, 1, size);
 }
 
-static Sint64 SDLCALL
-RWops2to3_write(struct SDL_RWops *rwops3, const void *ptr, Sint64 size)
+static size_t SDLCALL
+RWops2to3_write(struct SDL_RWops *rwops3, const void *ptr, size_t size)
 {
-    const size_t bw = SDL_RWwrite((SDL2_RWops *) rwops3->hidden.unknown.data1, ptr, 1, (size_t) size);
-    return (bw == 0) ? -1 : (Sint64) bw;
+    return SDL_RWwrite((SDL2_RWops *) rwops3->hidden.unknown.data1, ptr, 1, size);
 }
 
 static int SDLCALL
 RWops2to3_close(struct SDL_RWops *rwops3)
 {
     const int retval = SDL_RWclose((SDL2_RWops *) rwops3->hidden.unknown.data1);
-    SDL3_DestroyRW(rwops3);  /* !!! FIXME: _should_ we free this if SDL_RWclose failed? */
+    SDL3_DestroyRW(rwops3);  /* we should always free this */
     return retval;
 }
 
@@ -2235,7 +2205,7 @@ SDL_SaveBMP_RW(SDL_Surface *surface, SDL2_RWops *rwops2, int freedst)
     int retval = -1;
     SDL_RWops *rwops3 = RWops2to3(rwops2);
     if (rwops3) {
-        retval = SDL3_SaveBMP_RW(surface, rwops3, freedst);
+        retval = SDL3_SaveBMP_RW(surface, rwops3, freedst ? SDL_TRUE : SDL_FALSE);
         if (!freedst) {
             SDL3_DestroyRW(rwops3);  /* don't close it because that'll close the SDL2_RWops. */
         }
@@ -2253,7 +2223,7 @@ SDL_GameControllerAddMappingsFromRW(SDL2_RWops *rwops2, int freerw)
     int retval = -1;
     SDL_RWops *rwops3 = RWops2to3(rwops2);
     if (rwops3) {
-        retval = SDL3_AddGamepadMappingsFromRW(rwops3, freerw);
+        retval = SDL3_AddGamepadMappingsFromRW(rwops3, freerw ? SDL_TRUE : SDL_FALSE);
         if (!freerw) {
             SDL3_DestroyRW(rwops3);  /* don't close it because that'll close the SDL2_RWops. */
         }
diff --git a/src/sdl3_syms.h b/src/sdl3_syms.h
index f3eee97..6077884 100644
--- a/src/sdl3_syms.h
+++ b/src/sdl3_syms.h
@@ -320,20 +320,6 @@ SDL3_SYM(SDL_RWops*,RWFromMem,(void *a, size_t b),(a,b),return)
 SDL3_SYM(SDL_RWops*,RWFromConstMem,(const void *a, size_t b),(a,b),return)
 SDL3_SYM(SDL_RWops*,CreateRW,(void),(),return)
 SDL3_SYM(void,DestroyRW,(SDL_RWops *a),(a),)
-SDL3_SYM(Uint8,ReadU8,(SDL_RWops *a),(a),return)
-SDL3_SYM(Uint16,ReadLE16,(SDL_RWops *a),(a),return)
-SDL3_SYM(Uint16,ReadBE16,(SDL_RWops *a),(a),return)
-SDL3_SYM(Uint32,ReadLE32,(SDL_RWops *a),(a),return)
-SDL3_SYM(Uint32,ReadBE32,(SDL_RWops *a),(a),return)
-SDL3_SYM(Uint64,ReadLE64,(SDL_RWops *a),(a),return)
-SDL3_SYM(Uint64,ReadBE64,(SDL_RWops *a),(a),return)
-SDL3_SYM(size_t,WriteU8,(SDL_RWops *a, Uint8 b),(a,b),return)
-SDL3_SYM(size_t,WriteLE16,(SDL_RWops *a, Uint16 b),(a,b),return)
-SDL3_SYM(size_t,WriteBE16,(SDL_RWops *a, Uint16 b),(a,b),return)
-SDL3_SYM(size_t,WriteLE32,(SDL_RWops *a, Uint32 b),(a,b),return)
-SDL3_SYM(size_t,WriteBE32,(SDL_RWops *a, Uint32 b),(a,b),return)
-SDL3_SYM(size_t,WriteLE64,(SDL_RWops *a, Uint64 b),(a,b),return)
-SDL3_SYM(size_t,WriteBE64,(SDL_RWops *a, Uint64 b),(a,b),return)
 SDL3_SYM(SDL_Window*,CreateShapedWindow,(const char *a, int b, int c, Uint32 d),(a,b,c,d),return)
 SDL3_SYM_PASSTHROUGH(SDL_bool,IsShapedWindow,(const SDL_Window *a),(a),return)
 SDL3_SYM_PASSTHROUGH(int,SetWindowShape,(SDL_Window *a, SDL_Surface *b, SDL_WindowShapeMode *c),(a,b,c),return)
@@ -413,7 +399,7 @@ SDL3_SYM_PASSTHROUGH(int,SetSurfacePalette,(SDL_Surface *a, SDL_Palette *b),(a,b
 SDL3_SYM_PASSTHROUGH(int,LockSurface,(SDL_Surface *a),(a),return)
 SDL3_SYM_PASSTHROUGH(void,UnlockSurface,(SDL_Surface *a),(a),)
 SDL3_SYM(SDL_Surface*,LoadBMP_RW,(SDL_RWops *a, SDL_bool b),(a,b),return)
-SDL3_SYM(int,SaveBMP_RW,(SDL_Surface *a, SDL_RWops *b, int c),(a,b,c),return)
+SDL3_SYM(int,SaveBMP_RW,(SDL_Surface *a, SDL_RWops *b, SDL_bool c),(a,b,c),return)
 SDL3_SYM_PASSTHROUGH(int,SetSurfaceRLE,(SDL_Surface *a, int b),(a,b),return)
 SDL3_SYM_RENAMED(int,SetColorKey,SetSurfaceColorKey,(SDL_Surface *a, int b, Uint32 c),(a,b,c),return)
 SDL3_SYM_RENAMED(int,GetColorKey,GetSurfaceColorKey,(SDL_Surface *a, Uint32 *b),(a,b),return)
@@ -518,7 +504,7 @@ SDL3_SYM(int,GL_GetSwapInterval,(int *a),(a),return)
 SDL3_SYM(int,GL_SwapWindow,(SDL_Window *a),(a),return)
 SDL3_SYM(int,GL_DeleteContext,(SDL_GLContext a),(a),return)
 SDL3_SYM_PASSTHROUGH(int,vsscanf,(const char *a, const char *b, va_list c),(a,b,c),return)
-SDL3_SYM(int,AddGamepadMappingsFromRW,(SDL_RWops *a, int b),(a,b),return)
+SDL3_SYM(int,AddGamepadMappingsFromRW,(SDL_RWops *a, SDL_bool b),(a,b),return)
 SDL3_SYM_PASSTHROUGH(void,GL_ResetAttributes,(void),(),)
 SDL3_SYM_PASSTHROUGH(SDL_bool,HasAVX,(void),(),return)
 SDL3_SYM_PASSTHROUGH(SDL_AssertionHandler,GetDefaultAssertionHandler,(void),(),return)