From e51fe5a34885abdfc4a5563f7bffa078f2b222bf Mon Sep 17 00:00:00 2001 From: q66 Date: Wed, 27 Apr 2016 01:08:01 +0100 Subject: [PATCH] add an extensions directory and first extension, sdl rwops stream integration --- ostd/ext/sdl_rwops.hh | 49 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 ostd/ext/sdl_rwops.hh diff --git a/ostd/ext/sdl_rwops.hh b/ostd/ext/sdl_rwops.hh new file mode 100644 index 0000000..fbc9bbc --- /dev/null +++ b/ostd/ext/sdl_rwops.hh @@ -0,0 +1,49 @@ +/* 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 +#else +#include +#endif + +#include "ostd/types.hh" +#include "ostd/stream.hh" + +namespace ostd { +namespace sdl { + +inline SDL_RWops *stream_to_rwops(Stream &s) { + SDL_RWops *rwr = SDL_AllocRW(); + if (!rwr) { + return nullptr; + } + rwr->hidden.unknown.data1 = &s; + rwr->seek = [](SDL_RWops *rw, Int64 pos, int whence) -> Int64 { + Stream *is = (Stream *)rw->hidden.unknown.data1; + if (!pos && whence == SEEK_CUR) + return (Int64)is->tell(); + if (is->seek(((StreamOffset)pos, (StreamSeek)whence)) + return (Int64)is->tell(); + return -1; + } + rwr->read = [](SDL_RWops *rw, void *buf, Size size, Size nb) -> Size { + Stream *is = (Stream *)rw->hidden.unknown.data1; + return f->read_bytes(buf, size * nb) / size; + } + rwr->write = [](SDL_RWops *rw, const void *buf, Size size, Size nb) -> Size { + Stream *is = (Stream *)rw->hidden.unknown.data1; + return f->write_bytes(buf, size * nb) / size; + } + rw->close = [](SDL_RWops *) -> int { return 0; } +} + +} +} + +#endif