summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpixel <pixel>2006-02-09 17:04:57 +0000
committerpixel <pixel>2006-02-09 17:04:57 +0000
commite6a49b5d2d202a45c209d561cf353dd67fff9bdb (patch)
tree1c3426728b1763aa7cb927932a108d295e6c3c5c
parent58d8064ce2beb470ed36ea6bf4b0ac5bed8d0079 (diff)
Better handling of SDL's RWops.
-rw-r--r--include/engine.h4
-rw-r--r--lib/engine.cc36
2 files changed, 25 insertions, 15 deletions
diff --git a/include/engine.h b/include/engine.h
index 9d597b2..0d4afbc 100644
--- a/include/engine.h
+++ b/include/engine.h
@@ -17,7 +17,7 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
-/* $Id: engine.h,v 1.21 2005-12-01 13:48:12 pixel Exp $ */
+/* $Id: engine.h,v 1.22 2006-02-09 17:04:57 pixel Exp $ */
#ifndef __ENGINE_H__
#define __ENGINE_H__
@@ -53,7 +53,7 @@ namespace mogltk {
static int setup() throw(GeneralException);
static int postsetup() throw(GeneralException);
static int GetInited();
- static SDL_RWops * RWFromHandle(Handle *) throw (GeneralException);
+ static SDL_RWops * RWFromHandle(Handle *, bool = false) throw (GeneralException);
static void pollevents() throw (GeneralException);
static void setappactive(bool);
static bool getappactive();
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 <SDL.h>
#include <Input.h>
@@ -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;
}