1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
|
// VERSION C++
// .h
// cpBB est une instance d'une typedef struct qui devient une ref class CBox // avec la conversion
typedef struct cpShape{
cpBB (*cacheData)(struct cpShape *shape, cpVect p, cpVect rot);
cpBB bb;
//...
}cpShape;
cpShape* cpShapeInit(cpShape *shape, cpShapeType type, cpBody *body);
cpBB cpShapeCacheBB(cpShape *shape);
typedef struct cpCircleShape{
cpShape shape;
// ...
}cpCircleShape;
cpCircleShape *cpCircleShapeInit(cpCircleShape *circle, cpBody *body, cpFloat radius, cpVect offset);
cpBB cpCircleShapeCacheData(cpShape *shape, cpVect p, cpVect rot);
// .cpp
cpShape*
cpShapeInit(cpShape *shape, cpShapeType type, cpBody *body)
{
//...
cpShapeCacheBB(shape);
return shape;
}
cpBB
cpShapeCacheBB(cpShape *shape)
{
cpBody *body = shape->body;
shape->bb = shape->cacheData(shape, body->p, body->rot);
return shape->bb;
}
cpCircleShape *
cpCircleShapeInit(cpCircleShape *circle, cpBody *body, cpFloat radius, cpVect offset)
{
circle->shape.cacheData = &cpCircleShapeCacheData;
cpShapeInit((cpShape *)circle, CP_CIRCLE_SHAPE, body);
//...
return circle;
}
static cpBB
cpCircleShapeCacheData(cpShape *shape, cpVect p, cpVect rot)
{
cpCircleShape *circle = (cpCircleShape *)shape;
circle->tc = cpvadd(p, cpvrotate(circle->c, rot));
return bbFromCircle(circle->tc, circle->r); // retourne instance cpBB
}
// VERSION C++/CLI
// .h
public ref class CShape
{
public:
CShape(ShapeType type, CBody ^body);
//déclarations...
static CBox ^cacheBox(CShape ^shape);
CBox ^(*cacheData)(CShape ^shape, CVect ^p, CVect ^rot);
CBox ^CachedBox;
};
public ref class CCircleShape
{
public:
CCircleShape(CBody ^body, float radius, CVect ^offset);
CShape ^shape;
CBox ^CacheData(CShape ^shape, CVect ^p, CVect ^rot);
};
// .cpp
PhysicEngine::Shapes::CShape::CShape(ShapeType type, CBody ^body)
{
this->cacheBox(this);
// ...
}
CBox ^PhysicEngine::Shapes::CShape::cacheBox(CShape ^shape)
{
CBody ^body = shape->body;
shape->CachedBox = shape->cacheData(shape, body->p, body->rot);
return shape->CachedBox;
}
PhysicEngine::Shapes::CCircleShape::CCircleShape(CBody ^body, float radius, CVect ^offset)
{
this->shape->cacheData = &CCircleShape::CacheData; /* même type d'erreur que celle énoncé dans mon premier message */
this->shape = gcnew CShape(PhysicEngine::ShapeType::CP_CIRCLE_SHAPE, body);
}
CBox ^PhysicEngine::Shapes::CCircleShape::CacheData(CShape ^shape, CVect ^p, CVect ^rot)
{
CCircleShape ^circle = (CCircleShape ^) (shape); /* Le cast ne marche pas et rend un pointeur nul */
circle->tc = CVect::vadd(p, CVect::vrotate(circle->c, rot));
return BoxFromCircle(circle->tc, circle->r);
} |
Partager