summaryrefslogtreecommitdiff
path: root/src/physics.cpp
diff options
context:
space:
mode:
authorJonathan Bradley <jcb@pikum.xyz>2025-05-21 13:35:55 -0400
committerJonathan Bradley <jcb@pikum.xyz>2025-05-21 13:58:08 -0400
commit0c1005dae0832e52a79ae3bbb01235c8756ab8f2 (patch)
treea2e186bb1f42e4bc3accabe6ab4feaba709595ad /src/physics.cpp
parentb87c81d7cfc12637fc2f59ff188ab0fb41f7ae41 (diff)
pke: physics DynArray to pk_arr_t
Diffstat (limited to 'src/physics.cpp')
-rw-r--r--src/physics.cpp43
1 files changed, 22 insertions, 21 deletions
diff --git a/src/physics.cpp b/src/physics.cpp
index 3f61c19..3a20f8b 100644
--- a/src/physics.cpp
+++ b/src/physics.cpp
@@ -2,7 +2,6 @@
#include "physics.hpp"
#include "components.hpp"
-#include "dynamic-array.hpp"
#include "ecs.hpp"
#include "game-settings.hpp"
#include "pk.h"
@@ -21,7 +20,7 @@ struct AllocedData {
void *data;
std::size_t size;
};
-DynArray<AllocedData> *bulletAllocs;
+pk_arr_t<AllocedData> bulletAllocs{};
btDefaultCollisionConfiguration *btConfiguration = nullptr;
btCollisionDispatcher *btDispatcher = nullptr;
@@ -35,41 +34,41 @@ pk_arr_t<EntityCollision> collisionsThisTick{};
void *pke_btAlignedAllocFunc(size_t size, int alignment) {
void *ptr = pk_new_bkt(size, alignment, MemBkt_Bullet);
- bulletAllocs->Push({ptr, size});
+ pk_arr_append_t(&bulletAllocs, {ptr, size});
return ptr;
}
void pke_btAlignedFreeFunc(void *memBlock) {
- auto &arr = *bulletAllocs;
- auto count = arr.Count();
- long index = -1;
- for (long i = 0; i < count; ++i) {
- if (arr[i].data == memBlock) {
+ int64_t index = -1;
+ uint32_t i, count;
+ count = bulletAllocs.next;
+ for (i = 0; i < count; ++i) {
+ if (bulletAllocs[i].data == memBlock) {
index = i;
break;
}
}
assert(index != -1);
- pk_delete_bkt(memBlock, arr[index].size, MemBkt_Bullet);
- bulletAllocs->Remove(index);
+ pk_delete_bkt(memBlock, bulletAllocs[index].size, MemBkt_Bullet);
+ pk_arr_remove_at(&bulletAllocs, index);
}
void *pke_btAllocFunc(size_t size) {
void *ptr = pk_new_bkt(size, PK_MINIMUM_ALIGNMENT, MemBkt_Bullet);
- bulletAllocs->Push({ptr, size});
+ pk_arr_append_t(&bulletAllocs, {ptr, size});
return ptr;
}
void pke_btFreeFunc(void *memBlock) {
- auto &arr = *bulletAllocs;
- auto count = arr.Count();
- long index = -1;
- for (long i = 0; i < count; ++i) {
- if (arr[i].data == memBlock) {
+ int64_t index = -1;
+ uint32_t i, count;
+ count = bulletAllocs.next;
+ for (i = 0; i < count; ++i) {
+ if (bulletAllocs[i].data == memBlock) {
index = i;
break;
}
}
assert(index != -1);
- pk_delete_bkt(memBlock, arr[index].size, MemBkt_Bullet);
- bulletAllocs->Remove(index);
+ pk_delete_bkt(memBlock, bulletAllocs[index].size, MemBkt_Bullet);
+ pk_arr_remove_at(&bulletAllocs, index);
}
struct CollisionHandlerStruct : public btOverlapFilterCallback {
@@ -97,9 +96,8 @@ struct CollisionHandlerStruct : public btOverlapFilterCallback {
void Physics_Init() {
MemBkt_Bullet = pk_bucket_create("physics", PK_DEFAULT_BUCKET_SIZE, false);
- bulletAllocs = pk_new<DynArray<AllocedData>>(MemBkt_Bullet);
- new (bulletAllocs) DynArray<AllocedData>(MemBkt_Bullet);
- bulletAllocs->Reserve(1024);
+ bulletAllocs.bkt = MemBkt_Bullet;
+ pk_arr_reserve(&bulletAllocs, 1024);
btAlignedAllocSetCustom(pke_btAllocFunc, pke_btFreeFunc);
btAlignedAllocSetCustomAligned(pke_btAlignedAllocFunc, pke_btAlignedFreeFunc);
@@ -136,5 +134,8 @@ void Physics_Teardown() {
pk_arr_reset(&collisionsThisTick);
pk_delete<btDiscreteDynamicsWorld>(BtDynamicsWorld, MemBkt_Bullet);
BtDynamicsWorld = nullptr;
+ // TODO should we manually delete each bullet alloc?
+ // or just drop?
+ pk_arr_reset(&bulletAllocs);
pk_bucket_destroy(MemBkt_Bullet);
}