src/SDL12_compat.c
changeset 10 e6176c053187
parent 9 408821d72ab2
child 11 ed314328463b
     1.1 --- a/src/SDL12_compat.c	Wed Mar 06 01:48:25 2013 -0500
     1.2 +++ b/src/SDL12_compat.c	Thu Mar 07 00:59:19 2013 -0500
     1.3 @@ -33,8 +33,6 @@
     1.4  //#include "video/SDL_pixels_c.h"
     1.5  //#include "render/SDL_yuv_sw_c.h"
     1.6  
     1.7 -// !!! IMPLEMENT_ME SDL_AddTimer
     1.8 -// !!! IMPLEMENT_ME SDL_AllocRW
     1.9  // !!! IMPLEMENT_ME SDL_AudioInit
    1.10  // !!! IMPLEMENT_ME SDL_AudioQuit
    1.11  // !!! IMPLEMENT_ME SDL_ConvertSurface
    1.12 @@ -46,7 +44,6 @@
    1.13  // !!! IMPLEMENT_ME SDL_EventState
    1.14  // !!! IMPLEMENT_ME SDL_FillRect
    1.15  // !!! IMPLEMENT_ME SDL_FreeCursor
    1.16 -// !!! IMPLEMENT_ME SDL_FreeRW
    1.17  // !!! IMPLEMENT_ME SDL_FreeSurface
    1.18  // !!! IMPLEMENT_ME SDL_GL_GetAttribute
    1.19  // !!! IMPLEMENT_ME SDL_GL_GetProcAddress
    1.20 @@ -65,8 +62,6 @@
    1.21  // !!! IMPLEMENT_ME SDL_GetRGB
    1.22  // !!! IMPLEMENT_ME SDL_GetRGBA
    1.23  // !!! IMPLEMENT_ME SDL_GetRelativeMouseState
    1.24 -// !!! IMPLEMENT_ME SDL_GetThreadID
    1.25 -// !!! IMPLEMENT_ME SDL_Init
    1.26  // !!! IMPLEMENT_ME SDL_InitSubSystem
    1.27  // !!! IMPLEMENT_ME SDL_LoadBMP_RW
    1.28  // !!! IMPLEMENT_ME SDL_LoadWAV_RW
    1.29 @@ -79,17 +74,6 @@
    1.30  // !!! IMPLEMENT_ME SDL_PumpEvents
    1.31  // !!! IMPLEMENT_ME SDL_PushEvent
    1.32  // !!! IMPLEMENT_ME SDL_QuitSubSystem
    1.33 -// !!! IMPLEMENT_ME SDL_RWFromConstMem
    1.34 -// !!! IMPLEMENT_ME SDL_RWFromFP
    1.35 -// !!! IMPLEMENT_ME SDL_RWFromFile
    1.36 -// !!! IMPLEMENT_ME SDL_RWFromMem
    1.37 -// !!! IMPLEMENT_ME SDL_ReadBE16
    1.38 -// !!! IMPLEMENT_ME SDL_ReadBE32
    1.39 -// !!! IMPLEMENT_ME SDL_ReadBE64
    1.40 -// !!! IMPLEMENT_ME SDL_ReadLE16
    1.41 -// !!! IMPLEMENT_ME SDL_ReadLE32
    1.42 -// !!! IMPLEMENT_ME SDL_ReadLE64
    1.43 -// !!! IMPLEMENT_ME SDL_RemoveTimer
    1.44  // !!! IMPLEMENT_ME SDL_SaveBMP_RW
    1.45  // !!! IMPLEMENT_ME SDL_SetClipRect
    1.46  // !!! IMPLEMENT_ME SDL_SetColorKey
    1.47 @@ -105,12 +89,6 @@
    1.48  // !!! IMPLEMENT_ME SDL_VideoQuit
    1.49  // !!! IMPLEMENT_ME SDL_WaitEvent
    1.50  // !!! IMPLEMENT_ME SDL_WasInit
    1.51 -// !!! IMPLEMENT_ME SDL_WriteBE16
    1.52 -// !!! IMPLEMENT_ME SDL_WriteBE32
    1.53 -// !!! IMPLEMENT_ME SDL_WriteBE64
    1.54 -// !!! IMPLEMENT_ME SDL_WriteLE16
    1.55 -// !!! IMPLEMENT_ME SDL_WriteLE32
    1.56 -// !!! IMPLEMENT_ME SDL_WriteLE64
    1.57  // !!! IMPLEMENT_ME SDL_iconv
    1.58  // !!! IMPLEMENT_ME SDL_iconv_string
    1.59  // !!! IMPLEMENT_ME SDL_lltoa
    1.60 @@ -2079,12 +2057,12 @@
    1.61      static SDL_TimerID compat_timer;
    1.62  
    1.63      if (compat_timer) {
    1.64 -        SDL_RemoveTimer(compat_timer);
    1.65 +        SDL20_RemoveTimer(compat_timer);
    1.66          compat_timer = 0;
    1.67      }
    1.68  
    1.69      if (interval && callback) {
    1.70 -        compat_timer = SDL_AddTimer(interval, SDL_SetTimerCallback, callback);
    1.71 +        compat_timer = SDL20_AddTimer(interval, SDL_SetTimerCallback, callback);
    1.72          if (!compat_timer) {
    1.73              return -1;
    1.74          }
    1.75 @@ -2147,6 +2125,152 @@
    1.76  /* !!! FIXME: Removed from 2.0; do nothing. We can't even report failure. */
    1.77  void SDL_KillThread(SDL_Thread *thread) {}
    1.78  
    1.79 +/* This changed from an opaque pointer to an int in 2.0. */
    1.80 +typedef struct _SDL12_TimerID *SDL12_TimerID;
    1.81 +SDL_COMPILE_TIME_ASSERT(timer, sizeof(SDL12_TimerID) >= sizeof(SDL_TimerID));
    1.82 +
    1.83 +SDL12_TimerID
    1.84 +SDL_AddTimer(Uint32 interval, SDL_NewTimerCallback callback, void *param)
    1.85 +{
    1.86 +    return (SDL12_TimerID) ((size_t) SDL20_AddTimer(interval, callback, param));
    1.87 +}
    1.88 +
    1.89 +SDL_bool
    1.90 +SDL_RemoveTimer(SDL12_TimerID id)
    1.91 +{
    1.92 +    return SDL20_RemoveTimer((SDL_TimerID) ((size_t)id));
    1.93 +}
    1.94 +
    1.95 +
    1.96 +typedef struct SDL12_RWops {
    1.97 +    int (SDLCALL *seek)(struct SDL_RWops *context, int offset, int whence);
    1.98 +    int (SDLCALL *read)(struct SDL_RWops *context, void *ptr, int size, int maxnum);
    1.99 +    int (SDLCALL *write)(struct SDL_RWops *context, const void *ptr, int size, int num);
   1.100 +    int (SDLCALL *close)(struct SDL_RWops *context);
   1.101 +    Uint32 type;
   1.102 +    void *padding[8];
   1.103 +    SDL_RWops *rwops20;
   1.104 +} SDL12_RWops;
   1.105 +
   1.106 +
   1.107 +SDL12_RWops *
   1.108 +SDL_AllocRW(void)
   1.109 +{
   1.110 +    SDL12_RWops *rwops = (SDL12_RWops *) SDL_malloc(sizeof (SDL12_RWops));
   1.111 +    if (!rwops)
   1.112 +        SDL20_OutOfMemory();
   1.113 +    return rwops;
   1.114 +}
   1.115 +
   1.116 +void
   1.117 +SDL_FreeRW(SDL12_RWops *rwops12)
   1.118 +{
   1.119 +    SDL_free(rwops12);
   1.120 +}
   1.121 +
   1.122 +static int SDLCALL
   1.123 +WrapRWops_seek(struct SDL12_RWops *rwops12, int offset, int whence)
   1.124 +{
   1.125 +    return rwops12->rwops20->seek(rwops12->rwops20, offset, whence);
   1.126 +}
   1.127 +
   1.128 +static int SDLCALL
   1.129 +WrapRWops_read(struct SDL12_RWops *rwops12, void *ptr, int size, int maxnum)
   1.130 +{
   1.131 +    return rwops12->rwops20->read(rwops12->rwops20, ptr, size, maxnum);
   1.132 +}
   1.133 +
   1.134 +static int SDLCALL
   1.135 +WrapRWops_write(struct SDL12_RWops *rwops12, const void *ptr, int size, int num)
   1.136 +{
   1.137 +    return rwops12->rwops20->write(rwops12->rwops20, ptr, size, num);
   1.138 +}
   1.139 +
   1.140 +static int SDLCALL
   1.141 +WrapRWops_close(struct SDL12_RWops *rwops12)
   1.142 +{
   1.143 +    int rc = 0;
   1.144 +    if (rwops12)
   1.145 +    {
   1.146 +        rc = rwops12->rwops20->close(rwops12->rwops20);
   1.147 +        if (rc == 0)
   1.148 +            SDL_FreeRW(rwops12);
   1.149 +    }
   1.150 +    return rc;
   1.151 +}
   1.152 +
   1.153 +static SDL12_RWops *
   1.154 +WrapRWops(SDL12_RWops *rwops12, SDL_RWops *rwops20)
   1.155 +{
   1.156 +    if (!rwops20)
   1.157 +    {
   1.158 +        SDL_FreeRW(rwops12);
   1.159 +        return NULL;
   1.160 +    }
   1.161 +    SDL_zerop(rwops12);
   1.162 +    rwops12->type = rwops20->type;
   1.163 +    rwops12->rwops20 = rwops20;
   1.164 +    rwops12->seek = WrapRWops_seek;
   1.165 +    rwops12->read = WrapRWops_read;
   1.166 +    rwops12->write = WrapRWops_write;
   1.167 +    rwops12->close = WrapRWops_close;
   1.168 +}
   1.169 +
   1.170 +SDL12_RWops *
   1.171 +SDL_RWFromFile(const char *file, const char *mode)
   1.172 +{
   1.173 +    SDL12_RWops *rwops12 = SDL_AllocRW();
   1.174 +    return rwops12 ? WrapRWops(rwops12, SDL20_RWFromFile(file, mode)) : NULL;
   1.175 +}
   1.176 +
   1.177 +SDL12_RWops *
   1.178 +SDL_RWFromFP(FILE *io, int autoclose)
   1.179 +{
   1.180 +    SDL12_RWops *rwops12 = SDL_AllocRW();
   1.181 +    return rwops12 ? WrapRWops(rwops12, SDL20_RWFromFP(io, autoclose)) : NULL;
   1.182 +}
   1.183 +
   1.184 +SDL12_RWops *
   1.185 +SDL_RWFromMem(void *mem, int size)
   1.186 +{
   1.187 +    SDL12_RWops *rwops12 = SDL_AllocRW();
   1.188 +    return rwops12 ? WrapRWops(rwops12, SDL20_RWFromMem(mem, size)) : NULL;
   1.189 +}
   1.190 +
   1.191 +SDL12_RWops *
   1.192 +SDL_RWFromConstMem(const void *mem, int size)
   1.193 +{
   1.194 +    SDL12_RWops *rwops12 = SDL_AllocRW();
   1.195 +    return rwops12 ? WrapRWops(rwops12, SDL20_RWFromConstMem(mem, size)) : NULL;
   1.196 +}
   1.197 +
   1.198 +#define READ_AND_BYTESWAP(endian, bits) \
   1.199 +    Uint##bits SDL_Read##endian##bits(SDL12_RWops *rwops12) { \
   1.200 +        Uint##bits val; rwops12->read(rwops12, &val, sizeof (val), 1); \
   1.201 +        return SDL_Swap##endian##bits(val); \
   1.202 +    }
   1.203 +
   1.204 +READ_AND_BYTESWAP(LE,16)
   1.205 +READ_AND_BYTESWAP(BE,16)
   1.206 +READ_AND_BYTESWAP(LE,32)
   1.207 +READ_AND_BYTESWAP(BE,32)
   1.208 +READ_AND_BYTESWAP(LE,64)
   1.209 +READ_AND_BYTESWAP(BE,64)
   1.210 +#undef READ_AND_BYTESWAP
   1.211 +
   1.212 +#define BYTESWAP_AND_WRITE(endian, bits) \
   1.213 +    int SDL_Write##endian##bits(SDL12_RWops *rwops12, Uint##endian##bits val) { \
   1.214 +        val = SDL_Swap##endian##bits(val); \
   1.215 +        return rwops12->write(rwops12, &val, sizeof (val), 1); \
   1.216 +    }
   1.217 +BYTESWAP_AND_WRITE(LE,16)
   1.218 +BYTESWAP_AND_WRITE(BE,16)
   1.219 +BYTESWAP_AND_WRITE(LE,32)
   1.220 +BYTESWAP_AND_WRITE(BE,32)
   1.221 +BYTESWAP_AND_WRITE(LE,64)
   1.222 +BYTESWAP_AND_WRITE(BE,64)
   1.223 +#undef BYTESWAP_AND_WRITE
   1.224 +
   1.225  /* Things that _should_ be binary compatible pass right through... */
   1.226  #define SDL20_SYM(rc,fn,params,args,ret)
   1.227  #define SDL20_SYM_PASSTHROUGH(rc,fn,params,args,ret) \
   1.228 @@ -2155,5 +2279,4 @@
   1.229  #undef SDL20_SYM_PASSTHROUGH
   1.230  #undef SDL20_SYM
   1.231  
   1.232 -
   1.233  /* vi: set ts=4 sw=4 expandtab: */