move from imgui to CLAY
This commit is contained in:
53
src/base/base_arena.cpp
Normal file
53
src/base/base_arena.cpp
Normal file
@@ -0,0 +1,53 @@
|
||||
#include "base/base_arena.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
Arena *arena_alloc(U64 cap) {
|
||||
U8 *mem = (U8 *)malloc(sizeof(Arena) + cap);
|
||||
if (!mem) return nullptr;
|
||||
Arena *arena = (Arena *)mem;
|
||||
arena->base = mem + sizeof(Arena);
|
||||
arena->pos = 0;
|
||||
arena->cap = cap;
|
||||
return arena;
|
||||
}
|
||||
|
||||
void arena_release(Arena *arena) {
|
||||
if (arena) free(arena);
|
||||
}
|
||||
|
||||
void *arena_push(Arena *arena, U64 size) {
|
||||
U64 aligned = AlignPow2(size, 8);
|
||||
if (arena->pos + aligned > arena->cap) {
|
||||
Assert(!"Arena overflow");
|
||||
return nullptr;
|
||||
}
|
||||
void *result = arena->base + arena->pos;
|
||||
arena->pos += aligned;
|
||||
MemoryZero(result, aligned);
|
||||
return result;
|
||||
}
|
||||
|
||||
void *arena_push_no_zero(Arena *arena, U64 size) {
|
||||
U64 aligned = AlignPow2(size, 8);
|
||||
if (arena->pos + aligned > arena->cap) {
|
||||
Assert(!"Arena overflow");
|
||||
return nullptr;
|
||||
}
|
||||
void *result = arena->base + arena->pos;
|
||||
arena->pos += aligned;
|
||||
return result;
|
||||
}
|
||||
|
||||
U64 arena_pos(Arena *arena) {
|
||||
return arena->pos;
|
||||
}
|
||||
|
||||
void arena_pop_to(Arena *arena, U64 pos) {
|
||||
if (pos < arena->pos) {
|
||||
arena->pos = pos;
|
||||
}
|
||||
}
|
||||
|
||||
void arena_clear(Arena *arena) {
|
||||
arena->pos = 0;
|
||||
}
|
||||
44
src/base/base_arena.h
Normal file
44
src/base/base_arena.h
Normal file
@@ -0,0 +1,44 @@
|
||||
#pragma once
|
||||
// base_arena.h - Linear arena allocator
|
||||
// Simplified from raddebugger's virtual-memory-backed arena to a malloc-based one.
|
||||
// Suitable for per-frame scratch allocations and persistent state.
|
||||
|
||||
#include "base/base_core.h"
|
||||
|
||||
////////////////////////////////
|
||||
// Arena type
|
||||
|
||||
struct Arena {
|
||||
U8 *base;
|
||||
U64 pos;
|
||||
U64 cap;
|
||||
};
|
||||
|
||||
// Temporary scope (save/restore position)
|
||||
struct Temp {
|
||||
Arena *arena;
|
||||
U64 pos;
|
||||
};
|
||||
|
||||
////////////////////////////////
|
||||
// Arena functions
|
||||
|
||||
Arena *arena_alloc(U64 cap);
|
||||
void arena_release(Arena *arena);
|
||||
void *arena_push(Arena *arena, U64 size);
|
||||
void *arena_push_no_zero(Arena *arena, U64 size);
|
||||
U64 arena_pos(Arena *arena);
|
||||
void arena_pop_to(Arena *arena, U64 pos);
|
||||
void arena_clear(Arena *arena);
|
||||
|
||||
////////////////////////////////
|
||||
// Temporary scope helpers
|
||||
|
||||
inline Temp temp_begin(Arena *arena) { return {arena, arena->pos}; }
|
||||
inline void temp_end(Temp temp) { arena_pop_to(temp.arena, temp.pos); }
|
||||
|
||||
////////////////////////////////
|
||||
// Push helper macros
|
||||
|
||||
#define push_array(arena, T, count) ((T *)arena_push((arena), sizeof(T) * (count)))
|
||||
#define push_array_no_zero(arena, T, count) ((T *)arena_push_no_zero((arena), sizeof(T) * (count)))
|
||||
176
src/base/base_core.h
Normal file
176
src/base/base_core.h
Normal file
@@ -0,0 +1,176 @@
|
||||
#pragma once
|
||||
// base_core.h - Fundamental types, macros, and linked list helpers
|
||||
// Inspired by raddebugger's base_core.h
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdarg.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
|
||||
////////////////////////////////
|
||||
// Codebase keywords
|
||||
|
||||
#define internal static
|
||||
#define global static
|
||||
#define local_persist static
|
||||
|
||||
////////////////////////////////
|
||||
// Base types
|
||||
|
||||
typedef uint8_t U8;
|
||||
typedef uint16_t U16;
|
||||
typedef uint32_t U32;
|
||||
typedef uint64_t U64;
|
||||
typedef int8_t S8;
|
||||
typedef int16_t S16;
|
||||
typedef int32_t S32;
|
||||
typedef int64_t S64;
|
||||
typedef S32 B32;
|
||||
typedef float F32;
|
||||
typedef double F64;
|
||||
|
||||
////////////////////////////////
|
||||
// Limits
|
||||
|
||||
#define max_U8 0xFF
|
||||
#define max_U16 0xFFFF
|
||||
#define max_U32 0xFFFFFFFF
|
||||
#define max_U64 0xFFFFFFFFFFFFFFFFull
|
||||
#define max_S8 0x7F
|
||||
#define max_S16 0x7FFF
|
||||
#define max_S32 0x7FFFFFFF
|
||||
#define max_S64 0x7FFFFFFFFFFFFFFFll
|
||||
|
||||
////////////////////////////////
|
||||
// Units
|
||||
|
||||
#define KB(n) (((U64)(n)) << 10)
|
||||
#define MB(n) (((U64)(n)) << 20)
|
||||
#define GB(n) (((U64)(n)) << 30)
|
||||
|
||||
////////////////////////////////
|
||||
// Clamps, Mins, Maxes
|
||||
|
||||
#define Min(A, B) (((A) < (B)) ? (A) : (B))
|
||||
#define Max(A, B) (((A) > (B)) ? (A) : (B))
|
||||
#define ClampTop(A, X) Min(A, X)
|
||||
#define ClampBot(X, B) Max(X, B)
|
||||
#define Clamp(A, X, B) (((X) < (A)) ? (A) : ((X) > (B)) ? (B) : (X))
|
||||
|
||||
////////////////////////////////
|
||||
// Alignment / Sizing
|
||||
|
||||
#define AlignPow2(x, b) (((x) + (b) - 1) & (~((b) - 1)))
|
||||
#define AlignDownPow2(x, b) ((x) & (~((b) - 1)))
|
||||
#define ArrayCount(a) (sizeof(a) / sizeof((a)[0]))
|
||||
|
||||
////////////////////////////////
|
||||
// Memory macros
|
||||
|
||||
#define MemoryCopy(dst, src, size) memmove((dst), (src), (size))
|
||||
#define MemorySet(dst, byte, size) memset((dst), (byte), (size))
|
||||
#define MemoryCompare(a, b, size) memcmp((a), (b), (size))
|
||||
#define MemoryZero(s, z) memset((s), 0, (z))
|
||||
#define MemoryZeroStruct(s) MemoryZero((s), sizeof(*(s)))
|
||||
#define MemoryZeroArray(a) MemoryZero((a), sizeof(a))
|
||||
#define MemoryMatch(a, b, z) (MemoryCompare((a), (b), (z)) == 0)
|
||||
|
||||
////////////////////////////////
|
||||
// Pointer / integer casts
|
||||
|
||||
#define IntFromPtr(ptr) ((U64)(ptr))
|
||||
#define PtrFromInt(i) (void *)(i)
|
||||
#define OffsetOf(T, m) IntFromPtr(&(((T *)0)->m))
|
||||
|
||||
////////////////////////////////
|
||||
// Member access
|
||||
|
||||
#define CastFromMember(T, m, ptr) (T *)(((U8 *)(ptr)) - OffsetOf(T, m))
|
||||
|
||||
////////////////////////////////
|
||||
// For-Loop construct macros
|
||||
|
||||
#define DeferLoop(begin, end) for (int _i_ = ((begin), 0); !_i_; _i_ += 1, (end))
|
||||
#define DeferLoopChecked(begin, end) for (int _i_ = 2 * !(begin); (_i_ == 2 ? ((end), 0) : !_i_); _i_ += 1, (end))
|
||||
|
||||
#define EachIndex(it, count) (U64 it = 0; it < (count); it += 1)
|
||||
#define EachElement(it, array) (U64 it = 0; it < ArrayCount(array); it += 1)
|
||||
|
||||
////////////////////////////////
|
||||
// Glue / Stringify
|
||||
|
||||
#define Stringify_(S) #S
|
||||
#define Stringify(S) Stringify_(S)
|
||||
#define Glue_(A, B) A##B
|
||||
#define Glue(A, B) Glue_(A, B)
|
||||
|
||||
#define Swap(T, a, b) do { T t__ = a; a = b; b = t__; } while (0)
|
||||
|
||||
////////////////////////////////
|
||||
// Assert
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
# define Trap() __debugbreak()
|
||||
#elif defined(__clang__) || defined(__GNUC__)
|
||||
# define Trap() __builtin_trap()
|
||||
#else
|
||||
# define Trap() (*(volatile int *)0 = 0)
|
||||
#endif
|
||||
|
||||
#define AssertAlways(x) do { if (!(x)) { Trap(); } } while (0)
|
||||
|
||||
#ifdef _DEBUG
|
||||
# define Assert(x) AssertAlways(x)
|
||||
#else
|
||||
# define Assert(x) (void)(x)
|
||||
#endif
|
||||
|
||||
#define InvalidPath Assert(!"Invalid Path!")
|
||||
#define NotImplemented Assert(!"Not Implemented!")
|
||||
|
||||
////////////////////////////////
|
||||
// Linked list macros
|
||||
// Nil-aware doubly-linked-list operations
|
||||
|
||||
#define CheckNil(nil, p) ((p) == 0 || (p) == nil)
|
||||
#define SetNil(nil, p) ((p) = nil)
|
||||
|
||||
// Doubly-linked-list (with nil support)
|
||||
#define DLLInsert_NPZ(nil, f, l, p, n, next, prev) \
|
||||
(CheckNil(nil, f) ? \
|
||||
((f) = (l) = (n), SetNil(nil, (n)->next), SetNil(nil, (n)->prev)) : \
|
||||
CheckNil(nil, p) ? \
|
||||
((n)->next = (f), (f)->prev = (n), (f) = (n), SetNil(nil, (n)->prev)) : \
|
||||
((p) == (l)) ? \
|
||||
((l)->next = (n), (n)->prev = (l), (l) = (n), SetNil(nil, (n)->next)) : \
|
||||
(((!CheckNil(nil, p) && CheckNil(nil, (p)->next)) ? (0) : ((p)->next->prev = (n))), \
|
||||
((n)->next = (p)->next), ((p)->next = (n)), ((n)->prev = (p))))
|
||||
|
||||
#define DLLPushBack_NPZ(nil, f, l, n, next, prev) DLLInsert_NPZ(nil, f, l, l, n, next, prev)
|
||||
#define DLLPushFront_NPZ(nil, f, l, n, next, prev) DLLInsert_NPZ(nil, l, f, f, n, prev, next)
|
||||
|
||||
#define DLLRemove_NPZ(nil, f, l, n, next, prev) \
|
||||
(((n) == (f) ? (f) = (n)->next : (0)), \
|
||||
((n) == (l) ? (l) = (l)->prev : (0)), \
|
||||
(CheckNil(nil, (n)->prev) ? (0) : ((n)->prev->next = (n)->next)), \
|
||||
(CheckNil(nil, (n)->next) ? (0) : ((n)->next->prev = (n)->prev)))
|
||||
|
||||
// Convenience wrappers using 0 as nil
|
||||
#define DLLPushBack(f, l, n) DLLPushBack_NPZ(0, f, l, n, next, prev)
|
||||
#define DLLPushFront(f, l, n) DLLPushFront_NPZ(0, f, l, n, next, prev)
|
||||
#define DLLRemove(f, l, n) DLLRemove_NPZ(0, f, l, n, next, prev)
|
||||
|
||||
// Singly-linked queue (doubly-headed)
|
||||
#define SLLQueuePush_NZ(nil, f, l, n, next) \
|
||||
(CheckNil(nil, f) ? \
|
||||
((f) = (l) = (n), SetNil(nil, (n)->next)) : \
|
||||
((l)->next = (n), (l) = (n), SetNil(nil, (n)->next)))
|
||||
|
||||
#define SLLQueuePush(f, l, n) SLLQueuePush_NZ(0, f, l, n, next)
|
||||
#define SLLQueuePushFront(f, l, n) (((n)->next = (f)), ((f) = (n)))
|
||||
#define SLLQueuePop(f, l) ((f) == (l) ? ((f) = 0, (l) = 0) : ((f) = (f)->next))
|
||||
|
||||
// Singly-linked stack
|
||||
#define SLLStackPush(f, n) ((n)->next = (f), (f) = (n))
|
||||
#define SLLStackPop(f) ((f) = (f)->next)
|
||||
3
src/base/base_inc.cpp
Normal file
3
src/base/base_inc.cpp
Normal file
@@ -0,0 +1,3 @@
|
||||
// base_inc.cpp - Unity build for the base layer
|
||||
#include "base/base_arena.cpp"
|
||||
#include "base/base_strings.cpp"
|
||||
8
src/base/base_inc.h
Normal file
8
src/base/base_inc.h
Normal file
@@ -0,0 +1,8 @@
|
||||
#pragma once
|
||||
// base_inc.h - Umbrella include for the base layer
|
||||
// Include this one header to get all base types.
|
||||
|
||||
#include "base/base_core.h"
|
||||
#include "base/base_arena.h"
|
||||
#include "base/base_math.h"
|
||||
#include "base/base_strings.h"
|
||||
110
src/base/base_math.h
Normal file
110
src/base/base_math.h
Normal file
@@ -0,0 +1,110 @@
|
||||
#pragma once
|
||||
// base_math.h - Vector, range, and color types
|
||||
// Inspired by raddebugger's base_math.h
|
||||
|
||||
#include "base/base_core.h"
|
||||
|
||||
////////////////////////////////
|
||||
// Axis enum
|
||||
|
||||
enum Axis2 {
|
||||
Axis2_X = 0,
|
||||
Axis2_Y = 1,
|
||||
Axis2_COUNT,
|
||||
};
|
||||
|
||||
enum Side {
|
||||
Side_Min = 0,
|
||||
Side_Max = 1,
|
||||
Side_COUNT,
|
||||
};
|
||||
|
||||
enum Corner {
|
||||
Corner_00 = 0, // top-left
|
||||
Corner_01 = 1, // top-right
|
||||
Corner_10 = 2, // bottom-left
|
||||
Corner_11 = 3, // bottom-right
|
||||
Corner_COUNT,
|
||||
};
|
||||
|
||||
////////////////////////////////
|
||||
// Vector types
|
||||
|
||||
struct Vec2F32 { F32 x, y; };
|
||||
struct Vec2S32 { S32 x, y; };
|
||||
struct Vec3F32 { F32 x, y, z; };
|
||||
struct Vec4F32 { F32 x, y, z, w; };
|
||||
|
||||
////////////////////////////////
|
||||
// Range types
|
||||
|
||||
struct Rng1F32 { F32 min, max; };
|
||||
struct Rng1S64 { S64 min, max; };
|
||||
struct Rng2F32 { Vec2F32 p0, p1; };
|
||||
|
||||
////////////////////////////////
|
||||
// Constructors
|
||||
|
||||
static inline Vec2F32 v2f32(F32 x, F32 y) { return {x, y}; }
|
||||
static inline Vec2S32 v2s32(S32 x, S32 y) { return {x, y}; }
|
||||
static inline Vec3F32 v3f32(F32 x, F32 y, F32 z) { return {x, y, z}; }
|
||||
static inline Vec4F32 v4f32(F32 x, F32 y, F32 z, F32 w) { return {x, y, z, w}; }
|
||||
static inline Rng1F32 rng1f32(F32 min, F32 max) { return {min, max}; }
|
||||
static inline Rng1S64 rng1s64(S64 min, S64 max) { return {min, max}; }
|
||||
static inline Rng2F32 rng2f32(Vec2F32 p0, Vec2F32 p1) { return {p0, p1}; }
|
||||
static inline Rng2F32 rng2f32p(F32 x0, F32 y0, F32 x1, F32 y1) { return {{x0, y0}, {x1, y1}}; }
|
||||
|
||||
////////////////////////////////
|
||||
// Vec2F32 operations
|
||||
|
||||
static inline Vec2F32 add_2f32(Vec2F32 a, Vec2F32 b) { return {a.x + b.x, a.y + b.y}; }
|
||||
static inline Vec2F32 sub_2f32(Vec2F32 a, Vec2F32 b) { return {a.x - b.x, a.y - b.y}; }
|
||||
static inline Vec2F32 mul_2f32(Vec2F32 a, Vec2F32 b) { return {a.x * b.x, a.y * b.y}; }
|
||||
static inline Vec2F32 scale_2f32(Vec2F32 v, F32 s) { return {v.x * s, v.y * s}; }
|
||||
|
||||
// Axis-indexed access
|
||||
static inline F32 v2f32_axis(Vec2F32 v, Axis2 a) { return a == Axis2_X ? v.x : v.y; }
|
||||
static inline void v2f32_set_axis(Vec2F32 *v, Axis2 a, F32 val) {
|
||||
if (a == Axis2_X) v->x = val; else v->y = val;
|
||||
}
|
||||
|
||||
////////////////////////////////
|
||||
// Vec4F32 operations
|
||||
|
||||
static inline Vec4F32 add_4f32(Vec4F32 a, Vec4F32 b) { return {a.x+b.x, a.y+b.y, a.z+b.z, a.w+b.w}; }
|
||||
static inline Vec4F32 scale_4f32(Vec4F32 v, F32 s) { return {v.x*s, v.y*s, v.z*s, v.w*s}; }
|
||||
static inline Vec4F32 lerp_4f32(Vec4F32 a, Vec4F32 b, F32 t) {
|
||||
return {a.x + (b.x - a.x)*t, a.y + (b.y - a.y)*t, a.z + (b.z - a.z)*t, a.w + (b.w - a.w)*t};
|
||||
}
|
||||
|
||||
////////////////////////////////
|
||||
// Rng2F32 operations
|
||||
|
||||
static inline F32 rng2f32_width(Rng2F32 r) { return r.p1.x - r.p0.x; }
|
||||
static inline F32 rng2f32_height(Rng2F32 r) { return r.p1.y - r.p0.y; }
|
||||
static inline Vec2F32 rng2f32_dim(Rng2F32 r) { return {r.p1.x - r.p0.x, r.p1.y - r.p0.y}; }
|
||||
static inline Vec2F32 rng2f32_center(Rng2F32 r) { return {(r.p0.x + r.p1.x)*0.5f, (r.p0.y + r.p1.y)*0.5f}; }
|
||||
static inline B32 rng2f32_contains(Rng2F32 r, Vec2F32 p) {
|
||||
return p.x >= r.p0.x && p.x <= r.p1.x && p.y >= r.p0.y && p.y <= r.p1.y;
|
||||
}
|
||||
static inline Rng2F32 rng2f32_pad(Rng2F32 r, F32 p) {
|
||||
return {{r.p0.x - p, r.p0.y - p}, {r.p1.x + p, r.p1.y + p}};
|
||||
}
|
||||
static inline Rng2F32 rng2f32_shift(Rng2F32 r, Vec2F32 v) {
|
||||
return {{r.p0.x + v.x, r.p0.y + v.y}, {r.p1.x + v.x, r.p1.y + v.y}};
|
||||
}
|
||||
static inline Rng2F32 rng2f32_intersect(Rng2F32 a, Rng2F32 b) {
|
||||
return {{Max(a.p0.x, b.p0.x), Max(a.p0.y, b.p0.y)},
|
||||
{Min(a.p1.x, b.p1.x), Min(a.p1.y, b.p1.y)}};
|
||||
}
|
||||
|
||||
// Axis-indexed range dimension
|
||||
static inline F32 rng2f32_dim_axis(Rng2F32 r, Axis2 a) {
|
||||
return a == Axis2_X ? (r.p1.x - r.p0.x) : (r.p1.y - r.p0.y);
|
||||
}
|
||||
|
||||
////////////////////////////////
|
||||
// F32 helpers
|
||||
|
||||
static inline F32 lerp_1f32(F32 a, F32 b, F32 t) { return a + (b - a) * t; }
|
||||
static inline F32 abs_f32(F32 x) { return x < 0 ? -x : x; }
|
||||
32
src/base/base_strings.cpp
Normal file
32
src/base/base_strings.cpp
Normal file
@@ -0,0 +1,32 @@
|
||||
#include "base/base_strings.h"
|
||||
#include "base/base_arena.h"
|
||||
|
||||
Str8 str8_pushf(Arena *arena, const char *fmt, ...) {
|
||||
va_list args, args2;
|
||||
va_start(args, fmt);
|
||||
va_copy(args2, args);
|
||||
int len = vsnprintf(nullptr, 0, fmt, args);
|
||||
va_end(args);
|
||||
|
||||
char *buf = push_array(arena, char, len + 1);
|
||||
vsnprintf(buf, len + 1, fmt, args2);
|
||||
va_end(args2);
|
||||
|
||||
return {buf, (U64)len};
|
||||
}
|
||||
|
||||
Str8 str8_push_copy(Arena *arena, Str8 s) {
|
||||
if (s.size == 0 || !s.str) return {nullptr, 0};
|
||||
char *buf = push_array_no_zero(arena, char, s.size + 1);
|
||||
MemoryCopy(buf, s.str, s.size);
|
||||
buf[s.size] = 0;
|
||||
return {buf, s.size};
|
||||
}
|
||||
|
||||
void str8_list_push(Arena *arena, Str8List *list, Str8 s) {
|
||||
Str8Node *node = push_array(arena, Str8Node, 1);
|
||||
node->string = s;
|
||||
SLLQueuePush(list->first, list->last, node);
|
||||
list->count++;
|
||||
list->total_size += s.size;
|
||||
}
|
||||
49
src/base/base_strings.h
Normal file
49
src/base/base_strings.h
Normal file
@@ -0,0 +1,49 @@
|
||||
#pragma once
|
||||
// base_strings.h - Simple length-delimited string type
|
||||
// Inspired by raddebugger's String8
|
||||
|
||||
#include "base/base_core.h"
|
||||
|
||||
////////////////////////////////
|
||||
// String types
|
||||
|
||||
struct Str8 {
|
||||
const char *str;
|
||||
U64 size;
|
||||
};
|
||||
|
||||
struct Str8Node {
|
||||
Str8Node *next;
|
||||
Str8 string;
|
||||
};
|
||||
|
||||
struct Str8List {
|
||||
Str8Node *first;
|
||||
Str8Node *last;
|
||||
U64 count;
|
||||
U64 total_size;
|
||||
};
|
||||
|
||||
////////////////////////////////
|
||||
// Forward declaration for Arena
|
||||
struct Arena;
|
||||
|
||||
////////////////////////////////
|
||||
// Constructors
|
||||
|
||||
static inline Str8 str8(const char *s, U64 len) { return {s, len}; }
|
||||
static inline Str8 str8_cstr(const char *s) { return {s, s ? (U64)strlen(s) : 0}; }
|
||||
static inline Str8 str8_lit(const char *s) { return {s, s ? (U64)strlen(s) : 0}; }
|
||||
static inline B32 str8_match(Str8 a, Str8 b) {
|
||||
if (a.size != b.size) return 0;
|
||||
return MemoryCompare(a.str, b.str, a.size) == 0;
|
||||
}
|
||||
static inline B32 str8_is_empty(Str8 s) { return s.size == 0 || s.str == nullptr; }
|
||||
|
||||
////////////////////////////////
|
||||
// String operations (require arena)
|
||||
|
||||
Str8 str8_pushf(Arena *arena, const char *fmt, ...);
|
||||
Str8 str8_push_copy(Arena *arena, Str8 s);
|
||||
|
||||
void str8_list_push(Arena *arena, Str8List *list, Str8 s);
|
||||
Reference in New Issue
Block a user