implement rwops inline

master
Daniel Kolesa 2020-07-30 19:47:33 +02:00
parent 7966cfeccb
commit 5482a3e39f
2 changed files with 32 additions and 41 deletions

View File

@ -5,6 +5,37 @@
#include "tools.hh"
SDL_RWops *stream_rwops(stream *s);
namespace detail {
inline Sint64 rwopsseek(SDL_RWops *rw, Sint64 pos, int whence) {
stream *f = (stream *)rw->hidden.unknown.data1;
if((!pos && whence==SEEK_CUR) || f->seek(pos, whence)) return (int)f->tell();
return -1;
}
inline size_t rwopsread(SDL_RWops *rw, void *buf, size_t size, size_t nmemb) {
stream *f = (stream *)rw->hidden.unknown.data1;
return f->read(buf, size*nmemb)/size;
}
inline size_t rwopswrite(SDL_RWops *rw, const void *buf, size_t size, size_t nmemb) {
stream *f = (stream *)rw->hidden.unknown.data1;
return f->write(buf, size*nmemb)/size;
}
inline int rwopsclose(SDL_RWops *rw) {
return 0;
}
}
inline SDL_RWops *stream_rwops(stream *s) {
SDL_RWops *rw = SDL_AllocRW();
if(!rw) return nullptr;
rw->hidden.unknown.data1 = s;
rw->seek = detail::rwopsseek;
rw->read = detail::rwopsread;
rw->write = detail::rwopswrite;
rw->close = detail::rwopsclose;
return rw;
}
#endif

View File

@ -1,7 +1,5 @@
#include <new>
#include <SDL.h>
#include <sauerlib/encoding.hh>
#include "tools.hh"
@ -320,44 +318,6 @@ int listfiles(const char *dir, const char *ext, vector<char *> &files)
return dirs;
}
#ifndef STANDALONE
static Sint64 rwopsseek(SDL_RWops *rw, Sint64 pos, int whence)
{
stream *f = (stream *)rw->hidden.unknown.data1;
if((!pos && whence==SEEK_CUR) || f->seek(pos, whence)) return (int)f->tell();
return -1;
}
static size_t rwopsread(SDL_RWops *rw, void *buf, size_t size, size_t nmemb)
{
stream *f = (stream *)rw->hidden.unknown.data1;
return f->read(buf, size*nmemb)/size;
}
static size_t rwopswrite(SDL_RWops *rw, const void *buf, size_t size, size_t nmemb)
{
stream *f = (stream *)rw->hidden.unknown.data1;
return f->write(buf, size*nmemb)/size;
}
static int rwopsclose(SDL_RWops *rw)
{
return 0;
}
SDL_RWops *stream_rwops(stream *s)
{
SDL_RWops *rw = SDL_AllocRW();
if(!rw) return nullptr;
rw->hidden.unknown.data1 = s;
rw->seek = rwopsseek;
rw->read = rwopsread;
rw->write = rwopswrite;
rw->close = rwopsclose;
return rw;
}
#endif
stream::offset stream::size()
{
offset pos = tell(), endpos;