Skip to content

Commit

Permalink
Fixes to SDL_RWops bridge code.
Browse files Browse the repository at this point in the history
- Correct return values (number of objects, not bytes), thanks Reto!
- Updated for SDL 2.0 RWops interface. Threw away SDL 1.3 support.
- 1.2 support remains. For now!
  • Loading branch information
icculus committed Jul 28, 2015
1 parent 57bbcc3 commit c4deb67
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 18 deletions.
3 changes: 3 additions & 0 deletions docs/CREDITS.txt
Expand Up @@ -148,6 +148,9 @@ Bug fixes:
Bug fixes: Bug fixes:
Xian Nox Xian Nox


Bug fixes:
Reto Schneider

pkg-config support: pkg-config support:
Jonas Kulla Jonas Kulla


Expand Down
62 changes: 44 additions & 18 deletions extras/physfsrwops.c
Expand Up @@ -24,26 +24,41 @@
#include <stdio.h> /* used for SEEK_SET, SEEK_CUR, SEEK_END ... */ #include <stdio.h> /* used for SEEK_SET, SEEK_CUR, SEEK_END ... */
#include "physfsrwops.h" #include "physfsrwops.h"


/* SDL's RWOPS interface changed a little in SDL 1.3... */ /* SDL's RWOPS interface changed a little in SDL 2.0... */
#if defined(SDL_VERSION_ATLEAST) #if defined(SDL_VERSION_ATLEAST)
#if SDL_VERSION_ATLEAST(1, 3, 0) #if SDL_VERSION_ATLEAST(2, 0, 0)
#define TARGET_SDL13 1 #define TARGET_SDL2 1
#endif #endif
#endif #endif


#if TARGET_SDL13 #if !TARGET_SDL2
static long SDLCALL physfsrwops_seek(struct SDL_RWops *rw, long offset, int whence) #define RW_SEEK_SET SEEK_SET
#define RW_SEEK_CUR SEEK_CUR
#define RW_SEEK_END SEEK_END
#endif

#if TARGET_SDL2
static Sint64 SDLCALL physfsrwops_size(struct SDL_RWops *rw)
{
PHYSFS_File *handle = (PHYSFS_File *) rw->hidden.unknown.data1;
return (Sint64) PHYSFS_fileLength(handle);
} /* physfsrwops_size */
#endif


#if TARGET_SDL2
static Sint64 SDLCALL physfsrwops_seek(struct SDL_RWops *rw, Sint64 offset, int whence)
#else #else
static int physfsrwops_seek(SDL_RWops *rw, int offset, int whence) static int physfsrwops_seek(SDL_RWops *rw, int offset, int whence)
#endif #endif
{ {
PHYSFS_File *handle = (PHYSFS_File *) rw->hidden.unknown.data1; PHYSFS_File *handle = (PHYSFS_File *) rw->hidden.unknown.data1;
PHYSFS_sint64 pos = 0; PHYSFS_sint64 pos = 0;


if (whence == SEEK_SET) if (whence == RW_SEEK_SET)
pos = (PHYSFS_sint64) offset; pos = (PHYSFS_sint64) offset;


else if (whence == SEEK_CUR) else if (whence == RW_SEEK_CUR)
{ {
const PHYSFS_sint64 current = PHYSFS_tell(handle); const PHYSFS_sint64 current = PHYSFS_tell(handle);
if (current == -1) if (current == -1)
Expand All @@ -55,8 +70,8 @@ static int physfsrwops_seek(SDL_RWops *rw, int offset, int whence)


if (offset == 0) /* this is a "tell" call. We're done. */ if (offset == 0) /* this is a "tell" call. We're done. */
{ {
#if TARGET_SDL13 #if TARGET_SDL2
return (long) current; return (Sint64) current;
#else #else
return (int) current; return (int) current;
#endif #endif
Expand All @@ -65,7 +80,7 @@ static int physfsrwops_seek(SDL_RWops *rw, int offset, int whence)
pos = current + ((PHYSFS_sint64) offset); pos = current + ((PHYSFS_sint64) offset);
} /* else if */ } /* else if */


else if (whence == SEEK_END) else if (whence == RW_SEEK_END)
{ {
const PHYSFS_sint64 len = PHYSFS_fileLength(handle); const PHYSFS_sint64 len = PHYSFS_fileLength(handle);
if (len == -1) if (len == -1)
Expand Down Expand Up @@ -95,15 +110,15 @@ static int physfsrwops_seek(SDL_RWops *rw, int offset, int whence)
return -1; return -1;
} /* if */ } /* if */


#if TARGET_SDL13 #if TARGET_SDL2
return (long) pos; return (Sint64) pos;
#else #else
return (int) pos; return (int) pos;
#endif #endif
} /* physfsrwops_seek */ } /* physfsrwops_seek */




#if TARGET_SDL13 #if TARGET_SDL2
static size_t SDLCALL physfsrwops_read(struct SDL_RWops *rw, void *ptr, static size_t SDLCALL physfsrwops_read(struct SDL_RWops *rw, void *ptr,
size_t size, size_t maxnum) size_t size, size_t maxnum)
#else #else
Expand All @@ -116,18 +131,26 @@ static int physfsrwops_read(SDL_RWops *rw, void *ptr, int size, int maxnum)
if (rc != ((PHYSFS_sint64) readlen)) if (rc != ((PHYSFS_sint64) readlen))
{ {
if (!PHYSFS_eof(handle)) /* not EOF? Must be an error. */ if (!PHYSFS_eof(handle)) /* not EOF? Must be an error. */
{
SDL_SetError("PhysicsFS error: %s", PHYSFS_getLastError()); SDL_SetError("PhysicsFS error: %s", PHYSFS_getLastError());

#if TARGET_SDL2
return 0;
#else
return -1;
#endif
} /* if */
} /* if */ } /* if */


#if TARGET_SDL13 #if TARGET_SDL2
return (size_t) rc; return (size_t) rc / size;
#else #else
return (int) rc; return (int) rc / size;
#endif #endif
} /* physfsrwops_read */ } /* physfsrwops_read */




#if TARGET_SDL13 #if TARGET_SDL2
static size_t SDLCALL physfsrwops_write(struct SDL_RWops *rw, const void *ptr, static size_t SDLCALL physfsrwops_write(struct SDL_RWops *rw, const void *ptr,
size_t size, size_t num) size_t size, size_t num)
#else #else
Expand All @@ -140,7 +163,7 @@ static int physfsrwops_write(SDL_RWops *rw, const void *ptr, int size, int num)
if (rc != ((PHYSFS_sint64) writelen)) if (rc != ((PHYSFS_sint64) writelen))
SDL_SetError("PhysicsFS error: %s", PHYSFS_getLastError()); SDL_SetError("PhysicsFS error: %s", PHYSFS_getLastError());


#if TARGET_SDL13 #if TARGET_SDL2
return (size_t) rc; return (size_t) rc;
#else #else
return (int) rc; return (int) rc;
Expand Down Expand Up @@ -173,6 +196,9 @@ static SDL_RWops *create_rwops(PHYSFS_File *handle)
retval = SDL_AllocRW(); retval = SDL_AllocRW();
if (retval != NULL) if (retval != NULL)
{ {
#if TARGET_SDL2
retval->size = physfsrwops_size;
#endif
retval->seek = physfsrwops_seek; retval->seek = physfsrwops_seek;
retval->read = physfsrwops_read; retval->read = physfsrwops_read;
retval->write = physfsrwops_write; retval->write = physfsrwops_write;
Expand Down

0 comments on commit c4deb67

Please sign in to comment.