...
This commit is contained in:
@@ -15,8 +15,7 @@ Matrix44 :: struct {
|
||||
}
|
||||
|
||||
mat4_zero :: (m: *Matrix44) {
|
||||
i := 0;
|
||||
while i < 16 { m.data[i] = 0.0; i += 1; }
|
||||
memset(xx @m.data[0], 0, 16 * size_of(f32));
|
||||
}
|
||||
|
||||
mat4_identity :: (m: *Matrix44) {
|
||||
@@ -41,8 +40,7 @@ mat4_multiply :: (out: *Matrix44, a: *Matrix44, b: *Matrix44) {
|
||||
}
|
||||
j += 1;
|
||||
}
|
||||
i := 0;
|
||||
while i < 16 { out.data[i] = tmp[i]; i += 1; }
|
||||
out.data = tmp;
|
||||
}
|
||||
|
||||
mat4_perspective :: (m: *Matrix44, fov: f32, aspect: f32, near: f32, far: f32) {
|
||||
@@ -149,7 +147,7 @@ main :: () {
|
||||
glDepthFunc(GL_LESS);
|
||||
|
||||
// Shaders
|
||||
vert_src : [:0]u8 = #string GLSL
|
||||
vert_src : [:0]u8 = #string glsl
|
||||
#version 330 core
|
||||
layout (location = 0) in vec3 aPos;
|
||||
layout (location = 1) in vec3 aNormal;
|
||||
@@ -162,7 +160,7 @@ void main() {
|
||||
vNormal = aNormal;
|
||||
vPos = aPos;
|
||||
}
|
||||
GLSL;
|
||||
glsl;
|
||||
|
||||
frag_src : [:0]u8 = #string GLSL
|
||||
#version 330 core
|
||||
|
||||
@@ -58,7 +58,7 @@ main :: () -> s32 {
|
||||
close(client);
|
||||
}
|
||||
|
||||
arena_destroy(@arena);
|
||||
arena_deinit(@arena);
|
||||
close(fd);
|
||||
0;
|
||||
}
|
||||
|
||||
@@ -545,6 +545,14 @@ END;
|
||||
mpw_ptr[2] = 99;
|
||||
print("mp-write: {}\n", mpw[2]);
|
||||
|
||||
// Pointer-null comparison
|
||||
np : *s32 = null;
|
||||
print("ptr==null: {}\n", np == null);
|
||||
print("ptr!=null: {}\n", np != null);
|
||||
np2 := @pv.x;
|
||||
print("ptr2==null: {}\n", np2 == null);
|
||||
print("ptr2!=null: {}\n", np2 != null);
|
||||
|
||||
// --- Vectors ---
|
||||
vc := vec3(1, 3, 2);
|
||||
print("vec-construct: {}\n", vc);
|
||||
|
||||
@@ -27,53 +27,49 @@ gpa_free :: (ctx: *void, ptr: *void) {
|
||||
}
|
||||
|
||||
gpa_create :: (gpa: *GPA) -> Allocator {
|
||||
ctx : *void = xx gpa;
|
||||
Allocator.{ ctx = ctx, alloc = gpa_alloc, free = gpa_free };
|
||||
Allocator.{ ctx = gpa, alloc = gpa_alloc, free = gpa_free };
|
||||
}
|
||||
|
||||
// --- Arena: multi-chunk bump allocator (Zig-inspired) ---
|
||||
|
||||
ArenaChunk :: struct {
|
||||
next: *void; // *ArenaChunk
|
||||
cap: s64; // total chunk size including this header
|
||||
next: *ArenaChunk;
|
||||
cap: s64;
|
||||
}
|
||||
|
||||
Arena :: struct {
|
||||
first: *void; // *ArenaChunk — head of list (newest first)
|
||||
end_index: s64; // bump position within current chunk's usable area
|
||||
parent: Allocator; // backing allocator
|
||||
first: *ArenaChunk;
|
||||
end_index: s64;
|
||||
parent: Allocator;
|
||||
}
|
||||
|
||||
arena_add_chunk :: (a: *Arena, min_size: s64) {
|
||||
first_i : s64 = xx a.first;
|
||||
prev_cap := if first_i != 0 then { c : *ArenaChunk = xx a.first; c.cap; } else 0;
|
||||
prev_cap := if a.first != null then a.first.cap else 0;
|
||||
needed := min_size + 16 + 16;
|
||||
len := (prev_cap + needed) * 3 / 2;
|
||||
raw := a.parent.alloc(a.parent.ctx, len);
|
||||
chunk : *ArenaChunk = xx raw;
|
||||
chunk.next = a.first;
|
||||
chunk.cap = len;
|
||||
a.first = xx chunk;
|
||||
a.first = chunk;
|
||||
a.end_index = 0;
|
||||
}
|
||||
|
||||
arena_alloc :: (ctx: *void, size: s64) -> *void {
|
||||
a : *Arena = xx ctx;
|
||||
aligned := (size + 7) & (0 - 8);
|
||||
first_i : s64 = xx a.first;
|
||||
if first_i != 0 {
|
||||
chunk : *ArenaChunk = xx a.first;
|
||||
usable := chunk.cap - 16;
|
||||
if a.first != null {
|
||||
usable := a.first.cap - 16;
|
||||
if a.end_index + aligned <= usable {
|
||||
buf : [*]u8 = xx a.first;
|
||||
ptr : *void = xx @buf[16 + a.end_index];
|
||||
ptr := @buf[16 + a.end_index];
|
||||
a.end_index = a.end_index + aligned;
|
||||
return ptr;
|
||||
}
|
||||
}
|
||||
arena_add_chunk(a, aligned);
|
||||
buf : [*]u8 = xx a.first;
|
||||
ptr : *void = xx @buf[16 + a.end_index];
|
||||
ptr := @buf[16 + a.end_index];
|
||||
a.end_index = a.end_index + aligned;
|
||||
ptr;
|
||||
}
|
||||
@@ -86,34 +82,29 @@ arena_create :: (a: *Arena, parent: Allocator, size: s64) -> Allocator {
|
||||
a.end_index = 0;
|
||||
a.parent = parent;
|
||||
arena_add_chunk(a, size);
|
||||
ctx : *void = xx a;
|
||||
Allocator.{ ctx = ctx, alloc = arena_alloc, free = arena_free };
|
||||
Allocator.{ ctx = a, alloc = arena_alloc, free = arena_free };
|
||||
}
|
||||
|
||||
arena_reset :: (a: *Arena) {
|
||||
// Keep first chunk (newest/largest), free the rest
|
||||
first_i : s64 = xx a.first;
|
||||
if first_i != 0 {
|
||||
chunk : *ArenaChunk = xx a.first;
|
||||
it : s64 = xx chunk.next;
|
||||
while it != 0 {
|
||||
c : *ArenaChunk = xx it;
|
||||
next_i : s64 = xx c.next;
|
||||
a.parent.free(a.parent.ctx, xx c);
|
||||
it = next_i;
|
||||
if a.first != null {
|
||||
it := a.first.next;
|
||||
while it != null {
|
||||
next := it.next;
|
||||
a.parent.free(a.parent.ctx, it);
|
||||
it = next;
|
||||
}
|
||||
chunk.next = null;
|
||||
a.first.next = null;
|
||||
}
|
||||
a.end_index = 0;
|
||||
}
|
||||
|
||||
arena_deinit :: (a: *Arena) {
|
||||
it : s64 = xx a.first;
|
||||
while it != 0 {
|
||||
c : *ArenaChunk = xx it;
|
||||
next_i : s64 = xx c.next;
|
||||
a.parent.free(a.parent.ctx, xx c);
|
||||
it = next_i;
|
||||
it := a.first;
|
||||
while it != null {
|
||||
next := it.next;
|
||||
a.parent.free(a.parent.ctx, it);
|
||||
it = next;
|
||||
}
|
||||
a.first = null;
|
||||
a.end_index = 0;
|
||||
@@ -133,7 +124,7 @@ buf_alloc :: (ctx: *void, size: s64) -> *void {
|
||||
if b.pos + aligned > b.len {
|
||||
return null;
|
||||
}
|
||||
ptr : *void = xx @b.buf[b.pos];
|
||||
ptr := @b.buf[b.pos];
|
||||
b.pos = b.pos + aligned;
|
||||
ptr;
|
||||
}
|
||||
@@ -145,8 +136,7 @@ buf_create :: (b: *BufAlloc, buf: [*]u8, len: s64) -> Allocator {
|
||||
b.buf = buf;
|
||||
b.len = len;
|
||||
b.pos = 0;
|
||||
ctx : *void = xx b;
|
||||
Allocator.{ ctx = ctx, alloc = buf_alloc, free = buf_free };
|
||||
Allocator.{ ctx = b, alloc = buf_alloc, free = buf_free };
|
||||
}
|
||||
|
||||
buf_reset :: (b: *BufAlloc) {
|
||||
|
||||
@@ -29,5 +29,5 @@ SockAddr :: struct {
|
||||
}
|
||||
|
||||
htons :: (port: s64) -> u16 {
|
||||
cast(u16) ((port / 256) | ((port % 256) * 256));
|
||||
cast(u16) (((port & 0xFF) << 8) | ((port >> 8) & 0xFF));
|
||||
}
|
||||
|
||||
@@ -32,8 +32,7 @@ context : Context = ---;
|
||||
// --- Slice & string allocation ---
|
||||
|
||||
cstring :: (size: s64) -> string {
|
||||
p : s64 = xx context.allocator.ctx;
|
||||
raw := if p != 0 then context.allocator.alloc(context.allocator.ctx, size + 1) else malloc(size + 1);
|
||||
raw := if context.allocator.ctx != null then context.allocator.alloc(context.allocator.ctx, size + 1) else malloc(size + 1);
|
||||
memset(raw, 0, size + 1);
|
||||
s : string = ---;
|
||||
s.ptr = xx raw;
|
||||
@@ -42,8 +41,7 @@ cstring :: (size: s64) -> string {
|
||||
}
|
||||
|
||||
alloc_slice :: ($T: Type, count: s64) -> []T {
|
||||
p : s64 = xx context.allocator.ctx;
|
||||
raw := if p != 0 then context.allocator.alloc(context.allocator.ctx, count * size_of(T)) else malloc(count * size_of(T));
|
||||
raw := if context.allocator.ctx != null then context.allocator.alloc(context.allocator.ctx, count * size_of(T)) else malloc(count * size_of(T));
|
||||
memset(raw, 0, count * size_of(T));
|
||||
s : []T = ---;
|
||||
s.ptr = xx raw;
|
||||
@@ -55,19 +53,16 @@ int_to_string :: (n: s64) -> string {
|
||||
if n == 0 { return "0"; }
|
||||
neg := n < 0;
|
||||
v := if neg then 0 - n else n;
|
||||
tmp := v;
|
||||
len := 0;
|
||||
while tmp > 0 { len += 1; tmp = tmp / 10; }
|
||||
total := if neg then len + 1 else len;
|
||||
buf := cstring(total);
|
||||
i := total - 1;
|
||||
// Single pass: fill digits backwards into temp string, then substr
|
||||
tmp := cstring(20);
|
||||
i := 19;
|
||||
while v > 0 {
|
||||
buf[i] = (v % 10) + 48;
|
||||
tmp[i] = (v % 10) + 48;
|
||||
v = v / 10;
|
||||
i -= 1;
|
||||
}
|
||||
if neg { buf[0] = 45; }
|
||||
buf;
|
||||
if neg { tmp[i] = 45; i -= 1; }
|
||||
substr(tmp, i + 1, 20 - i - 1);
|
||||
}
|
||||
|
||||
bool_to_string :: (b: bool) -> string {
|
||||
@@ -100,6 +95,17 @@ float_to_string :: (f: f64) -> string {
|
||||
buf;
|
||||
}
|
||||
|
||||
hex_group :: (buf: string, offset: s64, val: s64) {
|
||||
i := offset + 3;
|
||||
v := val;
|
||||
while i >= offset {
|
||||
d := v % 16;
|
||||
buf[i] = if d < 10 then d + 48 else d - 10 + 97;
|
||||
v = v / 16;
|
||||
i -= 1;
|
||||
}
|
||||
}
|
||||
|
||||
int_to_hex_string :: (n: s64) -> string {
|
||||
if n == 0 { return "0"; }
|
||||
|
||||
@@ -117,42 +123,11 @@ int_to_hex_string :: (n: s64) -> string {
|
||||
if g3 < 0 { g3 = g3 + 65536; }
|
||||
|
||||
buf := cstring(16);
|
||||
// Group 3: digits 0-3 (bits 48-63)
|
||||
i := 3;
|
||||
v := g3;
|
||||
while i >= 0 {
|
||||
d := v % 16;
|
||||
buf[i] = if d < 10 then d + 48 else d - 10 + 97;
|
||||
v = v / 16;
|
||||
i -= 1;
|
||||
}
|
||||
// Group 2: digits 4-7 (bits 32-47)
|
||||
i = 7;
|
||||
v = g2;
|
||||
while i >= 4 {
|
||||
d := v % 16;
|
||||
buf[i] = if d < 10 then d + 48 else d - 10 + 97;
|
||||
v = v / 16;
|
||||
i -= 1;
|
||||
}
|
||||
// Group 1: digits 8-11 (bits 16-31)
|
||||
i = 11;
|
||||
v = g1;
|
||||
while i >= 8 {
|
||||
d := v % 16;
|
||||
buf[i] = if d < 10 then d + 48 else d - 10 + 97;
|
||||
v = v / 16;
|
||||
i -= 1;
|
||||
}
|
||||
// Group 0: digits 12-15 (bits 0-15)
|
||||
i = 15;
|
||||
v = g0;
|
||||
while i >= 12 {
|
||||
d := v % 16;
|
||||
buf[i] = if d < 10 then d + 48 else d - 10 + 97;
|
||||
v = v / 16;
|
||||
i -= 1;
|
||||
}
|
||||
hex_group(buf, 0, g3);
|
||||
hex_group(buf, 4, g2);
|
||||
hex_group(buf, 8, g1);
|
||||
hex_group(buf, 12, g0);
|
||||
|
||||
// Skip leading zeros (keep at least 1 digit)
|
||||
start := 0;
|
||||
while start < 15 {
|
||||
|
||||
Reference in New Issue
Block a user