src/SDL12_compat.c
changeset 12 f9cfb7f12b95
parent 11 ed314328463b
child 15 fd1b88f919e7
     1.1 --- a/src/SDL12_compat.c	Thu Mar 07 01:57:55 2013 -0500
     1.2 +++ b/src/SDL12_compat.c	Thu Mar 07 11:56:49 2013 -0500
     1.3 @@ -61,8 +61,6 @@
     1.4  // !!! IMPLEMENT_ME SDL_GetRGB
     1.5  // !!! IMPLEMENT_ME SDL_GetRGBA
     1.6  // !!! IMPLEMENT_ME SDL_GetRelativeMouseState
     1.7 -// !!! IMPLEMENT_ME SDL_LoadBMP_RW
     1.8 -// !!! IMPLEMENT_ME SDL_LoadWAV_RW
     1.9  // !!! IMPLEMENT_ME SDL_LockSurface
    1.10  // !!! IMPLEMENT_ME SDL_LowerBlit
    1.11  // !!! IMPLEMENT_ME SDL_MapRGB
    1.12 @@ -368,9 +366,18 @@
    1.13  void
    1.14  SDL_SetError(const char *fmt, ...)
    1.15  {
    1.16 -    if (!Loaded_SDL20)
    1.17 -        return;
    1.18 -
    1.19 +    char *str = NULL;
    1.20 +    va_list ap;
    1.21 +    va_start(ap, fmt);
    1.22 +    vasprintf(&str, fmt, ap);
    1.23 +    va_end(ap);
    1.24 +    if (!str)
    1.25 +        SDL20_OutOfMemory();
    1.26 +    else
    1.27 +    {
    1.28 +        SDL20_SetError("%s", str);
    1.29 +        free(str);
    1.30 +    }
    1.31  }
    1.32  
    1.33  char *
    1.34 @@ -2221,25 +2228,25 @@
    1.35  }
    1.36  
    1.37  static int SDLCALL
    1.38 -WrapRWops_seek(struct SDL12_RWops *rwops12, int offset, int whence)
    1.39 +RWops12to20_seek(struct SDL12_RWops *rwops12, int offset, int whence)
    1.40  {
    1.41      return rwops12->rwops20->seek(rwops12->rwops20, offset, whence);
    1.42  }
    1.43  
    1.44  static int SDLCALL
    1.45 -WrapRWops_read(struct SDL12_RWops *rwops12, void *ptr, int size, int maxnum)
    1.46 +RWops12to20_read(struct SDL12_RWops *rwops12, void *ptr, int size, int maxnum)
    1.47  {
    1.48      return rwops12->rwops20->read(rwops12->rwops20, ptr, size, maxnum);
    1.49  }
    1.50  
    1.51  static int SDLCALL
    1.52 -WrapRWops_write(struct SDL12_RWops *rwops12, const void *ptr, int size, int num)
    1.53 +RWops12to20_write(struct SDL12_RWops *rwops12, const void *ptr, int size, int num)
    1.54  {
    1.55      return rwops12->rwops20->write(rwops12->rwops20, ptr, size, num);
    1.56  }
    1.57  
    1.58  static int SDLCALL
    1.59 -WrapRWops_close(struct SDL12_RWops *rwops12)
    1.60 +RWops12to20_close(struct SDL12_RWops *rwops12)
    1.61  {
    1.62      int rc = 0;
    1.63      if (rwops12)
    1.64 @@ -2252,7 +2259,7 @@
    1.65  }
    1.66  
    1.67  static SDL12_RWops *
    1.68 -WrapRWops(SDL12_RWops *rwops12, SDL_RWops *rwops20)
    1.69 +RWops12to20(SDL12_RWops *rwops12, SDL_RWops *rwops20)
    1.70  {
    1.71      if (!rwops20)
    1.72      {
    1.73 @@ -2262,38 +2269,39 @@
    1.74      SDL_zerop(rwops12);
    1.75      rwops12->type = rwops20->type;
    1.76      rwops12->rwops20 = rwops20;
    1.77 -    rwops12->seek = WrapRWops_seek;
    1.78 -    rwops12->read = WrapRWops_read;
    1.79 -    rwops12->write = WrapRWops_write;
    1.80 -    rwops12->close = WrapRWops_close;
    1.81 +    rwops12->seek = RWops12to20_seek;
    1.82 +    rwops12->read = RWops12to20_read;
    1.83 +    rwops12->write = RWops12to20_write;
    1.84 +    rwops12->close = RWops12to20_close;
    1.85 +    return rwops12;
    1.86  }
    1.87  
    1.88  SDL12_RWops *
    1.89  SDL_RWFromFile(const char *file, const char *mode)
    1.90  {
    1.91      SDL12_RWops *rwops12 = SDL_AllocRW();
    1.92 -    return rwops12 ? WrapRWops(rwops12, SDL20_RWFromFile(file, mode)) : NULL;
    1.93 +    return rwops12 ? RWops12to20(rwops12, SDL20_RWFromFile(file, mode)) : NULL;
    1.94  }
    1.95  
    1.96  SDL12_RWops *
    1.97  SDL_RWFromFP(FILE *io, int autoclose)
    1.98  {
    1.99      SDL12_RWops *rwops12 = SDL_AllocRW();
   1.100 -    return rwops12 ? WrapRWops(rwops12, SDL20_RWFromFP(io, autoclose)) : NULL;
   1.101 +    return rwops12 ? RWops12to20(rwops12, SDL20_RWFromFP(io, autoclose)) : NULL;
   1.102  }
   1.103  
   1.104  SDL12_RWops *
   1.105  SDL_RWFromMem(void *mem, int size)
   1.106  {
   1.107      SDL12_RWops *rwops12 = SDL_AllocRW();
   1.108 -    return rwops12 ? WrapRWops(rwops12, SDL20_RWFromMem(mem, size)) : NULL;
   1.109 +    return rwops12 ? RWops12to20(rwops12, SDL20_RWFromMem(mem, size)) : NULL;
   1.110  }
   1.111  
   1.112  SDL12_RWops *
   1.113  SDL_RWFromConstMem(const void *mem, int size)
   1.114  {
   1.115      SDL12_RWops *rwops12 = SDL_AllocRW();
   1.116 -    return rwops12 ? WrapRWops(rwops12, SDL20_RWFromConstMem(mem, size)) : NULL;
   1.117 +    return rwops12 ? RWops12to20(rwops12, SDL20_RWFromConstMem(mem, size)) : NULL;
   1.118  }
   1.119  
   1.120  #define READ_AND_BYTESWAP(endian, bits) \
   1.121 @@ -2331,21 +2339,109 @@
   1.122  #undef SDL20_SYM_PASSTHROUGH
   1.123  #undef SDL20_SYM
   1.124  
   1.125 -void
   1.126 -SDL_SetError(const char *fmt, ...)
   1.127 +
   1.128 +static Sint64 SDLCALL
   1.129 +RWops20to12_size(struct SDL_RWops *rwops20)
   1.130  {
   1.131 -    char *str = NULL;
   1.132 -    va_list ap;
   1.133 -    va_start(ap, fmt);
   1.134 -    vasprintf(&str, fmt, ap);
   1.135 -    va_end(ap);
   1.136 -    if (!str)
   1.137 -        SDL20_OutOfMemory();
   1.138 -    else
   1.139 +    SDL12_RWops *rwops12 = (SDL12_RWops *) rwops20->hidden.unknown.data1;
   1.140 +    int size = rwops20->hidden.unknown.data2;
   1.141 +    int pos;
   1.142 +
   1.143 +    if (size != -1)
   1.144 +        return size;
   1.145 +
   1.146 +    pos = rwops12->seek(rwops12, 0, SEEK_CUR);
   1.147 +    if (pos == -1)
   1.148 +        return -1;
   1.149 +
   1.150 +    size = (Sint64) rwops->seek(rwops12, 0, SEEK_END);
   1.151 +    if (size == -1)
   1.152 +        return -1;
   1.153 +
   1.154 +    rwops->seek(rwops12, pos, SEEK_SET);  /* !!! FIXME: and if this fails? */
   1.155 +    rwops20->hidden.unknown.data2 = size;
   1.156 +    return size;
   1.157 +}
   1.158 +
   1.159 +static Sint64
   1.160 +RWops20to12_seek(struct SDL_RWops *rwops20, Sint64 offset, int whence)
   1.161 +{
   1.162 +    /* !!! FIXME: fail if (offset) is too big */
   1.163 +    SDL12_RWops *rwops12 = (SDL12_RWops *) rwops20->hidden.unknown.data1;
   1.164 +    return (Sint64) rwops12->seek(rwops12, (int) offset, whence);
   1.165 +}
   1.166 +
   1.167 +static size_t SDLCALL
   1.168 +RWops20to12_read(struct SDL_RWops *rwops20, void *ptr, size_t size, size_t maxnum)
   1.169 +{
   1.170 +    /* !!! FIXME: fail if (size) or (maxnum) is too big */
   1.171 +    SDL12_RWops *rwops12 = (SDL12_RWops *) rwops20->hidden.unknown.data1;
   1.172 +    return (size_t) rwops12->read(rwops12, ptr, (int) size, (int) maxnum);
   1.173 +}
   1.174 +
   1.175 +static size_t SDLCALL
   1.176 +RWops20to12_write(struct SDL_RWops *rwops20, const void *ptr, size_t size, size_t num)
   1.177 +{
   1.178 +    /* !!! FIXME: fail if (size) or (maxnum) is too big */
   1.179 +    SDL12_RWops *rwops12 = (SDL12_RWops *) rwops20->hidden.unknown.data1;
   1.180 +    return (size_t) rwops12->write(rwops12, ptr, (int) size, (int) num);
   1.181 +}
   1.182 +
   1.183 +static int SDLCALL
   1.184 +RWops20to12_close(struct SDL_RWops *rwops20)
   1.185 +{
   1.186 +    int rc = 0;
   1.187 +    if (rwops20)
   1.188      {
   1.189 -        SDL20_SetError("%s", str);
   1.190 -        free(str);
   1.191 +        SDL12_RWops *rwops12 = (SDL12_RWops *) rwops20->hidden.unknown.data1;
   1.192 +        rc = rwops12->close(rwops12);
   1.193 +        if (rc == 0)
   1.194 +            SDL20_FreeRW(rwops20);
   1.195      }
   1.196 +    return rc;
   1.197 +}
   1.198 +
   1.199 +static SDL12_RWops *
   1.200 +RWops20to12(SDL12_RWops *rwops12)
   1.201 +{
   1.202 +    SDL20_RWops *rwops20;
   1.203 +
   1.204 +    if (!rwops12)
   1.205 +        return NULL;
   1.206 +
   1.207 +    rwops20 = SDL20_AllocRW();
   1.208 +    if (!rwops20)
   1.209 +        return NULL;
   1.210 +
   1.211 +    SDL_zerop(rwops20);
   1.212 +    rwops20->type = rwops12->type;
   1.213 +    rwops20->hidden.unknown.data1 = rwops12;
   1.214 +    rwops20->hidden.unknown.data2 = -1;  /* cached size of stream */
   1.215 +    rwops20->size = RWops20to12_size;
   1.216 +    rwops20->seek = RWops20to12_seek;
   1.217 +    rwops20->read = RWops20to12_read;
   1.218 +    rwops20->write = RWops20to12_write;
   1.219 +    rwops20->close = RWops20to12_close;
   1.220 +    return rwops20;
   1.221 +}
   1.222 +
   1.223 +SDL_Surface *
   1.224 +SDL_LoadBMP_RW(SDL12_RWops *rwops12, int freerwops12)
   1.225 +{
   1.226 +    SDL_RWops *rwops20 = RWops20to12(rwops12);
   1.227 +    SDL_Surface *retval = SDL20_LoadBMP_RW(rwops20, freerwops12);
   1.228 +    if (!freerwops12)  /* free our wrapper if SDL2 didn't close it. */
   1.229 +        SDL20_FreeRW(rwops20);
   1.230 +}
   1.231 +
   1.232 +SDL_AudioSpec *
   1.233 +SDL_LoadWAV_RW(SDL12_RWops *rwops12, int freerwops12,
   1.234 +               SDL_AudioSpec *spec, Uint8 **buf, Uint32 *len)
   1.235 +{
   1.236 +    SDL_RWops *rwops20 = RWops20to12(rwops12);
   1.237 +    SDL_Surface *retval = SDL20_LoadWAV_RW(rwops20, freerwops12, spec, buf, len);
   1.238 +    if (!freerwops12)  /* free our wrapper if SDL2 didn't close it. */
   1.239 +        SDL20_FreeRW(rwops20);
   1.240  }
   1.241  
   1.242  /* vi: set ts=4 sw=4 expandtab: */