chipmunk 4.1.0 → 5.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (71) hide show
  1. data/LICENSE +20 -0
  2. data/README +60 -0
  3. data/Rakefile +47 -40
  4. data/ext/chipmunk/chipmunk.c +39 -3
  5. data/ext/chipmunk/cpArbiter.c +91 -80
  6. data/ext/chipmunk/cpArray.c +24 -10
  7. data/ext/chipmunk/cpBB.c +5 -4
  8. data/ext/chipmunk/cpBody.c +30 -22
  9. data/ext/chipmunk/cpCollision.c +54 -53
  10. data/ext/chipmunk/cpConstraint.c +54 -0
  11. data/ext/chipmunk/cpDampedRotarySpring.c +106 -0
  12. data/ext/chipmunk/cpDampedSpring.c +117 -0
  13. data/ext/chipmunk/cpGearJoint.c +114 -0
  14. data/ext/chipmunk/cpGrooveJoint.c +138 -0
  15. data/ext/chipmunk/cpHashSet.c +74 -40
  16. data/ext/chipmunk/cpPinJoint.c +117 -0
  17. data/ext/chipmunk/cpPivotJoint.c +114 -0
  18. data/ext/chipmunk/cpPolyShape.c +117 -15
  19. data/ext/chipmunk/cpRatchetJoint.c +128 -0
  20. data/ext/chipmunk/cpRotaryLimitJoint.c +122 -0
  21. data/ext/chipmunk/cpShape.c +174 -18
  22. data/ext/chipmunk/cpSimpleMotor.c +99 -0
  23. data/ext/chipmunk/cpSlideJoint.c +131 -0
  24. data/ext/chipmunk/cpSpace.c +584 -215
  25. data/ext/chipmunk/cpSpaceHash.c +191 -105
  26. data/ext/chipmunk/cpVect.c +18 -10
  27. data/ext/chipmunk/extconf.rb +34 -4
  28. data/ext/chipmunk/{chipmunk.h → include/chipmunk/chipmunk.h} +63 -6
  29. data/ext/chipmunk/include/chipmunk/chipmunk_ffi.h +42 -0
  30. data/ext/chipmunk/include/chipmunk/chipmunk_types.h +80 -0
  31. data/ext/chipmunk/include/chipmunk/chipmunk_unsafe.h +54 -0
  32. data/ext/chipmunk/include/chipmunk/constraints/cpConstraint.h +92 -0
  33. data/ext/chipmunk/include/chipmunk/constraints/cpDampedRotarySpring.h +46 -0
  34. data/ext/chipmunk/include/chipmunk/constraints/cpDampedSpring.h +53 -0
  35. data/ext/chipmunk/include/chipmunk/constraints/cpGearJoint.h +41 -0
  36. data/ext/chipmunk/include/chipmunk/constraints/cpGrooveJoint.h +44 -0
  37. data/ext/chipmunk/include/chipmunk/constraints/cpPinJoint.h +43 -0
  38. data/ext/chipmunk/include/chipmunk/constraints/cpPivotJoint.h +42 -0
  39. data/ext/chipmunk/include/chipmunk/constraints/cpRatchetJoint.h +40 -0
  40. data/ext/chipmunk/include/chipmunk/constraints/cpRotaryLimitJoint.h +39 -0
  41. data/ext/chipmunk/include/chipmunk/constraints/cpSimpleMotor.h +37 -0
  42. data/ext/chipmunk/include/chipmunk/constraints/cpSlideJoint.h +44 -0
  43. data/ext/chipmunk/include/chipmunk/constraints/util.h +116 -0
  44. data/ext/chipmunk/{cpArbiter.h → include/chipmunk/cpArbiter.h} +66 -15
  45. data/ext/chipmunk/{cpArray.h → include/chipmunk/cpArray.h} +2 -1
  46. data/ext/chipmunk/{cpBB.h → include/chipmunk/cpBB.h} +21 -0
  47. data/ext/chipmunk/{cpBody.h → include/chipmunk/cpBody.h} +37 -9
  48. data/ext/chipmunk/{cpCollision.h → include/chipmunk/cpCollision.h} +1 -1
  49. data/ext/chipmunk/{cpHashSet.h → include/chipmunk/cpHashSet.h} +12 -9
  50. data/ext/chipmunk/{cpPolyShape.h → include/chipmunk/cpPolyShape.h} +13 -2
  51. data/ext/chipmunk/{cpShape.h → include/chipmunk/cpShape.h} +51 -18
  52. data/ext/chipmunk/include/chipmunk/cpSpace.h +180 -0
  53. data/ext/chipmunk/{cpSpaceHash.h → include/chipmunk/cpSpaceHash.h} +18 -9
  54. data/ext/chipmunk/{cpVect.h → include/chipmunk/cpVect.h} +61 -10
  55. data/ext/chipmunk/prime.h +32 -32
  56. data/ext/chipmunk/rb_chipmunk.c +125 -109
  57. data/ext/chipmunk/rb_chipmunk.h +96 -77
  58. data/ext/chipmunk/rb_cpArbiter.c +225 -0
  59. data/ext/chipmunk/rb_cpBB.c +174 -154
  60. data/ext/chipmunk/rb_cpBody.c +347 -239
  61. data/ext/chipmunk/rb_cpConstraint.c +346 -0
  62. data/ext/chipmunk/rb_cpShape.c +455 -292
  63. data/ext/chipmunk/rb_cpSpace.c +544 -330
  64. data/ext/chipmunk/rb_cpVect.c +321 -250
  65. data/lib/chipmunk.rb +28 -15
  66. data/lib/chipmunk/version.rb +3 -0
  67. metadata +74 -34
  68. data/ext/chipmunk/cpJoint.c +0 -553
  69. data/ext/chipmunk/cpJoint.h +0 -122
  70. data/ext/chipmunk/cpSpace.h +0 -120
  71. data/ext/chipmunk/rb_cpJoint.c +0 -136
@@ -1,77 +1,96 @@
1
- /* Copyright (c) 2007 Scott Lembcke
2
- *
3
- * Permission is hereby granted, free of charge, to any person obtaining a copy
4
- * of this software and associated documentation files (the "Software"), to deal
5
- * in the Software without restriction, including without limitation the rights
6
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
- * copies of the Software, and to permit persons to whom the Software is
8
- * furnished to do so, subject to the following conditions:
9
- *
10
- * The above copyright notice and this permission notice shall be included in
11
- * all copies or substantial portions of the Software.
12
- *
13
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
- * SOFTWARE.
20
- */
21
-
22
- extern VALUE m_Chipmunk;
23
-
24
- extern VALUE c_cpVect;
25
- extern VALUE c_cpBB;
26
- extern VALUE c_cpBody;
27
- extern VALUE m_cpShape;
28
- extern VALUE c_cpCircleShape;
29
- extern VALUE c_cpSegmentShape;
30
- extern VALUE c_cpPolyShape;
31
- extern VALUE m_cpJoint;
32
- extern VALUE c_cpSpace;
33
-
34
- extern ID id_parent;
35
-
36
- static inline VALUE
37
- VNEW(cpVect v)
38
- {
39
- cpVect *ptr = malloc(sizeof(cpVect));
40
- *ptr = v;
41
- return Data_Wrap_Struct(c_cpVect, NULL, free, ptr);
42
- }
43
-
44
- static inline VALUE
45
- VWRAP(VALUE parent, cpVect *v)
46
- {
47
- VALUE vec_obj = Data_Wrap_Struct(c_cpVect, NULL, NULL, v);
48
- rb_ivar_set(vec_obj, id_parent, parent);
49
-
50
- return vec_obj;
51
- }
52
-
53
- #define GETTER_TEMPLATE(func_name, klass, klass_name, type)\
54
- static inline type *\
55
- func_name(VALUE self)\
56
- {\
57
- if(!rb_obj_is_kind_of(self, klass))\
58
- rb_raise(rb_eTypeError, "wrong argument type %s (expected CP::klass_name)", rb_obj_classname(self));\
59
- type *ptr;\
60
- Data_Get_Struct(self, type, ptr);\
61
- return ptr;\
62
- }\
63
-
64
- GETTER_TEMPLATE(VGET , c_cpVect , Vec2 , cpVect )
65
- GETTER_TEMPLATE(BBGET, c_cpBB , BB , cpBB )
66
- GETTER_TEMPLATE(BODY , c_cpBody , Body , cpBody )
67
- GETTER_TEMPLATE(SHAPE, m_cpShape, Shape, cpShape)
68
- GETTER_TEMPLATE(JOINT, m_cpJoint, Joint, cpJoint)
69
- GETTER_TEMPLATE(SPACE, c_cpSpace, Space, cpSpace)
70
-
71
- void Init_chipmunk(void);
72
- void Init_cpVect();
73
- void Init_cpBB();
74
- void Init_cpBody();
75
- void Init_cpShape();
76
- void Init_cpJoint();
77
- void Init_cpSpace();
1
+ /* Copyright (c) 2007 Scott Lembcke
2
+ *
3
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ * of this software and associated documentation files (the "Software"), to deal
5
+ * in the Software without restriction, including without limitation the rights
6
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ * copies of the Software, and to permit persons to whom the Software is
8
+ * furnished to do so, subject to the following conditions:
9
+ *
10
+ * The above copyright notice and this permission notice shall be included in
11
+ * all copies or substantial portions of the Software.
12
+ *
13
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ * SOFTWARE.
20
+ */
21
+
22
+ extern VALUE m_Chipmunk;
23
+
24
+ extern VALUE c_cpArbiter;
25
+ extern VALUE c_cpVect;
26
+ extern VALUE c_cpBB;
27
+ extern VALUE c_cpBody;
28
+ extern VALUE m_cpShape;
29
+ extern VALUE c_cpCircleShape;
30
+ extern VALUE c_cpSegmentShape;
31
+ extern VALUE c_cpPolyShape;
32
+ extern VALUE m_cpConstraint;
33
+ extern VALUE c_cpSpace;
34
+
35
+ extern ID id_parent;
36
+
37
+ static inline VALUE
38
+ VNEW(cpVect v)
39
+ {
40
+ cpVect *ptr = malloc(sizeof(cpVect));
41
+ *ptr = v;
42
+ return Data_Wrap_Struct(c_cpVect, NULL, free, ptr);
43
+ }
44
+
45
+ static inline VALUE
46
+ VWRAP(VALUE parent, cpVect *v)
47
+ {
48
+ VALUE vec_obj = Data_Wrap_Struct(c_cpVect, NULL, NULL, v);
49
+ rb_ivar_set(vec_obj, id_parent, parent);
50
+
51
+ return vec_obj;
52
+ }
53
+
54
+ /*
55
+ * Arbiters may bot be allocated or deallocated by Ruby.
56
+ * Chipmunk handles them itself.
57
+ */
58
+ static inline VALUE
59
+ ARBWRAP(cpArbiter *arb)
60
+ {
61
+ VALUE arb_obj = Data_Wrap_Struct(c_cpArbiter, NULL, NULL, arb);
62
+ return arb_obj;
63
+ }
64
+
65
+
66
+ #define GETTER_TEMPLATE(func_name, klass, type)\
67
+ static inline type *\
68
+ func_name(VALUE self)\
69
+ {\
70
+ if(!rb_obj_is_kind_of(self, klass)){\
71
+ VALUE klass_name = rb_funcall(klass, rb_intern("to_s"), 0);\
72
+ rb_raise(rb_eTypeError, "wrong argument type %s (expected %s)", rb_obj_classname(self), StringValuePtr(klass_name));\
73
+ }\
74
+ type *ptr;\
75
+ Data_Get_Struct(self, type, ptr);\
76
+ return ptr;\
77
+ }\
78
+
79
+ GETTER_TEMPLATE(VGET , c_cpVect , cpVect )
80
+ GETTER_TEMPLATE(BBGET, c_cpBB , cpBB )
81
+ GETTER_TEMPLATE(BODY , c_cpBody , cpBody )
82
+ GETTER_TEMPLATE(SHAPE, m_cpShape, cpShape)
83
+ GETTER_TEMPLATE(CONSTRAINT, m_cpConstraint, cpConstraint)
84
+ GETTER_TEMPLATE(SPACE, c_cpSpace, cpSpace)
85
+ GETTER_TEMPLATE(ARBITER, c_cpArbiter, cpArbiter)
86
+
87
+
88
+ void Init_chipmunk(void);
89
+ void Init_cpVect();
90
+ void Init_cpArbiter();
91
+ void Init_cpBB();
92
+ void Init_cpBody();
93
+ void Init_cpShape();
94
+ void Init_cpConstraint();
95
+ void Init_cpSpace();
96
+
@@ -0,0 +1,225 @@
1
+ /* Copyright (c) 2010 Beoran (beoran@rubyforge.org)
2
+ *
3
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ * of this software and associated documentation files (the "Software"), to deal
5
+ * in the Software without restriction, including without limitation the rights
6
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ * copies of the Software, and to permit persons to whom the Software is
8
+ * furnished to do so, subject to the following conditions:
9
+ *
10
+ * The above copyright notice and this permission notice shall be included in
11
+ * all copies or substantial portions of the Software.
12
+ *
13
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ * SOFTWARE.
20
+ */
21
+
22
+ #include <stdlib.h>
23
+ #include "chipmunk.h"
24
+
25
+ #include "ruby.h"
26
+ #include "rb_chipmunk.h"
27
+
28
+ VALUE c_cpArbiter;
29
+
30
+ /*
31
+ * I quote the C docs on cpArbiter:
32
+ * Memory Management
33
+ * You should never need to create an arbiter, nor will you ever need to free one as they are handled by the space. More importantly, because they are handled by the space you should never hold onto a reference to an arbiter as you don't know when they will be destroyed. Use them within the callback where they are given to you and then forget about them or copy out the information you need from them.
34
+ *
35
+ * Thios means that Arbiter doesn't need an initialize, and also
36
+ * does NOT need any garbage collection.
37
+ */
38
+
39
+ VALUE rb_cpArbiterWrap(cpArbiter *arb)
40
+ {
41
+ return Data_Wrap_Struct(c_cpArbiter, NULL, NULL, arb);
42
+ }
43
+
44
+ /*
45
+ static VALUE
46
+ rb_cpArbiterAlloc(VALUE klass)
47
+ {
48
+ cpArbiter *arb = cpArbiterAlloc();
49
+ return Data_Wrap_Struct(klass, NULL, cpArbiterFree, arb);
50
+ }
51
+
52
+
53
+ static VALUE
54
+ rb_cpArbiterInitialize(VALUE self, VALUE a, VALUE b, VALUE r, VALUE t)
55
+ {
56
+ cpArbiter *arb = ARBITER(self);
57
+ cpShape *sa = SHAPE(a);
58
+ cpShape *sb = SHAPE(b);
59
+ cpArbiterInit(arb, sa, sb);
60
+ return self;
61
+ }
62
+ */
63
+
64
+ static VALUE
65
+ rb_cpArbiterTotalImpulse(VALUE self) {
66
+ cpArbiter *arb = ARBITER(self);
67
+ return VNEW(cpArbiterTotalImpulse(arb));
68
+ }
69
+
70
+ static VALUE
71
+ rb_cpArbiterTotalImpulseWithFriction(VALUE self) {
72
+ cpArbiter *arb = ARBITER(self);
73
+ return VNEW(cpArbiterTotalImpulseWithFriction(arb));
74
+ }
75
+
76
+ static VALUE
77
+ rb_cpArbiterGetImpulse(int argc, VALUE *argv, VALUE self)
78
+ {
79
+ VALUE friction = Qnil;
80
+ rb_scan_args(argc, argv, "01", &friction);
81
+ if (NIL_P(friction) || friction == Qnil) {
82
+ return rb_cpArbiterTotalImpulse(self);
83
+ }
84
+ /* Here, it's with friction */
85
+ return rb_cpArbiterTotalImpulseWithFriction(self);
86
+ }
87
+
88
+
89
+ static VALUE
90
+ rb_cpArbiterIgnore(VALUE self) {
91
+ cpArbiter *arb = ARBITER(self);
92
+ cpArbiterIgnore(arb);
93
+ return Qnil;
94
+ }
95
+
96
+ static VALUE
97
+ rb_cpArbiterGetShapes(VALUE self) {
98
+ cpArbiter *arb = ARBITER(self);
99
+ CP_ARBITER_GET_SHAPES(arb, a, b)
100
+ return rb_ary_new3(2, (VALUE)a->data, (VALUE)b->data);
101
+ }
102
+
103
+ static VALUE
104
+ rb_cpArbiterGetA(VALUE self) {
105
+ cpArbiter *arb = ARBITER(self);
106
+ CP_ARBITER_GET_SHAPES(arb, a, b)
107
+ return (VALUE)a->data;
108
+ }
109
+
110
+ static VALUE
111
+ rb_cpArbiterGetB(VALUE self) {
112
+ cpArbiter *arb = ARBITER(self);
113
+ CP_ARBITER_GET_SHAPES(arb, a, b)
114
+ return (VALUE)b->data;
115
+ }
116
+
117
+ static VALUE
118
+ rb_cpArbiterGetE(VALUE self) {
119
+ return rb_float_new(ARBITER(self)->e);
120
+ }
121
+
122
+ static VALUE
123
+ rb_cpArbiterSetE(VALUE self, VALUE e) {
124
+ ARBITER(self)->e = NUM2DBL(e);
125
+ return e;
126
+ }
127
+
128
+ static VALUE
129
+ rb_cpArbiterGetNumContacts(VALUE self) {
130
+ return INT2NUM(ARBITER(self)->numContacts);
131
+ }
132
+
133
+ static VALUE
134
+ rb_cpArbiterGetU(VALUE self) {
135
+ return rb_float_new(ARBITER(self)->u);
136
+ }
137
+
138
+ static VALUE
139
+ rb_cpArbiterSetU(VALUE self, VALUE u) {
140
+ ARBITER(self)->u = NUM2DBL(u);
141
+ return u;
142
+ }
143
+
144
+ static VALUE
145
+ rb_cpArbiterIsFirstContact(VALUE self) {
146
+ int bool = cpArbiterIsFirstContact(ARBITER(self));
147
+ return bool ? Qtrue : Qfalse;
148
+ }
149
+
150
+ static VALUE
151
+ rb_cpArbiterGetNormal(VALUE self, VALUE index) {
152
+ cpArbiter *arb = ARBITER(self);
153
+ int i = NUM2LONG(index);
154
+ if (i >= arb->numContacts) {
155
+ rb_raise(rb_eIndexError, "No such normal.");
156
+ }
157
+ return VNEW(cpArbiterGetNormal(arb, i));
158
+ }
159
+
160
+ static VALUE
161
+ rb_cpArbiterGetPoint(VALUE self, VALUE index) {
162
+ cpArbiter *arb = ARBITER(self);
163
+ int i = NUM2LONG(index);
164
+ if (i >= arb->numContacts) {
165
+ rb_raise(rb_eIndexError, "No such contact point.");
166
+ }
167
+ return VNEW(cpArbiterGetPoint(arb, i));
168
+ }
169
+
170
+ static VALUE
171
+ rb_cpArbiterToString(VALUE self)
172
+ {
173
+ char str[256];
174
+ cpArbiter *arb = ARBITER(self);
175
+ sprintf(str, "#<CP::Arbiter:%p>", arb);
176
+ return rb_str_new2(str);
177
+ }
178
+
179
+ static VALUE
180
+ rb_cpArbiterEachContact(VALUE self)
181
+ {
182
+ cpArbiter *arb = ARBITER(self);
183
+ int i = 0;
184
+ for( i = 0; i < arb->numContacts; i++) {
185
+ VALUE index = INT2NUM(i);
186
+ VALUE contact, normal;
187
+ normal = rb_cpArbiterGetNormal(self, index);
188
+ contact = rb_cpArbiterGetPoint(self, index);
189
+ /* Yield an array of contact and normal */
190
+ rb_yield(rb_ary_new3(2, contact, normal));
191
+ }
192
+ return self;
193
+ }
194
+
195
+
196
+
197
+ void
198
+ Init_cpArbiter(void)
199
+ {
200
+ c_cpArbiter = rb_define_class_under(m_Chipmunk, "Arbiter", rb_cObject);
201
+ /*
202
+ rb_define_alloc_func(c_cpArbiter, rb_cpArbiterAlloc);
203
+ rb_define_method(c_cpArbiter , "initialize", rb_cpArbiterInitialize, 2);
204
+ */
205
+ rb_define_method(c_cpArbiter, "a", rb_cpArbiterGetA, 0);
206
+ rb_define_method(c_cpArbiter, "b", rb_cpArbiterGetB, 0);
207
+ rb_define_method(c_cpArbiter, "e" , rb_cpArbiterGetE, 0);
208
+ rb_define_method(c_cpArbiter, "u" , rb_cpArbiterGetU, 0);
209
+ rb_define_method(c_cpArbiter, "e=", rb_cpArbiterSetE, 1);
210
+ rb_define_method(c_cpArbiter, "u=", rb_cpArbiterSetU, 1);
211
+
212
+ rb_define_method(c_cpArbiter, "point" , rb_cpArbiterGetPoint , 1);
213
+ rb_define_method(c_cpArbiter, "normal" , rb_cpArbiterGetNormal , 1);
214
+ rb_define_method(c_cpArbiter, "impulse" , rb_cpArbiterGetImpulse , 1);
215
+ rb_define_method(c_cpArbiter, "to_s", rb_cpArbiterToString, 0);
216
+
217
+ rb_define_method(c_cpArbiter, "first_contact?", rb_cpArbiterIsFirstContact, 0);
218
+ rb_define_method(c_cpArbiter, "num_contacts" , rb_cpArbiterGetNumContacts, 0);
219
+ rb_define_method(c_cpArbiter, "each_contact" , rb_cpArbiterEachContact , 0);
220
+
221
+ rb_define_method(c_cpArbiter, "shapes" , rb_cpArbiterGetShapes , 0);
222
+ /* Ignore is new in the 5.x API, I think. */
223
+ rb_define_method(c_cpArbiter, "ignore" , rb_cpArbiterIgnore , 0);
224
+
225
+ }
@@ -1,154 +1,174 @@
1
- /* Copyright (c) 2007 Scott Lembcke
2
- *
3
- * Permission is hereby granted, free of charge, to any person obtaining a copy
4
- * of this software and associated documentation files (the "Software"), to deal
5
- * in the Software without restriction, including without limitation the rights
6
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
- * copies of the Software, and to permit persons to whom the Software is
8
- * furnished to do so, subject to the following conditions:
9
- *
10
- * The above copyright notice and this permission notice shall be included in
11
- * all copies or substantial portions of the Software.
12
- *
13
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
- * SOFTWARE.
20
- */
21
-
22
- #include "chipmunk.h"
23
-
24
- #include "ruby.h"
25
- #include "rb_chipmunk.h"
26
-
27
- VALUE c_cpBB;
28
-
29
- static VALUE
30
- rb_cpBBAlloc(VALUE klass)
31
- {
32
- cpBB *bb = malloc(sizeof(cpBB));
33
- return Data_Wrap_Struct(klass, NULL, free, bb);
34
- }
35
-
36
- static VALUE
37
- rb_cpBBInitialize(VALUE self, VALUE l, VALUE b, VALUE r, VALUE t)
38
- {
39
- cpBB *bb = BBGET(self);
40
- bb->l = NUM2DBL(l);
41
- bb->b = NUM2DBL(b);
42
- bb->r = NUM2DBL(r);
43
- bb->t = NUM2DBL(t);
44
-
45
- return self;
46
- }
47
-
48
- static VALUE
49
- rb_cpBBintersects(VALUE self, VALUE other)
50
- {
51
- int bool = cpBBintersects(*BBGET(self), *BBGET(other));
52
- return bool ? Qtrue : Qfalse;
53
- }
54
-
55
- static VALUE
56
- rb_cpBBClampVect(VALUE self, VALUE v)
57
- {
58
- return VNEW(cpBBClampVect(*BBGET(self), *VGET(v)));
59
- }
60
-
61
- static VALUE
62
- rb_cpBBWrapVect(VALUE self, VALUE v)
63
- {
64
- return VNEW(cpBBWrapVect(*BBGET(self), *VGET(v)));
65
- }
66
-
67
- static VALUE
68
- rb_cpBBGetL(VALUE self)
69
- {
70
- return rb_float_new(BBGET(self)->l);
71
- }
72
-
73
- static VALUE
74
- rb_cpBBGetB(VALUE self)
75
- {
76
- return rb_float_new(BBGET(self)->b);
77
- }
78
-
79
- static VALUE
80
- rb_cpBBGetR(VALUE self)
81
- {
82
- return rb_float_new(BBGET(self)->r);
83
- }
84
-
85
- static VALUE
86
- rb_cpBBGetT(VALUE self)
87
- {
88
- return rb_float_new(BBGET(self)->t);
89
- }
90
-
91
- static VALUE
92
- rb_cpBBSetL(VALUE self, VALUE val)
93
- {
94
- BBGET(self)->l = NUM2DBL(val);
95
- return val;
96
- }
97
-
98
- static VALUE
99
- rb_cpBBSetB(VALUE self, VALUE val)
100
- {
101
- BBGET(self)->b = NUM2DBL(val);
102
- return val;
103
- }
104
-
105
- static VALUE
106
- rb_cpBBSetR(VALUE self, VALUE val)
107
- {
108
- BBGET(self)->r = NUM2DBL(val);
109
- return val;
110
- }
111
-
112
- static VALUE
113
- rb_cpBBSetT(VALUE self, VALUE val)
114
- {
115
- BBGET(self)->t = NUM2DBL(val);
116
- return val;
117
- }
118
-
119
- static VALUE
120
- rb_cpBBToString(VALUE self)
121
- {
122
- char str[256];
123
- cpBB *bb = BBGET(self);
124
-
125
- sprintf(str, "#<CP::BB:(% .3f, % .3f) -> (% .3f, % .3f)>", bb->l, bb->b, bb->r, bb->t);
126
-
127
- return rb_str_new2(str);
128
- }
129
-
130
- void
131
- Init_cpBB(void)
132
- {
133
- c_cpBB = rb_define_class_under(m_Chipmunk, "BB", rb_cObject);
134
- rb_define_alloc_func(c_cpBB, rb_cpBBAlloc);
135
- rb_define_method(c_cpBB, "initialize", rb_cpBBInitialize, 4);
136
-
137
- rb_define_method(c_cpBB, "l", rb_cpBBGetL, 0);
138
- rb_define_method(c_cpBB, "b", rb_cpBBGetB, 0);
139
- rb_define_method(c_cpBB, "r", rb_cpBBGetR, 0);
140
- rb_define_method(c_cpBB, "t", rb_cpBBGetT, 0);
141
-
142
- rb_define_method(c_cpBB, "l=", rb_cpBBSetL, 1);
143
- rb_define_method(c_cpBB, "b=", rb_cpBBSetB, 1);
144
- rb_define_method(c_cpBB, "r=", rb_cpBBSetR, 1);
145
- rb_define_method(c_cpBB, "t=", rb_cpBBSetT, 1);
146
-
147
- rb_define_method(c_cpBB, "intersect?", rb_cpBBintersects, 1);
148
- //containsBB
149
- //containsVect
150
- rb_define_method(c_cpBB, "clamp_vect", rb_cpBBClampVect, 1);
151
- rb_define_method(c_cpBB, "wrap_vect", rb_cpBBWrapVect, 1);
152
-
153
- rb_define_method(c_cpBB, "to_s", rb_cpBBToString, 0);
154
- }
1
+ /* Copyright (c) 2007 Scott Lembcke
2
+ *
3
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ * of this software and associated documentation files (the "Software"), to deal
5
+ * in the Software without restriction, including without limitation the rights
6
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ * copies of the Software, and to permit persons to whom the Software is
8
+ * furnished to do so, subject to the following conditions:
9
+ *
10
+ * The above copyright notice and this permission notice shall be included in
11
+ * all copies or substantial portions of the Software.
12
+ *
13
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ * SOFTWARE.
20
+ */
21
+
22
+ #include <stdlib.h>
23
+ #include "chipmunk.h"
24
+
25
+ #include "ruby.h"
26
+ #include "rb_chipmunk.h"
27
+
28
+ VALUE c_cpBB;
29
+
30
+ static VALUE
31
+ rb_cpBBAlloc(VALUE klass)
32
+ {
33
+ cpBB *bb = malloc(sizeof(cpBB));
34
+ return Data_Wrap_Struct(klass, NULL, free, bb);
35
+ }
36
+
37
+ static VALUE
38
+ rb_cpBBInitialize(VALUE self, VALUE l, VALUE b, VALUE r, VALUE t)
39
+ {
40
+ cpBB *bb = BBGET(self);
41
+ bb->l = NUM2DBL(l);
42
+ bb->b = NUM2DBL(b);
43
+ bb->r = NUM2DBL(r);
44
+ bb->t = NUM2DBL(t);
45
+
46
+ return self;
47
+ }
48
+
49
+ static VALUE
50
+ rb_cpBBintersects(VALUE self, VALUE other)
51
+ {
52
+ int bool = cpBBintersects(*BBGET(self), *BBGET(other));
53
+ return bool ? Qtrue : Qfalse;
54
+ }
55
+
56
+ static VALUE
57
+ rb_cpBBClampVect(VALUE self, VALUE v)
58
+ {
59
+ return VNEW(cpBBClampVect(*BBGET(self), *VGET(v)));
60
+ }
61
+
62
+ static VALUE
63
+ rb_cpBBWrapVect(VALUE self, VALUE v)
64
+ {
65
+ return VNEW(cpBBWrapVect(*BBGET(self), *VGET(v)));
66
+ }
67
+
68
+ static VALUE
69
+ rb_cpBBGetL(VALUE self)
70
+ {
71
+ return rb_float_new(BBGET(self)->l);
72
+ }
73
+
74
+ static VALUE
75
+ rb_cpBBGetB(VALUE self)
76
+ {
77
+ return rb_float_new(BBGET(self)->b);
78
+ }
79
+
80
+ static VALUE
81
+ rb_cpBBGetR(VALUE self)
82
+ {
83
+ return rb_float_new(BBGET(self)->r);
84
+ }
85
+
86
+ static VALUE
87
+ rb_cpBBGetT(VALUE self)
88
+ {
89
+ return rb_float_new(BBGET(self)->t);
90
+ }
91
+
92
+ static VALUE
93
+ rb_cpBBSetL(VALUE self, VALUE val)
94
+ {
95
+ BBGET(self)->l = NUM2DBL(val);
96
+ return val;
97
+ }
98
+
99
+ static VALUE
100
+ rb_cpBBSetB(VALUE self, VALUE val)
101
+ {
102
+ BBGET(self)->b = NUM2DBL(val);
103
+ return val;
104
+ }
105
+
106
+ static VALUE
107
+ rb_cpBBSetR(VALUE self, VALUE val)
108
+ {
109
+ BBGET(self)->r = NUM2DBL(val);
110
+ return val;
111
+ }
112
+
113
+ static VALUE
114
+ rb_cpBBSetT(VALUE self, VALUE val)
115
+ {
116
+ BBGET(self)->t = NUM2DBL(val);
117
+ return val;
118
+ }
119
+
120
+ static VALUE
121
+ rb_cpBBcontainsBB(VALUE self, VALUE other)
122
+ {
123
+ int bool = cpBBcontainsBB(*BBGET(self), *BBGET(other));
124
+ return bool ? Qtrue : Qfalse;
125
+ }
126
+
127
+ static VALUE
128
+ rb_cpBBcontainsVect(VALUE self, VALUE vector)
129
+ {
130
+ int bool = cpBBcontainsVect(*BBGET(self), *VGET(vector));
131
+ return bool ? Qtrue : Qfalse;
132
+ }
133
+
134
+
135
+ static VALUE
136
+ rb_cpBBToString(VALUE self)
137
+ {
138
+ char str[256];
139
+ cpBB *bb = BBGET(self);
140
+
141
+ sprintf(str, "#<CP::BB:(% .3f, % .3f) -> (% .3f, % .3f)>", bb->l, bb->b, bb->r, bb->t);
142
+
143
+ return rb_str_new2(str);
144
+ }
145
+
146
+ void
147
+ Init_cpBB(void)
148
+ {
149
+ c_cpBB = rb_define_class_under(m_Chipmunk, "BB", rb_cObject);
150
+ rb_define_alloc_func(c_cpBB, rb_cpBBAlloc);
151
+ rb_define_method(c_cpBB, "initialize", rb_cpBBInitialize, 4);
152
+
153
+ rb_define_method(c_cpBB, "l", rb_cpBBGetL, 0);
154
+ rb_define_method(c_cpBB, "b", rb_cpBBGetB, 0);
155
+ rb_define_method(c_cpBB, "r", rb_cpBBGetR, 0);
156
+ rb_define_method(c_cpBB, "t", rb_cpBBGetT, 0);
157
+
158
+ rb_define_method(c_cpBB, "l=", rb_cpBBSetL, 1);
159
+ rb_define_method(c_cpBB, "b=", rb_cpBBSetB, 1);
160
+ rb_define_method(c_cpBB, "r=", rb_cpBBSetR, 1);
161
+ rb_define_method(c_cpBB, "t=", rb_cpBBSetT, 1);
162
+
163
+ rb_define_method(c_cpBB, "intersect?" , rb_cpBBintersects, 1);
164
+ rb_define_method(c_cpBB, "contains_bb?" , rb_cpBBcontainsBB, 1);
165
+ rb_define_method(c_cpBB, "contains_vect?" , rb_cpBBcontainsVect, 1);
166
+
167
+
168
+ //containsBB
169
+ //containsVect
170
+ rb_define_method(c_cpBB, "clamp_vect", rb_cpBBClampVect, 1);
171
+ rb_define_method(c_cpBB, "wrap_vect", rb_cpBBWrapVect, 1);
172
+
173
+ rb_define_method(c_cpBB, "to_s", rb_cpBBToString, 0);
174
+ }