Skip to content
This repository has been archived by the owner on Nov 3, 2023. It is now read-only.

Commit

Permalink
Merge pull request #106 from StephaneDelcroix/chipmunk
Browse files Browse the repository at this point in the history
Chipmunk: AddPostStepCallback, equality overrides and fix the callbacks
  • Loading branch information
rolfbjarne committed Mar 4, 2013
2 parents 19ae9d5 + db193d2 commit 1a738d4
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 37 deletions.
2 changes: 1 addition & 1 deletion chipmunk/binding/src/Arbiter.cs
Expand Up @@ -15,7 +15,7 @@

namespace Chipmunk
{
public partial class Arbiter : ChipmunkObject
public sealed partial class Arbiter : ChipmunkObject
{
public Arbiter (IntPtr ptr) : base (ptr, false)
{
Expand Down
2 changes: 1 addition & 1 deletion chipmunk/binding/src/Body.cs
Expand Up @@ -20,7 +20,7 @@

namespace Chipmunk
{
public partial class Body : ChipmunkObject
public sealed partial class Body : ChipmunkObject
{
public Body (IntPtr ptr) : base (ptr)
{
Expand Down
30 changes: 30 additions & 0 deletions chipmunk/binding/src/ChipmunkObject.cs
Expand Up @@ -39,6 +39,36 @@ protected ChipmunkObject (IntPtr ptr, bool refcount = true)
AddRef (this, ptr);
}

public static bool operator ==(ChipmunkObject a, ChipmunkObject b)
{
if (System.Object.ReferenceEquals (a, b))
return true;

if (((object)a == null) || ((object)b == null))
return false;

return a.Handle.Handle == b.Handle.Handle;
}

public static bool operator != (ChipmunkObject a, ChipmunkObject b)
{
return !(a == b);
}

public override bool Equals (System.Object obj)
{
var other = obj as ChipmunkObject;
if ((object)other == null)
return false;

return this == other;
}

public override int GetHashCode ()
{
return Handle.Handle.GetHashCode ();
}

~ChipmunkObject ()
{
Cleanup ();
Expand Down
21 changes: 10 additions & 11 deletions chipmunk/binding/src/Constraint.cs
Expand Up @@ -89,7 +89,7 @@ protected override void Free ()

}

public partial class PinJoint : Constraint
public sealed partial class PinJoint : Constraint
{
[DllImport ("__Internal")]
extern static IntPtr cpPinJointNew (IntPtr bodyA, IntPtr bodyB, PointF anchr1, PointF anchr2);
Expand Down Expand Up @@ -132,7 +132,7 @@ public PinJoint (Body bodyA, Body bodyB, PointF anchr1, PointF anchr2) : base (c
}
}

public partial class SlideJoint : Constraint
public sealed partial class SlideJoint : Constraint
{
[DllImport ("__Internal")]
extern static IntPtr cpSlideJointNew (IntPtr bodyA, IntPtr bodyB, PointF anchr1, PointF anchr2, float min, float max);
Expand Down Expand Up @@ -186,7 +186,7 @@ public SlideJoint (Body bodyA, Body bodyB, PointF anchr1, PointF anchr2, float m
}
}

public partial class PivotJoint : Constraint
public sealed partial class PivotJoint : Constraint
{
[DllImport ("__Internal")]
extern static IntPtr cpPivotJointNew (IntPtr bodyA, IntPtr bodyB, PointF pivot);
Expand Down Expand Up @@ -225,7 +225,7 @@ public PivotJoint (Body bodyA, Body bodyB, PointF anchr1, PointF anchr2) : base
}
}

public partial class GrooveJoint : Constraint
public sealed partial class GrooveJoint : Constraint
{
[DllImport ("__Internal")]
extern static IntPtr cpGrooveJointNew (IntPtr bodyA, IntPtr bodyB, PointF grooveA, PointF grooveB, PointF anchr2);
Expand Down Expand Up @@ -268,7 +268,7 @@ public GrooveJoint (Body bodyA, Body bodyB, PointF grooveA, PointF grooveB, Poin
}
}

public partial class DampedSpring : Constraint
public sealed partial class DampedSpring : Constraint
{
[DllImport("__Internal")]
extern static IntPtr cpDampedSpringNew (IntPtr bodyA, IntPtr bodyB, PointF anchr1, PointF anchr2, float restLength, float stiffness, float damping);
Expand Down Expand Up @@ -333,7 +333,7 @@ public DampedSpring (Body bodyA, Body bodyB, PointF anchr1, PointF anchr2, float
}
}

public partial class DampedRotarySpring : Constraint
public sealed partial class DampedRotarySpring : Constraint
{
[DllImport ("__Internal")]
extern static IntPtr cpDampedRotarySpringNew (IntPtr bodyA, IntPtr bodyB, float restAngle, float stiffness, float damping);
Expand Down Expand Up @@ -376,7 +376,7 @@ public DampedRotarySpring (Body bodyA, Body bodyB, float restAngle, float stiffn
}
}

public partial class RotaryLimitJoint : Constraint
public sealed partial class RotaryLimitJoint : Constraint
{
[DllImport ("__Internal")]
extern static IntPtr cpRotaryLimitJointNew (IntPtr bodyA, IntPtr bodyB, float min, float max);
Expand Down Expand Up @@ -408,7 +408,7 @@ public RotaryLimitJoint (Body bodyA, Body bodyB, float min, float max) : base (c
}
}

public partial class RatchetJoint : Constraint
public sealed partial class RatchetJoint : Constraint
{
[DllImport ("__Internal")]
extern static IntPtr cpRatchetJointNew (IntPtr bodyA, IntPtr bodyB, float phase, float ratchet);
Expand Down Expand Up @@ -451,7 +451,7 @@ public RatchetJoint (Body bodyA, Body bodyB, float phase, float ratchet) : base
}
}

public partial class GearJoint : Constraint
public sealed partial class GearJoint : Constraint
{
[DllImport ("__Internal")]
extern static IntPtr cpGearJointNew (IntPtr bodyA, IntPtr bodyB, float pahse, float ratio);
Expand Down Expand Up @@ -483,7 +483,7 @@ public GearJoint (Body bodyA, Body bodyB, float phase, float ratio) : base (cpGe
}
}

public partial class SimpleMotor : Constraint
public sealed partial class SimpleMotor : Constraint
{
[DllImport ("__Internal")]
extern static IntPtr cpSimpleMotorNew (IntPtr bodyA, IntPtr bodyB, float rate);
Expand All @@ -504,4 +504,3 @@ public SimpleMotor (Body bodyA, Body bodyB, float rate) : base (cpSimpleMotorNew
}
}
}

6 changes: 3 additions & 3 deletions chipmunk/binding/src/Shape.cs
Expand Up @@ -156,7 +156,7 @@ public RectangleF Update (PointF position, PointF rotation)
public extern static void ResetShapeIdCounter ();
}

public partial class CircleShape : Shape
public sealed partial class CircleShape : Shape
{
[DllImport ("__Internal")]
extern static IntPtr cpCircleShapeNew (IntPtr body, float radius, PointF offset);
Expand All @@ -180,7 +180,7 @@ public CircleShape (Body body, float radius, PointF offset) : base (cpCircleShap
}
}

public partial class SegmentShape:Shape
public sealed partial class SegmentShape:Shape
{
[DllImport("__Internal")]
extern static IntPtr cpSegmentShapeNew (IntPtr body, PointF a, PointF b, float radius);
Expand Down Expand Up @@ -218,7 +218,7 @@ public SegmentShape (Body body, PointF a, PointF b, float radius) : base (cpSegm
}
}

public partial class PolygonShape : Shape
public sealed partial class PolygonShape : Shape
{
[DllImport("__Internal")]
extern static IntPtr cpPolyShapeNew (IntPtr body, int numVerts, PointF[] verts, PointF offset);
Expand Down
117 changes: 96 additions & 21 deletions chipmunk/binding/src/Space.cs
Expand Up @@ -17,7 +17,7 @@

namespace Chipmunk
{
public partial class Space : ChipmunkObject
public sealed partial class Space : ChipmunkObject
{
[DllImport ("__Internal")]
extern static IntPtr cpSpaceNew ();
Expand Down Expand Up @@ -242,7 +242,7 @@ public bool Contains (Constraint constraint)
{
return cpSpaceContainsConstraint (Handle.Handle, constraint.Handle.Handle);
}

[DllImport("__Internal")]
extern static IntPtr cpSpaceAddStaticShape (IntPtr space, IntPtr shape);

Expand Down Expand Up @@ -279,43 +279,67 @@ public void ReindexStaticShapes ()
}

//iterators
delegate void BodyIterator (IntPtr body, IntPtr data);
delegate void BodyIteratorFunc (IntPtr body, IntPtr data);

[MonoTouch.MonoPInvokeCallback (typeof (BodyIteratorFunc))]
static void BodyIterator (IntPtr body, IntPtr data)
{
var handle = GCHandle.FromIntPtr (data);
var action = (Action<Body>)handle.Target;
action (new Body(body));
}

[DllImport ("__Internal")]
extern static void cpSpaceEachBody (IntPtr space, BodyIterator iterator, IntPtr data);
extern static void cpSpaceEachBody (IntPtr space, BodyIteratorFunc iterator, IntPtr data);

public void EachBody (Action<Body> action)
{
BodyIterator iterator = (body, data) => {
action (new Body (body));
};
cpSpaceEachBody (Handle.Handle, iterator, IntPtr.Zero);
var handle = GCHandle.Alloc (action);
var data = GCHandle.ToIntPtr(handle);
cpSpaceEachBody (Handle.Handle, BodyIterator, data);
handle.Free ();
}

delegate void ShapeIterator (IntPtr shape, IntPtr data);
delegate void ShapeIteratorFunc (IntPtr shape, IntPtr data);

[MonoTouch.MonoPInvokeCallback (typeof (ShapeIteratorFunc))]
static void ShapeIterator (IntPtr shape, IntPtr data)
{
var handle = GCHandle.FromIntPtr (data);
var action = (Action<Shape>)handle.Target;
action (new Shape(shape));
}

[DllImport ("__Internal")]
extern static void cpSpaceEachShape (IntPtr space, ShapeIterator iterator, IntPtr data);
extern static void cpSpaceEachShape (IntPtr space, ShapeIteratorFunc iterator, IntPtr data);

public void EachShape (Action<Shape> action)
{
ShapeIterator iterator = (shape, data) => {
action (new Shape(shape));
};
cpSpaceEachShape (Handle.Handle, iterator, IntPtr.Zero);
var handle = GCHandle.Alloc (action);
var data = GCHandle.ToIntPtr(handle);
cpSpaceEachShape (Handle.Handle, ShapeIterator, data);
handle.Free ();
}

delegate void ConstraintIterator (IntPtr constraint, IntPtr data);
delegate void ConstraintIteratorFunc (IntPtr constraint, IntPtr data);

[MonoTouch.MonoPInvokeCallback (typeof (ConstraintIteratorFunc))]
static void ConstraintIterator (IntPtr constraint, IntPtr data)
{
var handle = GCHandle.FromIntPtr (data);
var action = (Action<Constraint>)handle.Target;
action (new Constraint (constraint));
}

[DllImport ("__Internal")]
extern static void cpSpaceEachConstraint(IntPtr body, ConstraintIterator iterator, IntPtr data);
extern static void cpSpaceEachConstraint(IntPtr body, ConstraintIteratorFunc iterator, IntPtr data);

public void EachConstraint (Action<Constraint> action)
{
ConstraintIterator iterator = (constraint, data) => {
action (new Constraint(constraint));
};
cpSpaceEachConstraint (Handle.Handle, iterator, IntPtr.Zero);
var handle = GCHandle.Alloc (action);
var data = GCHandle.ToIntPtr(handle);
cpSpaceEachConstraint (Handle.Handle, ConstraintIterator, data);
handle.Free ();
}

//simulating the space
Expand All @@ -324,9 +348,60 @@ public void EachConstraint (Action<Constraint> action)

public void Step (float dt)
{
cpSpaceStep(Handle.Handle, dt);
cpSpaceStep(Handle.Handle, dt);
}

//Post step
delegate void PostStepFunc (IntPtr space, IntPtr obj, IntPtr data);

[MonoTouch.MonoPInvokeCallback (typeof (PostStepFunc))]
static void PostStepForBody (IntPtr space, IntPtr obj, IntPtr data)
{
var handle = GCHandle.FromIntPtr (data);
var action = (Action<Body>)handle.Target;
handle.Free ();
action (obj == IntPtr.Zero ? null : new Body (obj));
}

[MonoTouch.MonoPInvokeCallback (typeof (PostStepFunc))]
static void PostStepForShape (IntPtr space, IntPtr obj, IntPtr data)
{
var handle = GCHandle.FromIntPtr (data);
var action = (Action<Shape>)handle.Target;
handle.Free ();
action (obj == IntPtr.Zero ? null : new Shape (obj));
}

[MonoTouch.MonoPInvokeCallback (typeof (PostStepFunc))]
static void PostStepForConstraint (IntPtr space, IntPtr obj, IntPtr data)
{
var handle = GCHandle.FromIntPtr (data);
var action = (Action<Constraint>)handle.Target;
handle.Free ();
action (obj == IntPtr.Zero ? null : new Constraint (obj));
}

[DllImport ("__Internal")]
extern static void cpSpaceAddPostStepCallback (IntPtr space, PostStepFunc func, IntPtr key, IntPtr data);

public void AddPostStepCallback (Action<Body> action, Body obj)
{
var data = GCHandle.ToIntPtr(GCHandle.Alloc (action));
cpSpaceAddPostStepCallback (Handle.Handle, PostStepForBody, obj.Handle.Handle, data);
}

public void AddPostStepCallback (Action<Shape> action, Shape obj)
{
var data = GCHandle.ToIntPtr(GCHandle.Alloc (action));
cpSpaceAddPostStepCallback (Handle.Handle, PostStepForShape, obj.Handle.Handle, data);
}

public void AddPostStepCallback (Action<Constraint> action, Constraint obj)
{
var data = GCHandle.ToIntPtr(GCHandle.Alloc (action));
cpSpaceAddPostStepCallback (Handle.Handle, PostStepForConstraint, obj.Handle.Handle, data);
}

//spatial hash
[DllImport ("__Internal")]
extern static void cpSpaceUseSpatialHash(IntPtr space, float dim, int count);
Expand Down

0 comments on commit 1a738d4

Please sign in to comment.