Death to C++

This commit is contained in:
2026-03-09 16:17:05 -04:00
parent b074d2113f
commit 066ac22605
80 changed files with 5671 additions and 39268 deletions

View File

@@ -3,7 +3,7 @@
Arena *arena_alloc(U64 cap) {
U8 *mem = (U8 *)malloc(sizeof(Arena) + cap);
if (!mem) return nullptr;
if (!mem) return NULL;
Arena *arena = (Arena *)mem;
arena->base = mem + sizeof(Arena);
arena->pos = 0;
@@ -19,7 +19,7 @@ void *arena_push(Arena *arena, U64 size) {
U64 aligned = AlignPow2(size, 8);
if (arena->pos + aligned > arena->cap) {
Assert(!"Arena overflow");
return nullptr;
return NULL;
}
void *result = arena->base + arena->pos;
arena->pos += aligned;
@@ -31,7 +31,7 @@ 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;
return NULL;
}
void *result = arena->base + arena->pos;
arena->pos += aligned;

View File

@@ -8,17 +8,17 @@
////////////////////////////////
// Arena type
struct Arena {
typedef struct Arena {
U8 *base;
U64 pos;
U64 cap;
};
} Arena;
// Temporary scope (save/restore position)
struct Temp {
typedef struct Temp {
Arena *arena;
U64 pos;
};
} Temp;
////////////////////////////////
// Arena functions
@@ -34,8 +34,8 @@ 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); }
static inline Temp temp_begin(Arena *arena) { Temp t = {arena, arena->pos}; return t; }
static inline void temp_end(Temp temp) { arena_pop_to(temp.arena, temp.pos); }
////////////////////////////////
// Push helper macros

View File

@@ -7,6 +7,7 @@
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <stdbool.h>
////////////////////////////////
// Codebase keywords
@@ -17,7 +18,7 @@
#endif
#define local_persist static
#define trvke true
#define trvke 1
////////////////////////////////
// Base types

3
src/base/base_inc.c Normal file
View File

@@ -0,0 +1,3 @@
// base_inc.c - Unity build for the base layer
#include "base/base_arena.c"
#include "base/base_strings.c"

View File

@@ -1,3 +0,0 @@
// base_inc.cpp - Unity build for the base layer
#include "base/base_arena.cpp"
#include "base/base_strings.cpp"

View File

@@ -7,60 +7,60 @@
////////////////////////////////
// Axis enum
enum Axis2 {
typedef enum Axis2 {
Axis2_X = 0,
Axis2_Y = 1,
Axis2_COUNT,
};
} Axis2;
enum Side {
typedef enum Side {
Side_Min = 0,
Side_Max = 1,
Side_COUNT,
};
} Side;
enum Corner {
typedef enum Corner {
Corner_00 = 0, // top-left
Corner_01 = 1, // top-right
Corner_10 = 2, // bottom-left
Corner_11 = 3, // bottom-right
Corner_COUNT,
};
} Corner;
////////////////////////////////
// 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; };
typedef struct Vec2F32 { F32 x, y; } Vec2F32;
typedef struct Vec2S32 { S32 x, y; } Vec2S32;
typedef struct Vec3F32 { F32 x, y, z; } Vec3F32;
typedef struct Vec4F32 { F32 x, y, z, w; } Vec4F32;
////////////////////////////////
// Range types
struct Rng1F32 { F32 min, max; };
struct Rng1S64 { S64 min, max; };
struct Rng2F32 { Vec2F32 p0, p1; };
typedef struct Rng1F32 { F32 min, max; } Rng1F32;
typedef struct Rng1S64 { S64 min, max; } Rng1S64;
typedef struct Rng2F32 { Vec2F32 p0, p1; } Rng2F32;
////////////////////////////////
// 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}}; }
static inline Vec2F32 v2f32(F32 x, F32 y) { return (Vec2F32){x, y}; }
static inline Vec2S32 v2s32(S32 x, S32 y) { return (Vec2S32){x, y}; }
static inline Vec3F32 v3f32(F32 x, F32 y, F32 z) { return (Vec3F32){x, y, z}; }
static inline Vec4F32 v4f32(F32 x, F32 y, F32 z, F32 w) { return (Vec4F32){x, y, z, w}; }
static inline Rng1F32 rng1f32(F32 min, F32 max) { return (Rng1F32){min, max}; }
static inline Rng1S64 rng1s64(S64 min, S64 max) { return (Rng1S64){min, max}; }
static inline Rng2F32 rng2f32(Vec2F32 p0, Vec2F32 p1) { return (Rng2F32){p0, p1}; }
static inline Rng2F32 rng2f32p(F32 x0, F32 y0, F32 x1, F32 y1) { return (Rng2F32){{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}; }
static inline Vec2F32 add_2f32(Vec2F32 a, Vec2F32 b) { return (Vec2F32){a.x + b.x, a.y + b.y}; }
static inline Vec2F32 sub_2f32(Vec2F32 a, Vec2F32 b) { return (Vec2F32){a.x - b.x, a.y - b.y}; }
static inline Vec2F32 mul_2f32(Vec2F32 a, Vec2F32 b) { return (Vec2F32){a.x * b.x, a.y * b.y}; }
static inline Vec2F32 scale_2f32(Vec2F32 v, F32 s) { return (Vec2F32){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; }
@@ -71,10 +71,10 @@ static inline void v2f32_set_axis(Vec2F32 *v, Axis2 a, F32 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 add_4f32(Vec4F32 a, Vec4F32 b) { return (Vec4F32){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 (Vec4F32){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};
return (Vec4F32){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};
}
////////////////////////////////
@@ -82,19 +82,19 @@ static inline Vec4F32 lerp_4f32(Vec4F32 a, Vec4F32 b, F32 t) {
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 Vec2F32 rng2f32_dim(Rng2F32 r) { return (Vec2F32){r.p1.x - r.p0.x, r.p1.y - r.p0.y}; }
static inline Vec2F32 rng2f32_center(Rng2F32 r) { return (Vec2F32){(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}};
return (Rng2F32){{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}};
return (Rng2F32){{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)},
return (Rng2F32){{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)}};
}

View File

@@ -5,22 +5,24 @@ Str8 str8_pushf(Arena *arena, const char *fmt, ...) {
va_list args, args2;
va_start(args, fmt);
va_copy(args2, args);
S32 len = vsnprintf(nullptr, 0, fmt, args);
S32 len = vsnprintf(NULL, 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 r = {buf, (U64)len};
return r;
}
Str8 str8_push_copy(Arena *arena, Str8 s) {
if (s.size == 0 || !s.str) return {nullptr, 0};
if (s.size == 0 || !s.str) { Str8 r = {NULL, 0}; return r; }
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};
Str8 r = {buf, s.size};
return r;
}
void str8_list_push(Arena *arena, Str8List *list, Str8 s) {

View File

@@ -7,38 +7,38 @@
////////////////////////////////
// String types
struct Str8 {
typedef struct Str8 {
const char *str;
U64 size;
};
} Str8;
struct Str8Node {
Str8Node *next;
Str8 string;
};
typedef struct Str8Node {
struct Str8Node *next;
Str8 string;
} Str8Node;
struct Str8List {
typedef struct Str8List {
Str8Node *first;
Str8Node *last;
U64 count;
U64 total_size;
};
} Str8List;
////////////////////////////////
// Forward declaration for Arena
struct Arena;
typedef struct Arena 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 Str8 str8(const char *s, U64 len) { Str8 r = {s, len}; return r; }
static inline Str8 str8_cstr(const char *s) { Str8 r = {s, s ? (U64)strlen(s) : 0}; return r; }
static inline Str8 str8_lit(const char *s) { Str8 r = {s, s ? (U64)strlen(s) : 0}; return r; }
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; }
static inline B32 str8_is_empty(Str8 s) { return s.size == 0 || s.str == NULL; }
////////////////////////////////
// String operations (require arena)