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

Commit

Permalink
[chipmunk] Fix the callback passed to native
Browse files Browse the repository at this point in the history
The callback used to work... on the simulator, but failed when I tried
them on a device. This adds a nice layer of abstraction to hide the fact
that you have to flag the callbacks with [MonoPInvokeCallback] and that
you can't have instance methods.
  • Loading branch information
StephaneDelcroix committed Mar 1, 2013
1 parent 52a8e6e commit db193d2
Showing 1 changed file with 95 additions and 34 deletions.
129 changes: 95 additions & 34 deletions chipmunk/binding/src/Space.cs
Expand Up @@ -242,21 +242,7 @@ public bool Contains (Constraint constraint)
{
return cpSpaceContainsConstraint (Handle.Handle, constraint.Handle.Handle);
}

delegate void PostStepFunc (IntPtr space, IntPtr obj, IntPtr data);

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

public void AddPostStepCallback<T> (Action<Space, T> action, T obj) where T : ChipmunkObject
{
PostStepFunc func = (space, o, data) => {
action (this, o == IntPtr.Zero ? null : (T)(object)new Body (o));
};

cpSpaceAddPostStepCallback (Handle.Handle, func, obj.Handle.Handle, IntPtr.Zero);
}


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

Expand Down Expand Up @@ -293,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 @@ -338,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 db193d2

Please sign in to comment.