forked from mkxp-z/mkxp-z
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtilemap-binding.cpp
204 lines (151 loc) · 5.38 KB
/
tilemap-binding.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
** tilemap-binding.cpp
**
** This file is part of mkxp.
**
** Copyright (C) 2013 - 2021 Amaryllis Kulla <[email protected]>
**
** 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 "bitmap.h"
#include "table.h"
#include "tilemap.h"
#include "viewport.h"
#include "binding-types.h"
#include "binding-util.h"
#include "disposable-binding.h"
#if RAPI_FULL > 187
DEF_TYPE_CUSTOMFREE(TilemapAutotiles, RUBY_TYPED_NEVER_FREE);
#else
#define TilemapAutotilesType "TilemapAutotiles"
#endif
RB_METHOD(tilemapAutotilesSet) {
Tilemap::Autotiles *a = getPrivateDataNoRaise<Tilemap::Autotiles>(self);
if (!a)
return self;
int i;
VALUE bitmapObj;
rb_get_args(argc, argv, "io", &i, &bitmapObj RB_ARG_END);
Bitmap *bitmap = getPrivateDataCheck<Bitmap>(bitmapObj, BitmapType);
GFX_LOCK;
a->set(i, bitmap);
VALUE ary = rb_iv_get(self, "array");
rb_ary_store(ary, i, bitmapObj);
GFX_UNLOCK;
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);
}
#if RAPI_FULL > 187
DEF_TYPE(Tilemap);
#else
DEF_ALLOCFUNC(Tilemap);
#endif
RB_METHOD(tilemapInitialize) {
Tilemap *t;
/* Get parameters */
VALUE viewportObj = Qnil;
Viewport *viewport = 0;
rb_get_args(argc, argv, "|o", &viewportObj RB_ARG_END);
if (!NIL_P(viewportObj))
viewport = getPrivateDataCheck<Viewport>(viewportObj, ViewportType);
GFX_LOCK;
/* Construct object */
t = new Tilemap(viewport);
rb_iv_set(self, "viewport", viewportObj);
setPrivateData(self, t);
t->initDynAttribs();
/* Dispose the old autotiles if we're reinitializing.
* See the comment in setPrivateData for more info. */
VALUE autotilesObj = rb_iv_get(self, "autotiles");
if (autotilesObj != Qnil)
setPrivateData(autotilesObj, 0);
wrapProperty(self, &t->getAutotiles(), "autotiles", TilemapAutotilesType);
wrapProperty(self, &t->getColor(), "color", ColorType);
wrapProperty(self, &t->getTone(), "tone", ToneType);
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);
/* Circular reference so both objects are always
* alive at the same time */
rb_iv_set(autotilesObj, "tilemap", self);
GFX_UNLOCK;
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);
GFX_LOCK;
t->update();
GFX_UNLOCK;
return Qnil;
}
RB_METHOD(tilemapGetViewport) {
RB_UNUSED_PARAM;
checkDisposed<Tilemap>(self);
return rb_iv_get(self, "viewport");
}
DEF_GFX_PROP_OBJ_REF(Tilemap, Bitmap, Tileset, "tileset")
DEF_GFX_PROP_OBJ_REF(Tilemap, Table, MapData, "map_data")
DEF_GFX_PROP_OBJ_REF(Tilemap, Table, FlashData, "flash_data")
DEF_GFX_PROP_OBJ_REF(Tilemap, Table, Priorities, "priorities")
DEF_GFX_PROP_OBJ_VAL(Tilemap, Color, Color, "color")
DEF_GFX_PROP_OBJ_VAL(Tilemap, Tone, Tone, "tone")
DEF_GFX_PROP_B(Tilemap, Visible)
DEF_GFX_PROP_I(Tilemap, OX)
DEF_GFX_PROP_I(Tilemap, OY)
DEF_GFX_PROP_I(Tilemap, Opacity)
DEF_GFX_PROP_I(Tilemap, BlendType)
void tilemapBindingInit() {
VALUE klass = rb_define_class("TilemapAutotiles", rb_cObject);
#if RAPI_FULL > 187
rb_define_alloc_func(klass, classAllocate<&TilemapAutotilesType>);
#endif
_rb_define_method(klass, "[]=", tilemapAutotilesSet);
_rb_define_method(klass, "[]", tilemapAutotilesGet);
klass = rb_define_class("Tilemap", rb_cObject);
#if RAPI_FULL > 187
rb_define_alloc_func(klass, classAllocate<&TilemapType>);
#else
rb_define_alloc_func(klass, TilemapAllocate);
#endif
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");
INIT_PROP_BIND(Tilemap, Opacity, "opacity");
INIT_PROP_BIND(Tilemap, BlendType, "blend_type");
INIT_PROP_BIND(Tilemap, Color, "color");
INIT_PROP_BIND(Tilemap, Tone, "tone");
}