cyross-ruby-miyako 2.0.5.1 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (91) hide show
  1. data/README +1092 -929
  2. data/Rakefile +7 -7
  3. data/defines.h +144 -144
  4. data/extern.h +29 -29
  5. data/install_miyako.rb +87 -87
  6. data/lib/Miyako/API/audio.rb +572 -198
  7. data/lib/Miyako/API/basic_data.rb +825 -573
  8. data/lib/Miyako/API/bitmap.rb +534 -507
  9. data/lib/Miyako/API/choices.rb +481 -475
  10. data/lib/Miyako/API/collision.rb +486 -460
  11. data/lib/Miyako/API/diagram.rb +586 -561
  12. data/lib/Miyako/API/drawing.rb +151 -151
  13. data/lib/Miyako/API/exceptions.rb +105 -0
  14. data/lib/Miyako/API/fixedmap.rb +462 -428
  15. data/lib/Miyako/API/font.rb +430 -403
  16. data/lib/Miyako/API/input.rb +456 -447
  17. data/lib/Miyako/API/layout.rb +636 -433
  18. data/lib/Miyako/API/map.rb +583 -529
  19. data/lib/Miyako/API/map_event.rb +222 -198
  20. data/lib/Miyako/API/modules.rb +357 -109
  21. data/lib/Miyako/API/movie.rb +166 -154
  22. data/lib/Miyako/API/parts.rb +276 -189
  23. data/lib/Miyako/API/plane.rb +205 -166
  24. data/lib/Miyako/API/screen.rb +341 -325
  25. data/lib/Miyako/API/shape.rb +443 -443
  26. data/lib/Miyako/API/sprite.rb +771 -752
  27. data/lib/Miyako/API/sprite_animation.rb +490 -481
  28. data/lib/Miyako/API/sprite_list.rb +1135 -0
  29. data/lib/Miyako/API/spriteunit.rb +168 -147
  30. data/lib/Miyako/API/story.rb +350 -300
  31. data/lib/Miyako/API/textbox.rb +770 -725
  32. data/lib/Miyako/API/utility.rb +419 -388
  33. data/lib/Miyako/API/viewport.rb +189 -139
  34. data/lib/Miyako/API/yuki.rb +1226 -996
  35. data/lib/Miyako/EXT/miyako_cairo.rb +62 -62
  36. data/lib/Miyako/EXT/raster_scroll.rb +138 -138
  37. data/lib/Miyako/EXT/slides.rb +157 -157
  38. data/lib/Miyako/miyako.rb +201 -171
  39. data/lib/Miyako/miyako_require_only.rb +35 -0
  40. data/miyako_basicdata.c +590 -590
  41. data/miyako_bitmap.c +1225 -1225
  42. data/miyako_collision.c +403 -403
  43. data/miyako_drawing.c +187 -187
  44. data/miyako_font.c +334 -334
  45. data/miyako_hsv.c +830 -830
  46. data/miyako_input_audio.c +254 -0
  47. data/miyako_layout.c +191 -191
  48. data/miyako_no_katana.c +1078 -1074
  49. data/miyako_sprite2.c +431 -0
  50. data/miyako_transform.c +438 -438
  51. data/miyako_utility.c +288 -288
  52. data/sample/Animation1/m1ku.rb +68 -68
  53. data/sample/Animation2/lex.rb +96 -96
  54. data/sample/Diagram_sample/diagram_sample_yuki2.rb +328 -386
  55. data/sample/Room3/blue.rb +297 -297
  56. data/sample/Room3/ending.rb +180 -180
  57. data/sample/Room3/green.rb +220 -220
  58. data/sample/Room3/main.rb +119 -119
  59. data/sample/Room3/main_component.rb +59 -59
  60. data/sample/Room3/red.rb +227 -227
  61. data/sample/Room3/room3.rb +25 -27
  62. data/sample/Room3/title.rb +184 -184
  63. data/sample/ball_action_sample.rb +204 -204
  64. data/sample/blit_rop.rb +70 -70
  65. data/sample/cairo_sample.rb +25 -25
  66. data/sample/circle_collision_test.rb +66 -66
  67. data/sample/collision_test.rb +33 -33
  68. data/sample/collision_test2.rb +108 -108
  69. data/sample/fixed_map_test/fixed_map_sample.rb +140 -140
  70. data/sample/fixed_map_test/readme.txt +72 -72
  71. data/sample/map_test/chara.rb +58 -58
  72. data/sample/map_test/main_parts.rb +69 -69
  73. data/sample/map_test/main_scene.rb +153 -153
  74. data/sample/map_test/map_manager.rb +75 -75
  75. data/sample/map_test/map_test.rb +23 -23
  76. data/sample/map_test/oasis.rb +71 -71
  77. data/sample/map_test/readme.txt +89 -89
  78. data/sample/map_test/route.rb +157 -157
  79. data/sample/map_test/town.rb +74 -74
  80. data/sample/polygon_test.rb +35 -35
  81. data/sample/rasterscroll.rb +24 -24
  82. data/sample/takahashi.rb +42 -42
  83. data/sample/textbox_sample.rb +189 -189
  84. data/sample/transform.rb +54 -54
  85. data/sample/utility_test.rb +73 -73
  86. data/sample/utility_test2.rb +61 -61
  87. data/sample/utility_test3.rb +64 -64
  88. data/sample/utility_test4.rb +73 -73
  89. data/uninstall_miyako.rb +19 -19
  90. data/win/miyako_no_katana.so +0 -0
  91. metadata +7 -2
data/miyako_collision.c CHANGED
@@ -1,403 +1,403 @@
1
- /*
2
- --
3
- Miyako v2.0 Extend Library "Miyako no Katana"
4
- Copyright (C) 2008 Cyross Makoto
5
-
6
- This library is free software; you can redistribute it and/or
7
- modify it under the terms of the GNU Lesser General Public
8
- License as published by the Free Software Foundation; either
9
- version 2.1 of the License, or (at your option) any later version.
10
-
11
- This library is distributed in the hope that it will be useful,
12
- but WITHOUT ANY WARRANTY; without even the implied warranty of
13
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
- Lesser General Public License for more details.
15
-
16
- You should have received a copy of the GNU Lesser General Public
17
- License along with this library; if not, write to the Free Software
18
- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
- ++
20
- */
21
-
22
- /*
23
- =拡張ライブラリmiyako_no_katana
24
- Authors:: サイロス誠
25
- Version:: 2.0
26
- Copyright:: 2007-2009 Cyross Makoto
27
- License:: LGPL2.1
28
- */
29
- #include "defines.h"
30
-
31
- static VALUE mSDL = Qnil;
32
- static VALUE mMiyako = Qnil;
33
- static VALUE eMiyakoError = Qnil;
34
- static VALUE cCollision = Qnil;
35
- static VALUE cCircleCollision = Qnil;
36
- static VALUE cCollisions = Qnil;
37
- static VALUE nZero = Qnil;
38
- static VALUE nOne = Qnil;
39
- static volatile ID id_update = Qnil;
40
- static volatile ID id_kakko = Qnil;
41
- static volatile ID id_render = Qnil;
42
- static volatile ID id_to_a = Qnil;
43
- static volatile int zero = Qnil;
44
- static volatile int one = Qnil;
45
-
46
- /*
47
- :nodoc:
48
- */
49
- static VALUE collision_get_position(VALUE pos, VALUE *x, VALUE *y)
50
- {
51
- VALUE *tmp;
52
- switch(TYPE(pos))
53
- {
54
- case T_ARRAY:
55
- if(RARRAY_LEN(pos) < 2)
56
- rb_raise(eMiyakoError, "pairs have illegal array!");
57
- tmp = RARRAY_PTR(pos);
58
- *x = *tmp++;
59
- *y = *tmp;
60
- break;
61
- case T_STRUCT:
62
- if(RSTRUCT_LEN(pos) < 2)
63
- rb_raise(eMiyakoError, "pairs have illegal struct!");
64
- tmp = RSTRUCT_PTR(pos);
65
- *x = *tmp++;
66
- *y = *tmp;
67
- break;
68
- default:
69
- *x = rb_funcall(pos, rb_intern("x"), 0);
70
- *y = rb_funcall(pos, rb_intern("y"), 0);
71
- break;
72
- }
73
- return Qnil;
74
- }
75
-
76
- /*
77
- :nodoc:
78
- */
79
- static VALUE collision_c_collision(VALUE self, VALUE c1, VALUE pos1, VALUE c2, VALUE pos2)
80
- {
81
- VALUE *prect1 = RSTRUCT_PTR(rb_iv_get(c1, "@rect"));
82
- VALUE *prect2 = RSTRUCT_PTR(rb_iv_get(c2, "@rect"));
83
- VALUE x1, y1, x2, y2;
84
- collision_get_position(pos1, &x1, &y1);
85
- collision_get_position(pos2, &x2, &y2);
86
- double l1 = NUM2DBL(x1) + NUM2DBL(*prect1);
87
- double t1 = NUM2DBL(y1) + NUM2DBL(*(prect1+1));
88
- double r1 = l1 + NUM2DBL(*(prect1+2)) - 1;
89
- double b1 = t1 + NUM2DBL(*(prect1+3)) - 1;
90
- double l2 = NUM2DBL(x2) + NUM2DBL(*prect2);
91
- double t2 = NUM2DBL(y2) + NUM2DBL(*(prect2+1));
92
- double r2 = l2 + NUM2DBL(*(prect2+2)) - 1;
93
- double b2 = t2 + NUM2DBL(*(prect2+3)) - 1;
94
-
95
- if(l1 <= l2 && l2 <= r1 && t1 <= t2 && t2 <= b1) return Qtrue;
96
- if(l1 <= r2 && r2 <= r1 && t1 <= b2 && b2 <= b1) return Qtrue;
97
- if(l2 <= l1 && l1 <= r2 && t2 <= t1 && t1 <= b2) return Qtrue;
98
- if(l2 <= r1 && r1 <= r2 && t2 <= b1 && b1 <= b2) return Qtrue;
99
- return Qfalse;
100
- }
101
-
102
- /*
103
- :nodoc:
104
- */
105
- static VALUE collision_c_meet(VALUE self, VALUE c1, VALUE pos1, VALUE c2, VALUE pos2)
106
- {
107
- VALUE *prect1 = RSTRUCT_PTR(rb_iv_get(c1, "@rect"));
108
- VALUE *prect2 = RSTRUCT_PTR(rb_iv_get(c2, "@rect"));
109
- VALUE x1, y1, x2, y2;
110
- collision_get_position(pos1, &x1, &y1);
111
- collision_get_position(pos2, &x2, &y2);
112
- double l1 = NUM2DBL(x1) + NUM2DBL(*prect1);
113
- double t1 = NUM2DBL(y1) + NUM2DBL(*(prect1+1));
114
- double r1 = l1 + NUM2DBL(*(prect1+2)) - 1;
115
- double b1 = t1 + NUM2DBL(*(prect1+3)) - 1;
116
- double l2 = NUM2DBL(x2) + NUM2DBL(*prect2);
117
- double t2 = NUM2DBL(y2) + NUM2DBL(*(prect2+1));
118
- double r2 = l2 + NUM2DBL(*(prect2+2)) - 1;
119
- double b2 = t2 + NUM2DBL(*(prect2+3)) - 1;
120
-
121
- if(r1 == l2 || b1 == t2 || l1 == r2 || t1 == b2) return Qtrue;
122
- return Qfalse;
123
- }
124
-
125
- /*
126
- :nodoc:
127
- */
128
- static VALUE collision_c_cover(VALUE self, VALUE c1, VALUE pos1, VALUE c2, VALUE pos2)
129
- {
130
- VALUE *prect1 = RSTRUCT_PTR(rb_iv_get(c1, "@rect"));
131
- VALUE *prect2 = RSTRUCT_PTR(rb_iv_get(c2, "@rect"));
132
- VALUE x1, y1, x2, y2;
133
- collision_get_position(pos1, &x1, &y1);
134
- collision_get_position(pos2, &x2, &y2);
135
- double l1 = NUM2DBL(x1) + NUM2DBL(*prect1);
136
- double t1 = NUM2DBL(y1) + NUM2DBL(*(prect1+1));
137
- double r1 = l1 + NUM2DBL(*(prect1+2)) - 1;
138
- double b1 = t1 + NUM2DBL(*(prect1+3)) - 1;
139
- double l2 = NUM2DBL(x2) + NUM2DBL(*prect2);
140
- double t2 = NUM2DBL(y2) + NUM2DBL(*(prect2+1));
141
- double r2 = l2 + NUM2DBL(*(prect2+2)) - 1;
142
- double b2 = t2 + NUM2DBL(*(prect2+3)) - 1;
143
-
144
- if(l1 >= l2 && r1 <= r2 && t1 >= t2 && b1 <= b2) return Qtrue;
145
- if(l1 <= l2 && r1 >= r2 && t1 <= t2 && b1 >= b2) return Qtrue;
146
- if(l2 >= l1 && r2 <= r1 && t2 >= t1 && b2 <= b1) return Qtrue;
147
- if(l2 <= l1 && r2 >= r1 && t2 <= t1 && b2 >= b1) return Qtrue;
148
- return Qfalse;
149
- }
150
-
151
- /*
152
- :nodoc:
153
- */
154
- static VALUE collision_collision(VALUE self, VALUE pos1, VALUE c2, VALUE pos2)
155
- {
156
- return collision_c_collision(cCollision, self, pos1, c2, pos2);
157
- }
158
-
159
- /*
160
- :nodoc:
161
- */
162
- static VALUE collision_meet(VALUE self, VALUE pos1, VALUE c2, VALUE pos2)
163
- {
164
- return collision_c_meet(cCollision, self, pos1, c2, pos2);
165
- }
166
-
167
- /*
168
- :nodoc:
169
- */
170
- static VALUE collision_cover(VALUE self, VALUE pos1, VALUE c2, VALUE pos2)
171
- {
172
- return collision_c_cover(cCollision, self, pos1, c2, pos2);
173
- }
174
-
175
- /*
176
- :nodoc:
177
- */
178
- static VALUE circlecollision_c_collision(VALUE self, VALUE c1, VALUE pos1, VALUE c2, VALUE pos2)
179
- {
180
- VALUE *pcenter1 = RSTRUCT_PTR(rb_iv_get(c1, "@center"));
181
- VALUE *pcenter2 = RSTRUCT_PTR(rb_iv_get(c2, "@center"));
182
- double r1 = NUM2DBL(rb_iv_get(c1, "@radius"));
183
- double r2 = NUM2DBL(rb_iv_get(c2, "@radius"));
184
- double r = (r1 + r2) * (r1 + r2);
185
- VALUE x1, y1, x2, y2;
186
- collision_get_position(pos1, &x1, &y1);
187
- collision_get_position(pos2, &x2, &y2);
188
- double cx1 = NUM2DBL(x1) + NUM2DBL(*pcenter1);
189
- double cy1 = NUM2DBL(y1) + NUM2DBL(*(pcenter1+1));
190
- double cx2 = NUM2DBL(x2) + NUM2DBL(*pcenter2);
191
- double cy2 = NUM2DBL(y2) + NUM2DBL(*(pcenter2+1));
192
- double d = (cx1-cx2) * (cx1-cx2) + (cy1-cy2) * (cy1-cy2);
193
-
194
- if(d <= r) return Qtrue;
195
- return Qfalse;
196
- }
197
-
198
- /*
199
- :nodoc:
200
- */
201
- static VALUE circlecollision_c_meet(VALUE self, VALUE c1, VALUE pos1, VALUE c2, VALUE pos2)
202
- {
203
- VALUE *pcenter1 = RSTRUCT_PTR(rb_iv_get(c1, "@center"));
204
- VALUE *pcenter2 = RSTRUCT_PTR(rb_iv_get(c2, "@center"));
205
- double r1 = NUM2DBL(rb_iv_get(c1, "@radius"));
206
- double r2 = NUM2DBL(rb_iv_get(c2, "@radius"));
207
- double r = (r1 + r2) * (r1 + r2);
208
- VALUE x1, y1, x2, y2;
209
- collision_get_position(pos1, &x1, &y1);
210
- collision_get_position(pos2, &x2, &y2);
211
- double cx1 = NUM2DBL(x1) + NUM2DBL(*pcenter1);
212
- double cy1 = NUM2DBL(y1) + NUM2DBL(*(pcenter1+1));
213
- double cx2 = NUM2DBL(x2) + NUM2DBL(*pcenter2);
214
- double cy2 = NUM2DBL(y2) + NUM2DBL(*(pcenter2+1));
215
- double d = (cx1-cx2) * (cx1-cx2) + (cy1-cy2) * (cy1-cy2);
216
-
217
- if(d == r) return Qtrue;
218
- return Qfalse;
219
- }
220
-
221
- /*
222
- :nodoc:
223
- */
224
- static VALUE circlecollision_c_cover(VALUE self, VALUE c1, VALUE pos1, VALUE c2, VALUE pos2)
225
- {
226
- VALUE *pcenter1 = RSTRUCT_PTR(rb_iv_get(c1, "@center"));
227
- VALUE *pcenter2 = RSTRUCT_PTR(rb_iv_get(c2, "@center"));
228
- double r1 = NUM2DBL(rb_iv_get(c1, "@radius"));
229
- double r2 = NUM2DBL(rb_iv_get(c2, "@radius"));
230
- double r = (r1 - r2) * (r1 - r2); // y = (x-a)^2 -> y = x^2 - 2ax + a^2
231
- VALUE x1, y1, x2, y2;
232
- collision_get_position(pos1, &x1, &y1);
233
- collision_get_position(pos2, &x2, &y2);
234
- double cx1 = NUM2DBL(x1) + NUM2DBL(*pcenter1);
235
- double cy1 = NUM2DBL(y1) + NUM2DBL(*(pcenter1+1));
236
- double cx2 = NUM2DBL(x2) + NUM2DBL(*pcenter2);
237
- double cy2 = NUM2DBL(y2) + NUM2DBL(*(pcenter2+1));
238
- double d = (cx1-cx2) * (cx1-cx2) + (cy1-cy2) * (cy1-cy2);
239
-
240
- if(d <= r) return Qtrue;
241
- return Qfalse;
242
- }
243
-
244
- /*
245
- :nodoc:
246
- */
247
- static VALUE circlecollision_collision(VALUE self, VALUE pos1, VALUE c2, VALUE pos2)
248
- {
249
- return circlecollision_c_collision(cCircleCollision, self, pos1, c2, pos2);
250
- }
251
-
252
- /*
253
- :nodoc:
254
- */
255
- static VALUE circlecollision_meet(VALUE self, VALUE pos1, VALUE c2, VALUE pos2)
256
- {
257
- return circlecollision_c_meet(cCircleCollision, self, pos1, c2, pos2);
258
- }
259
-
260
- /*
261
- :nodoc:
262
- */
263
- static VALUE circlecollision_cover(VALUE self, VALUE pos1, VALUE c2, VALUE pos2)
264
- {
265
- return circlecollision_c_cover(cCircleCollision, self, pos1, c2, pos2);
266
- }
267
-
268
- /*
269
- :nodoc:
270
- */
271
- static VALUE collisions_collision(VALUE self, VALUE c, VALUE pos)
272
- {
273
- VALUE collisions = rb_iv_get(self, "@collisions");
274
- int i=0;
275
- for(i=0; i<RARRAY_LEN(collisions); i++){
276
- VALUE cs = *(RARRAY_PTR(collisions) + i);
277
- VALUE *cc = RARRAY_PTR(cs);
278
- if(collision_collision(c, pos, *cc, *(cc+1)) == Qtrue){ return cs; }
279
- }
280
- return Qnil;
281
- }
282
-
283
- /*
284
- :nodoc:
285
- */
286
- static VALUE collisions_meet(VALUE self, VALUE c, VALUE pos)
287
- {
288
- VALUE collisions = rb_iv_get(self, "@collisions");
289
- int i=0;
290
- for(i=0; i<RARRAY_LEN(collisions); i++){
291
- VALUE cs = *(RARRAY_PTR(collisions) + i);
292
- VALUE *cc = RARRAY_PTR(cs);
293
- if(collision_meet(c, pos, *cc, *(cc+1)) == Qtrue){ return cs; }
294
- }
295
- return Qnil;
296
- }
297
-
298
- /*
299
- :nodoc:
300
- */
301
- static VALUE collisions_cover(VALUE self, VALUE c, VALUE pos)
302
- {
303
- VALUE collisions = rb_iv_get(self, "@collisions");
304
- int i=0;
305
- for(i=0; i<RARRAY_LEN(collisions); i++){
306
- VALUE cs = *(RARRAY_PTR(collisions) + i);
307
- VALUE *cc = RARRAY_PTR(cs);
308
- if(collision_cover(c, pos, *cc, *(cc+1)) == Qtrue){ return cs; }
309
- }
310
- return Qnil;
311
- }
312
-
313
- /*
314
- :nodoc:
315
- */
316
- static VALUE collisions_collision_all(VALUE self, VALUE c, VALUE pos)
317
- {
318
- VALUE collisions = rb_iv_get(self, "@collisions");
319
- VALUE ret = rb_ary_new();
320
- int i=0;
321
- for(i=0; i<RARRAY_LEN(collisions); i++){
322
- VALUE cs = *(RARRAY_PTR(collisions) + i);
323
- VALUE *cc = RARRAY_PTR(cs);
324
- if(collision_collision(c, pos, *cc, *(cc+1)) == Qtrue){ rb_ary_push(ret, cs); }
325
- }
326
- if(RARRAY_LEN(ret) == 0){ return Qnil; }
327
- return ret;
328
- }
329
-
330
- /*
331
- :nodoc:
332
- */
333
- static VALUE collisions_meet_all(VALUE self, VALUE c, VALUE pos)
334
- {
335
- VALUE collisions = rb_iv_get(self, "@collisions");
336
- VALUE ret = rb_ary_new();
337
- int i=0;
338
- for(i=0; i<RARRAY_LEN(collisions); i++){
339
- VALUE cs = *(RARRAY_PTR(collisions) + i);
340
- VALUE *cc = RARRAY_PTR(cs);
341
- if(collision_meet(c, pos, *cc, *(cc+1)) == Qtrue){ rb_ary_push(ret, cs); }
342
- }
343
- if(RARRAY_LEN(ret) == 0){ return Qnil; }
344
- return ret;
345
- }
346
-
347
- /*
348
- :nodoc:
349
- */
350
- static VALUE collisions_cover_all(VALUE self, VALUE c, VALUE pos)
351
- {
352
- VALUE collisions = rb_iv_get(self, "@collisions");
353
- VALUE ret = rb_ary_new();
354
- int i=0;
355
- for(i=0; i<RARRAY_LEN(collisions); i++){
356
- VALUE cs = *(RARRAY_PTR(collisions) + i);
357
- VALUE *cc = RARRAY_PTR(cs);
358
- if(collision_cover(c, pos, *cc, *(cc+1)) == Qtrue){ rb_ary_push(ret, cs); }
359
- }
360
- if(RARRAY_LEN(ret) == 0){ return Qnil; }
361
- return ret;
362
- }
363
-
364
- void Init_miyako_collision()
365
- {
366
- mSDL = rb_define_module("SDL");
367
- mMiyako = rb_define_module("Miyako");
368
- eMiyakoError = rb_define_class_under(mMiyako, "MiyakoError", rb_eException);
369
- cCollision = rb_define_class_under(mMiyako, "Collision", rb_cObject);
370
- cCircleCollision = rb_define_class_under(mMiyako, "CircleCollision", rb_cObject);
371
- cCollisions = rb_define_class_under(mMiyako, "Collisions", rb_cObject);
372
-
373
- id_update = rb_intern("update");
374
- id_kakko = rb_intern("[]");
375
- id_render = rb_intern("render");
376
- id_to_a = rb_intern("to_a");
377
-
378
- zero = 0;
379
- nZero = INT2NUM(zero);
380
- one = 1;
381
- nOne = INT2NUM(one);
382
-
383
- rb_define_singleton_method(cCollision, "collision?", collision_c_collision, 4);
384
- rb_define_singleton_method(cCollision, "meet?", collision_c_meet, 4);
385
- rb_define_singleton_method(cCollision, "cover?", collision_c_cover, 4);
386
- rb_define_method(cCollision, "collision?", collision_collision, 3);
387
- rb_define_method(cCollision, "meet?", collision_meet, 3);
388
- rb_define_method(cCollision, "cover?", collision_cover, 3);
389
-
390
- rb_define_singleton_method(cCircleCollision, "collision?", circlecollision_c_collision, 4);
391
- rb_define_singleton_method(cCircleCollision, "meet?", circlecollision_c_meet, 4);
392
- rb_define_singleton_method(cCircleCollision, "cover?", circlecollision_c_cover, 4);
393
- rb_define_method(cCircleCollision, "collision?", circlecollision_collision, 3);
394
- rb_define_method(cCircleCollision, "meet?", circlecollision_meet, 3);
395
- rb_define_method(cCircleCollision, "cover?", circlecollision_cover, 3);
396
-
397
- rb_define_method(cCollisions, "collision?", collisions_collision, 2);
398
- rb_define_method(cCollisions, "meet?", collisions_meet, 2);
399
- rb_define_method(cCollisions, "cover?", collisions_cover, 2);
400
- rb_define_method(cCollisions, "collision_all?", collisions_collision_all, 2);
401
- rb_define_method(cCollisions, "meet_all?", collisions_meet_all, 2);
402
- rb_define_method(cCollisions, "cover_all?", collisions_cover_all, 2);
403
- }
1
+ /*
2
+ --
3
+ Miyako v2.0 Extend Library "Miyako no Katana"
4
+ Copyright (C) 2008 Cyross Makoto
5
+
6
+ This library is free software; you can redistribute it and/or
7
+ modify it under the terms of the GNU Lesser General Public
8
+ License as published by the Free Software Foundation; either
9
+ version 2.1 of the License, or (at your option) any later version.
10
+
11
+ This library is distributed in the hope that it will be useful,
12
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
+ Lesser General Public License for more details.
15
+
16
+ You should have received a copy of the GNU Lesser General Public
17
+ License along with this library; if not, write to the Free Software
18
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
+ ++
20
+ */
21
+
22
+ /*
23
+ =拡張ライブラリmiyako_no_katana
24
+ Authors:: サイロス誠
25
+ Version:: 2.0
26
+ Copyright:: 2007-2009 Cyross Makoto
27
+ License:: LGPL2.1
28
+ */
29
+ #include "defines.h"
30
+
31
+ static VALUE mSDL = Qnil;
32
+ static VALUE mMiyako = Qnil;
33
+ static VALUE eMiyakoError = Qnil;
34
+ static VALUE cCollision = Qnil;
35
+ static VALUE cCircleCollision = Qnil;
36
+ static VALUE cCollisions = Qnil;
37
+ static VALUE nZero = Qnil;
38
+ static VALUE nOne = Qnil;
39
+ static volatile ID id_update = Qnil;
40
+ static volatile ID id_kakko = Qnil;
41
+ static volatile ID id_render = Qnil;
42
+ static volatile ID id_to_a = Qnil;
43
+ static volatile int zero = Qnil;
44
+ static volatile int one = Qnil;
45
+
46
+ /*
47
+ :nodoc:
48
+ */
49
+ static VALUE collision_get_position(VALUE pos, VALUE *x, VALUE *y)
50
+ {
51
+ VALUE *tmp;
52
+ switch(TYPE(pos))
53
+ {
54
+ case T_ARRAY:
55
+ if(RARRAY_LEN(pos) < 2)
56
+ rb_raise(eMiyakoError, "pairs have illegal array!");
57
+ tmp = RARRAY_PTR(pos);
58
+ *x = *tmp++;
59
+ *y = *tmp;
60
+ break;
61
+ case T_STRUCT:
62
+ if(RSTRUCT_LEN(pos) < 2)
63
+ rb_raise(eMiyakoError, "pairs have illegal struct!");
64
+ tmp = RSTRUCT_PTR(pos);
65
+ *x = *tmp++;
66
+ *y = *tmp;
67
+ break;
68
+ default:
69
+ *x = rb_funcall(pos, rb_intern("x"), 0);
70
+ *y = rb_funcall(pos, rb_intern("y"), 0);
71
+ break;
72
+ }
73
+ return Qnil;
74
+ }
75
+
76
+ /*
77
+ :nodoc:
78
+ */
79
+ static VALUE collision_c_collision(VALUE self, VALUE c1, VALUE pos1, VALUE c2, VALUE pos2)
80
+ {
81
+ VALUE *prect1 = RSTRUCT_PTR(rb_iv_get(c1, "@rect"));
82
+ VALUE *prect2 = RSTRUCT_PTR(rb_iv_get(c2, "@rect"));
83
+ VALUE x1, y1, x2, y2;
84
+ collision_get_position(pos1, &x1, &y1);
85
+ collision_get_position(pos2, &x2, &y2);
86
+ double l1 = NUM2DBL(x1) + NUM2DBL(*prect1);
87
+ double t1 = NUM2DBL(y1) + NUM2DBL(*(prect1+1));
88
+ double r1 = l1 + NUM2DBL(*(prect1+2)) - 1;
89
+ double b1 = t1 + NUM2DBL(*(prect1+3)) - 1;
90
+ double l2 = NUM2DBL(x2) + NUM2DBL(*prect2);
91
+ double t2 = NUM2DBL(y2) + NUM2DBL(*(prect2+1));
92
+ double r2 = l2 + NUM2DBL(*(prect2+2)) - 1;
93
+ double b2 = t2 + NUM2DBL(*(prect2+3)) - 1;
94
+
95
+ if(l1 <= l2 && l2 <= r1 && t1 <= t2 && t2 <= b1) return Qtrue;
96
+ if(l1 <= r2 && r2 <= r1 && t1 <= b2 && b2 <= b1) return Qtrue;
97
+ if(l2 <= l1 && l1 <= r2 && t2 <= t1 && t1 <= b2) return Qtrue;
98
+ if(l2 <= r1 && r1 <= r2 && t2 <= b1 && b1 <= b2) return Qtrue;
99
+ return Qfalse;
100
+ }
101
+
102
+ /*
103
+ :nodoc:
104
+ */
105
+ static VALUE collision_c_meet(VALUE self, VALUE c1, VALUE pos1, VALUE c2, VALUE pos2)
106
+ {
107
+ VALUE *prect1 = RSTRUCT_PTR(rb_iv_get(c1, "@rect"));
108
+ VALUE *prect2 = RSTRUCT_PTR(rb_iv_get(c2, "@rect"));
109
+ VALUE x1, y1, x2, y2;
110
+ collision_get_position(pos1, &x1, &y1);
111
+ collision_get_position(pos2, &x2, &y2);
112
+ double l1 = NUM2DBL(x1) + NUM2DBL(*prect1);
113
+ double t1 = NUM2DBL(y1) + NUM2DBL(*(prect1+1));
114
+ double r1 = l1 + NUM2DBL(*(prect1+2)) - 1;
115
+ double b1 = t1 + NUM2DBL(*(prect1+3)) - 1;
116
+ double l2 = NUM2DBL(x2) + NUM2DBL(*prect2);
117
+ double t2 = NUM2DBL(y2) + NUM2DBL(*(prect2+1));
118
+ double r2 = l2 + NUM2DBL(*(prect2+2)) - 1;
119
+ double b2 = t2 + NUM2DBL(*(prect2+3)) - 1;
120
+
121
+ if(r1 == l2 || b1 == t2 || l1 == r2 || t1 == b2) return Qtrue;
122
+ return Qfalse;
123
+ }
124
+
125
+ /*
126
+ :nodoc:
127
+ */
128
+ static VALUE collision_c_cover(VALUE self, VALUE c1, VALUE pos1, VALUE c2, VALUE pos2)
129
+ {
130
+ VALUE *prect1 = RSTRUCT_PTR(rb_iv_get(c1, "@rect"));
131
+ VALUE *prect2 = RSTRUCT_PTR(rb_iv_get(c2, "@rect"));
132
+ VALUE x1, y1, x2, y2;
133
+ collision_get_position(pos1, &x1, &y1);
134
+ collision_get_position(pos2, &x2, &y2);
135
+ double l1 = NUM2DBL(x1) + NUM2DBL(*prect1);
136
+ double t1 = NUM2DBL(y1) + NUM2DBL(*(prect1+1));
137
+ double r1 = l1 + NUM2DBL(*(prect1+2)) - 1;
138
+ double b1 = t1 + NUM2DBL(*(prect1+3)) - 1;
139
+ double l2 = NUM2DBL(x2) + NUM2DBL(*prect2);
140
+ double t2 = NUM2DBL(y2) + NUM2DBL(*(prect2+1));
141
+ double r2 = l2 + NUM2DBL(*(prect2+2)) - 1;
142
+ double b2 = t2 + NUM2DBL(*(prect2+3)) - 1;
143
+
144
+ if(l1 >= l2 && r1 <= r2 && t1 >= t2 && b1 <= b2) return Qtrue;
145
+ if(l1 <= l2 && r1 >= r2 && t1 <= t2 && b1 >= b2) return Qtrue;
146
+ if(l2 >= l1 && r2 <= r1 && t2 >= t1 && b2 <= b1) return Qtrue;
147
+ if(l2 <= l1 && r2 >= r1 && t2 <= t1 && b2 >= b1) return Qtrue;
148
+ return Qfalse;
149
+ }
150
+
151
+ /*
152
+ :nodoc:
153
+ */
154
+ static VALUE collision_collision(VALUE self, VALUE pos1, VALUE c2, VALUE pos2)
155
+ {
156
+ return collision_c_collision(cCollision, self, pos1, c2, pos2);
157
+ }
158
+
159
+ /*
160
+ :nodoc:
161
+ */
162
+ static VALUE collision_meet(VALUE self, VALUE pos1, VALUE c2, VALUE pos2)
163
+ {
164
+ return collision_c_meet(cCollision, self, pos1, c2, pos2);
165
+ }
166
+
167
+ /*
168
+ :nodoc:
169
+ */
170
+ static VALUE collision_cover(VALUE self, VALUE pos1, VALUE c2, VALUE pos2)
171
+ {
172
+ return collision_c_cover(cCollision, self, pos1, c2, pos2);
173
+ }
174
+
175
+ /*
176
+ :nodoc:
177
+ */
178
+ static VALUE circlecollision_c_collision(VALUE self, VALUE c1, VALUE pos1, VALUE c2, VALUE pos2)
179
+ {
180
+ VALUE *pcenter1 = RSTRUCT_PTR(rb_iv_get(c1, "@center"));
181
+ VALUE *pcenter2 = RSTRUCT_PTR(rb_iv_get(c2, "@center"));
182
+ double r1 = NUM2DBL(rb_iv_get(c1, "@radius"));
183
+ double r2 = NUM2DBL(rb_iv_get(c2, "@radius"));
184
+ double r = (r1 + r2) * (r1 + r2);
185
+ VALUE x1, y1, x2, y2;
186
+ collision_get_position(pos1, &x1, &y1);
187
+ collision_get_position(pos2, &x2, &y2);
188
+ double cx1 = NUM2DBL(x1) + NUM2DBL(*pcenter1);
189
+ double cy1 = NUM2DBL(y1) + NUM2DBL(*(pcenter1+1));
190
+ double cx2 = NUM2DBL(x2) + NUM2DBL(*pcenter2);
191
+ double cy2 = NUM2DBL(y2) + NUM2DBL(*(pcenter2+1));
192
+ double d = (cx1-cx2) * (cx1-cx2) + (cy1-cy2) * (cy1-cy2);
193
+
194
+ if(d <= r) return Qtrue;
195
+ return Qfalse;
196
+ }
197
+
198
+ /*
199
+ :nodoc:
200
+ */
201
+ static VALUE circlecollision_c_meet(VALUE self, VALUE c1, VALUE pos1, VALUE c2, VALUE pos2)
202
+ {
203
+ VALUE *pcenter1 = RSTRUCT_PTR(rb_iv_get(c1, "@center"));
204
+ VALUE *pcenter2 = RSTRUCT_PTR(rb_iv_get(c2, "@center"));
205
+ double r1 = NUM2DBL(rb_iv_get(c1, "@radius"));
206
+ double r2 = NUM2DBL(rb_iv_get(c2, "@radius"));
207
+ double r = (r1 + r2) * (r1 + r2);
208
+ VALUE x1, y1, x2, y2;
209
+ collision_get_position(pos1, &x1, &y1);
210
+ collision_get_position(pos2, &x2, &y2);
211
+ double cx1 = NUM2DBL(x1) + NUM2DBL(*pcenter1);
212
+ double cy1 = NUM2DBL(y1) + NUM2DBL(*(pcenter1+1));
213
+ double cx2 = NUM2DBL(x2) + NUM2DBL(*pcenter2);
214
+ double cy2 = NUM2DBL(y2) + NUM2DBL(*(pcenter2+1));
215
+ double d = (cx1-cx2) * (cx1-cx2) + (cy1-cy2) * (cy1-cy2);
216
+
217
+ if(d == r) return Qtrue;
218
+ return Qfalse;
219
+ }
220
+
221
+ /*
222
+ :nodoc:
223
+ */
224
+ static VALUE circlecollision_c_cover(VALUE self, VALUE c1, VALUE pos1, VALUE c2, VALUE pos2)
225
+ {
226
+ VALUE *pcenter1 = RSTRUCT_PTR(rb_iv_get(c1, "@center"));
227
+ VALUE *pcenter2 = RSTRUCT_PTR(rb_iv_get(c2, "@center"));
228
+ double r1 = NUM2DBL(rb_iv_get(c1, "@radius"));
229
+ double r2 = NUM2DBL(rb_iv_get(c2, "@radius"));
230
+ double r = (r1 - r2) * (r1 - r2); // y = (x-a)^2 -> y = x^2 - 2ax + a^2
231
+ VALUE x1, y1, x2, y2;
232
+ collision_get_position(pos1, &x1, &y1);
233
+ collision_get_position(pos2, &x2, &y2);
234
+ double cx1 = NUM2DBL(x1) + NUM2DBL(*pcenter1);
235
+ double cy1 = NUM2DBL(y1) + NUM2DBL(*(pcenter1+1));
236
+ double cx2 = NUM2DBL(x2) + NUM2DBL(*pcenter2);
237
+ double cy2 = NUM2DBL(y2) + NUM2DBL(*(pcenter2+1));
238
+ double d = (cx1-cx2) * (cx1-cx2) + (cy1-cy2) * (cy1-cy2);
239
+
240
+ if(d <= r) return Qtrue;
241
+ return Qfalse;
242
+ }
243
+
244
+ /*
245
+ :nodoc:
246
+ */
247
+ static VALUE circlecollision_collision(VALUE self, VALUE pos1, VALUE c2, VALUE pos2)
248
+ {
249
+ return circlecollision_c_collision(cCircleCollision, self, pos1, c2, pos2);
250
+ }
251
+
252
+ /*
253
+ :nodoc:
254
+ */
255
+ static VALUE circlecollision_meet(VALUE self, VALUE pos1, VALUE c2, VALUE pos2)
256
+ {
257
+ return circlecollision_c_meet(cCircleCollision, self, pos1, c2, pos2);
258
+ }
259
+
260
+ /*
261
+ :nodoc:
262
+ */
263
+ static VALUE circlecollision_cover(VALUE self, VALUE pos1, VALUE c2, VALUE pos2)
264
+ {
265
+ return circlecollision_c_cover(cCircleCollision, self, pos1, c2, pos2);
266
+ }
267
+
268
+ /*
269
+ :nodoc:
270
+ */
271
+ static VALUE collisions_collision(VALUE self, VALUE c, VALUE pos)
272
+ {
273
+ VALUE collisions = rb_iv_get(self, "@collisions");
274
+ int i=0;
275
+ for(i=0; i<RARRAY_LEN(collisions); i++){
276
+ VALUE cs = *(RARRAY_PTR(collisions) + i);
277
+ VALUE *cc = RARRAY_PTR(cs);
278
+ if(collision_collision(c, pos, *cc, *(cc+1)) == Qtrue){ return cs; }
279
+ }
280
+ return Qnil;
281
+ }
282
+
283
+ /*
284
+ :nodoc:
285
+ */
286
+ static VALUE collisions_meet(VALUE self, VALUE c, VALUE pos)
287
+ {
288
+ VALUE collisions = rb_iv_get(self, "@collisions");
289
+ int i=0;
290
+ for(i=0; i<RARRAY_LEN(collisions); i++){
291
+ VALUE cs = *(RARRAY_PTR(collisions) + i);
292
+ VALUE *cc = RARRAY_PTR(cs);
293
+ if(collision_meet(c, pos, *cc, *(cc+1)) == Qtrue){ return cs; }
294
+ }
295
+ return Qnil;
296
+ }
297
+
298
+ /*
299
+ :nodoc:
300
+ */
301
+ static VALUE collisions_cover(VALUE self, VALUE c, VALUE pos)
302
+ {
303
+ VALUE collisions = rb_iv_get(self, "@collisions");
304
+ int i=0;
305
+ for(i=0; i<RARRAY_LEN(collisions); i++){
306
+ VALUE cs = *(RARRAY_PTR(collisions) + i);
307
+ VALUE *cc = RARRAY_PTR(cs);
308
+ if(collision_cover(c, pos, *cc, *(cc+1)) == Qtrue){ return cs; }
309
+ }
310
+ return Qnil;
311
+ }
312
+
313
+ /*
314
+ :nodoc:
315
+ */
316
+ static VALUE collisions_collision_all(VALUE self, VALUE c, VALUE pos)
317
+ {
318
+ VALUE collisions = rb_iv_get(self, "@collisions");
319
+ VALUE ret = rb_ary_new();
320
+ int i=0;
321
+ for(i=0; i<RARRAY_LEN(collisions); i++){
322
+ VALUE cs = *(RARRAY_PTR(collisions) + i);
323
+ VALUE *cc = RARRAY_PTR(cs);
324
+ if(collision_collision(c, pos, *cc, *(cc+1)) == Qtrue){ rb_ary_push(ret, cs); }
325
+ }
326
+ if(RARRAY_LEN(ret) == 0){ return Qnil; }
327
+ return ret;
328
+ }
329
+
330
+ /*
331
+ :nodoc:
332
+ */
333
+ static VALUE collisions_meet_all(VALUE self, VALUE c, VALUE pos)
334
+ {
335
+ VALUE collisions = rb_iv_get(self, "@collisions");
336
+ VALUE ret = rb_ary_new();
337
+ int i=0;
338
+ for(i=0; i<RARRAY_LEN(collisions); i++){
339
+ VALUE cs = *(RARRAY_PTR(collisions) + i);
340
+ VALUE *cc = RARRAY_PTR(cs);
341
+ if(collision_meet(c, pos, *cc, *(cc+1)) == Qtrue){ rb_ary_push(ret, cs); }
342
+ }
343
+ if(RARRAY_LEN(ret) == 0){ return Qnil; }
344
+ return ret;
345
+ }
346
+
347
+ /*
348
+ :nodoc:
349
+ */
350
+ static VALUE collisions_cover_all(VALUE self, VALUE c, VALUE pos)
351
+ {
352
+ VALUE collisions = rb_iv_get(self, "@collisions");
353
+ VALUE ret = rb_ary_new();
354
+ int i=0;
355
+ for(i=0; i<RARRAY_LEN(collisions); i++){
356
+ VALUE cs = *(RARRAY_PTR(collisions) + i);
357
+ VALUE *cc = RARRAY_PTR(cs);
358
+ if(collision_cover(c, pos, *cc, *(cc+1)) == Qtrue){ rb_ary_push(ret, cs); }
359
+ }
360
+ if(RARRAY_LEN(ret) == 0){ return Qnil; }
361
+ return ret;
362
+ }
363
+
364
+ void Init_miyako_collision()
365
+ {
366
+ mSDL = rb_define_module("SDL");
367
+ mMiyako = rb_define_module("Miyako");
368
+ eMiyakoError = rb_define_class_under(mMiyako, "MiyakoError", rb_eException);
369
+ cCollision = rb_define_class_under(mMiyako, "Collision", rb_cObject);
370
+ cCircleCollision = rb_define_class_under(mMiyako, "CircleCollision", rb_cObject);
371
+ cCollisions = rb_define_class_under(mMiyako, "Collisions", rb_cObject);
372
+
373
+ id_update = rb_intern("update");
374
+ id_kakko = rb_intern("[]");
375
+ id_render = rb_intern("render");
376
+ id_to_a = rb_intern("to_a");
377
+
378
+ zero = 0;
379
+ nZero = INT2NUM(zero);
380
+ one = 1;
381
+ nOne = INT2NUM(one);
382
+
383
+ rb_define_singleton_method(cCollision, "collision?", collision_c_collision, 4);
384
+ rb_define_singleton_method(cCollision, "meet?", collision_c_meet, 4);
385
+ rb_define_singleton_method(cCollision, "cover?", collision_c_cover, 4);
386
+ rb_define_method(cCollision, "collision?", collision_collision, 3);
387
+ rb_define_method(cCollision, "meet?", collision_meet, 3);
388
+ rb_define_method(cCollision, "cover?", collision_cover, 3);
389
+
390
+ rb_define_singleton_method(cCircleCollision, "collision?", circlecollision_c_collision, 4);
391
+ rb_define_singleton_method(cCircleCollision, "meet?", circlecollision_c_meet, 4);
392
+ rb_define_singleton_method(cCircleCollision, "cover?", circlecollision_c_cover, 4);
393
+ rb_define_method(cCircleCollision, "collision?", circlecollision_collision, 3);
394
+ rb_define_method(cCircleCollision, "meet?", circlecollision_meet, 3);
395
+ rb_define_method(cCircleCollision, "cover?", circlecollision_cover, 3);
396
+
397
+ rb_define_method(cCollisions, "collision?", collisions_collision, 2);
398
+ rb_define_method(cCollisions, "meet?", collisions_meet, 2);
399
+ rb_define_method(cCollisions, "cover?", collisions_cover, 2);
400
+ rb_define_method(cCollisions, "collision_all?", collisions_collision_all, 2);
401
+ rb_define_method(cCollisions, "meet_all?", collisions_meet_all, 2);
402
+ rb_define_method(cCollisions, "cover_all?", collisions_cover_all, 2);
403
+ }