From e6a49b5d2d202a45c209d561cf353dd67fff9bdb Mon Sep 17 00:00:00 2001 From: pixel Date: Thu, 9 Feb 2006 17:04:57 +0000 Subject: Better handling of SDL's RWops. --- lib/engine.cc | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) (limited to 'lib/engine.cc') diff --git a/lib/engine.cc b/lib/engine.cc index 65f3c55..c372f0b 100644 --- a/lib/engine.cc +++ b/lib/engine.cc @@ -17,7 +17,7 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -/* $Id: engine.cc,v 1.29 2005-12-01 13:48:12 pixel Exp $ */ +/* $Id: engine.cc,v 1.30 2006-02-09 17:04:58 pixel Exp $ */ #include #include @@ -156,15 +156,25 @@ int mogltk::engine::GetInited() { class embedRWops : public Base { public: - embedRWops(Handle *); + embedRWops(Handle *, bool); + virtual ~embedRWops(); int seek(int, int); int read(void *, int, int); int write(const void *, int, int); + static int s_seek(SDL_RWops * context, int, int); + static int s_read(SDL_RWops * context, void *, int, int); + static int s_write(SDL_RWops * context, const void *, int, int); + static int s_close(SDL_RWops * context); private: Handle * h; + bool destroy_when_close; }; -embedRWops::embedRWops(Handle * ah) : h(ah) {} +embedRWops::embedRWops(Handle * ah, bool _destroy_when_close) : h(ah), destroy_when_close(_destroy_when_close) {} +embedRWops::~embedRWops() { + if (destroy_when_close) + delete h; +} int embedRWops::seek(int offset, int whence) { return h->seek(offset, whence); @@ -178,25 +188,25 @@ int embedRWops::write(const void * ptr, int size, int num) { return h->write(ptr, size * num); } -static int embedRWseek(SDL_RWops * context, int offset, int whence) { +int embedRWops::s_seek(SDL_RWops * context, int offset, int whence) { if (context->hidden.unknown.data1) return ((embedRWops *)(context->hidden.unknown.data1))->seek(offset, whence); return -1; } -static int embedRWread(SDL_RWops * context, void * ptr, int size, int num) { +int embedRWops::s_read(SDL_RWops * context, void * ptr, int size, int num) { if (context->hidden.unknown.data1) return ((embedRWops *)(context->hidden.unknown.data1))->read(ptr, size, num); return -1; } -static int embedRWwrite(SDL_RWops * context, const void * ptr, int size, int num) { +int embedRWops::s_write(SDL_RWops * context, const void * ptr, int size, int num) { if (context->hidden.unknown.data1) return ((embedRWops *)(context->hidden.unknown.data1))->write(ptr, size, num); return -1; } -static int embedRWclose(SDL_RWops * context) { +int embedRWops::s_close(SDL_RWops * context) { if (context->hidden.unknown.data1) { delete ((embedRWops *)(context->hidden.unknown.data1)); context->hidden.unknown.data1 = 0; @@ -205,16 +215,16 @@ static int embedRWclose(SDL_RWops * context) { return -1; } -SDL_RWops * mogltk::engine::RWFromHandle(Handle * h) throw (GeneralException) { +SDL_RWops * mogltk::engine::RWFromHandle(Handle * h, bool destroy_when_close) throw (GeneralException) { SDL_RWops * r = 0; if (h) { if (!(r = SDL_AllocRW())) throw GeneralException(_("Couldn't allocate memory for SDL_RWops")); - r->hidden.unknown.data1 = (void *) new embedRWops(h); - r->seek = embedRWseek; - r->read = embedRWread; - r->write = embedRWwrite; - r->close = embedRWclose; + r->hidden.unknown.data1 = (void *) new embedRWops(h, destroy_when_close); + r->seek = embedRWops::s_seek; + r->read = embedRWops::s_read; + r->write = embedRWops::s_write; + r->close = embedRWops::s_close; } return r; } -- cgit v1.2.3