rgss 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (64) hide show
  1. checksums.yaml +7 -0
  2. data/.clang-format +6 -0
  3. data/.gitignore +167 -0
  4. data/.yardopts +6 -0
  5. data/CHANGELOG.md +4 -0
  6. data/Gemfile +4 -0
  7. data/LICENSE.txt +21 -0
  8. data/Rakefile +9 -0
  9. data/ext/rgss/cglm-v0.7.9.tar.gz +0 -0
  10. data/ext/rgss/color.c +599 -0
  11. data/ext/rgss/entity.c +373 -0
  12. data/ext/rgss/extconf.rb +53 -0
  13. data/ext/rgss/font.c +135 -0
  14. data/ext/rgss/game.c +469 -0
  15. data/ext/rgss/game.h +99 -0
  16. data/ext/rgss/gl.c +3217 -0
  17. data/ext/rgss/glad.c +1140 -0
  18. data/ext/rgss/glad.h +2129 -0
  19. data/ext/rgss/glfw.c +1453 -0
  20. data/ext/rgss/graphics.c +324 -0
  21. data/ext/rgss/image.c +274 -0
  22. data/ext/rgss/input.c +745 -0
  23. data/ext/rgss/khrplatform.h +290 -0
  24. data/ext/rgss/mat4.c +279 -0
  25. data/ext/rgss/pax_global_header +1 -0
  26. data/ext/rgss/point.c +253 -0
  27. data/ext/rgss/rect.c +449 -0
  28. data/ext/rgss/rgss.c +56 -0
  29. data/ext/rgss/rgss.h +241 -0
  30. data/ext/rgss/stb_image.h +7762 -0
  31. data/ext/rgss/stb_image_write.h +1690 -0
  32. data/ext/rgss/stb_rect_pack.h +628 -0
  33. data/ext/rgss/stb_truetype.h +5011 -0
  34. data/ext/rgss/utf8.h +1652 -0
  35. data/ext/rgss/uthash.h +1133 -0
  36. data/ext/rgss/vec.c +114 -0
  37. data/ext/rgss/vec.h +192 -0
  38. data/ext/rgss/vec2.c +489 -0
  39. data/ext/rgss/vec3.c +751 -0
  40. data/ext/rgss/vec4.c +681 -0
  41. data/lib/rgss.rb +140 -0
  42. data/lib/rgss/batch.rb +57 -0
  43. data/lib/rgss/blend.rb +47 -0
  44. data/lib/rgss/game_object.rb +28 -0
  45. data/lib/rgss/plane.rb +95 -0
  46. data/lib/rgss/renderable.rb +158 -0
  47. data/lib/rgss/rgss.so +0 -0
  48. data/lib/rgss/shader.rb +94 -0
  49. data/lib/rgss/shaders/sprite-frag.glsl +40 -0
  50. data/lib/rgss/shaders/sprite-vert.glsl +17 -0
  51. data/lib/rgss/sprite.rb +139 -0
  52. data/lib/rgss/stubs/color.rb +318 -0
  53. data/lib/rgss/stubs/gl.rb +1999 -0
  54. data/lib/rgss/stubs/glfw.rb +626 -0
  55. data/lib/rgss/stubs/rect.rb +324 -0
  56. data/lib/rgss/stubs/rpg.rb +267 -0
  57. data/lib/rgss/stubs/tone.rb +65 -0
  58. data/lib/rgss/texture.rb +132 -0
  59. data/lib/rgss/tilemap.rb +116 -0
  60. data/lib/rgss/version.rb +3 -0
  61. data/lib/rgss/viewport.rb +67 -0
  62. data/rgss.gemspec +44 -0
  63. data/test.png +0 -0
  64. metadata +178 -0
@@ -0,0 +1 @@
1
+ 52 comment=ec852c7682fbebacccbee01e3076e8ee4f378d8e
@@ -0,0 +1,253 @@
1
+ #include "rgss.h"
2
+
3
+ VALUE rb_cIVec2;
4
+ VALUE rb_cPoint;
5
+ VALUE rb_cSize;
6
+
7
+ #define IVEC_SIZE (sizeof(int) * 2)
8
+
9
+ RGSS_DEFINE_MARSHAL(RGSS_Point)
10
+
11
+ static VALUE RGSS_IVec_Alloc(VALUE klass)
12
+ {
13
+ int *vec = xmalloc(IVEC_SIZE);
14
+ memset(vec, 0, IVEC_SIZE);
15
+ return Data_Wrap_Struct(klass, NULL, RUBY_DEFAULT_FREE, vec);
16
+ }
17
+
18
+ static VALUE RGSS_IVec_GetX(VALUE self)
19
+ {
20
+ return INT2NUM(((int*) DATA_PTR(self))[0]);
21
+ }
22
+
23
+ static VALUE RGSS_IVec_GetY(VALUE self)
24
+ {
25
+ return INT2NUM(((int*) DATA_PTR(self))[1]);
26
+ }
27
+
28
+ static VALUE RGSS_IVec_SetX(VALUE self, VALUE value)
29
+ {
30
+ RGSS_ASSERT_UNFROZEN(self);
31
+ int *vec = DATA_PTR(self);
32
+ vec[0] = NUM2INT(value);
33
+ return value;
34
+ }
35
+
36
+ static VALUE RGSS_IVec_SetY(VALUE self, VALUE value)
37
+ {
38
+ RGSS_ASSERT_UNFROZEN(self);
39
+ int *vec = DATA_PTR(self);
40
+ vec[1] = NUM2INT(value);
41
+ return value;
42
+ }
43
+
44
+ static VALUE RGSS_IVec_Equal(VALUE self, VALUE other)
45
+ {
46
+ if (CLASS_OF(self) != CLASS_OF(other))
47
+ return Qfalse;
48
+
49
+ int *v1 = DATA_PTR(self), *v2 = DATA_PTR(other);
50
+ return RB_BOOL(v1[0] == v2[0] && v1[1] == v2[1]);
51
+ }
52
+
53
+ static VALUE RGSS_IVec_GetIndex(VALUE self, VALUE index)
54
+ {
55
+ int i = NUM2INT(index);
56
+ if (i < 0 || i > 1)
57
+ return Qnil;
58
+ return INT2NUM(((int*) DATA_PTR(self))[i]);
59
+ }
60
+
61
+ static VALUE RGSS_IVec_SetIndex(VALUE self, VALUE index, VALUE value)
62
+ {
63
+ RGSS_ASSERT_UNFROZEN(self);
64
+ int i = NUM2INT(index);
65
+ if (i < 0 || i > 1)
66
+ rb_raise(rb_eArgError, "index out of range (given %d, expect 0..1)", i);
67
+
68
+ int *vec = DATA_PTR(self);
69
+ vec[i] = NUM2INT(value);
70
+ return value;
71
+ }
72
+
73
+ static VALUE RGSS_IVec_IsEmpty(VALUE self)
74
+ {
75
+ int *i = DATA_PTR(self);
76
+ return RB_BOOL(i[0] == 0 && i[1] == 0);
77
+ }
78
+
79
+ static VALUE RGSS_IVec_Add(VALUE self, VALUE other)
80
+ {
81
+ int *v1 = DATA_PTR(self), *v2 = DATA_PTR(other), *dst = xmalloc(IVEC_SIZE);
82
+
83
+ dst[0] = v1[0] + v2[0];
84
+ dst[1] = v1[1] + v2[1];
85
+ return Data_Wrap_Struct(CLASS_OF(self), NULL, RUBY_DEFAULT_FREE, dst);
86
+ }
87
+
88
+ static VALUE RGSS_IVec_Subtract(VALUE self, VALUE other)
89
+ {
90
+ int *v1 = DATA_PTR(self), *v2 = DATA_PTR(other), *dst = xmalloc(IVEC_SIZE);
91
+
92
+ dst[0] = v1[0] - v2[0];
93
+ dst[1] = v1[1] - v2[1];
94
+ return Data_Wrap_Struct(CLASS_OF(self), NULL, RUBY_DEFAULT_FREE, dst);
95
+ }
96
+
97
+ static VALUE RGSS_IVec_Clone(VALUE self)
98
+ {
99
+ int *src = DATA_PTR(self), *dst = xmalloc(IVEC_SIZE);
100
+ memcpy(dst, src, IVEC_SIZE);
101
+
102
+ VALUE value = Data_Wrap_Struct(CLASS_OF(self), NULL, RUBY_DEFAULT_FREE, dst);
103
+ if (rb_obj_frozen_p(self))
104
+ rb_obj_freeze(value);
105
+
106
+ return value;
107
+ }
108
+
109
+ static VALUE RGSS_IVec_Dupe(VALUE self)
110
+ {
111
+ int *src = DATA_PTR(self), *dst = xmalloc(IVEC_SIZE);
112
+ memcpy(dst, src, IVEC_SIZE);
113
+ return Data_Wrap_Struct(CLASS_OF(self), NULL, RUBY_DEFAULT_FREE, dst);
114
+ }
115
+
116
+ static VALUE RGSS_IVec_OffsetBang(int argc, VALUE *argv, VALUE self)
117
+ {
118
+ RGSS_ASSERT_UNFROZEN(self);
119
+ VALUE a1, a2;
120
+ rb_scan_args(argc, argv, "11", &a1, &a2);
121
+
122
+ int *vec = DATA_PTR(self);
123
+ int x, y;
124
+
125
+ if (argc == 1)
126
+ {
127
+ int *v = DATA_PTR(a1);
128
+ x = v[0];
129
+ y = v[1];
130
+ }
131
+ else
132
+ {
133
+ x = NUM2INT(a1);
134
+ y = NUM2INT(a2);
135
+ }
136
+
137
+ vec[0] += x;
138
+ vec[1] += y;
139
+ return self;
140
+ }
141
+
142
+ static VALUE RGSS_IVec_Offset(int argc, VALUE *argv, VALUE self)
143
+ {
144
+ return RGSS_IVec_OffsetBang(argc, argv, RGSS_IVec_Dupe(self));
145
+ }
146
+
147
+ static VALUE RGSS_IVec_Inspect(VALUE self)
148
+ {
149
+ int *vec = DATA_PTR(self);
150
+ return rb_sprintf("<%d, %d>", vec[0], vec[1]);
151
+ }
152
+
153
+ static VALUE RGSS_IVec_Set(VALUE self, VALUE x, VALUE y)
154
+ {
155
+ RGSS_ASSERT_UNFROZEN(self);
156
+ int *vec = DATA_PTR(self);
157
+ vec[0] = NUM2INT(x);
158
+ vec[1] = NUM2INT(y);
159
+ return self;
160
+ }
161
+
162
+ static VALUE RGSS_IVec_ToArray(VALUE self)
163
+ {
164
+ int *vec = DATA_PTR(self);
165
+ return rb_ary_new_from_args(2, INT2NUM(vec[0]), INT2NUM(vec[1]));
166
+ }
167
+
168
+ static VALUE RGSS_Point_ToHash(VALUE self)
169
+ {
170
+ int *vec = DATA_PTR(self);
171
+ VALUE hash = rb_hash_new();
172
+ rb_hash_aset(hash, STR2SYM("x"), INT2NUM(vec[0]));
173
+ rb_hash_aset(hash, STR2SYM("y"), INT2NUM(vec[1]));
174
+ return hash;
175
+ }
176
+
177
+ static VALUE RGSS_Size_ToHash(VALUE self)
178
+ {
179
+ int *vec = DATA_PTR(self);
180
+ VALUE hash = rb_hash_new();
181
+ rb_hash_aset(hash, STR2SYM("width"), INT2NUM(vec[0]));
182
+ rb_hash_aset(hash, STR2SYM("height"), INT2NUM(vec[1]));
183
+ return hash;
184
+ }
185
+
186
+ static VALUE RGSS_IVec_Initialize(int argc, VALUE *argv, VALUE self)
187
+ {
188
+ VALUE x, y;
189
+ rb_scan_args(argc, argv, "02", &x, &y);
190
+
191
+ int *vec = DATA_PTR(self);
192
+
193
+ switch (argc)
194
+ {
195
+ case 0: break;
196
+ case 1:
197
+ {
198
+ memcpy(vec, DATA_PTR(x), IVEC_SIZE);
199
+ break;
200
+ }
201
+ case 2:
202
+ {
203
+ vec[0] = NUM2INT(x);
204
+ vec[1] = NUM2INT(y);
205
+ break;
206
+ }
207
+ }
208
+ return Qnil;
209
+ }
210
+
211
+ void RGSS_Init_PointAndSize(VALUE parent)
212
+ {
213
+ rb_cIVec2 = rb_define_class_under(parent, "IVec2", rb_cObject);
214
+ rb_cPoint = rb_define_class_under(parent, "Point", rb_cIVec2);
215
+ rb_cSize = rb_define_class_under(parent, "Size", rb_cIVec2);
216
+ rb_define_alloc_func(rb_cIVec2, RGSS_IVec_Alloc);
217
+ rb_define_alloc_func(rb_cPoint, RGSS_IVec_Alloc);
218
+ rb_define_alloc_func(rb_cSize, RGSS_IVec_Alloc);
219
+
220
+ rb_define_methodm1(rb_cIVec2, "initialize", RGSS_IVec_Initialize, -1);
221
+ rb_define_method1(rb_cIVec2, "[]", RGSS_IVec_GetIndex, 1);
222
+ rb_define_method2(rb_cIVec2, "[]=", RGSS_IVec_SetIndex, 2);
223
+ rb_define_method1(rb_cIVec2, "eql?", RGSS_IVec_Equal, 1);
224
+ rb_define_method1(rb_cIVec2, "==", RGSS_IVec_Equal, 1);
225
+ rb_define_method0(rb_cIVec2, "empty?", RGSS_IVec_IsEmpty, 0);
226
+ rb_define_method1(rb_cIVec2, "+", RGSS_IVec_Add, 1);
227
+ rb_define_method1(rb_cIVec2, "-", RGSS_IVec_Subtract, 1);
228
+ rb_define_method1(rb_cIVec2, "add", RGSS_IVec_Add, 1);
229
+ rb_define_method1(rb_cIVec2, "subtract", RGSS_IVec_Subtract, 1);
230
+ rb_define_method0(rb_cIVec2, "clone", RGSS_IVec_Clone, 0);
231
+ rb_define_method0(rb_cIVec2, "dup", RGSS_IVec_Dupe, 0);
232
+ rb_define_method0(rb_cIVec2, "inspect", RGSS_IVec_Inspect, 0);
233
+ rb_define_method2(rb_cIVec2, "set", RGSS_IVec_Set, 2);
234
+ rb_define_method0(rb_cIVec2, "to_a", RGSS_IVec_ToArray, 0);
235
+ rb_define_methodm1(rb_cIVec2, "_dump", RGSS_Point_Dump, -1);
236
+
237
+ rb_define_method0(rb_cPoint, "x", RGSS_IVec_GetX, 0);
238
+ rb_define_method0(rb_cPoint, "y", RGSS_IVec_GetY, 0);
239
+ rb_define_method1(rb_cPoint, "x=", RGSS_IVec_SetX, 1);
240
+ rb_define_method1(rb_cPoint, "y=", RGSS_IVec_SetY, 1);
241
+ rb_define_methodm1(rb_cPoint, "offset!", RGSS_IVec_OffsetBang, -1);
242
+ rb_define_methodm1(rb_cPoint, "offset", RGSS_IVec_Offset, -1);
243
+ rb_define_method0(rb_cPoint, "to_h", RGSS_Point_ToHash, 0);
244
+
245
+ rb_define_method0(rb_cSize, "width", RGSS_IVec_GetX, 0);
246
+ rb_define_method0(rb_cSize, "height", RGSS_IVec_GetY, 0);
247
+ rb_define_method1(rb_cSize, "width=", RGSS_IVec_SetX, 1);
248
+ rb_define_method1(rb_cSize, "height=", RGSS_IVec_SetY, 1);
249
+ rb_define_method0(rb_cSize, "to_h", RGSS_Size_ToHash, 0);
250
+
251
+ rb_define_singleton_method0(rb_cIVec2, "empty", RGSS_IVec_Alloc, 0);
252
+ rb_define_singleton_method1(rb_cIVec2, "_load", RGSS_Point_Load, 1);
253
+ }
@@ -0,0 +1,449 @@
1
+ #include "rgss.h"
2
+
3
+ VALUE rb_cRect;
4
+
5
+ RGSS_DEFINE_MARSHAL(RGSS_Rect)
6
+
7
+ static VALUE RGSS_Rect_Alloc(VALUE klass)
8
+ {
9
+ RGSS_Rect *rect = ALLOC(RGSS_Rect);
10
+ memset(rect, 0, sizeof(RGSS_Rect));
11
+ return Data_Wrap_Struct(klass, NULL, RUBY_DEFAULT_FREE, rect);
12
+ }
13
+
14
+ static VALUE RGSS_Rect_GetX(VALUE self)
15
+ {
16
+ RGSS_Rect *rect = DATA_PTR(self);
17
+ return INT2NUM(rect->x);
18
+ }
19
+
20
+ static VALUE RGSS_Rect_GetY(VALUE self)
21
+ {
22
+ RGSS_Rect *rect = DATA_PTR(self);
23
+ return INT2NUM(rect->y);
24
+ }
25
+
26
+ static VALUE RGSS_Rect_GetWidth(VALUE self)
27
+ {
28
+ RGSS_Rect *rect = DATA_PTR(self);
29
+ return INT2NUM(rect->width);
30
+ }
31
+
32
+ static VALUE RGSS_Rect_GetHeight(VALUE self)
33
+ {
34
+ RGSS_Rect *rect = DATA_PTR(self);
35
+ return INT2NUM(rect->height);
36
+ }
37
+
38
+ static VALUE RGSS_Rect_GetLocation(VALUE self)
39
+ {
40
+ RGSS_Rect *rect = DATA_PTR(self);
41
+ return RGSS_IVec2_New(rb_cPoint, rect->x, rect->y);
42
+ }
43
+
44
+ static VALUE RGSS_Rect_GetSize(VALUE self)
45
+ {
46
+ RGSS_Rect *rect = DATA_PTR(self);
47
+ return RGSS_IVec2_New(rb_cSize, rect->width, rect->height);
48
+ }
49
+
50
+ static VALUE RGSS_Rect_GetIndex(VALUE self, VALUE index)
51
+ {
52
+ int i = NUM2INT(index);
53
+ if (i < 0 || i > 3)
54
+ return Qnil;
55
+ return INT2NUM(((int*) DATA_PTR(self))[i]);
56
+ }
57
+
58
+ static VALUE RGSS_Rect_SetIndex(VALUE self, VALUE index, VALUE value)
59
+ {
60
+ int i = NUM2INT(index);
61
+ if (i < 0 || i > 3)
62
+ rb_raise(rb_eArgError, "index out of range (given %d, expect 0..3)", i);
63
+
64
+ int *vec = DATA_PTR(self);
65
+ vec[i] = NUM2INT(value);
66
+ return value;
67
+ }
68
+
69
+ static VALUE RGSS_Rect_SetX(VALUE self, VALUE value)
70
+ {
71
+ RGSS_ASSERT_UNFROZEN(self);
72
+ RGSS_Rect *rect = DATA_PTR(self);
73
+ rect->x = NUM2INT(value);
74
+ return value;
75
+ }
76
+
77
+ static VALUE RGSS_Rect_SetY(VALUE self, VALUE value)
78
+ {
79
+ RGSS_ASSERT_UNFROZEN(self);
80
+ RGSS_Rect *rect = DATA_PTR(self);
81
+ rect->y = NUM2INT(value);
82
+ return value;
83
+ }
84
+
85
+ static VALUE RGSS_Rect_SetWidth(VALUE self, VALUE value)
86
+ {
87
+ RGSS_ASSERT_UNFROZEN(self);
88
+ RGSS_Rect *rect = DATA_PTR(self);
89
+ rect->width = NUM2INT(value);
90
+ return value;
91
+ }
92
+
93
+ static VALUE RGSS_Rect_SetHeight(VALUE self, VALUE value)
94
+ {
95
+ RGSS_ASSERT_UNFROZEN(self);
96
+ RGSS_Rect *rect = DATA_PTR(self);
97
+ rect->y = NUM2INT(value);
98
+ return value;
99
+ }
100
+
101
+ static VALUE RGSS_Rect_SetLocation(VALUE self, VALUE value)
102
+ {
103
+ RGSS_ASSERT_UNFROZEN(self);
104
+ RGSS_Rect *rect = DATA_PTR(self);
105
+ RGSS_Point *ivec = DATA_PTR(value);
106
+ memcpy(&rect->location, ivec, sizeof(RGSS_Point));
107
+ return value;
108
+ }
109
+
110
+ static VALUE RGSS_Rect_SetSize(VALUE self, VALUE value)
111
+ {
112
+ RGSS_ASSERT_UNFROZEN(self);
113
+ RGSS_Rect *rect = DATA_PTR(self);
114
+ RGSS_Size *ivec = DATA_PTR(value);
115
+ memcpy(&rect->size, ivec, sizeof(RGSS_Size));
116
+ return value;
117
+ }
118
+
119
+ static VALUE RGSS_Rect_Set(VALUE self, VALUE x, VALUE y, VALUE width, VALUE height)
120
+ {
121
+ RGSS_ASSERT_UNFROZEN(self);
122
+ RGSS_Rect *rect = DATA_PTR(self);
123
+
124
+ rect->x = NUM2INT(x);
125
+ rect->y = NUM2INT(y);
126
+ rect->width = NUM2INT(width);
127
+ rect->height = NUM2INT(height);
128
+
129
+ return self;
130
+ }
131
+
132
+ static VALUE RGSS_Rect_Equal(VALUE self, VALUE other)
133
+ {
134
+ if (CLASS_OF(self) != CLASS_OF(other))
135
+ return Qfalse;
136
+
137
+ RGSS_Rect *r1 = DATA_PTR(self), *r2 = DATA_PTR(other);
138
+ return RB_BOOL(r1->x == r2->x && r1->y == r2->y && r1->width == r2->width && r1->height == r2->height);
139
+ }
140
+
141
+ static VALUE RGSS_Rect_Dupe(VALUE self)
142
+ {
143
+ RGSS_Rect *src = DATA_PTR(self), *dst = ALLOC(RGSS_Rect);
144
+ memcpy(dst, src, sizeof(RGSS_Rect));
145
+ return Data_Wrap_Struct(CLASS_OF(self), NULL, RUBY_DEFAULT_FREE, dst);
146
+ }
147
+
148
+ static VALUE RGSS_Rect_Clone(VALUE self)
149
+ {
150
+ RGSS_Rect *src = DATA_PTR(self), *dst = ALLOC(RGSS_Rect);
151
+ memcpy(dst, src, sizeof(RGSS_Rect));
152
+
153
+ VALUE value = Data_Wrap_Struct(CLASS_OF(self), NULL, RUBY_DEFAULT_FREE, dst);
154
+ if (rb_obj_frozen_p(self))
155
+ rb_obj_freeze(value);
156
+
157
+ return value;
158
+ }
159
+
160
+ static VALUE RGSS_Rect_IsEmpty(VALUE self)
161
+ {
162
+ RGSS_Rect *rect = DATA_PTR(self);
163
+ return RB_BOOL(rect->x == 0 && rect->y == 0 && rect->width == 0 && rect->height == 0);
164
+ }
165
+
166
+ static VALUE RGSS_Rect_FromLTRB(VALUE klass, VALUE left, VALUE top, VALUE right, VALUE bottom)
167
+ {
168
+ RGSS_Rect *rect = ALLOC(RGSS_Rect);
169
+
170
+ int x, y, width, height;
171
+ rect->x = NUM2INT(left);
172
+ rect->y = NUM2INT(top);
173
+ rect->width = NUM2INT(right) - rect->x;
174
+ rect->height = NUM2INT(bottom) - rect->y;
175
+
176
+ return Data_Wrap_Struct(klass, NULL, RUBY_DEFAULT_FREE, rect);
177
+ }
178
+
179
+ static VALUE RGSS_Rect_GetRight(VALUE self)
180
+ {
181
+ RGSS_Rect *rect = ALLOC(RGSS_Rect);
182
+ return INT2NUM(rect->x + rect->width);
183
+ }
184
+
185
+ static VALUE RGSS_Rect_GetBottom(VALUE self)
186
+ {
187
+ RGSS_Rect *rect = ALLOC(RGSS_Rect);
188
+ return INT2NUM(rect->y + rect->height);
189
+ }
190
+
191
+ static VALUE RGSS_Rect_Contains(int argc, VALUE *argv, VALUE self)
192
+ {
193
+ VALUE arg1, arg2;
194
+ rb_scan_args(argc, argv, "11", &arg1, &arg2);
195
+
196
+ RGSS_Rect *rect = ALLOC(RGSS_Rect);
197
+
198
+ if (argc == 2)
199
+ {
200
+ int x = NUM2INT(arg1);
201
+ int y = NUM2INT(arg2);
202
+ return RB_BOOL(rect->x <= x && x < rect->x + rect->width && rect->y <= y && y < rect->y + rect->height);
203
+ }
204
+
205
+ if (rb_obj_is_kind_of(arg1, rb_cPoint))
206
+ {
207
+ RGSS_Point *pnt = DATA_PTR(arg1);
208
+ return RB_BOOL(rect->x <= pnt->x && pnt->x < rect->x + rect->width && rect->y <= pnt->y &&
209
+ pnt->y < rect->y + rect->height);
210
+ }
211
+
212
+ RGSS_Rect *other = DATA_PTR(arg1);
213
+ return RB_BOOL((rect->x <= other->x) && ((other->x + other->width) <= (rect->x + rect->width)) &&
214
+ (rect->y <= other->y) && ((other->y + other->height) <= (rect->y + rect->height)));
215
+ }
216
+
217
+ static VALUE RGSS_Rect_InflateBang(int argc, VALUE *argv, VALUE self)
218
+ {
219
+ VALUE a1, a2;
220
+ rb_scan_args(argc, argv, "11", &a1, &a2);
221
+
222
+ RGSS_ASSERT_UNFROZEN(self);
223
+ RGSS_Rect *rect = DATA_PTR(self);
224
+
225
+ int x, y;
226
+ if (argc == 1)
227
+ {
228
+ int *point = DATA_PTR(a1);
229
+ x = point[0];
230
+ y = point[1];
231
+ }
232
+ else
233
+ {
234
+ x = NUM2INT(a1);
235
+ y = NUM2INT(a2);
236
+ }
237
+
238
+ rect->x -= x;
239
+ rect->y -= y;
240
+ rect->width += (2 * x);
241
+ rect->height += (2 * y);
242
+
243
+ return self;
244
+ }
245
+
246
+ static VALUE RGSS_Rect_Inflate(int argc, VALUE *argv, VALUE self)
247
+ {
248
+ return RGSS_Rect_InflateBang(argc, argv, RGSS_Rect_Dupe(self));
249
+ }
250
+
251
+ static VALUE RGSS_Rect_IntersectBang(VALUE self, VALUE other)
252
+ {
253
+ RGSS_ASSERT_UNFROZEN(self);
254
+ RGSS_Rect *a = DATA_PTR(self), *b = DATA_PTR(other);
255
+
256
+ int x1 = RGSS_MAX(a->x, b->x);
257
+ int x2 = RGSS_MIN(a->x + a->width, b->x + b->width);
258
+ int y1 = RGSS_MAX(a->y, b->y);
259
+ int y2 = RGSS_MIN(a->y + a->height, b->y + b->height);
260
+
261
+ if (x2 >= x1 && y2 >= y1)
262
+ {
263
+ a->x = x1;
264
+ a->y = y1;
265
+ a->width = x2 - x1;
266
+ a->height = y2 - y1;
267
+ }
268
+ else
269
+ {
270
+ memset(a, 0, sizeof(RGSS_Rect));
271
+ }
272
+
273
+ return self;
274
+ }
275
+
276
+ static VALUE RGSS_Rect_Intersect(VALUE self, VALUE other)
277
+ {
278
+ return RGSS_Rect_Intersect(RGSS_Rect_Dupe(self), other);
279
+ }
280
+
281
+ static VALUE RGSS_Rect_IntersectWith(VALUE self, VALUE other)
282
+ {
283
+ RGSS_Rect *a = DATA_PTR(self), *b = DATA_PTR(other);
284
+ return RB_BOOL((b->x < a->x + a->width) && (a->x < (b->x + b->width)) && (b->y < a->y + a->height) &&
285
+ (a->y < b->y + b->height));
286
+ }
287
+
288
+ static VALUE RGSS_Rect_UnionBang(VALUE self, VALUE other)
289
+ {
290
+ RGSS_ASSERT_UNFROZEN(self);
291
+ RGSS_Rect *a = DATA_PTR(self), *b = DATA_PTR(other);
292
+
293
+ int x1 = RGSS_MIN(a->x, b->x);
294
+ int y1 = RGSS_MIN(a->y, b->y);
295
+ int x2 = RGSS_MAX(a->x + a->width, b->x + b->width);
296
+ int y2 = RGSS_MAX(a->y + a->height, b->y + b->height);
297
+
298
+ a->x = x1;
299
+ a->y = y1;
300
+ a->width = x2 - x1;
301
+ a->height = y2 - y1;
302
+
303
+ return self;
304
+ }
305
+
306
+ static VALUE RGSS_Rect_Union(VALUE self, VALUE other)
307
+ {
308
+ return RGSS_Rect_UnionBang(RGSS_Rect_Dupe(self), other);
309
+ }
310
+
311
+ static VALUE RGSS_Rect_OffsetBang(int argc, VALUE *argv, VALUE self)
312
+ {
313
+ RGSS_ASSERT_UNFROZEN(self);
314
+ VALUE a1, a2;
315
+ rb_scan_args(argc, argv, "11", &a1, &a2);
316
+
317
+ RGSS_Rect *rect = DATA_PTR(self);
318
+ int x, y;
319
+
320
+ if (argc == 1)
321
+ {
322
+ int *v = DATA_PTR(a1);
323
+ x = v[0];
324
+ y = v[1];
325
+ }
326
+ else
327
+ {
328
+ x = NUM2INT(a1);
329
+ y = NUM2INT(a2);
330
+ }
331
+
332
+ rect->x += x;
333
+ rect->y += y;
334
+ return self;
335
+ }
336
+
337
+ static VALUE RGSS_Rect_Offset(int argc, VALUE *argv, VALUE self)
338
+ {
339
+ return RGSS_Rect_OffsetBang(argc, argv, RGSS_Rect_Dupe(self));
340
+ }
341
+
342
+ static VALUE RGSS_Rect_Inspect(VALUE self)
343
+ {
344
+ RGSS_Rect *rect = DATA_PTR(self);
345
+ return rb_sprintf("[%d, %d, %d, %d]", rect->x, rect->y, rect->width, rect->height);
346
+ }
347
+
348
+ static VALUE RGSS_Rect_ToArray(VALUE self)
349
+ {
350
+ int *rect = DATA_PTR(self);
351
+ VALUE ary = rb_ary_new_capa(4);
352
+
353
+ for (int i = 0; i < 4; i++)
354
+ rb_ary_store(ary, i, INT2NUM(rect[i]));
355
+ return ary;
356
+ }
357
+
358
+ static VALUE RGSS_Rect_ToHash(VALUE self)
359
+ {
360
+ RGSS_Rect *rect = DATA_PTR(self);
361
+ VALUE hash = rb_hash_new();
362
+
363
+ rb_hash_aset(hash, STR2SYM("x"), INT2NUM(rect->x));
364
+ rb_hash_aset(hash, STR2SYM("y"), INT2NUM(rect->y));
365
+ rb_hash_aset(hash, STR2SYM("width"), INT2NUM(rect->width));
366
+ rb_hash_aset(hash, STR2SYM("height"), INT2NUM(rect->height));
367
+
368
+ return hash;
369
+ }
370
+
371
+ static VALUE RGSS_Rect_Initialize(int argc, VALUE *argv, VALUE self)
372
+ {
373
+ VALUE a1, a2, a3, a4;
374
+ rb_scan_args(argc, argv, "04", &a1, &a2, &a3, &a4);
375
+
376
+ RGSS_Rect *rect = DATA_PTR(self);
377
+
378
+ switch (argc)
379
+ {
380
+ case 0: break;
381
+ case 2:
382
+ {
383
+ memcpy(&rect->location, DATA_PTR(a1), sizeof(RGSS_Point));
384
+ memcpy(&rect->size, DATA_PTR(a2), sizeof(RGSS_Size));
385
+ break;
386
+ }
387
+ case 4:
388
+ {
389
+ rect->x = NUM2INT(a1);
390
+ rect->y = NUM2INT(a2);
391
+ rect->width = NUM2INT(a3);
392
+ rect->height = NUM2INT(a4);
393
+ break;
394
+ }
395
+ default: rb_raise(rb_eArgError, "wrong number of arguments (given %d, expected 0, 2, 4)", argc);
396
+ }
397
+
398
+ return Qnil;
399
+ }
400
+
401
+ void RGSS_Init_Rect(VALUE parent)
402
+ {
403
+ rb_cRect = rb_define_class_under(parent, "Rect", rb_cObject);
404
+ rb_define_alloc_func(rb_cRect, RGSS_Rect_Alloc);
405
+ rb_define_methodm1(rb_cRect, "initialize", RGSS_Rect_Initialize, -1);
406
+
407
+ rb_define_method0(rb_cRect, "x", RGSS_Rect_GetX, 0);
408
+ rb_define_method0(rb_cRect, "y", RGSS_Rect_GetY, 0);
409
+ rb_define_method0(rb_cRect, "width", RGSS_Rect_GetWidth, 0);
410
+ rb_define_method0(rb_cRect, "height", RGSS_Rect_GetHeight, 0);
411
+ rb_define_method0(rb_cRect, "location", RGSS_Rect_GetLocation, 0);
412
+ rb_define_method0(rb_cRect, "size", RGSS_Rect_GetSize, 0);
413
+ rb_define_method1(rb_cRect, "x=", RGSS_Rect_SetX, 0);
414
+ rb_define_method1(rb_cRect, "y=", RGSS_Rect_SetY, 0);
415
+ rb_define_method1(rb_cRect, "width=", RGSS_Rect_SetWidth, 0);
416
+ rb_define_method1(rb_cRect, "height=", RGSS_Rect_SetHeight, 0);
417
+ rb_define_method1(rb_cRect, "location=", RGSS_Rect_SetLocation, 0);
418
+ rb_define_method1(rb_cRect, "size=", RGSS_Rect_SetSize, 0);
419
+ rb_define_method1(rb_cRect, "[]", RGSS_Rect_GetIndex, 1);
420
+ rb_define_method2(rb_cRect, "[]=", RGSS_Rect_SetIndex, 2);
421
+ rb_define_method4(rb_cRect, "set", RGSS_Rect_Set, 4);
422
+ rb_define_method1(rb_cRect, "==", RGSS_Rect_Equal, 1);
423
+ rb_define_method1(rb_cRect, "eql?", RGSS_Rect_Equal, 1);
424
+ rb_define_method0(rb_cRect, "dup", RGSS_Rect_Dupe, 0);
425
+ rb_define_method0(rb_cRect, "clone", RGSS_Rect_Clone, 0);
426
+ rb_define_method0(rb_cRect, "empty?", RGSS_Rect_IsEmpty, 0);
427
+ rb_define_method0(rb_cRect, "left", RGSS_Rect_GetX, 0);
428
+ rb_define_method0(rb_cRect, "top", RGSS_Rect_GetY, 0);
429
+ rb_define_method0(rb_cRect, "right", RGSS_Rect_GetRight, 0);
430
+ rb_define_method0(rb_cRect, "bottom", RGSS_Rect_GetBottom, 0);
431
+ rb_define_methodm1(rb_cRect, "contains?", RGSS_Rect_Contains, -1);
432
+ rb_define_methodm1(rb_cRect, "inflate!", RGSS_Rect_InflateBang, -1);
433
+ rb_define_methodm1(rb_cRect, "inflate", RGSS_Rect_Inflate, -1);
434
+ rb_define_method1(rb_cRect, "intersect!", RGSS_Rect_IntersectBang, 1);
435
+ rb_define_method1(rb_cRect, "intersect", RGSS_Rect_Intersect, 1);
436
+ rb_define_method1(rb_cRect, "intersect?", RGSS_Rect_IntersectWith, 1);
437
+ rb_define_methodm1(rb_cRect, "offset!", RGSS_Rect_OffsetBang, -1);
438
+ rb_define_methodm1(rb_cRect, "offset", RGSS_Rect_Offset, -1);
439
+ rb_define_method0(rb_cRect, "inspect", RGSS_Rect_Inspect, 0);
440
+ rb_define_method0(rb_cRect, "to_a", RGSS_Rect_ToArray, 0);
441
+ rb_define_method0(rb_cRect, "to_h", RGSS_Rect_ToHash, 0);
442
+ rb_define_methodm1(rb_cRect, "_dump", RGSS_Rect_Dump, -1);
443
+
444
+ rb_define_singleton_method0(rb_cRect, "empty", RGSS_Rect_Alloc, 0);
445
+ rb_define_singleton_method4(rb_cRect, "ltrb", RGSS_Rect_FromLTRB, 4);
446
+ rb_define_singleton_method1(rb_cRect, "_load", RGSS_Rect_Load, 1);
447
+
448
+ rb_define_alias(rb_cRect, "include?", "contains?");
449
+ }