Initial work on physics

This commit is contained in:
2024-02-29 16:17:55 +08:00
parent 8e6b6cec9b
commit e8f0b0c63a
9 changed files with 179 additions and 11 deletions

21
util/assert.h Normal file
View File

@ -0,0 +1,21 @@
#pragma once
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
inline static void __panicf(const char *fmt, ...) {
va_list args;
va_start(args, fmt);
int ret = vfprintf(stderr, fmt, args);
va_end(args);
abort();
}
#define ASSERT(expr) \
do { \
if (!(expr)) \
__panicf("Assertion failed: " __FILE__ ":%d[%s]\n Expression: %s", __LINE__, __func__, #expr); \
} while (0)

View File

@ -36,6 +36,18 @@ void *vector_Push(vector_Vector *vec, const void *data) {
return vec->data + vec->size - vec->objectSize;
}
bool vector_Pop(vector_Vector *vec, void *out_data) {
if (!vector_Size(vec))
return false;
if (out_data)
memcpy(out_data, vec->data + (vec->size - vec->objectSize), vec->objectSize);
vector_Resize(vec, vector_Size(vec) - 1);
return true;
}
void vector_Append(vector_Vector *vec, const void *data, uintptr_t n) {
uintptr_t oldsize = vec->size, addsize = vec->objectSize * n;
vector_Resize(vec, oldsize + addsize);

View File

@ -1,6 +1,7 @@
#pragma once
#include <stdint.h>
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
@ -27,6 +28,14 @@ void vector_Destroy(vector_Vector *vec);
// Returns a pointer to data.
void *vector_Push(vector_Vector *vec, const void *data);
// Pop pops one object at the back of the vector.
//
// If out_data is not NULL, the popped data is copied.
//
// If the vector is already empty, return false.
// Returns true otherwise.
bool vector_Pop(vector_Vector *vec, void *out_data);
// Append pushes multiple objects at the back of the buffer.
//
// If data is NULL, the data is zeroed.