diff options
| author | esquizo <esquizo+noreply@esquizo.net> | 2026-06-02 23:42:05 -0300 |
|---|---|---|
| committer | esquizo <esquizo+noreply@esquizo.net> | 2026-06-02 23:42:05 -0300 |
| commit | 005f0a3185eb8085f6e0de11e3cfd0a3d9167719 (patch) | |
| tree | 27e8268b818068a4ca574025ef3b04917dd8a044 /libphysics/body.c | |
| parent | 518b93c3a40b0c60054f50356357efc0ead36895 (diff) | |
| parent | ff51d49dc9c7024d29b9177f8e748522625ff1f4 (diff) | |
Merge branch 'libphysics/generational_id'
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); } |
