forked from tas/major_tom
Initial commit
This commit is contained in:
53
src/util/darray.h
Normal file
53
src/util/darray.h
Normal file
@@ -0,0 +1,53 @@
|
||||
#ifndef JNR_DARRAY_H
|
||||
#define JNR_DARRAY_H
|
||||
|
||||
/*
|
||||
* Simple type-safe dynamic array using macros.
|
||||
*
|
||||
* Usage:
|
||||
* DArray(int) nums = {0};
|
||||
* da_push(&nums, 42);
|
||||
* da_push(&nums, 99);
|
||||
* for (int i = 0; i < nums.count; i++) printf("%d\n", nums.items[i]);
|
||||
* da_free(&nums);
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define DArray(T) struct { T *items; int count; int capacity; }
|
||||
|
||||
#define da_push(arr, val) do { \
|
||||
if ((arr)->count >= (arr)->capacity) { \
|
||||
(arr)->capacity = (arr)->capacity ? (arr)->capacity * 2 : 8; \
|
||||
(arr)->items = realloc((arr)->items, \
|
||||
(arr)->capacity * sizeof(*(arr)->items)); \
|
||||
} \
|
||||
(arr)->items[(arr)->count++] = (val); \
|
||||
} while (0)
|
||||
|
||||
#define da_pop(arr) ((arr)->items[--(arr)->count])
|
||||
|
||||
#define da_clear(arr) ((arr)->count = 0)
|
||||
|
||||
#define da_free(arr) do { \
|
||||
free((arr)->items); \
|
||||
(arr)->items = NULL; \
|
||||
(arr)->count = 0; \
|
||||
(arr)->capacity = 0; \
|
||||
} while (0)
|
||||
|
||||
#define da_remove(arr, idx) do { \
|
||||
if ((idx) < (arr)->count - 1) { \
|
||||
memmove(&(arr)->items[(idx)], &(arr)->items[(idx) + 1], \
|
||||
((arr)->count - (idx) - 1) * sizeof(*(arr)->items)); \
|
||||
} \
|
||||
(arr)->count--; \
|
||||
} while (0)
|
||||
|
||||
/* Swap-remove: O(1) but doesn't preserve order */
|
||||
#define da_remove_fast(arr, idx) do { \
|
||||
(arr)->items[(idx)] = (arr)->items[--(arr)->count]; \
|
||||
} while (0)
|
||||
|
||||
#endif /* JNR_DARRAY_H */
|
||||
82
src/util/vec2.h
Normal file
82
src/util/vec2.h
Normal file
@@ -0,0 +1,82 @@
|
||||
#ifndef JNR_VEC2_H
|
||||
#define JNR_VEC2_H
|
||||
|
||||
#include <math.h>
|
||||
|
||||
typedef struct Vec2 {
|
||||
float x, y;
|
||||
} Vec2;
|
||||
|
||||
static inline Vec2 vec2(float x, float y) {
|
||||
return (Vec2){ x, y };
|
||||
}
|
||||
|
||||
static inline Vec2 vec2_zero(void) {
|
||||
return (Vec2){ 0.0f, 0.0f };
|
||||
}
|
||||
|
||||
static inline Vec2 vec2_add(Vec2 a, Vec2 b) {
|
||||
return (Vec2){ a.x + b.x, a.y + b.y };
|
||||
}
|
||||
|
||||
static inline Vec2 vec2_sub(Vec2 a, Vec2 b) {
|
||||
return (Vec2){ a.x - b.x, a.y - b.y };
|
||||
}
|
||||
|
||||
static inline Vec2 vec2_scale(Vec2 v, float s) {
|
||||
return (Vec2){ v.x * s, v.y * s };
|
||||
}
|
||||
|
||||
static inline float vec2_dot(Vec2 a, Vec2 b) {
|
||||
return a.x * b.x + a.y * b.y;
|
||||
}
|
||||
|
||||
static inline float vec2_len(Vec2 v) {
|
||||
return sqrtf(v.x * v.x + v.y * v.y);
|
||||
}
|
||||
|
||||
static inline float vec2_len_sq(Vec2 v) {
|
||||
return v.x * v.x + v.y * v.y;
|
||||
}
|
||||
|
||||
static inline Vec2 vec2_norm(Vec2 v) {
|
||||
float len = vec2_len(v);
|
||||
if (len < 0.0001f) return vec2_zero();
|
||||
return vec2_scale(v, 1.0f / len);
|
||||
}
|
||||
|
||||
static inline Vec2 vec2_lerp(Vec2 a, Vec2 b, float t) {
|
||||
return (Vec2){
|
||||
a.x + (b.x - a.x) * t,
|
||||
a.y + (b.y - a.y) * t
|
||||
};
|
||||
}
|
||||
|
||||
static inline float vec2_dist(Vec2 a, Vec2 b) {
|
||||
return vec2_len(vec2_sub(a, b));
|
||||
}
|
||||
|
||||
static inline Vec2 vec2_clamp(Vec2 v, Vec2 min, Vec2 max) {
|
||||
Vec2 r;
|
||||
r.x = v.x < min.x ? min.x : (v.x > max.x ? max.x : v.x);
|
||||
r.y = v.y < min.y ? min.y : (v.y > max.y ? max.y : v.y);
|
||||
return r;
|
||||
}
|
||||
|
||||
static inline float clampf(float v, float min, float max) {
|
||||
return v < min ? min : (v > max ? max : v);
|
||||
}
|
||||
|
||||
static inline float lerpf(float a, float b, float t) {
|
||||
return a + (b - a) * t;
|
||||
}
|
||||
|
||||
static inline float signf(float v) {
|
||||
return (v > 0.0f) - (v < 0.0f);
|
||||
}
|
||||
|
||||
static inline float absf(float v) {
|
||||
return v < 0.0f ? -v : v;
|
||||
}
|
||||
|
||||
#endif /* JNR_VEC2_H */
|
||||
Reference in New Issue
Block a user