Rename utils to util

This commit is contained in:
2023-11-23 10:31:51 +01:00
parent 783db8ba90
commit adaada9a22
12 changed files with 6 additions and 6 deletions

View File

@@ -0,0 +1,99 @@
#include "array.h"
#include "../core/memory.h"
typedef struct BzArrayHead {
i32 capacity;
i32 size;
i32 stride;
} BzArrayHead;
#define ARRAY_HEAD(array) (((BzArrayHead *) array) -1)
void *_bzArrayCreate(i32 startCapacity, i32 stride) {
BzArrayHead *head = bzAlloc(sizeof(*head) + startCapacity * stride);
BZ_ASSERT(startCapacity > 0);
head[0] = (BzArrayHead) {
.capacity=startCapacity,
.size=0,
.stride=stride
};
return &head[1];
}
void _bzArrayDestroy(void *arr) {
bzFree(ARRAY_HEAD(arr));
}
void _bzArrayClear(void *arr) {
ARRAY_HEAD(arr)->size = 0;
}
i32 _bzArraySize(void *arr) {
return ARRAY_HEAD(arr)->size;
}
i32 _bzArrayCapacity(void *arr) {
return ARRAY_HEAD(arr)->capacity;
}
void *_bzArrayResize(void **arr, i32 newSize) {
BzArrayHead *old = ARRAY_HEAD(*arr);
BZ_ASSERT(newSize >= old->size);
if (newSize == old->capacity) return *arr;
BzArrayHead *new = bzResize(old, sizeof(*new) + newSize * old->stride);
new[0].capacity = newSize;
return &new[1];
}
void *_bzArrayEnsureCapacity(void **arr, i32 capacity) {
BzArrayHead *head = ARRAY_HEAD(*arr);
if (capacity > head->capacity)
return _bzArrayResize(arr, head->capacity << 1);
return *arr;
}
i32 _bzArrayGet(void *arr, i32 idx) {
BzArrayHead *head = ARRAY_HEAD(arr);
BZ_ASSERT(idx >= 0 && idx < head->size);
return idx;
}
void _bzArrayDelN(void *arr, i32 idx, i32 n) {
BzArrayHead *head = ARRAY_HEAD(arr);
BZ_ASSERT(n <= head->size);
BZ_ASSERT(idx >= 0 && idx + n <= head->size);
bzMemMove((u8 *) arr + idx * head->stride,
(u8 *) arr + (idx + n) * head->stride,
n * head->stride);
head->size -= n;
}
i32 _bzArrayPush(void *arr) {
BzArrayHead *head = ARRAY_HEAD(arr);
BZ_ASSERT(head->size < head->capacity);
i32 insertIdx = head->size;
head->size++;
return insertIdx;
}
i32 _bzArrayIns(void *arr, i32 idx) {
BzArrayHead *head = ARRAY_HEAD(arr);
BZ_ASSERT(idx >= 0 && idx <= head->size);
BZ_ASSERT(head->size + 1 <= head->capacity);
if (idx != head->size) {
bzMemMove((u8 *) arr + (idx + 1) * head->stride,
(u8 *) arr + idx * head->stride, head->stride);
}
head->size++;
return idx;
}
i32 _bzArraySet(void *arr, i32 idx) {
BzArrayHead *head = ARRAY_HEAD(arr);
BZ_ASSERT(idx >= 0 && idx < head->size);
return idx;
}
i32 _bzArrayPop(void *arr) {
BzArrayHead *head = ARRAY_HEAD(arr);
BZ_ASSERT(head->size > 0);
head->size--;
return head->size;
}

View File

@@ -0,0 +1,54 @@
#ifndef BREEZE_ARRAY_H
#define BREEZE_ARRAY_H
#include "../defines.h"
void *_bzArrayCreate(i32 startCapacity, i32 stride);
void _bzArrayDestroy(void *arr);
void _bzArrayClear(void *arr);
i32 _bzArraySize(void *arr);
i32 _bzArrayCapacity(void *arr);
void *_bzArrayResize(void **arr, i32 newSize);
void *_bzArrayEnsureCapacity(void **arr, i32 capacity);
i32 _bzArrayGet(void *arr, i32 idx);
void _bzArrayDelN(void *arr, i32 idx, i32 n);
i32 _bzArrayPush(void *arr);
i32 _bzArrayIns(void *arr, i32 idx);
i32 _bzArraySet(void *arr, i32 idx);
i32 _bzArrayPop(void *arr);
#define bzArrayCreate(T, n) (T*) _bzArrayCreate(n, sizeof(T))
#define bzArrayDestroy(arr) do { \
_bzArrayDestroy(arr); \
arr = NULL; \
} while(0)
#define bzArrayClear(arr) _bzArrayClear(arr)
#define bzArraySize(arr) _bzArraySize(arr)
#define bzArrayCapacity(arr) _bzArrayCapacity(arr)
#define bzArrayResize(arr, size) _bzArrayResize((void **) &arr, size)
#define bzArrayEnsureCapacity(arr, capacity) _bzArrayEnsureCapacity((void **) &arr, capacity)
#define bzArrayGet(arr, idx) (arr)[_bzArrayGet(arr, idx)]
#define bzArrayDel(arr, idx) _bzArrayDelN(arr, idx, 1)
#define bzArrayDelN(arr, idx, n) _bzArrayDelN(arr, idx, n)
#define bzArrayPush(arr, e) \
do { \
(arr) = bzArrayEnsureCapacity(arr, bzArraySize(arr) + 1); \
(arr)[_bzArrayPush(arr)] = (e); \
} while (0)
#define bzArrayIns(arr, idx, e) \
do { \
(arr) = bzArrayEnsureCapacity(arr, bzArraySize(arr) + 1); \
(arr)[_bzArrayIns(arr, idx)] = (e); \
} while (0)
#define bzArraySet(arr, idx, e) (arr)[_bzArraySet(arr, idx)] = e
#define bzArrayPop(arr) (arr)[_bzArrayPop(arr)]
#endif //BREEZE_ARRAY_H

127
engine/breeze/util/heap.c Normal file
View File

@@ -0,0 +1,127 @@
#include "heap.h"
#include "../core/memory.h"
typedef struct BzHeapHead {
i32 capacity;
i32 size;
i32 stride;
i32 weightOffset;
} BzHeapHead;
#define HEAP_HEAD(heap) (((BzHeapHead *)heap) - 1)
#define HEAP_LEFT(i) ((i32)(i * 2 + 1))
#define HEAP_RIGHT(i) ((i32)(i * 2 + 2))
#define HEAP_PARENT(i) ((i32) ((i - 1) / 2))
static void heapSiftUp(BzHeapHead *head, void *heap);
static void heapSiftDown(BzHeapHead *head, void *heap);
void *_bzHeapCreate(i32 startCapacity, i32 stride, i32 weightOffset) {
i32 numBytes = sizeof(BzHeapHead) + startCapacity * stride;
BzHeapHead *heap = bzAlloc(numBytes);
heap[0] = (BzHeapHead) {
.capacity=startCapacity,
.size=0,
.stride=stride,
.weightOffset=weightOffset
};
return &heap[1];
}
void _bzHeapDestroy(void *heap) {
bzFree(HEAP_HEAD(heap));
}
void _bzHeapClear(void *heap) {
BzHeapHead *head = HEAP_HEAD(heap);
head->size = 0;
}
i32 _bzHeapSize(void *heap) {
return HEAP_HEAD(heap)->size;
}
bool _bzHeapIsEmpty(void *heap) {
return HEAP_HEAD(heap)->size == 0;
}
i32 _bzHeapPop(void *heap) {
BzHeapHead *head = HEAP_HEAD(heap);
BZ_ASSERT(head->size > 0);
// Move first to index capacity (for output)
bzMemMove(((u8 *)heap) + head->capacity * head->stride, heap, head->stride);
head->size--;
if (head->size == 0) return 0;
// Move last to index 0
bzMemMove(heap, ((u8 *) heap) + head->size * head->stride, head->stride);
heapSiftDown(head, heap);
return head->capacity;
}
void _bzHeapPush(void *heap) {
BzHeapHead *head = HEAP_HEAD(heap);
BZ_ASSERT(head->size < head->capacity);
void *item = ((u8 *)heap) + head->size * head->stride;
bzMemMove(((u8 *)heap) + head->size * head->stride, item, head->stride);
head->size++;
heapSiftUp(head, heap);
}
i32 _bzHeapPushIdx(void *heap) {
BzHeapHead *head = HEAP_HEAD(heap);
BZ_ASSERT(head->size < head->capacity);
return head->size;
}
static void heapSwap(BzHeapHead *head, void *heap, i32 aIdx, i32 bIdx) {
u8 *aItem = ((u8 *)heap) + aIdx * head->stride;
u8 *bItem = ((u8 *)heap) + bIdx * head->stride;
u8 tmp[head->stride];
bzMemMove(tmp, aItem, head->stride);
bzMemMove(aItem, bItem, head->stride);
bzMemMove(bItem, tmp, head->stride);
}
static int heapCmp(BzHeapHead *head, void *heap, i32 lhs, i32 rhs) {
int *aWeight = (i32 *) (((u8 *)heap) + lhs * head->stride + head->weightOffset);
int *bWeight = (i32 *) (((u8 *)heap) + rhs * head->stride + head->weightOffset);
return (*aWeight) - (*bWeight);
}
static void heapSiftUp(BzHeapHead *head, void *heap) {
i32 idx = head->size - 1;
while (idx >= 0) {
i32 parent = HEAP_PARENT(idx);
if (heapCmp(head, heap, idx, parent) >= 0)
break;
heapSwap(head, heap, idx, parent);
idx = parent;
}
}
static void heapSiftDown(BzHeapHead *head, void *heap) {
i32 idx = 0;
while (idx < head->size) {
i32 l = HEAP_LEFT(idx);
i32 r = HEAP_RIGHT(idx);
i32 smallest = idx;
if (l < head->size && heapCmp(head, heap, l, idx) < 0)
smallest = l;
if (r < head->size && heapCmp(head, heap, r, smallest) < 0)
smallest = r;
if (smallest == idx) break;
heapSwap(head, heap, idx, smallest);
idx = smallest;
}
}

30
engine/breeze/util/heap.h Normal file
View File

@@ -0,0 +1,30 @@
#ifndef BREEZE_HEAP_H
#define BREEZE_HEAP_H
#include "../defines.h"
void *_bzHeapCreate(i32 startCapacity, i32 stride, i32 weightOffset);
void _bzHeapDestroy(void *heap);
void _bzHeapClear(void *heap);
i32 _bzHeapSize(void *heap);
bool _bzHeapIsEmpty(void *heap);
i32 _bzHeapPop(void *heap);
void _bzHeapPush(void *heap);
i32 _bzHeapPushIdx(void *heap);
#define bzHeapCreate(T, n) (T *) ((T *)_bzHeapCreate((n), sizeof(T), offsetof(T, weight)))
#define bzHeapDestroy(heap) _bzHeapDestroy((void *) (heap))
#define bzHeapClear(heap) _bzHeapClear((void *) (heap))
#define bzHeapSize(heap) _bzHeapSize((void *) (heap))
#define bzHeapIsEmpty(heap) _bzHeapIsEmpty((void *) (heap))
#define bzHeapPop(heap) ((heap)[_bzHeapPop((void *) (heap))])
#define bzHeapPush(heap, ...) do { \
void *h = (void *) (heap); \
(heap)[_bzHeapPushIdx(h)] = (__VA_ARGS__); \
_bzHeapPush(h); \
} while(0)
#endif //BREEZE_HEAP_H

View File

@@ -0,0 +1,65 @@
#include "object_pool.h"
#include "../core/memory.h"
typedef struct BzObjectPool {
void *objects;
size_t stride;
size_t numObjects;
i32 firstFree;
} BzObjectPool;
BzObjectPool *bzObjectPoolCreate(const BzObjectPoolDesc *desc) {
BZ_ASSERT(desc->objectSize > 0);
// NOTE: Since object bits are used as a free list
// when not in use, we must ensure they can hold i32.
size_t stride = desc->objectSize;
if (stride < sizeof(i32)) {
stride = sizeof(i32);
}
size_t numBytes = sizeof(BzObjectPool) + desc->numObjects * stride;
BzObjectPool *pool = bzAlloc(numBytes);
*pool = (BzObjectPool) {
.objects=pool + 1,
.stride=stride,
.numObjects=desc->numObjects,
.firstFree=0,
};
// Link free list
for (size_t i = 0; i < pool->numObjects - 1; i++) {
i32 *object = bzObjectPoolGetObject(pool, i);
*object = (i32) (i + 1);
}
i32 *lastObject = bzObjectPoolGetObject(pool, pool->numObjects - 1);
*lastObject = -1;
return pool;
}
void bzObjectPoolDestroy(BzObjectPool *pool) {
bzFree(pool);
}
void *bzObjectPool(BzObjectPool *pool) {
if (pool->firstFree == -1)
return NULL;
i32 *object = bzObjectPoolGetObject(pool, pool->firstFree);
pool->firstFree = *object;
return object;
}
void *bzObjectPoolGetObject(BzObjectPool *pool, i32 idx) {
BZ_ASSERT(idx >= 0 && (size_t) idx < pool->numObjects);
return (void *) ((u8 *) pool->objects + idx * pool->stride);
}
i32 bzObjectPoolGetIdx(BzObjectPool *pool, void *object) {
size_t objectIdx = (size_t) object - (size_t) pool->objects;
return (i32) (objectIdx / pool->stride);
}
void bzObjectPoolRelease(BzObjectPool *pool, void *object) {
size_t objectIdx = bzObjectPoolGetIdx(pool, object);
BZ_ASSERT(objectIdx < pool->numObjects);
*(i32 *) ((u8 *) pool->objects + objectIdx * pool->stride) = pool->firstFree;
pool->firstFree = (i32) objectIdx;
}

View File

@@ -0,0 +1,21 @@
#ifndef BREEZE_OBJECT_POOL_H
#define BREEZE_OBJECT_POOL_H
#include "../defines.h"
typedef struct BzObjectPool BzObjectPool;
typedef struct BzObjectPoolDesc {
size_t objectSize;
size_t numObjects;
} BzObjectPoolDesc;
BzObjectPool *bzObjectPoolCreate(const BzObjectPoolDesc *desc);
void bzObjectPoolDestroy(BzObjectPool *pool);
void *bzObjectPool(BzObjectPool *pool);
void *bzObjectPoolGetObject(BzObjectPool *pool, i32 idx);
i32 bzObjectPoolGetIdx(BzObjectPool *pool, void *object);
void bzObjectPoolRelease(BzObjectPool *pool, void *object);
#endif //BREEZE_OBJECT_POOL_H

View File

@@ -0,0 +1,258 @@
#include "spatial_grid.h"
#include "../core/memory.h"
#include "array.h"
#include "object_pool.h"
#include <math.h>
typedef struct BzSpatialGrid {
BzSpatialGridID **cells;
BzObjectPool *entriesPool;
i32 width;
i32 height;
i32 cellWidth;
i32 cellHeight;
i32 userDataSize;
i32 queryCount;
} BzSpatialGrid;
static BzSpatialGridID **getCell(const BzSpatialGrid *grid, i32 x, i32 y);
BzSpatialGrid *bzSpatialGridCreate(const BzSpatialGridDesc *desc) {
i32 width = (desc->maxWidth + desc->cellWidth - 1) / desc->cellWidth;
i32 height = (desc->maxHeight + desc->cellHeight - 1) / desc->cellHeight;
size_t cellsNumBytes = width * height * sizeof(BzSpatialGridID *);
size_t numBytes = sizeof(BzSpatialGrid) + cellsNumBytes;
BzSpatialGrid *grid = bzAlloc(numBytes);
grid->width = width;
grid->height = height;
grid->cellWidth = desc->cellWidth;
grid->cellHeight = desc->cellHeight;
grid->userDataSize = desc->userDataSize;
grid->queryCount = 0;
grid->cells = (BzSpatialGridID **) (grid + 1);
for (i32 y = 0; y < grid->height; y++) {
for (i32 x = 0; x < grid->width; x++) {
BzSpatialGridID **cell = getCell(grid, x, y);
*cell = bzArrayCreate(BzSpatialGridID, 4);
}
}
grid->entriesPool = bzObjectPoolCreate(&(BzObjectPoolDesc) {
.objectSize=sizeof(BzSpatialGridEntry) + desc->userDataSize,
.numObjects=1024,
});
return grid;
}
void bzSpatialGridDestroy(BzSpatialGrid *grid) {
bzObjectPoolDestroy(grid->entriesPool);
grid->entriesPool = NULL;
for (i32 y = 0; y < grid->height; y++) {
for (i32 x = 0; x < grid->width; x++) {
BzSpatialGridID **cell = getCell(grid, x, y);
bzArrayDestroy(*cell);
*cell = NULL;
}
}
grid->cells = NULL;
bzFree(grid);
}
void *bzSpatialGridGetData(const BzSpatialGrid *grid, BzSpatialGridID id) {
return (u8 *)bzObjectPoolGetObject(grid->entriesPool, id) + sizeof(BzSpatialGridEntry);
}
static BzSpatialGridID **getCell(const BzSpatialGrid *grid, i32 x, i32 y) {
return grid->cells + y * grid->width + x;
}
static void insertCell(BzSpatialGrid *grid, BzSpatialGridID id, i32 x, i32 y) {
BzSpatialGridID **cell = getCell(grid, x, y);
bzArrayPush(*cell, id);
}
static void removeCell(BzSpatialGrid *grid, BzSpatialGridID id, i32 x, i32 y) {
BzSpatialGridID **cell = getCell(grid, x, y);
i32 i;
for (i = 0; i < bzArraySize(*cell); i++) {
if (id == (*cell)[i]) {
break;
}
}
if (i < bzArraySize(*cell)) {
BzSpatialGridID lastID = bzArrayPop(*cell);
(*cell)[i] = lastID;
}
}
static BzSpatialGridIndex calculateGridIndex(BzSpatialGrid *grid, f32 posX, f32 posY, f32 sizeX, f32 sizeY) {
i32 minX = (i32) floorf(posX);
i32 minY = (i32) floorf(posY);
i32 maxX = (i32) ceilf(posX + sizeX);
i32 maxY = (i32) ceilf(posY + sizeY);
BzSpatialGridIndex index = {
(i16) (minX / grid->cellWidth),
(i16) (minY / grid->cellHeight),
(i16) (maxX / grid->cellWidth),
(i16) (maxY / grid->cellHeight),
};
if (index.minX < 0) index.minX = 0;
if (index.minY < 0) index.minY = 0;
if (index.maxX >= grid->width) index.maxX = grid->width - 1;
if (index.maxY >= grid->height) index.maxY = grid->height - 1;
return index;
}
BzSpatialGridID bzSpatialGridInsert(BzSpatialGrid *grid, void *data, f32 posX, f32 posY, f32 sizeX, f32 sizeY) {
BzSpatialGridEntry *entry = bzObjectPool(grid->entriesPool);
BZ_ASSERT(entry);
BzSpatialGridIndex index = calculateGridIndex(grid, posX, posY, sizeX, sizeY);
BzSpatialGridID id = bzObjectPoolGetIdx(grid->entriesPool, entry);
for (i32 y = index.minY; y <= index.maxY; y++) {
for (i32 x = index.minX; x <= index.maxX; x++) {
insertCell(grid, id, x, y);
}
}
entry->id = id;
entry->queryIdx = 0;
entry->index = index;
if (grid->userDataSize) {
bzMemCpy(entry + 1, data, grid->userDataSize);
}
return id;
}
static bool indexContains(BzSpatialGridIndex index, i32 x, i32 y) {
return x >= index.minX && x <= index.maxX && y >= index.minY && y <= index.maxY;
}
void bzSpatialGridUpdate(BzSpatialGrid *grid, BzSpatialGridID id, f32 posX, f32 posY, f32 sizeX, f32 sizeY) {
BzSpatialGridEntry *entry = bzObjectPoolGetObject(grid->entriesPool, id);
BZ_ASSERT(entry && entry->id == id);
BzSpatialGridIndex oldIndex = entry->index;
BzSpatialGridIndex newIndex = calculateGridIndex(grid, posX, posY, sizeX, sizeY);
if (oldIndex.minX == newIndex.minX &&
oldIndex.minY == newIndex.minY &&
oldIndex.maxX == newIndex.maxX &&
oldIndex.maxY == newIndex.maxY) {
return;
}
for (i32 y = oldIndex.minY; y <= oldIndex.maxY; y++) {
for (i32 x = oldIndex.minX; x <= oldIndex.maxX; x++) {
bool inNew = indexContains(newIndex, x, y);
if (!inNew) removeCell(grid, id, x, y);
}
}
for (i32 y = newIndex.minY; y <= newIndex.maxY; y++) {
for (i32 x = newIndex.minX; x <= newIndex.maxX; x++) {
bool inOld = indexContains(oldIndex, x, y);
if (!inOld) insertCell(grid, id, x, y);
}
}
entry->index = newIndex;
}
void bzSpatialGridRemove(BzSpatialGrid *grid, BzSpatialGridID id) {
BzSpatialGridEntry *entry = bzObjectPoolGetObject(grid->entriesPool, id);
BZ_ASSERT(entry && entry->id == id);
BzSpatialGridIndex index = entry->index;
for (i32 y = index.minY; y <= index.maxY; y++) {
for (i32 x = index.minX; x <= index.maxX; x++) {
removeCell(grid, id, x, y);
}
}
bzObjectPoolRelease(grid->entriesPool, entry);
}
BzSpatialGridIter bzSpatialGridIter(BzSpatialGrid *grid, f32 posX, f32 posY, f32 sizeX, f32 sizeY) {
BzSpatialGridIndex index = calculateGridIndex(grid, posX, posY, sizeX, sizeY);
BzSpatialGridIter it = {
.index=index,
._grid=grid,
._queryIdx=grid->queryCount++,
._x=index.minX,
._y=index.minY,
._cellIdx=0
};
return it;
}
bool bzSpatialGridQueryNext(BzSpatialGridIter *it) {
BzSpatialGridID **cell = getCell(it->_grid, it->_x, it->_y);
while (it->_cellIdx >= bzArraySize(*cell)) {
it->_cellIdx = 0;
it->_x++;
if (it->_x > it->index.maxX) {
it->_x = it->index.minX;
it->_y++;
if (it->_y > it->index.maxY) {
return false;
}
}
cell = getCell(it->_grid, it->_x, it->_y);
}
BzSpatialGridID id = bzArrayGet(*cell, it->_cellIdx);
BzSpatialGridEntry *entry = bzObjectPoolGetObject(it->_grid->entriesPool, id);
BZ_ASSERT(entry && id == entry->id);
it->entry = *entry;
it->data = (void *) (entry + 1);
it->_cellIdx++;
return true;
}
#include <raylib.h>
#include <stdio.h>
void bzSpatialGridDrawDebugGrid(const BzSpatialGrid *grid) {
// Draw lines
i32 posX = 0;
i32 posY = 0;
for (i32 x = 0; x <= grid->width; x++) {
DrawLine(posX, 0, posX, grid->height * grid->cellHeight, BLACK);
posX += grid->cellWidth;
}
for (i32 y = 0; y <= grid->height; y++) {
DrawLine(0, posY, grid->width * grid->cellWidth, posY, BLACK);
posY += grid->cellHeight;
}
posX = 0;
posY = 0;
for (i32 y = 0; y < grid->height; y++) {
for (i32 x = 0; x < grid->width; x++) {
BzSpatialGridID **cell = getCell(grid, x, y);
i32 numEntries = bzArraySize(*cell);
Color color = WHITE;
i32 tint = numEntries;
if (tint > 10) tint = 10;
tint = 10 - tint;
color.g = 25 * tint;
color.b = 25 * tint;
char buf[8];
snprintf(buf, sizeof(buf), "%d", numEntries);
DrawText(buf, posX + 1, posY + 1, 6, color);
posX += grid->cellWidth;
}
posX = 0;
posY += grid->cellHeight;
}
}

View File

@@ -0,0 +1,56 @@
#ifndef BREEZE_SPATIAL_GRID_H
#define BREEZE_SPATIAL_GRID_H
#include "../defines.h"
typedef struct BzSpatialGridIndex {
i32 minX, minY;
i32 maxX, maxY;
} BzSpatialGridIndex;
typedef i32 BzSpatialGridID;
typedef struct BzSpatialGridEntry {
i32 queryIdx;
BzSpatialGridID id;
BzSpatialGridIndex index;
} BzSpatialGridEntry;
typedef struct BzSpatialGrid BzSpatialGrid;
typedef struct BzSpatialGridIter {
BzSpatialGridEntry entry;
void *data;
BzSpatialGridIndex index;
BzSpatialGrid *_grid;
i32 _queryIdx;
i32 _x;
i32 _y;
i32 _cellIdx;
} BzSpatialGridIter;
typedef struct BzSpatialGridDesc {
i32 maxWidth;
i32 maxHeight;
i32 cellWidth;
i32 cellHeight;
i32 userDataSize;
} BzSpatialGridDesc;
BzSpatialGrid *bzSpatialGridCreate(const BzSpatialGridDesc *desc);
void bzSpatialGridDestroy(BzSpatialGrid *grid);
void *bzSpatialGridGetData(const BzSpatialGrid *grid, BzSpatialGridID id);
BzSpatialGridID bzSpatialGridInsert(BzSpatialGrid *grid, void *data, f32 posX, f32 posY, f32 sizeX, f32 sizeY);
void bzSpatialGridUpdate(BzSpatialGrid *grid, BzSpatialGridID id, f32 posX, f32 posY, f32 sizeX, f32 sizeY);
void bzSpatialGridRemove(BzSpatialGrid *grid, BzSpatialGridID id);
BzSpatialGridIter bzSpatialGridIter(BzSpatialGrid *grid, f32 posX, f32 posY, f32 sizeX, f32 sizeY);
bool bzSpatialGridQueryNext(BzSpatialGridIter *it);
void bzSpatialGridDrawDebugGrid(const BzSpatialGrid *grid);
#endif //BREEZE_SPATIAL_GRID_H

View File

@@ -0,0 +1,20 @@
#ifndef BREEZE_STRING_H
#define BREEZE_STRING_H
#include "../defines.h"
typedef u32 (*BzStringHashFunc)(const char *str);
// djb2 hash algorithm
// From: https://stackoverflow.com/questions/7666509/hash-function-for-string
// http://www.cse.yorku.ca/~oz/hash.html
static u32 bzStringDefaultHash(const char *str) {
u32 hash = 5381;
int c;
while ((c = (int) *str++)) {
hash = ((hash << 5) + hash) + c; /* hash + 33 + c */
}
return hash;
}
#endif //BREEZE_STRING_H

View File

@@ -0,0 +1,47 @@
#include "tokenizer.h"
Tokenizer tokenizerCreate(const char *source) {
int line = source ? 1 : 0;
return (Tokenizer) {source, line};
}
static bool isWhitespace(char c) {
switch (c) {
case '\n':
case ' ':
case '\r':
case '\t':
return true;
default:
return false;
}
}
static void skipWhitespace(Tokenizer *tokenizer) {
while (1) {
char c = *tokenizer->current;
if (c == '\n') {
tokenizer->line++;
continue;
}
if (!isWhitespace(c))
break;
}
}
Token tokenizerScan(Tokenizer *tokenizer) {
skipWhitespace(tokenizer);
Token token = {tokenizer->current, 0, tokenizer->line};
if (*tokenizer->current == '\0') {
token.start = NULL;
return token;
}
while (!isWhitespace(*tokenizer->current) || *tokenizer->current != '\0') {
tokenizer->current++;
}
token.length = (int) (tokenizer->current - token.start);
return token;
}

View File

@@ -0,0 +1,20 @@
#ifndef BREEZE_TOKENIZER_H
#define BREEZE_TOKENIZER_H
#include "../defines.h"
typedef struct Token {
const char *start;
int length;
int line;
} Token;
typedef struct Tokenizer {
const char *current;
int line;
} Tokenizer;
Tokenizer tokenizerCreate(const char *source);
Token tokenizerScan(Tokenizer *tokenizer);
#endif //BREEZE_TOKENIZER_H