mkxp/binding-mri/tilemap-binding.cpp
Jonas Kulla f067e0eff8 MRI-Binding: Reduce number of object allocations
Previously, wrapped instances of mkxp core classes were
stored as RData ivars inside the actual object. This turned
out to be pointless as RData objects themselves are perfectly
valid objects that can carry ivars and have parent classes.
Therefore, the RData objects are now exposed directly to
the user scripts, effectively halving the amount of object
allocations.
2013-10-30 10:06:24 +01:00

173 lines
4 KiB
C++

/*
** tilemap-binding.cpp
**
** This file is part of mkxp.
**
** Copyright (C) 2013 Jonas Kulla <Nyocurio@gmail.com>
**
** mkxp is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 2 of the License, or
** (at your option) any later version.
**
** mkxp is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with mkxp. If not, see <http://www.gnu.org/licenses/>.
*/
#include "tilemap.h"
#include "viewport.h"
#include "bitmap.h"
#include "table.h"
#include "disposable-binding.h"
#include "binding-util.h"
#include "binding-types.h"
rb_data_type_struct TilemapAutotilesType;
RB_METHOD(tilemapAutotilesSet)
{
Tilemap::Autotiles *a = getPrivateData<Tilemap::Autotiles>(self);
int i;
VALUE bitmapObj;
rb_get_args(argc, argv, "io", &i, &bitmapObj, RB_ARG_END);
Bitmap *bitmap = getPrivateDataCheck<Bitmap>(bitmapObj, BitmapType);
a->set(i, bitmap);
VALUE ary = rb_iv_get(self, "array");
rb_ary_store(ary, i, bitmapObj);
return self;
}
RB_METHOD(tilemapAutotilesGet)
{
int i;
rb_get_args (argc, argv, "i", &i, RB_ARG_END);
if (i < 0 || i > 6)
return Qnil;
VALUE ary = rb_iv_get(self, "array");
return rb_ary_entry(ary, i);
}
DEF_TYPE(Tilemap);
RB_METHOD(tilemapInitialize)
{
Tilemap *t;
/* Get parameters */
VALUE viewportObj = Qnil;
Viewport *viewport = 0;
rb_get_args(argc, argv, "|o", &viewportObj, RB_ARG_END);
if (rb_type(viewportObj) != RUBY_T_NIL)
viewport = getPrivateDataCheck<Viewport>(viewportObj, ViewportType);
/* Construct object */
t = new Tilemap(viewport);
setPrivateData(self, t);
rb_iv_set(self, "viewport", viewportObj);
wrapProperty(self, &t->getAutotiles(), "autotiles", TilemapAutotilesType);
VALUE autotilesObj = rb_iv_get(self, "autotiles");
VALUE ary = rb_ary_new2(7);
for (int i = 0; i < 7; ++i)
rb_ary_push(ary, Qnil);
rb_iv_set(autotilesObj, "array", ary);
return self;
}
RB_METHOD(tilemapGetAutotiles)
{
RB_UNUSED_PARAM;
return rb_iv_get(self, "autotiles");
}
RB_METHOD(tilemapUpdate)
{
RB_UNUSED_PARAM;
Tilemap *t = getPrivateData<Tilemap>(self);
t->update();
return Qnil;
}
#define DISP_CLASS_NAME "tilemap"
RB_METHOD(tilemapGetViewport)
{
RB_UNUSED_PARAM;
Tilemap *t = getPrivateData<Tilemap>(self);
checkDisposed(t, DISP_CLASS_NAME);
return rb_iv_get(self, "viewport");
}
DEF_PROP_OBJ(Tilemap, Bitmap, Tileset, "tileset")
DEF_PROP_OBJ(Tilemap, Table, MapData, "map_data")
DEF_PROP_OBJ(Tilemap, Table, FlashData, "flash_data")
DEF_PROP_OBJ(Tilemap, Table, Priorities, "priorities")
DEF_PROP_B(Tilemap, Visible)
DEF_PROP_I(Tilemap, OX)
DEF_PROP_I(Tilemap, OY)
void
tilemapBindingInit()
{
initType(TilemapAutotilesType, "TilemapAutotiles", 0);
VALUE klass = rb_define_class("TilemapAutotiles", rb_cObject);
rb_define_alloc_func(klass, classAllocate<&TilemapAutotilesType>);
_rb_define_method(klass, "[]=", tilemapAutotilesSet);
_rb_define_method(klass, "[]", tilemapAutotilesGet);
INIT_TYPE(Tilemap);
klass = rb_define_class("Tilemap", rb_cObject);
rb_define_alloc_func(klass, classAllocate<&TilemapType>);
disposableBindingInit<Tilemap>(klass);
_rb_define_method(klass, "initialize", tilemapInitialize);
_rb_define_method(klass, "autotiles", tilemapGetAutotiles);
_rb_define_method(klass, "update", tilemapUpdate);
_rb_define_method(klass, "viewport", tilemapGetViewport);
INIT_PROP_BIND( Tilemap, Tileset, "tileset" );
INIT_PROP_BIND( Tilemap, MapData, "map_data" );
INIT_PROP_BIND( Tilemap, FlashData, "flash_data" );
INIT_PROP_BIND( Tilemap, Priorities, "priorities" );
INIT_PROP_BIND( Tilemap, Visible, "visible" );
INIT_PROP_BIND( Tilemap, OX, "ox" );
INIT_PROP_BIND( Tilemap, OY, "oy" );
}