Revert Disposable concept back into core

Pretty much a revert of
e858bbdcf5.

We need this in core to properly implement F12 reset.
This commit is contained in:
Jonas Kulla 2014-09-23 21:12:58 +02:00
parent 3983fe66e9
commit 81ac0780f8
36 changed files with 476 additions and 210 deletions

View File

@ -101,22 +101,12 @@ static void freeInstance(void *inst)
void void
raiseDisposedAccess(VALUE self); raiseDisposedAccess(VALUE self);
inline void
checkDisposed(VALUE self)
{
if (!RTYPEDDATA_DATA(self))
raiseDisposedAccess(self);
}
template<class C> template<class C>
inline C * inline C *
getPrivateData(VALUE self) getPrivateData(VALUE self)
{ {
C *c = static_cast<C*>(RTYPEDDATA_DATA(self)); C *c = static_cast<C*>(RTYPEDDATA_DATA(self));
if (!c)
raiseDisposedAccess(self);
return c; return c;
} }
@ -124,8 +114,6 @@ template<class C>
static inline C * static inline C *
getPrivateDataCheck(VALUE self, const rb_data_type_t &type) getPrivateDataCheck(VALUE self, const rb_data_type_t &type)
{ {
/* We don't check for disposed here because any disposable
* property is always also nullable */
void *obj = Check_TypedStruct(self, &type); void *obj = Check_TypedStruct(self, &type);
return static_cast<C*>(obj); return static_cast<C*>(obj);
} }
@ -341,7 +329,7 @@ rb_check_argc(int actual, int expected)
RB_METHOD(Klass##Get##PropName) \ RB_METHOD(Klass##Get##PropName) \
{ \ { \
RB_UNUSED_PARAM; \ RB_UNUSED_PARAM; \
checkDisposed(self); \ checkDisposed<Klass>(self); \
return rb_iv_get(self, prop_iv); \ return rb_iv_get(self, prop_iv); \
} \ } \
RB_METHOD(Klass##Set##PropName) \ RB_METHOD(Klass##Set##PropName) \
@ -360,7 +348,9 @@ rb_check_argc(int actual, int expected)
{ \ { \
RB_UNUSED_PARAM; \ RB_UNUSED_PARAM; \
Klass *k = getPrivateData<Klass>(self); \ Klass *k = getPrivateData<Klass>(self); \
return value_fun(k->get##PropName()); \ type value; \
GUARD_EXC( value = k->get##PropName(); ) \
return value_fun(value); \
} \ } \
RB_METHOD(Klass##Set##PropName) \ RB_METHOD(Klass##Set##PropName) \
{ \ { \

View File

@ -128,7 +128,7 @@ RB_METHOD(bitmapBlt)
src = getPrivateDataCheck<Bitmap>(srcObj, BitmapType); src = getPrivateDataCheck<Bitmap>(srcObj, BitmapType);
srcRect = getPrivateDataCheck<Rect>(srcRectObj, RectType); srcRect = getPrivateDataCheck<Rect>(srcRectObj, RectType);
GUARD_EXC( b->blt(x, y, src, srcRect->toIntRect(), opacity); ); GUARD_EXC( b->blt(x, y, *src, srcRect->toIntRect(), opacity); );
return self; return self;
} }
@ -151,7 +151,7 @@ RB_METHOD(bitmapStretchBlt)
destRect = getPrivateDataCheck<Rect>(destRectObj, RectType); destRect = getPrivateDataCheck<Rect>(destRectObj, RectType);
srcRect = getPrivateDataCheck<Rect>(srcRectObj, RectType); srcRect = getPrivateDataCheck<Rect>(srcRectObj, RectType);
GUARD_EXC( b->stretchBlt(destRect->toIntRect(), src, srcRect->toIntRect(), opacity); ); GUARD_EXC( b->stretchBlt(destRect->toIntRect(), *src, srcRect->toIntRect(), opacity); );
return self; return self;
} }

View File

@ -64,32 +64,48 @@ RB_METHOD(disposableDispose)
{ {
RB_UNUSED_PARAM; RB_UNUSED_PARAM;
C *c = static_cast<C*>(RTYPEDDATA_DATA(self)); C *d = getPrivateData<C>(self);
if (!d)
return Qnil;
/* Nothing to do if already disposed */ /* Nothing to do if already disposed */
if (!c) if (d->isDisposed())
return Qnil; return Qnil;
disposableDisposeChildren(self); disposableDisposeChildren(self);
delete c; d->dispose();
setPrivateData(self, 0);
return Qnil; return Qnil;
} }
template<class C>
RB_METHOD(disposableIsDisposed) RB_METHOD(disposableIsDisposed)
{ {
RB_UNUSED_PARAM; RB_UNUSED_PARAM;
return rb_bool_new(RTYPEDDATA_DATA(self) == 0); C *d = getPrivateData<C>(self);
if (!d)
return Qtrue;
return rb_bool_new(d->isDisposed());
} }
template<class C> template<class C>
static void disposableBindingInit(VALUE klass) static void disposableBindingInit(VALUE klass)
{ {
_rb_define_method(klass, "dispose", disposableDispose<C>); _rb_define_method(klass, "dispose", disposableDispose<C>);
_rb_define_method(klass, "disposed?", disposableIsDisposed); _rb_define_method(klass, "disposed?", disposableIsDisposed<C>);
}
template<class C>
inline void
checkDisposed(VALUE self)
{
if (disposableIsDisposed<C>(0, 0, self) == Qtrue)
raiseDisposedAccess(self);
} }
#endif // DISPOSABLEBINDING_H #endif // DISPOSABLEBINDING_H

View File

@ -160,6 +160,9 @@ RB_METHOD(FontSetName)
return argv[0]; return argv[0];
} }
template<class C>
static void checkDisposed(VALUE) {}
DEF_PROP_OBJ_VAL(Font, Color, Color, "color") DEF_PROP_OBJ_VAL(Font, Color, Color, "color")
DEF_PROP_OBJ_VAL(Font, Color, OutColor, "out_color") DEF_PROP_OBJ_VAL(Font, Color, OutColor, "out_color")

View File

@ -101,6 +101,8 @@ RB_METHOD(tilemapGetAutotiles)
{ {
RB_UNUSED_PARAM; RB_UNUSED_PARAM;
checkDisposed<Tilemap>(self);
return rb_iv_get(self, "autotiles"); return rb_iv_get(self, "autotiles");
} }
@ -119,7 +121,7 @@ RB_METHOD(tilemapGetViewport)
{ {
RB_UNUSED_PARAM; RB_UNUSED_PARAM;
checkDisposed(self); checkDisposed<Tilemap>(self);
return rb_iv_get(self, "viewport"); return rb_iv_get(self, "viewport");
} }

View File

@ -71,6 +71,8 @@ RB_METHOD(tilemapVXGetBitmapArray)
{ {
RB_UNUSED_PARAM; RB_UNUSED_PARAM;
checkDisposed<TilemapVX>(self);
return rb_iv_get(self, "bitmap_array"); return rb_iv_get(self, "bitmap_array");
} }

View File

@ -35,7 +35,7 @@ RB_METHOD(viewportElementGetViewport)
{ {
RB_UNUSED_PARAM; RB_UNUSED_PARAM;
checkDisposed(self); checkDisposed<C>(self);
return rb_iv_get(self, "viewport"); return rb_iv_get(self, "viewport");
} }

View File

@ -155,7 +155,7 @@ defineClass(mrb_state *mrb, const char *name)
#define DEF_PROP_OBJ_VAL(Klass, PropKlass, PropName, prop_iv) \ #define DEF_PROP_OBJ_VAL(Klass, PropKlass, PropName, prop_iv) \
MRB_METHOD(Klass##Get##PropName) \ MRB_METHOD(Klass##Get##PropName) \
{ \ { \
checkDisposed(mrb, self); \ checkDisposed<Klass>(mrb, self); \
return getProperty(mrb, self, prop_iv); \ return getProperty(mrb, self, prop_iv); \
} \ } \
MRB_METHOD(Klass##Set##PropName) \ MRB_METHOD(Klass##Set##PropName) \
@ -194,7 +194,9 @@ defineClass(mrb_state *mrb, const char *name)
MRB_METHOD(Klass##Get##PropName) \ MRB_METHOD(Klass##Get##PropName) \
{ \ { \
Klass *k = getPrivateData<Klass>(mrb, self); \ Klass *k = getPrivateData<Klass>(mrb, self); \
return mrb_##conv_t##_value(k->get##PropName()); \ mrb_type value; \
GUARD_EXC( value = k->get##PropName(); ) \
return mrb_##conv_t##_value(value); \
} \ } \
MRB_METHOD(Klass##Set##PropName) \ MRB_METHOD(Klass##Set##PropName) \
{ \ { \
@ -256,21 +258,12 @@ getSym(mrb_state *mrb, CommonSymbol sym)
void void
raiseDisposedAccess(mrb_state *mrb, mrb_value self); raiseDisposedAccess(mrb_state *mrb, mrb_value self);
inline void checkDisposed(mrb_state *mrb, mrb_value self)
{
if (!DATA_PTR(self))
raiseDisposedAccess(mrb, self);
}
template<class C> template<class C>
inline C * inline C *
getPrivateData(mrb_state *mrb, mrb_value self) getPrivateData(mrb_state *, mrb_value self)
{ {
C *c = static_cast<C*>(DATA_PTR(self)); C *c = static_cast<C*>(DATA_PTR(self));
if (!c)
raiseDisposedAccess(mrb, self);
return c; return c;
} }

View File

@ -114,7 +114,7 @@ MRB_METHOD(bitmapBlt)
src = getPrivateDataCheck<Bitmap>(mrb, srcObj, BitmapType); src = getPrivateDataCheck<Bitmap>(mrb, srcObj, BitmapType);
srcRect = getPrivateDataCheck<Rect>(mrb, srcRectObj, RectType); srcRect = getPrivateDataCheck<Rect>(mrb, srcRectObj, RectType);
GUARD_EXC( b->blt(x, y, src, srcRect->toIntRect(), opacity); ) GUARD_EXC( b->blt(x, y, *src, srcRect->toIntRect(), opacity); )
return mrb_nil_value(); return mrb_nil_value();
} }
@ -137,7 +137,7 @@ MRB_METHOD(bitmapStretchBlt)
destRect = getPrivateDataCheck<Rect>(mrb, destRectObj, RectType); destRect = getPrivateDataCheck<Rect>(mrb, destRectObj, RectType);
srcRect = getPrivateDataCheck<Rect>(mrb, srcRectObj, RectType); srcRect = getPrivateDataCheck<Rect>(mrb, srcRectObj, RectType);
GUARD_EXC( b->stretchBlt(destRect->toIntRect(), src, srcRect->toIntRect(), opacity); ) GUARD_EXC( b->stretchBlt(destRect->toIntRect(), *src, srcRect->toIntRect(), opacity); )
return mrb_nil_value(); return mrb_nil_value();
} }
@ -280,7 +280,7 @@ MRB_METHOD(bitmapTextSize)
MRB_METHOD(bitmapGetFont) MRB_METHOD(bitmapGetFont)
{ {
checkDisposed(mrb, self); checkDisposed<Bitmap>(mrb, self);
return getProperty(mrb, self, CSfont); return getProperty(mrb, self, CSfont);
} }

View File

@ -66,33 +66,47 @@ disposableDisposeChildren(mrb_state *mrb, mrb_value disp)
template<class C> template<class C>
MRB_METHOD(disposableDispose) MRB_METHOD(disposableDispose)
{ {
C *c = static_cast<C*>(DATA_PTR(self)); C *d = static_cast<C*>(DATA_PTR(self));
/* Nothing to do if already disposed */ if (!d)
if (!c) return mrb_nil_value();
if (d->isDisposed())
return mrb_nil_value(); return mrb_nil_value();
disposableDisposeChildren(mrb, self); disposableDisposeChildren(mrb, self);
delete c; d->dispose();
DATA_PTR(self) = 0;
return mrb_nil_value(); return mrb_nil_value();
} }
template<class C> template<class C>
MRB_METHOD(disposableDisposed) MRB_METHOD(disposableIsDisposed)
{ {
MRB_UNUSED_PARAM; MRB_UNUSED_PARAM;
return mrb_bool_value(DATA_PTR(self) == 0); C *d = static_cast<C*>(DATA_PTR(self));
if (!d)
return mrb_true_value();
return mrb_bool_value(d->isDisposed());
} }
template<class C> template<class C>
static void disposableBindingInit(mrb_state *mrb, RClass *klass) static void disposableBindingInit(mrb_state *mrb, RClass *klass)
{ {
mrb_define_method(mrb, klass, "dispose", disposableDispose<C>, MRB_ARGS_NONE()); mrb_define_method(mrb, klass, "dispose", disposableDispose<C>, MRB_ARGS_NONE());
mrb_define_method(mrb, klass, "disposed?", disposableDisposed<C>, MRB_ARGS_NONE()); mrb_define_method(mrb, klass, "disposed?", disposableIsDisposed<C>, MRB_ARGS_NONE());
}
template<class C>
inline void
checkDisposed(mrb_state *mrb, mrb_value self)
{
if (mrb_test(disposableIsDisposed<C>(0, self)))
raiseDisposedAccess(mrb, self);
} }
#endif // DISPOSABLEBINDING_H #endif // DISPOSABLEBINDING_H

View File

@ -103,6 +103,9 @@ MRB_METHOD(FontSetName)
return name; return name;
} }
template<class C>
static void checkDisposed(mrb_state *, mrb_value) {}
DEF_PROP_I(Font, Size) DEF_PROP_I(Font, Size)
DEF_PROP_B(Font, Bold) DEF_PROP_B(Font, Bold)
DEF_PROP_B(Font, Italic) DEF_PROP_B(Font, Italic)

View File

@ -106,6 +106,8 @@ MRB_METHOD(tilemapInitialize)
MRB_METHOD(tilemapGetAutotiles) MRB_METHOD(tilemapGetAutotiles)
{ {
checkDisposed<Tilemap>(mrb, self);
return getProperty(mrb, self, CSautotiles); return getProperty(mrb, self, CSautotiles);
} }
@ -120,7 +122,7 @@ MRB_METHOD(tilemapUpdate)
MRB_METHOD(tilemapGetViewport) MRB_METHOD(tilemapGetViewport)
{ {
checkDisposed(mrb, self); checkDisposed<Tilemap>(mrb, self);
return getProperty(mrb, self, CSviewport); return getProperty(mrb, self, CSviewport);
} }

View File

@ -32,7 +32,7 @@
template<class C> template<class C>
MRB_METHOD(viewportElementGetViewport) MRB_METHOD(viewportElementGetViewport)
{ {
checkDisposed(mrb, self); checkDisposed<C>(mrb, self);
return getProperty(mrb, self, CSviewport); return getProperty(mrb, self, CSviewport);
} }

View File

@ -297,21 +297,18 @@ Bitmap::Bitmap(const Bitmap &other)
p->gl = shState->texPool().request(other.width(), other.height()); p->gl = shState->texPool().request(other.width(), other.height());
blt(0, 0, &other, rect()); blt(0, 0, other, rect());
} }
Bitmap::~Bitmap() Bitmap::~Bitmap()
{ {
if (p->megaSurface) dispose();
SDL_FreeSurface(p->megaSurface);
else
shState->texPool().release(p->gl);
delete p;
} }
int Bitmap::width() const int Bitmap::width() const
{ {
guardDisposed();
if (p->megaSurface) if (p->megaSurface)
return p->megaSurface->w; return p->megaSurface->w;
@ -320,6 +317,8 @@ int Bitmap::width() const
int Bitmap::height() const int Bitmap::height() const
{ {
guardDisposed();
if (p->megaSurface) if (p->megaSurface)
return p->megaSurface->h; return p->megaSurface->h;
@ -328,14 +327,16 @@ int Bitmap::height() const
IntRect Bitmap::rect() const IntRect Bitmap::rect() const
{ {
guardDisposed();
return IntRect(0, 0, width(), height()); return IntRect(0, 0, width(), height());
} }
void Bitmap::blt(int x, int y, void Bitmap::blt(int x, int y,
const Bitmap *source, IntRect rect, const Bitmap &source, IntRect rect,
int opacity) int opacity)
{ {
if (!source) if (source.isDisposed())
return; return;
// FIXME: RGSS allows the source rect to both lie outside // FIXME: RGSS allows the source rect to both lie outside
@ -344,23 +345,25 @@ void Bitmap::blt(int x, int y,
// doesn't fix anything for a direct stretch_blt call). // doesn't fix anything for a direct stretch_blt call).
/* Clamp rect to source bitmap size */ /* Clamp rect to source bitmap size */
if (rect.x + rect.w > source->width()) if (rect.x + rect.w > source.width())
rect.w = source->width() - rect.x; rect.w = source.width() - rect.x;
if (rect.y + rect.h > source->height()) if (rect.y + rect.h > source.height())
rect.h = source->height() - rect.y; rect.h = source.height() - rect.y;
stretchBlt(IntRect(x, y, rect.w, rect.h), stretchBlt(IntRect(x, y, rect.w, rect.h),
source, rect, opacity); source, rect, opacity);
} }
void Bitmap::stretchBlt(const IntRect &destRect, void Bitmap::stretchBlt(const IntRect &destRect,
const Bitmap *source, const IntRect &sourceRect, const Bitmap &source, const IntRect &sourceRect,
int opacity) int opacity)
{ {
guardDisposed();
GUARD_MEGA; GUARD_MEGA;
if (!source) if (source.isDisposed())
return; return;
opacity = clamp(opacity, 0, 255); opacity = clamp(opacity, 0, 255);
@ -368,13 +371,13 @@ void Bitmap::stretchBlt(const IntRect &destRect,
if (opacity == 0) if (opacity == 0)
return; return;
if (source->megaSurface()) if (source.megaSurface())
{ {
/* Don't do transparent blits for now */ /* Don't do transparent blits for now */
if (opacity < 255) if (opacity < 255)
source->ensureNonMega(); source.ensureNonMega();
SDL_Surface *srcSurf = source->megaSurface(); SDL_Surface *srcSurf = source.megaSurface();
SDL_Rect srcRect = sourceRect; SDL_Rect srcRect = sourceRect;
SDL_Rect dstRect = destRect; SDL_Rect dstRect = destRect;
@ -421,7 +424,7 @@ void Bitmap::stretchBlt(const IntRect &destRect,
{ {
/* Fast blit */ /* Fast blit */
GLMeta::blitBegin(p->gl); GLMeta::blitBegin(p->gl);
GLMeta::blitSource(source->p->gl); GLMeta::blitSource(source.p->gl);
GLMeta::blitRectangle(sourceRect, destRect); GLMeta::blitRectangle(sourceRect, destRect);
GLMeta::blitEnd(); GLMeta::blitEnd();
} }
@ -437,10 +440,10 @@ void Bitmap::stretchBlt(const IntRect &destRect,
GLMeta::blitRectangle(destRect, Vec2i()); GLMeta::blitRectangle(destRect, Vec2i());
GLMeta::blitEnd(); GLMeta::blitEnd();
FloatRect bltSubRect((float) sourceRect.x / source->width(), FloatRect bltSubRect((float) sourceRect.x / source.width(),
(float) sourceRect.y / source->height(), (float) sourceRect.y / source.height(),
((float) source->width() / sourceRect.w) * ((float) destRect.w / gpTex.width), ((float) source.width() / sourceRect.w) * ((float) destRect.w / gpTex.width),
((float) source->height() / sourceRect.h) * ((float) destRect.h / gpTex.height)); ((float) source.height() / sourceRect.h) * ((float) destRect.h / gpTex.height));
BltShader &shader = shState->shaders().blt; BltShader &shader = shState->shaders().blt;
shader.bind(); shader.bind();
@ -452,7 +455,7 @@ void Bitmap::stretchBlt(const IntRect &destRect,
quad.setTexPosRect(sourceRect, destRect); quad.setTexPosRect(sourceRect, destRect);
quad.setColor(Vec4(1, 1, 1, normOpacity)); quad.setColor(Vec4(1, 1, 1, normOpacity));
source->p->bindTexture(shader); source.p->bindTexture(shader);
p->bindFBO(); p->bindFBO();
p->pushSetViewport(shader); p->pushSetViewport(shader);
@ -475,6 +478,8 @@ void Bitmap::fillRect(int x, int y,
void Bitmap::fillRect(const IntRect &rect, const Vec4 &color) void Bitmap::fillRect(const IntRect &rect, const Vec4 &color)
{ {
guardDisposed();
GUARD_MEGA; GUARD_MEGA;
p->fillRect(rect, color); p->fillRect(rect, color);
@ -501,6 +506,8 @@ void Bitmap::gradientFillRect(const IntRect &rect,
const Vec4 &color1, const Vec4 &color2, const Vec4 &color1, const Vec4 &color2,
bool vertical) bool vertical)
{ {
guardDisposed();
GUARD_MEGA; GUARD_MEGA;
SimpleColorShader &shader = shState->shaders().simpleColor; SimpleColorShader &shader = shState->shaders().simpleColor;
@ -545,6 +552,8 @@ void Bitmap::clearRect(int x, int y, int width, int height)
void Bitmap::clearRect(const IntRect &rect) void Bitmap::clearRect(const IntRect &rect)
{ {
guardDisposed();
GUARD_MEGA; GUARD_MEGA;
p->fillRect(rect, Vec4()); p->fillRect(rect, Vec4());
@ -554,6 +563,8 @@ void Bitmap::clearRect(const IntRect &rect)
void Bitmap::blur() void Bitmap::blur()
{ {
guardDisposed();
GUARD_MEGA; GUARD_MEGA;
Quad &quad = shState->gpQuad(); Quad &quad = shState->gpQuad();
@ -597,6 +608,8 @@ void Bitmap::blur()
void Bitmap::radialBlur(int angle, int divisions) void Bitmap::radialBlur(int angle, int divisions)
{ {
guardDisposed();
GUARD_MEGA; GUARD_MEGA;
angle = clamp<int>(angle, 0, 359); angle = clamp<int>(angle, 0, 359);
@ -690,6 +703,8 @@ void Bitmap::radialBlur(int angle, int divisions)
void Bitmap::clear() void Bitmap::clear()
{ {
guardDisposed();
GUARD_MEGA; GUARD_MEGA;
p->bindFBO(); p->bindFBO();
@ -707,6 +722,8 @@ void Bitmap::clear()
Color Bitmap::getPixel(int x, int y) const Color Bitmap::getPixel(int x, int y) const
{ {
guardDisposed();
GUARD_MEGA; GUARD_MEGA;
if (x < 0 || y < 0 || x >= width() || y >= height()) if (x < 0 || y < 0 || x >= width() || y >= height())
@ -737,6 +754,8 @@ Color Bitmap::getPixel(int x, int y) const
void Bitmap::setPixel(int x, int y, const Color &color) void Bitmap::setPixel(int x, int y, const Color &color)
{ {
guardDisposed();
GUARD_MEGA; GUARD_MEGA;
uint8_t pixel[] = uint8_t pixel[] =
@ -757,6 +776,8 @@ void Bitmap::setPixel(int x, int y, const Color &color)
void Bitmap::hueChange(int hue) void Bitmap::hueChange(int hue)
{ {
guardDisposed();
GUARD_MEGA; GUARD_MEGA;
if ((hue % 360) == 0) if ((hue % 360) == 0)
@ -818,6 +839,8 @@ static std::string fixupString(const char *str)
void Bitmap::drawText(const IntRect &rect, const char *str, int align) void Bitmap::drawText(const IntRect &rect, const char *str, int align)
{ {
guardDisposed();
GUARD_MEGA; GUARD_MEGA;
std::string fixed = fixupString(str); std::string fixed = fixupString(str);
@ -1046,6 +1069,8 @@ static uint16_t utf8_to_ucs2(const char *_input,
IntRect Bitmap::textSize(const char *str) IntRect Bitmap::textSize(const char *str)
{ {
guardDisposed();
GUARD_MEGA; GUARD_MEGA;
TTF_Font *font = p->font->getSdlFont(); TTF_Font *font = p->font->getSdlFont();
@ -1092,6 +1117,9 @@ SDL_Surface *Bitmap::megaSurface() const
void Bitmap::ensureNonMega() const void Bitmap::ensureNonMega() const
{ {
if (isDisposed())
return;
GUARD_MEGA; GUARD_MEGA;
} }
@ -1104,3 +1132,13 @@ void Bitmap::taintArea(const IntRect &rect)
{ {
p->addTaintedArea(rect); p->addTaintedArea(rect);
} }
void Bitmap::releaseResources()
{
if (p->megaSurface)
SDL_FreeSurface(p->megaSurface);
else
shState->texPool().release(p->gl);
delete p;
}

View File

@ -49,11 +49,11 @@ public:
IntRect rect() const; IntRect rect() const;
void blt(int x, int y, void blt(int x, int y,
const Bitmap *source, IntRect rect, const Bitmap &source, IntRect rect,
int opacity = 255); int opacity = 255);
void stretchBlt(const IntRect &destRect, void stretchBlt(const IntRect &destRect,
const Bitmap *source, const IntRect &sourceRect, const Bitmap &source, const IntRect &sourceRect,
int opacity = 255); int opacity = 255);
void fillRect(int x, int y, void fillRect(int x, int y,
@ -120,6 +120,9 @@ public:
sigc::signal<void> modified; sigc::signal<void> modified;
private: private:
void releaseResources();
const char *klassName() const { return "bitmap"; }
BitmapPrivate *p; BitmapPrivate *p;
}; };

View File

@ -24,69 +24,65 @@
#include "exception.h" #include "exception.h"
#include <assert.h>
#include <sigc++/signal.h> #include <sigc++/signal.h>
#include <sigc++/connection.h> #include <sigc++/connection.h>
class Disposable class Disposable
{ {
public: public:
~Disposable() Disposable()
: disposed(false)
{}
virtual ~Disposable()
{ {
assert(disposed);
}
void dispose()
{
if (disposed)
return;
releaseResources();
disposed = true;
wasDisposed(); wasDisposed();
} }
sigc::signal<void> wasDisposed; bool isDisposed() const
};
/* A helper struct which monitors the dispose signal of
* properties, and automatically sets the prop pointer to
* null. Can call an optional notify method when prop is
* nulled */
template<class C, typename P>
struct DisposeWatch
{
typedef void (C::*NotifyFun)();
DisposeWatch(C &owner, P *&propLocation, NotifyFun notify = 0)
: owner(owner),
notify(notify),
propLocation(propLocation)
{}
~DisposeWatch()
{ {
dispCon.disconnect(); return disposed;
} }
/* Call this when a new object was set for the prop */ sigc::signal<void> wasDisposed;
void update(Disposable *prop)
protected:
void guardDisposed() const
{ {
dispCon.disconnect(); if (isDisposed())
throw Exception(Exception::RGSSError,
if (!prop) "disposed %s", klassName());
return;
dispCon = prop->wasDisposed.connect
(sigc::mem_fun(this, &DisposeWatch::onDisposed));
} }
private: private:
/* The object owning the prop (and this helper) */ virtual void releaseResources() = 0;
C &owner; virtual const char *klassName() const = 0;
/* Optional notify method */
const NotifyFun notify;
/* Location of the prop pointer inside the owner */
P * &propLocation;
sigc::connection dispCon;
void onDisposed() bool disposed;
{
dispCon.disconnect();
propLocation = 0;
if (notify)
(owner.*notify)();
}
}; };
template<class C>
inline bool
nullOrDisposed(const C *d)
{
if (!d)
return true;
if (d->isDisposed())
return true;
return false;
}
#endif // DISPOSABLE_H #endif // DISPOSABLE_H

View File

@ -341,8 +341,7 @@ void Font::setSize(int value)
p->sdlFont = 0; p->sdlFont = 0;
} }
#undef CHK_DISP static void guardDisposed() {}
#define CHK_DISP
DEF_ATTR_RD_SIMPLE(Font, Size, int, p->size) DEF_ATTR_RD_SIMPLE(Font, Size, int, p->size)
DEF_ATTR_SIMPLE (Font, Bold, bool, p->bold) DEF_ATTR_SIMPLE (Font, Bold, bool, p->bold)

View File

@ -674,11 +674,7 @@ void Graphics::frameReset()
p->fpsLimiter.resetFrameAdjust(); p->fpsLimiter.resetFrameAdjust();
} }
#undef RET_IF_DISP static void guardDisposed() {}
#define RET_IF_DISP(x)
#undef CHK_DISP
#define CHK_DISP
DEF_ATTR_RD_SIMPLE(Graphics, FrameRate, int, p->frameRate) DEF_ATTR_RD_SIMPLE(Graphics, FrameRate, int, p->frameRate)

View File

@ -45,7 +45,6 @@ static float fwrap(float value, float range)
struct PlanePrivate struct PlanePrivate
{ {
Bitmap *bitmap; Bitmap *bitmap;
DisposeWatch<PlanePrivate, Bitmap> bitmapWatch;
NormValue opacity; NormValue opacity;
BlendType blendType; BlendType blendType;
@ -67,7 +66,6 @@ struct PlanePrivate
PlanePrivate() PlanePrivate()
: bitmap(0), : bitmap(0),
bitmapWatch(*this, bitmap),
opacity(255), opacity(255),
blendType(BlendNormal), blendType(BlendNormal),
color(&tmp.color), color(&tmp.color),
@ -103,7 +101,7 @@ struct PlanePrivate
return; return;
} }
if (!bitmap) if (nullOrDisposed(bitmap))
return; return;
/* Scaled (zoomed) bitmap dimensions */ /* Scaled (zoomed) bitmap dimensions */
@ -169,15 +167,14 @@ DEF_ATTR_OBJ_VALUE(Plane, Tone, Tone*, p->tone)
Plane::~Plane() Plane::~Plane()
{ {
unlink(); dispose();
delete p;
} }
void Plane::setBitmap(Bitmap *value) void Plane::setBitmap(Bitmap *value)
{ {
guardDisposed();
p->bitmap = value; p->bitmap = value;
p->bitmapWatch.update(value);
if (!value) if (!value)
return; return;
@ -187,6 +184,8 @@ void Plane::setBitmap(Bitmap *value)
void Plane::setOX(int value) void Plane::setOX(int value)
{ {
guardDisposed();
if (p->ox == value) if (p->ox == value)
return; return;
@ -196,6 +195,8 @@ void Plane::setOX(int value)
void Plane::setOY(int value) void Plane::setOY(int value)
{ {
guardDisposed();
if (p->oy == value) if (p->oy == value)
return; return;
@ -205,6 +206,8 @@ void Plane::setOY(int value)
void Plane::setZoomX(float value) void Plane::setZoomX(float value)
{ {
guardDisposed();
if (p->zoomX == value) if (p->zoomX == value)
return; return;
@ -214,6 +217,8 @@ void Plane::setZoomX(float value)
void Plane::setZoomY(float value) void Plane::setZoomY(float value)
{ {
guardDisposed();
if (p->zoomY == value) if (p->zoomY == value)
return; return;
@ -223,6 +228,8 @@ void Plane::setZoomY(float value)
void Plane::setBlendType(int value) void Plane::setBlendType(int value)
{ {
guardDisposed();
switch (value) switch (value)
{ {
default : default :
@ -246,7 +253,7 @@ void Plane::initDynAttribs()
void Plane::draw() void Plane::draw()
{ {
if (!p->bitmap) if (nullOrDisposed(p->bitmap))
return; return;
if (!p->opacity) if (!p->opacity)
@ -301,3 +308,10 @@ void Plane::onGeometryChange(const Scene::Geometry &geo)
p->sceneGeo = geo; p->sceneGeo = geo;
p->quadSourceDirty = true; p->quadSourceDirty = true;
} }
void Plane::releaseResources()
{
unlink();
delete p;
}

View File

@ -54,6 +54,11 @@ private:
void draw(); void draw();
void onGeometryChange(const Scene::Geometry &); void onGeometryChange(const Scene::Geometry &);
void releaseResources();
const char *klassName() const { return "plane"; }
ABOUT_TO_ACCESS_DISP
}; };
#endif // PLANE_H #endif // PLANE_H

View File

@ -134,11 +134,15 @@ void SceneElement::setScene(Scene &scene)
int SceneElement::getZ() const int SceneElement::getZ() const
{ {
aboutToAccess();
return z; return z;
} }
void SceneElement::setZ(int value) void SceneElement::setZ(int value)
{ {
aboutToAccess();
if (z == value) if (z == value)
return; return;
@ -148,11 +152,15 @@ void SceneElement::setZ(int value)
bool SceneElement::getVisible() const bool SceneElement::getVisible() const
{ {
aboutToAccess();
return visible; return visible;
} }
void SceneElement::setVisible(bool value) void SceneElement::setVisible(bool value)
{ {
aboutToAccess();
visible = value; visible = value;
} }

View File

@ -79,6 +79,8 @@ public:
DECL_ATTR_VIRT( Z, int ) DECL_ATTR_VIRT( Z, int )
DECL_ATTR_VIRT( Visible, bool ) DECL_ATTR_VIRT( Visible, bool )
virtual void aboutToAccess() const = 0;
protected: protected:
/* A bit about OpenGL state: /* A bit about OpenGL state:
* *
@ -134,4 +136,10 @@ private:
int spriteY; int spriteY;
}; };
#define ABOUT_TO_ACCESS_NOOP \
void aboutToAccess() const {}
#define ABOUT_TO_ACCESS_DISP \
void aboutToAccess() const { guardDisposed(); }
#endif // SCENE_H #endif // SCENE_H

View File

@ -43,7 +43,6 @@
struct SpritePrivate struct SpritePrivate
{ {
Bitmap *bitmap; Bitmap *bitmap;
DisposeWatch<SpritePrivate, Bitmap> bitmapWatch;
Quad quad; Quad quad;
Transform trans; Transform trans;
@ -87,7 +86,6 @@ struct SpritePrivate
SpritePrivate() SpritePrivate()
: bitmap(0), : bitmap(0),
bitmapWatch(*this, bitmap),
srcRect(&tmp.rect), srcRect(&tmp.rect),
mirrored(false), mirrored(false),
bushDepth(0), bushDepth(0),
@ -159,7 +157,7 @@ struct SpritePrivate
{ {
isVisible = false; isVisible = false;
if (!bitmap) if (nullOrDisposed(bitmap))
return; return;
if (!opacity) if (!opacity)
@ -209,7 +207,7 @@ struct SpritePrivate
void updateWave() void updateWave()
{ {
if (!bitmap) if (nullOrDisposed(bitmap))
return; return;
if (wave.amp == 0) if (wave.amp == 0)
@ -298,9 +296,7 @@ Sprite::Sprite(Viewport *viewport)
Sprite::~Sprite() Sprite::~Sprite()
{ {
unlink(); dispose();
delete p;
} }
DEF_ATTR_RD_SIMPLE(Sprite, Bitmap, Bitmap*, p->bitmap) DEF_ATTR_RD_SIMPLE(Sprite, Bitmap, Bitmap*, p->bitmap)
@ -330,11 +326,12 @@ DEF_ATTR_OBJ_VALUE(Sprite, Tone, Tone*, p->tone)
void Sprite::setBitmap(Bitmap *bitmap) void Sprite::setBitmap(Bitmap *bitmap)
{ {
guardDisposed();
if (p->bitmap == bitmap) if (p->bitmap == bitmap)
return; return;
p->bitmap = bitmap; p->bitmap = bitmap;
p->bitmapWatch.update(bitmap);
if (!bitmap) if (!bitmap)
return; return;
@ -350,6 +347,8 @@ void Sprite::setBitmap(Bitmap *bitmap)
void Sprite::setX(int value) void Sprite::setX(int value)
{ {
guardDisposed();
if (p->trans.getPosition().x == value) if (p->trans.getPosition().x == value)
return; return;
@ -358,6 +357,8 @@ void Sprite::setX(int value)
void Sprite::setY(int value) void Sprite::setY(int value)
{ {
guardDisposed();
if (p->trans.getPosition().y == value) if (p->trans.getPosition().y == value)
return; return;
@ -372,6 +373,8 @@ void Sprite::setY(int value)
void Sprite::setOX(int value) void Sprite::setOX(int value)
{ {
guardDisposed();
if (p->trans.getOrigin().x == value) if (p->trans.getOrigin().x == value)
return; return;
@ -380,6 +383,8 @@ void Sprite::setOX(int value)
void Sprite::setOY(int value) void Sprite::setOY(int value)
{ {
guardDisposed();
if (p->trans.getOrigin().y == value) if (p->trans.getOrigin().y == value)
return; return;
@ -388,6 +393,8 @@ void Sprite::setOY(int value)
void Sprite::setZoomX(float value) void Sprite::setZoomX(float value)
{ {
guardDisposed();
if (p->trans.getScale().x == value) if (p->trans.getScale().x == value)
return; return;
@ -396,6 +403,8 @@ void Sprite::setZoomX(float value)
void Sprite::setZoomY(float value) void Sprite::setZoomY(float value)
{ {
guardDisposed();
if (p->trans.getScale().y == value) if (p->trans.getScale().y == value)
return; return;
@ -408,6 +417,8 @@ void Sprite::setZoomY(float value)
void Sprite::setAngle(float value) void Sprite::setAngle(float value)
{ {
guardDisposed();
if (p->trans.getRotation() == value) if (p->trans.getRotation() == value)
return; return;
@ -416,6 +427,8 @@ void Sprite::setAngle(float value)
void Sprite::setMirror(bool mirrored) void Sprite::setMirror(bool mirrored)
{ {
guardDisposed();
if (p->mirrored == mirrored) if (p->mirrored == mirrored)
return; return;
@ -425,6 +438,8 @@ void Sprite::setMirror(bool mirrored)
void Sprite::setBushDepth(int value) void Sprite::setBushDepth(int value)
{ {
guardDisposed();
if (p->bushDepth == value) if (p->bushDepth == value)
return; return;
@ -434,6 +449,8 @@ void Sprite::setBushDepth(int value)
void Sprite::setBlendType(int type) void Sprite::setBlendType(int type)
{ {
guardDisposed();
switch (type) switch (type)
{ {
default : default :
@ -452,6 +469,7 @@ void Sprite::setBlendType(int type)
#define DEF_WAVE_SETTER(Name, name, type) \ #define DEF_WAVE_SETTER(Name, name, type) \
void Sprite::setWave##Name(type value) \ void Sprite::setWave##Name(type value) \
{ \ { \
guardDisposed(); \
if (p->wave.name == value) \ if (p->wave.name == value) \
return; \ return; \
p->wave.name = value; \ p->wave.name = value; \
@ -477,6 +495,8 @@ void Sprite::initDynAttribs()
/* Flashable */ /* Flashable */
void Sprite::update() void Sprite::update()
{ {
guardDisposed();
Flashable::update(); Flashable::update();
p->wave.phase += p->wave.speed / 180; p->wave.phase += p->wave.speed / 180;
@ -556,3 +576,10 @@ void Sprite::onGeometryChange(const Scene::Geometry &geo)
p->sceneRect.w = geo.rect.w; p->sceneRect.w = geo.rect.w;
p->sceneRect.h = geo.rect.h; p->sceneRect.h = geo.rect.h;
} }
void Sprite::releaseResources()
{
unlink();
delete p;
}

View File

@ -74,6 +74,11 @@ private:
void draw(); void draw();
void onGeometryChange(const Scene::Geometry &); void onGeometryChange(const Scene::Geometry &);
void releaseResources();
const char *klassName() const { return "sprite"; }
ABOUT_TO_ACCESS_DISP
}; };
#endif // SPRITE_H #endif // SPRITE_H

View File

@ -291,7 +291,7 @@ void build(TEXFBO &tf, Bitmap *bitmaps[BM_COUNT])
Bitmap *bm; Bitmap *bm;
#define EXEC_BLITS(part) \ #define EXEC_BLITS(part) \
if ((bm = bitmaps[BM_##part])) \ if (!nullOrDisposed(bm = bitmaps[BM_##part])) \
{ \ { \
GLMeta::blitSource(bm->getGLTypes()); \ GLMeta::blitSource(bm->getGLTypes()); \
for (size_t i = 0; i < blits##part##N; ++i) \ for (size_t i = 0; i < blits##part##N; ++i) \

View File

@ -38,7 +38,6 @@
#include "tileatlas.h" #include "tileatlas.h"
#include <sigc++/connection.h> #include <sigc++/connection.h>
#include <sigc++/bind.h>
#include <string.h> #include <string.h>
#include <stdint.h> #include <stdint.h>
@ -204,6 +203,8 @@ struct GroundLayer : public ViewportElement
void drawFlashInt(); void drawFlashInt();
void onGeometryChange(const Scene::Geometry &geo); void onGeometryChange(const Scene::Geometry &geo);
ABOUT_TO_ACCESS_NOOP
}; };
struct ZLayer : public ViewportElement struct ZLayer : public ViewportElement
@ -232,6 +233,8 @@ struct ZLayer : public ViewportElement
void initUpdateZ(); void initUpdateZ();
void finiUpdateZ(ZLayer *prev); void finiUpdateZ(ZLayer *prev);
ABOUT_TO_ACCESS_NOOP
}; };
struct TilemapPrivate struct TilemapPrivate
@ -242,7 +245,6 @@ struct TilemapPrivate
Bitmap *autotiles[autotileCount]; Bitmap *autotiles[autotileCount];
Bitmap *tileset; Bitmap *tileset;
DisposeWatch<TilemapPrivate, Bitmap> tilesetWatch;
Table *mapData; Table *mapData;
Table *flashData; Table *flashData;
@ -347,7 +349,6 @@ struct TilemapPrivate
TilemapPrivate(Viewport *viewport) TilemapPrivate(Viewport *viewport)
: viewport(viewport), : viewport(viewport),
tileset(0), tileset(0),
tilesetWatch(*this, tileset),
mapData(0), mapData(0),
flashData(0), flashData(0),
priorities(0), priorities(0),
@ -432,7 +433,7 @@ struct TilemapPrivate
void updateAtlasInfo() void updateAtlasInfo()
{ {
if (!tileset) if (nullOrDisposed(tileset))
{ {
atlas.size = Vec2i(); atlas.size = Vec2i();
return; return;
@ -458,7 +459,7 @@ struct TilemapPrivate
for (int i = 0; i < autotileCount; ++i) for (int i = 0; i < autotileCount; ++i)
{ {
if (!autotiles[i]) if (nullOrDisposed(autotiles[i]))
continue; continue;
if (autotiles[i]->megaSurface()) if (autotiles[i]->megaSurface())
@ -506,20 +507,10 @@ struct TilemapPrivate
flashDirty = true; flashDirty = true;
} }
void onAutotileDisposed(int i)
{
/* RMXP actually crashes if an active autotile bitmap is disposed..
* let's not crash ourselves, for consistency's sake */
autotiles[i] = 0;
autotilesCon[i].disconnect();
autotilesDispCon[i].disconnect();
atlasDirty = true;
}
/* Checks for the minimum amount of data needed to display */ /* Checks for the minimum amount of data needed to display */
bool verifyResources() bool verifyResources()
{ {
if (!tileset) if (nullOrDisposed(tileset))
return false; return false;
if (!mapData) if (!mapData)
@ -1208,7 +1199,7 @@ void Tilemap::Autotiles::set(int i, Bitmap *bitmap)
p->autotilesDispCon[i].disconnect(); p->autotilesDispCon[i].disconnect();
p->autotilesDispCon[i] = bitmap->wasDisposed.connect p->autotilesDispCon[i] = bitmap->wasDisposed.connect
(sigc::bind(sigc::mem_fun(p, &TilemapPrivate::onAutotileDisposed), i)); (sigc::mem_fun(p, &TilemapPrivate::invalidateAtlasContents));
p->updateAutotileInfo(); p->updateAutotileInfo();
} }
@ -1229,11 +1220,13 @@ Tilemap::Tilemap(Viewport *viewport)
Tilemap::~Tilemap() Tilemap::~Tilemap()
{ {
delete p; dispose();
} }
void Tilemap::update() void Tilemap::update()
{ {
guardDisposed();
if (!p->tilemapReady) if (!p->tilemapReady)
return; return;
@ -1253,6 +1246,8 @@ void Tilemap::update()
Tilemap::Autotiles &Tilemap::getAutotiles() const Tilemap::Autotiles &Tilemap::getAutotiles() const
{ {
guardDisposed();
return p->autotilesProxy; return p->autotilesProxy;
} }
@ -1267,11 +1262,12 @@ DEF_ATTR_RD_SIMPLE(Tilemap, OY, int, p->offset.y)
void Tilemap::setTileset(Bitmap *value) void Tilemap::setTileset(Bitmap *value)
{ {
guardDisposed();
if (p->tileset == value) if (p->tileset == value)
return; return;
p->tileset = value; p->tileset = value;
p->tilesetWatch.update(value);
if (!value) if (!value)
return; return;
@ -1286,6 +1282,8 @@ void Tilemap::setTileset(Bitmap *value)
void Tilemap::setMapData(Table *value) void Tilemap::setMapData(Table *value)
{ {
guardDisposed();
if (p->mapData == value) if (p->mapData == value)
return; return;
@ -1302,6 +1300,8 @@ void Tilemap::setMapData(Table *value)
void Tilemap::setFlashData(Table *value) void Tilemap::setFlashData(Table *value)
{ {
guardDisposed();
if (p->flashData == value) if (p->flashData == value)
return; return;
@ -1318,6 +1318,8 @@ void Tilemap::setFlashData(Table *value)
void Tilemap::setPriorities(Table *value) void Tilemap::setPriorities(Table *value)
{ {
guardDisposed();
if (p->priorities == value) if (p->priorities == value)
return; return;
@ -1334,6 +1336,8 @@ void Tilemap::setPriorities(Table *value)
void Tilemap::setVisible(bool value) void Tilemap::setVisible(bool value)
{ {
guardDisposed();
if (p->visible == value) if (p->visible == value)
return; return;
@ -1349,6 +1353,8 @@ void Tilemap::setVisible(bool value)
void Tilemap::setOX(int value) void Tilemap::setOX(int value)
{ {
guardDisposed();
if (p->offset.x == value) if (p->offset.x == value)
return; return;
@ -1359,6 +1365,8 @@ void Tilemap::setOX(int value)
void Tilemap::setOY(int value) void Tilemap::setOY(int value)
{ {
guardDisposed();
if (p->offset.y == value) if (p->offset.y == value)
return; return;
@ -1367,3 +1375,8 @@ void Tilemap::setOY(int value)
p->zOrderDirty = true; p->zOrderDirty = true;
p->mapViewportDirty = true; p->mapViewportDirty = true;
} }
void Tilemap::releaseResources()
{
delete p;
}

View File

@ -68,6 +68,9 @@ public:
private: private:
TilemapPrivate *p; TilemapPrivate *p;
void releaseResources();
const char *klassName() const { return "tilemap"; }
}; };
#endif // TILEMAP_H #endif // TILEMAP_H

View File

@ -36,7 +36,6 @@
#include <vector> #include <vector>
#include <sigc++/connection.h> #include <sigc++/connection.h>
#include <sigc++/bind.h>
// FIXME: Implement flash // FIXME: Implement flash
@ -95,6 +94,8 @@ struct TilemapVXPrivate : public ViewportElement, TileAtlasVX::Reader
{ {
p->drawAbove(); p->drawAbove();
} }
ABOUT_TO_ACCESS_NOOP
}; };
AboveLayer above; AboveLayer above;
@ -154,15 +155,6 @@ struct TilemapVXPrivate : public ViewportElement, TileAtlasVX::Reader
atlasDirty = true; atlasDirty = true;
} }
void onBitmapDisposed(int i)
{
bitmaps[i] = 0;
bmChangedCons[i].disconnect();
bmDisposedCons[i].disconnect();
atlasDirty = true;
}
void invalidateBuffers() void invalidateBuffers()
{ {
buffersDirty = true; buffersDirty = true;
@ -290,7 +282,7 @@ struct TilemapVXPrivate : public ViewportElement, TileAtlasVX::Reader
{ {
ShaderBase *shader; ShaderBase *shader;
if (bitmaps[BM_A1] != 0) if (!nullOrDisposed(bitmaps[BM_A1]))
{ {
/* Animated tileset */ /* Animated tileset */
TilemapVXShader &tmShader = shState->shaders().tilemapVX; TilemapVXShader &tmShader = shState->shaders().tilemapVX;
@ -351,6 +343,8 @@ struct TilemapVXPrivate : public ViewportElement, TileAtlasVX::Reader
mapViewportDirty = true; mapViewportDirty = true;
} }
ABOUT_TO_ACCESS_NOOP
/* TileAtlasVX::Reader */ /* TileAtlasVX::Reader */
void onQuads(const FloatRect *t, const FloatRect *p, void onQuads(const FloatRect *t, const FloatRect *p,
size_t n, bool overPlayer) size_t n, bool overPlayer)
@ -379,7 +373,7 @@ void TilemapVX::BitmapArray::set(int i, Bitmap *bitmap)
p->bmDisposedCons[i].disconnect(); p->bmDisposedCons[i].disconnect();
p->bmDisposedCons[i] = bitmap->wasDisposed.connect p->bmDisposedCons[i] = bitmap->wasDisposed.connect
(sigc::bind(sigc::mem_fun(p, &TilemapVXPrivate::onBitmapDisposed), i)); (sigc::mem_fun(p, &TilemapVXPrivate::invalidateAtlas));
} }
Bitmap *TilemapVX::BitmapArray::get(int i) const Bitmap *TilemapVX::BitmapArray::get(int i) const
@ -398,11 +392,13 @@ TilemapVX::TilemapVX(Viewport *viewport)
TilemapVX::~TilemapVX() TilemapVX::~TilemapVX()
{ {
delete p; dispose();
} }
void TilemapVX::update() void TilemapVX::update()
{ {
guardDisposed();
if (++p->frameIdx >= 30*3*4) if (++p->frameIdx >= 30*3*4)
p->frameIdx = 0; p->frameIdx = 0;
@ -419,6 +415,8 @@ void TilemapVX::update()
TilemapVX::BitmapArray &TilemapVX::getBitmapArray() const TilemapVX::BitmapArray &TilemapVX::getBitmapArray() const
{ {
guardDisposed();
return p->bitmapsProxy; return p->bitmapsProxy;
} }
@ -430,22 +428,30 @@ DEF_ATTR_RD_SIMPLE(TilemapVX, OY, int, p->offset.y)
Viewport *TilemapVX::getViewport() const Viewport *TilemapVX::getViewport() const
{ {
guardDisposed();
return p->getViewport(); return p->getViewport();
} }
bool TilemapVX::getVisible() const bool TilemapVX::getVisible() const
{ {
guardDisposed();
return p->getVisible(); return p->getVisible();
} }
void TilemapVX::setViewport(Viewport *value) void TilemapVX::setViewport(Viewport *value)
{ {
guardDisposed();
p->setViewport(value); p->setViewport(value);
p->above.setViewport(value); p->above.setViewport(value);
} }
void TilemapVX::setMapData(Table *value) void TilemapVX::setMapData(Table *value)
{ {
guardDisposed();
if (p->mapData == value) if (p->mapData == value)
return; return;
@ -459,6 +465,8 @@ void TilemapVX::setMapData(Table *value)
void TilemapVX::setFlashData(Table *value) void TilemapVX::setFlashData(Table *value)
{ {
guardDisposed();
if (p->flashData == value) if (p->flashData == value)
return; return;
@ -467,6 +475,8 @@ void TilemapVX::setFlashData(Table *value)
void TilemapVX::setFlags(Table *value) void TilemapVX::setFlags(Table *value)
{ {
guardDisposed();
if (p->flags == value) if (p->flags == value)
return; return;
@ -480,12 +490,16 @@ void TilemapVX::setFlags(Table *value)
void TilemapVX::setVisible(bool value) void TilemapVX::setVisible(bool value)
{ {
guardDisposed();
p->setVisible(value); p->setVisible(value);
p->above.setVisible(value); p->above.setVisible(value);
} }
void TilemapVX::setOX(int value) void TilemapVX::setOX(int value)
{ {
guardDisposed();
if (p->offset.x == value) if (p->offset.x == value)
return; return;
@ -495,9 +509,16 @@ void TilemapVX::setOX(int value)
void TilemapVX::setOY(int value) void TilemapVX::setOY(int value)
{ {
guardDisposed();
if (p->offset.y == value) if (p->offset.y == value)
return; return;
p->offset.y = value; p->offset.y = value;
p->mapViewportDirty = true; p->mapViewportDirty = true;
} }
void TilemapVX::releaseResources()
{
delete p;
}

View File

@ -66,6 +66,9 @@ public:
private: private:
TilemapVXPrivate *p; TilemapVXPrivate *p;
void releaseResources();
const char *klassName() const { return "tilemap"; }
}; };
#endif // TILEMAPVX_H #endif // TILEMAPVX_H

View File

@ -119,6 +119,7 @@ inline bool contains(const C &c, const V &v)
#define DEF_ATTR_RD_SIMPLE_DETAILED(klass, name, type, location, keyword1) \ #define DEF_ATTR_RD_SIMPLE_DETAILED(klass, name, type, location, keyword1) \
type klass :: get##name() keyword1 \ type klass :: get##name() keyword1 \
{ \ { \
guardDisposed(); \
return location; \ return location; \
} }
@ -126,6 +127,7 @@ inline bool contains(const C &c, const V &v)
DEF_ATTR_RD_SIMPLE_DETAILED(klass, name, type, location, keyword1) \ DEF_ATTR_RD_SIMPLE_DETAILED(klass, name, type, location, keyword1) \
void klass :: set##name(type value) \ void klass :: set##name(type value) \
{ \ { \
guardDisposed(); \
location = value; \ location = value; \
} }
@ -141,6 +143,7 @@ inline bool contains(const C &c, const V &v)
DEF_ATTR_RD_SIMPLE_DETAILED(klass, name, type, location, const) \ DEF_ATTR_RD_SIMPLE_DETAILED(klass, name, type, location, const) \
void klass :: set##name(type value) \ void klass :: set##name(type value) \
{ \ { \
guardDisposed(); \
*location = *value; \ *location = *value; \
} }
@ -148,6 +151,7 @@ inline bool contains(const C &c, const V &v)
DEF_ATTR_RD_SIMPLE_DETAILED(klass, name, type, location, ) \ DEF_ATTR_RD_SIMPLE_DETAILED(klass, name, type, location, ) \
void klass :: set##name(type value) \ void klass :: set##name(type value) \
{ \ { \
guardDisposed(); \
*location = *value; \ *location = *value; \
} }

View File

@ -134,9 +134,14 @@ void Viewport::initViewport(int x, int y, int width, int height)
Viewport::~Viewport() Viewport::~Viewport()
{ {
unlink(); dispose();
}
delete p; void Viewport::update()
{
guardDisposed();
Flashable::update();
} }
DEF_ATTR_RD_SIMPLE(Viewport, OX, int, geometry.xOrigin) DEF_ATTR_RD_SIMPLE(Viewport, OX, int, geometry.xOrigin)
@ -148,6 +153,8 @@ DEF_ATTR_OBJ_VALUE(Viewport, Tone, Tone*, p->tone)
void Viewport::setOX(int value) void Viewport::setOX(int value)
{ {
guardDisposed();
if (geometry.xOrigin == value) if (geometry.xOrigin == value)
return; return;
@ -157,6 +164,8 @@ void Viewport::setOX(int value)
void Viewport::setOY(int value) void Viewport::setOY(int value)
{ {
guardDisposed();
if (geometry.yOrigin == value) if (geometry.yOrigin == value)
return; return;
@ -212,6 +221,13 @@ void Viewport::onGeometryChange(const Geometry &geo)
p->recomputeOnScreen(); p->recomputeOnScreen();
} }
void Viewport::releaseResources()
{
unlink();
delete p;
}
ViewportElement::ViewportElement(Viewport *viewport, int z, int spriteY) ViewportElement::ViewportElement(Viewport *viewport, int z, int spriteY)
: SceneElement(viewport ? *viewport : *shState->screen(), z, spriteY), : SceneElement(viewport ? *viewport : *shState->screen(), z, spriteY),

View File

@ -37,6 +37,8 @@ public:
Viewport(); Viewport();
~Viewport(); ~Viewport();
void update();
DECL_ATTR( Rect, Rect* ) DECL_ATTR( Rect, Rect* )
DECL_ATTR( OX, int ) DECL_ATTR( OX, int )
DECL_ATTR( OY, int ) DECL_ATTR( OY, int )
@ -54,6 +56,11 @@ private:
void onGeometryChange(const Geometry &); void onGeometryChange(const Geometry &);
bool isEffectiveViewport(Rect *&, Color *&, Tone *&) const; bool isEffectiveViewport(Rect *&, Color *&, Tone *&) const;
void releaseResources();
const char *klassName() const { return "viewport"; }
ABOUT_TO_ACCESS_DISP
ViewportPrivate *p; ViewportPrivate *p;
friend struct ViewportPrivate; friend struct ViewportPrivate;

View File

@ -169,10 +169,8 @@ struct QuadChunk
struct WindowPrivate struct WindowPrivate
{ {
Bitmap *windowskin; Bitmap *windowskin;
DisposeWatch<WindowPrivate, Bitmap> windowskinWatch;
Bitmap *contents; Bitmap *contents;
DisposeWatch<WindowPrivate, Bitmap> contentsWatch;
bool bgStretch; bool bgStretch;
Rect *cursorRect; Rect *cursorRect;
@ -226,6 +224,8 @@ struct WindowPrivate
{ {
unlink(); unlink();
} }
ABOUT_TO_ACCESS_NOOP
}; };
WindowControls controlsElement; WindowControls controlsElement;
@ -250,9 +250,7 @@ struct WindowPrivate
WindowPrivate(Viewport *viewport = 0) WindowPrivate(Viewport *viewport = 0)
: windowskin(0), : windowskin(0),
windowskinWatch(*this, windowskin),
contents(0), contents(0),
contentsWatch(*this, contents, &WindowPrivate::markControlVertDirty),
bgStretch(true), bgStretch(true),
cursorRect(&tmp.rect), cursorRect(&tmp.rect),
active(true), active(true),
@ -550,7 +548,7 @@ struct WindowPrivate
void drawBase() void drawBase()
{ {
if (!windowskin) if (nullOrDisposed(windowskin))
return; return;
if (size == Vec2i(0, 0)) if (size == Vec2i(0, 0))
@ -584,7 +582,7 @@ struct WindowPrivate
void drawControls() void drawControls()
{ {
if (!windowskin && !contents) if (nullOrDisposed(windowskin) && nullOrDisposed(contents))
return; return;
if (size == Vec2i(0, 0)) if (size == Vec2i(0, 0))
@ -612,7 +610,7 @@ struct WindowPrivate
shader.bind(); shader.bind();
shader.applyViewportProj(); shader.applyViewportProj();
if (windowskin) if (!nullOrDisposed(windowskin))
{ {
shader.setTranslation(Vec2i(effectX, effectY)); shader.setTranslation(Vec2i(effectX, effectY));
@ -625,7 +623,7 @@ struct WindowPrivate
TEX::setSmooth(false); TEX::setSmooth(false);
} }
if (contents) if (!nullOrDisposed(contents))
{ {
/* Draw contents bitmap */ /* Draw contents bitmap */
glState.scissorBox.setIntersect(contentsRect); glState.scissorBox.setIntersect(contentsRect);
@ -692,15 +690,13 @@ Window::Window(Viewport *viewport)
Window::~Window() Window::~Window()
{ {
p->controlsElement.release(); dispose();
unlink();
delete p;
} }
void Window::update() void Window::update()
{ {
guardDisposed();
p->updateControls(); p->updateControls();
p->stepAnimations(); p->stepAnimations();
} }
@ -725,8 +721,9 @@ DEF_ATTR_OBJ_VALUE(Window, CursorRect, Rect*, p->cursorRect)
void Window::setWindowskin(Bitmap *value) void Window::setWindowskin(Bitmap *value)
{ {
guardDisposed();
p->windowskin = value; p->windowskin = value;
p->windowskinWatch.update(value);
if (!value) if (!value)
return; return;
@ -736,11 +733,12 @@ void Window::setWindowskin(Bitmap *value)
void Window::setContents(Bitmap *value) void Window::setContents(Bitmap *value)
{ {
guardDisposed();
if (p->contents == value) if (p->contents == value)
return; return;
p->contents = value; p->contents = value;
p->contentsWatch.update(value);
p->controlsVertDirty = true; p->controlsVertDirty = true;
if (!value) if (!value)
@ -752,6 +750,8 @@ void Window::setContents(Bitmap *value)
void Window::setStretch(bool value) void Window::setStretch(bool value)
{ {
guardDisposed();
if (value == p->bgStretch) if (value == p->bgStretch)
return; return;
@ -761,6 +761,8 @@ void Window::setStretch(bool value)
void Window::setActive(bool value) void Window::setActive(bool value)
{ {
guardDisposed();
if (p->active == value) if (p->active == value)
return; return;
@ -770,6 +772,8 @@ void Window::setActive(bool value)
void Window::setPause(bool value) void Window::setPause(bool value)
{ {
guardDisposed();
if (p->pause == value) if (p->pause == value)
return; return;
@ -781,6 +785,8 @@ void Window::setPause(bool value)
void Window::setWidth(int value) void Window::setWidth(int value)
{ {
guardDisposed();
if (p->size.x == value) if (p->size.x == value)
return; return;
@ -790,6 +796,8 @@ void Window::setWidth(int value)
void Window::setHeight(int value) void Window::setHeight(int value)
{ {
guardDisposed();
if (p->size.y == value) if (p->size.y == value)
return; return;
@ -799,6 +807,8 @@ void Window::setHeight(int value)
void Window::setOX(int value) void Window::setOX(int value)
{ {
guardDisposed();
if (p->contentsOffset.x == value) if (p->contentsOffset.x == value)
return; return;
@ -808,6 +818,8 @@ void Window::setOX(int value)
void Window::setOY(int value) void Window::setOY(int value)
{ {
guardDisposed();
if (p->contentsOffset.y == value) if (p->contentsOffset.y == value)
return; return;
@ -817,6 +829,8 @@ void Window::setOY(int value)
void Window::setOpacity(int value) void Window::setOpacity(int value)
{ {
guardDisposed();
if (p->opacity == value) if (p->opacity == value)
return; return;
@ -826,6 +840,8 @@ void Window::setOpacity(int value)
void Window::setBackOpacity(int value) void Window::setBackOpacity(int value)
{ {
guardDisposed();
if (p->backOpacity == value) if (p->backOpacity == value)
return; return;
@ -835,6 +851,8 @@ void Window::setBackOpacity(int value)
void Window::setContentsOpacity(int value) void Window::setContentsOpacity(int value)
{ {
guardDisposed();
if (p->contentsOpacity == value) if (p->contentsOpacity == value)
return; return;
@ -878,3 +896,12 @@ void Window::onViewportChange()
{ {
p->controlsElement.setScene(*this->scene); p->controlsElement.setScene(*this->scene);
} }
void Window::releaseResources()
{
p->controlsElement.release();
unlink();
delete p;
}

View File

@ -67,6 +67,11 @@ private:
void setVisible(bool value); void setVisible(bool value);
void onViewportChange(); void onViewportChange();
void releaseResources();
const char *klassName() const { return "window"; }
ABOUT_TO_ACCESS_DISP
}; };
#endif // WINDOW_H #endif // WINDOW_H

View File

@ -148,15 +148,11 @@ static const uint8_t pauseQuad[] =
static elementsN(pauseQuad); static elementsN(pauseQuad);
typedef DisposeWatch<WindowVXPrivate, Bitmap> BitmapWatch;
struct WindowVXPrivate struct WindowVXPrivate
{ {
Bitmap *windowskin; Bitmap *windowskin;
BitmapWatch windowskinWatch;
Bitmap *contents; Bitmap *contents;
BitmapWatch contentsWatch;
Rect *cursorRect; Rect *cursorRect;
bool active; bool active;
@ -227,9 +223,7 @@ struct WindowVXPrivate
WindowVXPrivate(int x, int y, int w, int h) WindowVXPrivate(int x, int y, int w, int h)
: windowskin(0), : windowskin(0),
windowskinWatch(*this, windowskin),
contents(0), contents(0),
contentsWatch(*this, contents),
cursorRect(&tmp.rect), cursorRect(&tmp.rect),
active(true), active(true),
arrowsVisible(true), arrowsVisible(true),
@ -401,7 +395,7 @@ struct WindowVXPrivate
void redrawBaseTex() void redrawBaseTex()
{ {
if (!windowskin) if (nullOrDisposed(windowskin))
return; return;
if (base.tex.tex == TEX::ID(0)) if (base.tex.tex == TEX::ID(0))
@ -514,7 +508,7 @@ struct WindowVXPrivate
size_t i = 0; size_t i = 0;
Vertex *vert = &ctrlVert.vertices[0]; Vertex *vert = &ctrlVert.vertices[0];
if (contents && arrowsVisible) if (!nullOrDisposed(contents) && arrowsVisible)
{ {
if (contentsOff.x > 0) if (contentsOff.x > 0)
i += Quad::setTexPosRect(&vert[i*4], scrollArrowSrc.l, arrowPos.l); i += Quad::setTexPosRect(&vert[i*4], scrollArrowSrc.l, arrowPos.l);
@ -721,6 +715,9 @@ struct WindowVXPrivate
if (base.tex.tex == TEX::ID(0)) if (base.tex.tex == TEX::ID(0))
return; return;
bool windowskinValid = !nullOrDisposed(windowskin);
bool contentsValid = !nullOrDisposed(contents);
Vec2i trans(geo.x + sceneOffset.x, Vec2i trans(geo.x + sceneOffset.x,
geo.y + sceneOffset.y); geo.y + sceneOffset.y);
@ -728,7 +725,7 @@ struct WindowVXPrivate
shader.bind(); shader.bind();
shader.applyViewportProj(); shader.applyViewportProj();
if (windowskin) if (windowskinValid)
{ {
shader.setTranslation(trans); shader.setTranslation(trans);
shader.setTexSize(Vec2i(base.tex.width, base.tex.height)); shader.setTexSize(Vec2i(base.tex.width, base.tex.height));
@ -749,9 +746,9 @@ struct WindowVXPrivate
if (openness < 255) if (openness < 255)
return; return;
bool drawCursor = cursorVert.count() > 0 && windowskin; bool drawCursor = cursorVert.count() > 0 && windowskinValid;
if (drawCursor || contents) if (drawCursor || contentsValid)
{ {
/* Translate cliprect from local into screen space */ /* Translate cliprect from local into screen space */
IntRect clip = clipRect; IntRect clip = clipRect;
@ -789,7 +786,7 @@ struct WindowVXPrivate
TEX::setSmooth(false); TEX::setSmooth(false);
} }
if (contents) if (contentsValid)
{ {
if (rgssVer <= 2) if (rgssVer <= 2)
glState.scissorBox.setIntersect(clip); glState.scissorBox.setIntersect(clip);
@ -828,13 +825,13 @@ WindowVX::WindowVX(int x, int y, int width, int height)
WindowVX::~WindowVX() WindowVX::~WindowVX()
{ {
unlink(); dispose();
delete p;
} }
void WindowVX::update() void WindowVX::update()
{ {
guardDisposed();
p->stepAnimations(); p->stepAnimations();
p->updatePauseQuad(); p->updatePauseQuad();
@ -843,6 +840,8 @@ void WindowVX::update()
void WindowVX::move(int x, int y, int width, int height) void WindowVX::move(int x, int y, int width, int height)
{ {
guardDisposed();
p->width = width; p->width = width;
p->height = height; p->height = height;
@ -857,11 +856,15 @@ void WindowVX::move(int x, int y, int width, int height)
bool WindowVX::isOpen() const bool WindowVX::isOpen() const
{ {
guardDisposed();
return p->openness == 255; return p->openness == 255;
} }
bool WindowVX::isClosed() const bool WindowVX::isClosed() const
{ {
guardDisposed();
return p->openness == 0; return p->openness == 0;
} }
@ -889,21 +892,23 @@ DEF_ATTR_OBJ_VALUE(WindowVX, Tone, Tone*, p->tone)
void WindowVX::setWindowskin(Bitmap *value) void WindowVX::setWindowskin(Bitmap *value)
{ {
guardDisposed();
if (p->windowskin == value) if (p->windowskin == value)
return; return;
p->windowskin = value; p->windowskin = value;
p->windowskinWatch.update(value);
p->base.texDirty = true; p->base.texDirty = true;
} }
void WindowVX::setContents(Bitmap *value) void WindowVX::setContents(Bitmap *value)
{ {
guardDisposed();
if (p->contents == value) if (p->contents == value)
return; return;
p->contents = value; p->contents = value;
p->contentsWatch.update(value);
FloatRect rect = p->contents->rect(); FloatRect rect = p->contents->rect();
p->contentsQuad.setTexPosRect(rect, rect); p->contentsQuad.setTexPosRect(rect, rect);
@ -912,6 +917,8 @@ void WindowVX::setContents(Bitmap *value)
void WindowVX::setActive(bool value) void WindowVX::setActive(bool value)
{ {
guardDisposed();
if (p->active == value) if (p->active == value)
return; return;
@ -922,6 +929,8 @@ void WindowVX::setActive(bool value)
void WindowVX::setArrowsVisible(bool value) void WindowVX::setArrowsVisible(bool value)
{ {
guardDisposed();
if (p->arrowsVisible == value) if (p->arrowsVisible == value)
return; return;
@ -931,6 +940,8 @@ void WindowVX::setArrowsVisible(bool value)
void WindowVX::setPause(bool value) void WindowVX::setPause(bool value)
{ {
guardDisposed();
if (p->pause == value) if (p->pause == value)
return; return;
@ -942,6 +953,8 @@ void WindowVX::setPause(bool value)
void WindowVX::setWidth(int value) void WindowVX::setWidth(int value)
{ {
guardDisposed();
if (p->width == value) if (p->width == value)
return; return;
@ -956,6 +969,8 @@ void WindowVX::setWidth(int value)
void WindowVX::setHeight(int value) void WindowVX::setHeight(int value)
{ {
guardDisposed();
if (p->height == value) if (p->height == value)
return; return;
@ -970,6 +985,8 @@ void WindowVX::setHeight(int value)
void WindowVX::setOX(int value) void WindowVX::setOX(int value)
{ {
guardDisposed();
if (p->contentsOff.x == value) if (p->contentsOff.x == value)
return; return;
@ -979,6 +996,8 @@ void WindowVX::setOX(int value)
void WindowVX::setOY(int value) void WindowVX::setOY(int value)
{ {
guardDisposed();
if (p->contentsOff.y == value) if (p->contentsOff.y == value)
return; return;
@ -988,6 +1007,8 @@ void WindowVX::setOY(int value)
void WindowVX::setPadding(int value) void WindowVX::setPadding(int value)
{ {
guardDisposed();
if (p->padding == value) if (p->padding == value)
return; return;
@ -998,6 +1019,8 @@ void WindowVX::setPadding(int value)
void WindowVX::setPaddingBottom(int value) void WindowVX::setPaddingBottom(int value)
{ {
guardDisposed();
if (p->paddingBottom == value) if (p->paddingBottom == value)
return; return;
@ -1007,6 +1030,8 @@ void WindowVX::setPaddingBottom(int value)
void WindowVX::setOpacity(int value) void WindowVX::setOpacity(int value)
{ {
guardDisposed();
if (p->opacity == value) if (p->opacity == value)
return; return;
@ -1016,6 +1041,8 @@ void WindowVX::setOpacity(int value)
void WindowVX::setBackOpacity(int value) void WindowVX::setBackOpacity(int value)
{ {
guardDisposed();
if (p->backOpacity == value) if (p->backOpacity == value)
return; return;
@ -1025,6 +1052,8 @@ void WindowVX::setBackOpacity(int value)
void WindowVX::setContentsOpacity(int value) void WindowVX::setContentsOpacity(int value)
{ {
guardDisposed();
if (p->contentsOpacity == value) if (p->contentsOpacity == value)
return; return;
@ -1034,6 +1063,8 @@ void WindowVX::setContentsOpacity(int value)
void WindowVX::setOpenness(int value) void WindowVX::setOpenness(int value)
{ {
guardDisposed();
if (p->openness == value) if (p->openness == value)
return; return;
@ -1063,3 +1094,10 @@ void WindowVX::onGeometryChange(const Scene::Geometry &geo)
p->sceneOffset.x = geo.rect.x - geo.xOrigin; p->sceneOffset.x = geo.rect.x - geo.xOrigin;
p->sceneOffset.y = geo.rect.y - geo.yOrigin; p->sceneOffset.y = geo.rect.y - geo.yOrigin;
} }
void WindowVX::releaseResources()
{
unlink();
delete p;
}

View File

@ -73,6 +73,11 @@ private:
void draw(); void draw();
void onGeometryChange(const Scene::Geometry &); void onGeometryChange(const Scene::Geometry &);
void releaseResources();
const char *klassName() const { return "window"; }
ABOUT_TO_ACCESS_DISP
}; };
#endif // WINDOWVX_H #endif // WINDOWVX_H