libostd/ostd/ext/sdl_rwops.hh

61 lines
1.7 KiB
C++
Raw Normal View History

/* SDL RWops integration.
*
* This file is part of OctaSTD. See COPYING.md for futher information.
*/
#ifndef OSTD_EXT_SDL_RWOPS_HH
#define OSTD_EXT_SDL_RWOPS_HH
#ifdef OSTD_EXT_SDL_USE_SDL1
#include <SDL/SDL.h>
#else
#include <SDL2/SDL.h>
#endif
#include "ostd/types.hh"
#include "ostd/stream.hh"
namespace ostd {
namespace sdl {
2016-04-27 02:17:26 +02:00
#ifdef OSTD_EXT_SDL_USE_SDL1
using SDLRWopsOffset = int;
#else
using SDLRWopsOffset = Int64;
#endif
inline SDL_RWops *stream_to_rwops(Stream &s) {
SDL_RWops *rwr = SDL_AllocRW();
if (!rwr) {
return nullptr;
}
rwr->hidden.unknown.data1 = &s;
2016-04-27 02:17:26 +02:00
rwr->size = [](SDL_RWops *rw) -> SDLRWopsOffset {
2016-07-02 05:56:23 +02:00
Stream *is = static_cast<Stream *>(rw->hidden.unknown.data1);
return static_cast<SDLRWopsOffset>(is->size());
2016-04-27 02:17:26 +02:00
};
rwr->seek = [](SDL_RWops *rw, SDLRWopsOffset pos, int whence) -> SDLRWopsOffset {
2016-07-02 05:56:23 +02:00
Stream *is = static_cast<Stream *>(rw->hidden.unknown.data1);
if (!pos && whence == SEEK_CUR)
2016-07-02 05:56:23 +02:00
return static_cast<SDLRWopsOffset>(is->tell());
if (is->seek(((StreamOffset)pos, (StreamSeek)whence))
2016-07-02 05:56:23 +02:00
return static_cast<SDLRWopsOffset>(is->tell());
return -1;
2016-04-27 02:17:26 +02:00
};
rwr->read = [](SDL_RWops *rw, void *buf, Size size, Size nb) -> Size {
2016-07-02 05:56:23 +02:00
Stream *is = static_cast<Stream *>(rw->hidden.unknown.data1);
2016-04-27 02:10:16 +02:00
return is->read_bytes(buf, size * nb) / size;
2016-04-27 02:17:26 +02:00
};
rwr->write = [](SDL_RWops *rw, const void *buf, Size size, Size nb) -> Size {
2016-07-02 05:56:23 +02:00
Stream *is = static_cast<Stream *>(rw->hidden.unknown.data1);
2016-04-27 02:10:16 +02:00
return is->write_bytes(buf, size * nb) / size;
2016-04-27 02:17:26 +02:00
};
rwr->close = [](SDL_RWops *) -> int { return 0; };
2016-04-27 02:10:16 +02:00
return rwr;
}
}
}
#endif