Lift 'Disposable' concept from core into bindings
Instead of replicating the RGSS Disposable interface in C++ and merely binding it, redefine the 'disposed' state as the entire core object being deleted (and the binding object's private pointer being null). This makes the behavior more accurate in regard to RMXP. It is now for example possible to subclass disposable classes and access their 'dispose'/'disposed?' methods without initializing the base class first (because the internal pointer is simply null before initialization). Accessing any other base methods will still raise an exception. There are some quirks and irregular behavior in RMXP; eg. most nullable bitmap attributes of disposable classes (Sprite, Plane etc.) can still be queried afterwards, but some cannot (Tilemap#tileset), and disposing certain attributes crashes RMXP entirely (Tilemap#autotiles[n]). mkxp tries to behave as close possible, but will be more lenient some circumstances. To the core, disposed bitmap attributes will look identically to null, which slightly diverges from RMXP (where they're treated as still existing, but aren't drawn). The Disposable interface has been retained containing a single signal, for the binding to inform core when objects are disposed (so active attributes can be set to null).
This commit is contained in:
parent
b7af8cc92f
commit
e858bbdcf5
38 changed files with 341 additions and 386 deletions
|
@ -25,41 +25,74 @@
|
|||
#include "disposable.h"
|
||||
#include "binding-util.h"
|
||||
|
||||
/* 'Children' are disposables that are disposed together
|
||||
* with their parent. Currently this is only used by Viewport
|
||||
* in RGSS1.
|
||||
* FIXME: Disable this behavior when RGSS2 or 3 */
|
||||
inline void
|
||||
disposableAddChild(VALUE disp, VALUE child)
|
||||
{
|
||||
VALUE children = rb_iv_get(disp, "children");
|
||||
|
||||
if (NIL_P(children))
|
||||
{
|
||||
children = rb_ary_new();
|
||||
rb_iv_set(disp, "children", children);
|
||||
}
|
||||
|
||||
/* Assumes children are never removed until destruction */
|
||||
rb_ary_push(children, child);
|
||||
}
|
||||
|
||||
inline void
|
||||
disposableDisposeChildren(VALUE disp)
|
||||
{
|
||||
VALUE children = rb_iv_get(disp, "children");
|
||||
|
||||
if (NIL_P(children))
|
||||
return;
|
||||
|
||||
ID dispFun = rb_intern("dispose");
|
||||
|
||||
/* Note: RMXP doesn't call overridden 'dispose' methods here */
|
||||
for (long i = 0; i < RARRAY_LEN(children); ++i)
|
||||
rb_funcall2(rb_ary_entry(children, i), dispFun, 0, 0);
|
||||
}
|
||||
|
||||
template<class C>
|
||||
RB_METHOD(disposableDispose)
|
||||
{
|
||||
RB_UNUSED_PARAM;
|
||||
|
||||
Disposable *d = getPrivateData<C>(self);
|
||||
C *c = static_cast<C*>(RTYPEDDATA_DATA(self));
|
||||
|
||||
d->dispose();
|
||||
/* Nothing to do if already disposed */
|
||||
if (!c)
|
||||
return Qnil;
|
||||
|
||||
/* Inform core */
|
||||
c->wasDisposed();
|
||||
|
||||
disposableDisposeChildren(self);
|
||||
|
||||
delete c;
|
||||
setPrivateData(self, 0);
|
||||
|
||||
return Qnil;
|
||||
}
|
||||
|
||||
template<class C>
|
||||
RB_METHOD(disposableIsDisposed)
|
||||
{
|
||||
RB_UNUSED_PARAM;
|
||||
|
||||
Disposable *d = getPrivateData<C>(self);
|
||||
|
||||
return rb_bool_new(d->isDisposed());
|
||||
return rb_bool_new(RTYPEDDATA_DATA(self) == 0);
|
||||
}
|
||||
|
||||
template<class C>
|
||||
static void disposableBindingInit(VALUE klass)
|
||||
{
|
||||
_rb_define_method(klass, "dispose", disposableDispose<C>);
|
||||
_rb_define_method(klass, "disposed?", disposableIsDisposed<C>);
|
||||
}
|
||||
|
||||
inline void checkDisposed(Disposable *d, const char *klassName)
|
||||
{
|
||||
RbData *data = getRbData(); (void) data;
|
||||
|
||||
if (d->isDisposed())
|
||||
rb_raise(getRbData()->exc[RGSS], "disposed %s", klassName);
|
||||
_rb_define_method(klass, "disposed?", disposableIsDisposed);
|
||||
}
|
||||
|
||||
#endif // DISPOSABLEBINDING_H
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue