INITIAL COMMIT
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
// vim:fileencoding=utf-8:foldmethod=marker
|
||||
|
||||
#ifndef PROP_TABLE_H
|
||||
#define PROP_TABLE_H
|
||||
|
||||
#include "entity_id.h"
|
||||
#include "wapp/wapp.h"
|
||||
|
||||
template<typename T>
|
||||
struct PropTable {
|
||||
PropTable(const Allocator *allocator, u64 capacity) {
|
||||
index = wapp_array_alloc_capacity(i32, allocator, capacity, ARRAY_INIT_FILLED);
|
||||
values = wapp_array_alloc_capacity(T, allocator, capacity, ARRAY_INIT_NONE);
|
||||
|
||||
for (u64 i = 0; i < wapp_array_count(index); ++i) {
|
||||
index[i] = -1;
|
||||
}
|
||||
}
|
||||
|
||||
void add_prop(EntityID id, T prop) {
|
||||
if (IS_INVALID_ID(id)) { return; }
|
||||
|
||||
i32 idx = (i32)wapp_array_count(values);
|
||||
wapp_array_set(i32, index, id.index, &idx);
|
||||
wapp_array_append_capped(T, values, &prop);
|
||||
}
|
||||
|
||||
void remove_prop(EntityID id) {
|
||||
if (IS_INVALID_ID(id) || *wapp_array_get(i32, index, id.index) == -1) { return; }
|
||||
|
||||
// Get indices for swapping
|
||||
i32 idx1 = *wapp_array_get(i32, index, id.index);
|
||||
i32 idx2 = ((i32)wapp_array_count(values)) - 1;
|
||||
|
||||
// Swap values
|
||||
values[idx1] = values[idx2];
|
||||
wapp_array_pop(T, values);
|
||||
|
||||
// Update index
|
||||
index[id.index] = -1;
|
||||
|
||||
i64 entity_index = -1;
|
||||
for (u64 i = 0; i < wapp_array_count(index); ++i) {
|
||||
if (index[i] == idx2) {
|
||||
entity_index = (i64)i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (entity_index != -1) {
|
||||
index[entity_index] = idx1;
|
||||
}
|
||||
}
|
||||
|
||||
// private:
|
||||
typedef T *TArray;
|
||||
|
||||
I32Array index;
|
||||
TArray values;
|
||||
};
|
||||
|
||||
#endif // !PROP_TABLE_H
|
||||
Reference in New Issue
Block a user