diff options
Diffstat (limited to 'libphysics/body.c')
| -rw-r--r-- | libphysics/body.c | 38 |
1 files changed, 30 insertions, 8 deletions
diff --git a/libphysics/body.c b/libphysics/body.c index d8a17e5..9495305 100644 --- a/libphysics/body.c +++ b/libphysics/body.c @@ -1,6 +1,7 @@ #include <vecmath.h> #include <physics.h> #include <stdlib.h> +#include <assert.h> #include "dat.h" @@ -19,44 +20,44 @@ phxnew(BodyType type) if((b = allocbody()) == -1) return b; - phxbodypool[b].type = type; + __getbodydata(b)->type = type; return b; } void phxdel(BodyID id) { - phxbodypool[id].active = 0; + __getbodydata(id)->active = 0; } void phxsetpos(BodyID b, float x, float y) { - vec2_dup(phxbodypool[b].pos, (vec2){ x, y }); + vec2_dup(__getbodydata(b)->pos, (vec2){ x, y }); } void phxsetsize(BodyID b, float w, float h) { - vec2_dup(phxbodypool[b].size, (vec2){ w, h }); + vec2_dup(__getbodydata(b)->size, (vec2){ w, h }); } void phxgetpos(BodyID b, float p[2]) { - vec2_dup(p, phxbodypool[b].pos); + vec2_dup(p, __getbodydata(b)->pos); } void phxgetsize(BodyID b, float p[2]) { - vec2_dup(p, phxbodypool[b].size); + vec2_dup(p, __getbodydata(b)->size); } void phxapplyaccel(BodyID b, float a[2]) { - vec2_add(phxbodypool[b].accel, phxbodypool[b].accel, a); + vec2_add(__getbodydata(b)->accel, __getbodydata(b)->accel, a); } BodyID @@ -75,5 +76,26 @@ allocbody(void) } phxbodypool[i].active = 1; - return i; + phxbodypool[i].generation++; + + return __getbodyid(phxbodypool + i); +} + +Body * +__getbodydata(BodyID id) +{ + unsigned int i = id >> 16; + unsigned int g = id & 0xFFFF; + + assert(i < POOL_SIZE); + assert((phxbodypool[i].generation & 0xFFFF) == g); + + return phxbodypool + i; +} + +BodyID +__getbodyid(Body *b) +{ + unsigned int i = b - phxbodypool; + return (i << 16) | (phxbodypool[i].generation & 0xFFFF); } |
