changeset 14:d88862c82f06

more properties
author Extrawurst
date Fri, 03 Dec 2010 23:55:20 +0100
parents c03a41d47b60
children df4ebc8add66
files trunk/chipmunkd/cpBody.d trunk/chipmunkd/cpShape.d
diffstat 2 files changed, 51 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- a/trunk/chipmunkd/cpBody.d	Fri Dec 03 21:38:01 2010 +0100
+++ b/trunk/chipmunkd/cpBody.d	Fri Dec 03 23:55:20 2010 +0100
@@ -127,25 +127,48 @@
 //CP_DefineBodyGetter(type, member, name) \
 //CP_DefineBodySetter(type, member, name)
 
+template CP_DefineBodyGetter(string type,string member,string name)
+{
+	enum CP_DefineBodyGetter = 
+		"static "~type~" BodyGet"~name~"(const cpBody *_body){"~
+		"return _body."~member~";"~
+		"}";
+}
+
+template CP_DefineBodySetter(string type,string member,string name)
+{
+	enum CP_DefineBodySetter = 
+		"static void BodySet"~name~"(cpBody *_body, const "~type~" value){"~
+		"cpBodyActivate(_body);"~
+		"_body."~member~" = value;"~
+		"}";
+}
+
+template CP_DefineBodyProperty(string type,string member,string name)
+{
+	enum CP_DefineBodyProperty = 
+		CP_DefineBodyGetter!(type,member,name)~CP_DefineBodySetter!(type,member,name);
+		
+}
 
 //// Accessors for cpBody struct members
-//CP_DefineBodyGetter(cpFloat, m, Mass);
+mixin(CP_DefineBodyGetter!("cpFloat","m","Mass"));
 //void cpBodySetMass(cpBody *body, cpFloat m);
 //
-//CP_DefineBodyGetter(cpFloat, i, Moment);
+mixin(CP_DefineBodyGetter!("cpFloat","i","Moment"));
 //void cpBodySetMoment(cpBody *body, cpFloat i);
 //
 //
-//CP_DefineBodyProperty(cpVect, p, Pos);
-//CP_DefineBodyProperty(cpVect, v, Vel);
-//CP_DefineBodyProperty(cpVect, f, Force);
-static cpFloat cpBodyGetAngle(const cpBody *_body){return _body.a;}
-static cpFloat cpBodySetAngle(cpBody *_body, const cpFloat value){cpBodyActivate(_body); return _body.a = value;}
-//CP_DefineBodyProperty(cpFloat, w, AngVel);
-//CP_DefineBodyProperty(cpFloat, t, Torque);
-//CP_DefineBodyGetter(cpVect, rot, Rot);
-//CP_DefineBodyProperty(cpFloat, v_limit, VelLimit);
-//CP_DefineBodyProperty(cpFloat, w_limit, AngVelLimit);
+
+mixin(CP_DefineBodyProperty!("cpVect","p","Pos"));
+mixin(CP_DefineBodyProperty!("cpVect","v","Vel"));
+mixin(CP_DefineBodyProperty!("cpVect","f","Force"));
+mixin(CP_DefineBodyProperty!("cpFloat","a","Angle"));
+mixin(CP_DefineBodyProperty!("cpFloat","w","AngVel"));
+mixin(CP_DefineBodyProperty!("cpFloat","t","Torque"));
+mixin(CP_DefineBodyProperty!("cpVect","rot","Rot"));
+mixin(CP_DefineBodyProperty!("cpFloat","v_limit","VelLimit"));
+mixin(CP_DefineBodyProperty!("cpFloat","w_limit","AngVelLimit"));
 
 ////  Modify the velocity of the body so that it will move to the specified absolute coordinates in the next timestep.
 //// Intended for objects that are moved manually with a custom velocity integration function.
--- a/trunk/chipmunkd/cpShape.d	Fri Dec 03 21:38:01 2010 +0100
+++ b/trunk/chipmunkd/cpShape.d	Fri Dec 03 23:55:20 2010 +0100
@@ -99,9 +99,14 @@
 //// Test if a point lies within a shape.
 //cpBool cpShapePointQuery(cpShape *shape, cpVect p);
 
-//TODO
-//#define CP_DeclareShapeGetter(struct, type, name) type struct##Get##name(cpShape *shape)
-//
+template CP_DefineShapeGetter(string _struct,string type,string member,string name)
+{
+	enum CP_DefineShapeGetter = 
+		type~" "~_struct~"Get"~name~"(cpShape *shape){"~
+		"assert(shape.klass == &"~_struct~"Class, \"shape is not a "~_struct~"\");"~
+		"return (cast("~_struct~"*)shape)."~member~";"~
+		"}";
+}
 
 // Circle shape structure.
 struct cpCircleShape{
@@ -120,9 +125,9 @@
 //cpCircleShape *cpCircleShapeAlloc(void);
 //cpCircleShape *cpCircleShapeInit(cpCircleShape *circle, cpBody *body, cpFloat radius, cpVect offset);
 //cpShape *cpCircleShapeNew(cpBody *body, cpFloat radius, cpVect offset);
-//
-//CP_DeclareShapeGetter(cpCircleShape, cpVect, Offset);
-//CP_DeclareShapeGetter(cpCircleShape, cpFloat, Radius);
+
+mixin(CP_DefineShapeGetter!("cpCircleShape", "cpVect", "c", "Offset"));
+mixin(CP_DefineShapeGetter!("cpCircleShape", "cpFloat", "r", "Radius"));
 
 // Segment shape structure.
 struct cpSegmentShape{
@@ -141,11 +146,11 @@
 //cpSegmentShape* cpSegmentShapeAlloc(void);
 //cpSegmentShape* cpSegmentShapeInit(cpSegmentShape *seg, cpBody *body, cpVect a, cpVect b, cpFloat radius);
 //cpShape* cpSegmentShapeNew(cpBody *body, cpVect a, cpVect b, cpFloat radius);
-//
-//CP_DeclareShapeGetter(cpSegmentShape, cpVect, A);
-//CP_DeclareShapeGetter(cpSegmentShape, cpVect, B);
-//CP_DeclareShapeGetter(cpSegmentShape, cpVect, Normal);
-//CP_DeclareShapeGetter(cpSegmentShape, cpFloat, Radius);
+
+mixin(CP_DefineShapeGetter!("cpSegmentShape", "cpVect", "a", "A"));
+mixin(CP_DefineShapeGetter!("cpSegmentShape", "cpVect", "b", "B"));
+mixin(CP_DefineShapeGetter!("cpSegmentShape", "cpVect", "n", "Normal"));
+mixin(CP_DefineShapeGetter!("cpSegmentShape", "cpFloat", "r", "Radius"));
 //
 //// For determinism, you can reset the shape id counter.
 //void cpResetShapeIdCounter(void);
@@ -331,9 +336,6 @@
 {
 	return cast(cpShape *)cpCircleShapeInit(cpCircleShapeAlloc(), _body, radius, offset);
 }
-//TODO:
-//CP_DefineShapeGetter(cpCircleShape, cpVect, c, Offset)
-//CP_DefineShapeGetter(cpCircleShape, cpFloat, r, Radius)
 
 cpSegmentShape *
 cpSegmentShapeAlloc()
@@ -487,11 +489,6 @@
 {
 	return cast(cpShape *)cpSegmentShapeInit(cpSegmentShapeAlloc(), _body, a, b, r);
 }
-//TODO:
-//CP_DefineShapeGetter(cpSegmentShape, cpVect, a, A)
-//CP_DefineShapeGetter(cpSegmentShape, cpVect, b, B)
-//CP_DefineShapeGetter(cpSegmentShape, cpVect, n, Normal)
-//CP_DefineShapeGetter(cpSegmentShape, cpFloat, r, Radius)
 
 // Unsafe API (chipmunk_unsafe.h)