From aa00f2f2d7cc70edb45726dc21d359bc05036387 Mon Sep 17 00:00:00 2001 From: pryazha Date: Wed, 19 Mar 2025 08:44:10 +0500 Subject: mainly string change --- example/example | Bin 65664 -> 62240 bytes example/example.c | 46 ++++---- prb_math.c | 305 ++++++++++++++++++++++++++++++++++++++---------------- prb_math.h | 101 +++++++++--------- prb_memory.c | 2 +- prb_os_io.c | 4 +- prb_os_io.h | 2 + prb_string.c | 146 ++++++++++++++++---------- prb_string.h | 23 ++-- prb_types.h | 87 ++++++++-------- 10 files changed, 448 insertions(+), 268 deletions(-) diff --git a/example/example b/example/example index 15b5b35..02b1008 100755 Binary files a/example/example and b/example/example differ diff --git a/example/example.c b/example/example.c index 1fa85b3..35d2b86 100644 --- a/example/example.c +++ b/example/example.c @@ -159,14 +159,14 @@ int main(void) printf("Matrices:\n"); printf("Identity:\n"); - Mat4 m = MAT4_IDENTITY; + MAT4 m = MAT4_IDENTITY; mat4print(m); m = mat4scale(m, v3a(10.0f)); - m = mat4translate(m, v3a(1.0f)); - m = mat4translate(m, v3(0.0f, 68.0f, 0.0f)); + m = mat4transl(m, v3a(1.0f)); + m = mat4transl(m, v3(0.0f, 68.0f, 0.0f)); mat4print(m); printf("Determinant: %f\n", mat4det(m)); - printf("Transpose:\n"); mat4print(mat4transpose(m)); + printf("Transpose:\n"); mat4print(mat4transp(m)); printf("mat4rotate(MAT4_IDENTITY, v3(0.0f, 45.0f, 0.0f)):\n"); mat4print(mat4rotate(MAT4_IDENTITY, v3(0.0f, 45.0f, 0.0f))); @@ -198,23 +198,23 @@ int main(void) Arena *str_arena = arena_alloc(0); printf("Strings:\n"); char *cstr = "This is a C str\n"; - Str8 str = str8_from_cstr(cstr); - str = str8_chop_start(str, 10); - cstr = str8_to_cstr(a, str); + Str8 str = str8fromcstr(cstr); + str = str8chopstart(str, 10); + cstr = str8tocstr(a, str); printf("%s", cstr); str8print(str); - Str8 choped_str = str8_chop_end(str, 3); + Str8 choped_str = str8chopend(str, 3); str8print(choped_str); printf("\n"); - Str8List *list = str8_list(str_arena); - str8_list_push(str_arena, list, str, 0); - str8_list_push(str_arena, list, str8_from_cstr("test"), 0); - str8_list_push(str_arena, list, str8_from_cstr("and this is also a test\n"), 0); - str8_list_push(str_arena, list, str8_from_cstr("Kinda works!"), 1); + Str8List *list = str8list(str_arena); + str8listpush(str_arena, list, str, 0); + str8listpush(str_arena, list, str8fromcstr("test"), 0); + str8listpush(str_arena, list, str8fromcstr("and this is also a test\n"), 0); + str8listpush(str_arena, list, str8fromcstr("Kinda works!"), 1); printf("Str8List: "); - str8_list_print(list); + str8listprint(list); arena_release(str_arena); - str8_list_print(list); + str8listprint(list); str_arena = arena_alloc(Kilobytes(10)); memory_size = 512; @@ -239,14 +239,18 @@ int main(void) printf("some ptr is %s\n", some ? "not null" : "null"); printf("str_arena(size): %lu\nMemory:\n", str_arena->cap); - Str8 new_str = str8_pushf(str_arena, "Test of the formatted string: %d\n", 69); + Str8 new_str = str8pushf(str_arena, "Test of the formatted string: %d\n", 69); str8print(new_str); - list = str8_list(str_arena); - str8_list_pushf(str_arena, list, 0, "This is a list %d", 34); - str8_list_pushf(str_arena, list, 0, " of formatted strings: %d", 35); - str8_list_pushf(str_arena, list, 1, "And you can push to the start"); - str8_list_print(list); + list = str8list(str_arena); + str8listpushf(str_arena, list, 0, "This is a list %d", 34); + str8listpushf(str_arena, list, 0, " of formatted strings: %d", 35); + str8listpushf(str_arena, list, 1, "And you can push to the start"); + /* TODO(pryazha): Think about simpler way of using string literals. + * Maybe you don't need to, I dunno */ + str8listpushf(str_arena, list, 1, "The most interesting part is that you can include the whole string like that: %*s\n", + str8expand(str8lit("Absolutely random string with absolutely random content (or not)."))); + str8listprint(list); arena_release(str_arena); diff --git a/prb_math.c b/prb_math.c index a2d58bc..bbc3161 100644 --- a/prb_math.c +++ b/prb_math.c @@ -1,242 +1,341 @@ F32 f32sin(F32 a) { - F32 r = sinf(a); + F32 r; + r = sinf(a); return r; } F32 f32cos(F32 a) { - F32 r = cosf(a); + F32 r; + r = cosf(a); + return r; +} + +F32 f32tan(F32 a) +{ + F32 r; + r = tanf(a); return r; } F32 f32sqrt(F32 a) { - F32 r = sqrtf(a); + F32 r; + r = sqrtf(a); return r; } /* NOTE(pryazha): Vectors */ V2 v2(F32 x, F32 y) { - V2 r = { x, y }; + V2 r; + r.x = x; + r.y = y; return r; } V2 v2a(F32 x) { - V2 r = { x, x }; + V2 r; + r = v2(x, x); return r; } -V2 v2neg(V2 a) +V2 v2inv(V2 a) { - V2 r = { -a.x, -a.y }; + V2 r; + r = v2(-a.x, -a.y); return r; } V2 v2add(V2 a, V2 b) { - V2 r = { a.x+b.x, a.y+b.y }; + V2 r; + r = v2(a.x+b.x, a.y+b.y); return r; } V2 v2sub(V2 a, V2 b) { - V2 r = { a.x-b.x, a.y-b.y }; + V2 r; + r = v2(a.x-b.x, a.y-b.y); return r; } V2 v2scalef(V2 a, F32 s) { - V2 r = { a.x*s, a.y*s }; + V2 r; + r = v2(a.x*s, a.y*s); return r; } V2 v2scale(V2 a, V2 s) { - V2 r = { a.x*s.x, a.y*s.y }; + V2 r; + r = v2(a.x*s.x, a.y*s.y); return r; } F32 v2dot(V2 a, V2 b) { - F32 r = a.x*b.x+a.y*b.y; + F32 r; + r = a.x*b.x+a.y*b.y; return r; } F32 v2len2(V2 a) { - F32 r = v2dot(a, a); + F32 r; + r = v2dot(a, a); return r; } F32 v2len(V2 a) { - F32 r = f32sqrt(v2len2(a)); + F32 r; + r = f32sqrt(v2len2(a)); + return r; +} + +V2 v2norm(V2 a) +{ + F32 len; + V2 r; + + r = V2_ZERO; + len = v2len(a); + if (len) + r = v2(a.x/len, a.y/len); + return r; } void v2print(V2 a) { - fprintf(stdout, "{%f, %f}\n", a.x, a.y); + fprintf(stdout, "{%f, %f}T\n", a.x, a.y); } V3 v3(F32 x, F32 y, F32 z) { - V3 r = { x, y, z }; + V3 r; + r.x = x; + r.y = y; + r.z = z; return r; } V3 v3a(F32 x) { - V3 r = { x, x, x }; + V3 r; + r = v3(x, x, x); return r; } -V3 v3neg(V3 a) +V3 v3fromv2(V2 a) { - V3 r = { -a.x, -a.y, -a.z }; + V3 r; + r = v3(a.x, a.y, 0.0f); + return r; +} + +V3 v3fromv4(V4 a) +{ + V3 r; + r = v3(a.x, a.y, a.z); + return r; +} + +V3 v3inv(V3 a) +{ + V3 r; + r = v3(-a.x, -a.y, -a.z); return r; } V3 v3add(V3 a, V3 b) { - V3 r = { a.x+b.x, a.y+b.y, a.z+b.z }; + V3 r; + r = v3(a.x+b.x, a.y+b.y, a.z+b.z); return r; } V3 v3sub(V3 a, V3 b) { - V3 r = { a.x-b.x, a.y-b.y, a.z-b.z }; + V3 r; + r = v3(a.x-b.x, a.y-b.y, a.z-b.z); return r; } V3 v3scalef(V3 a, F32 s) { - V3 r = { a.x*s, a.y*s, a.z*s }; + V3 r; + r = v3(a.x*s, a.y*s, a.z*s); return r; } V3 v3scale(V3 a, V3 s) { - V3 r = { a.x*s.x, a.y*s.y, a.z*s.z }; + V3 r; + r = v3(a.x*s.x, a.y*s.y, a.z*s.z); return r; } F32 v3dot(V3 a, V3 b) { - F32 r = a.x*b.x+a.y*b.y+a.z*b.z; + F32 r; + r = a.x*b.x + a.y*b.y + a.z*b.z; return r; } V3 v3cross(V3 left, V3 right) { - V3 r = { - (left.y*right.z-right.y*left.z), - (right.x*left.z-left.x*right.z), - (left.x*right.y-right.x*left.y) - }; + V3 r; + r = v3((left.y*right.z-right.y*left.z), + (right.x*left.z-left.x*right.z), + (left.x*right.y-right.x*left.y)); return r; } F32 v3len2(V3 a) { - F32 r = v3dot(a, a); + F32 r; + r = v3dot(a, a); return r; } F32 v3len(V3 a) { - F32 r = f32sqrt(v3len2(a)); + F32 r; + r = f32sqrt(v3len2(a)); + return r; +} + +V3 v3norm(V3 a) +{ + F32 len; + V3 r; + + r = V3_ZERO; + len = v3len(a); + if (len) + r = v3(a.x/len, a.y/len, a.z/len); + return r; } void v3print(V3 a) { - fprintf(stdout, "{%f, %f, %f}\n", a.x, a.y, a.z); + fprintf(stdout, "{%f, %f, %f}T\n", a.x, a.y, a.z); } V4 v4(F32 x, F32 y, F32 z, F32 w) { - V4 r = { x, y, z, w }; + V4 r; + r.x = x; + r.y = y; + r.z = z; + r.w = w; return r; } V4 v4a(F32 x) { - V4 r = { x, x, x, x }; + V4 r; + r = v4(x, x, x, x); return r; } V4 v4fromv3(V3 a) { - V4 r = { a.x, a.y, a.z, 0.0f }; + V4 r; + r = v4(a.x, a.y, a.z, 0.0f); return r; } -V4 v4neg(V4 a) +V4 v4inv(V4 a) { - V4 r = { -a.x, -a.y, -a.z, -a.w }; + V4 r; + r = v4(-a.x, -a.y, -a.z, -a.w); return r; } V4 v4add(V4 a, V4 b) { - V4 r = { a.x+b.x, a.y+b.y, a.z+b.z, a.w+b.w }; + V4 r; + r = v4(a.x+b.x, a.y+b.y, a.z+b.z, a.w+b.w); return r; } V4 v4sub(V4 a, V4 b) { - V4 r = { a.x-b.x, a.y-b.y, a.z-b.z, a.w-b.w }; + V4 r; + r = v4(a.x-b.x, a.y-b.y, a.z-b.z, a.w-b.w); return r; } V4 v4scalef(V4 a, F32 s) { - V4 r = { a.x*s, a.y*s, a.z*s, a.w*s }; + V4 r; + r = v4(a.x*s, a.y*s, a.z*s, a.w*s); return r; } V4 v4scale(V4 a, V4 s) { - V4 r = { a.x*s.x, a.y*s.y, a.z*s.z, a.w*s.w }; + V4 r; + r = v4(a.x*s.x, a.y*s.y, a.z*s.z, a.w*s.w); return r; } F32 v4dot(V4 a, V4 b) { - F32 r = a.x*b.x+a.y*b.y+a.z*b.z+a.w*b.w; + F32 r; + r = a.x*b.x+a.y*b.y+a.z*b.z+a.w*b.w; return r; } F32 v4len2(V4 a) { - F32 r = v4dot(a, a); + F32 r; + r = v4dot(a, a); return r; } F32 v4len(V4 a) { - F32 r = f32sqrt(v4len2(a)); + F32 r; + r = f32sqrt(v4len2(a)); + return r; +} + +V4 v4norm(V4 a) +{ + F32 len; + V4 r; + + r = V4_ZERO; + len = v4len(a); + if (len) + r = v4(a.x/len, a.y/len, a.z/len, a.w/len); + return r; } void v4print(V4 a) { - fprintf(stdout, "{%f, %f, %f, %f}\n", a.x, a.y, a.z, a.w); + fprintf(stdout, "{%f, %f, %f, %f}T\n", a.x, a.y, a.z, a.w); } /* NOTE(pryazha): Matrices */ -F32 mat4det(Mat4 m) +F32 mat4det(MAT4 m) { - F32 r, m00minor, m01minor, m02minor, m03minor; + F32 r, m00minor, m01minor, m02minor, m03minor; - F32 m00 = m.m0.x, m10 = m.m0.y, m20 = m.m0.z, m30 = m.m0.w; - F32 m01 = m.m1.x, m11 = m.m1.y, m21 = m.m1.z, m31 = m.m1.w; - F32 m02 = m.m2.x, m12 = m.m2.y, m22 = m.m2.z, m32 = m.m2.w; - F32 m03 = m.m3.x, m13 = m.m3.y, m23 = m.m3.z, m33 = m.m3.w; + F32 m00 = m.m0.x, m10 = m.m0.y, m20 = m.m0.z, m30 = m.m0.w; + F32 m01 = m.m1.x, m11 = m.m1.y, m21 = m.m1.z, m31 = m.m1.w; + F32 m02 = m.m2.x, m12 = m.m2.y, m22 = m.m2.z, m32 = m.m2.w; + F32 m03 = m.m3.x, m13 = m.m3.y, m23 = m.m3.z, m33 = m.m3.w; m00minor = ((m11*m22*m33)+(m12*m23*m31)+(m21*m32*m13)- (m31*m22*m13)-(m21*m12*m33)-(m11*m32*m23)); @@ -250,14 +349,16 @@ F32 mat4det(Mat4 m) m03minor = ((m10*m21*m32)+(m20*m31*m12)+(m11*m22*m30)- (m12*m21*m30)-(m11*m20*m32)-(m22*m31*m10)); - r = m00*m00minor+m01*m01minor-m02*m02minor+m03*m03minor; + r = m00*m00minor + m01*m01minor - m02*m02minor + m03*m03minor; return r; } -Mat4 mat4transpose(Mat4 m) +MAT4 mat4transp(MAT4 m) { - Mat4 r = m; + MAT4 r; + + r = m; Swap(F32, r.m0.y, r.m1.x); Swap(F32, r.m0.z, r.m2.x); @@ -271,19 +372,29 @@ Mat4 mat4transpose(Mat4 m) return r; } -Mat4 mat4mul(Mat4 left, Mat4 right) +MAT4 mat4mul(MAT4 left, MAT4 right) { - F32 l00 = left.m0.x, l01 = left.m0.y, l02 = left.m0.z, l03 = left.m0.w; - F32 l10 = left.m1.x, l11 = left.m1.y, l12 = left.m1.z, l13 = left.m1.w; - F32 l20 = left.m2.x, l21 = left.m2.y, l22 = left.m2.z, l23 = left.m2.w; - F32 l30 = left.m3.x, l31 = left.m3.y, l32 = left.m3.z, l33 = left.m3.w; + F32 l00, l01, l02, l03; + F32 l10, l11, l12, l13; + F32 l20, l21, l22, l23; + F32 l30, l31, l32, l33; - F32 r00 = right.m0.x, r01 = right.m0.y, r02 = right.m0.z, r03 = right.m0.w; - F32 r10 = right.m1.x, r11 = right.m1.y, r12 = right.m1.z, r13 = right.m1.w; - F32 r20 = right.m2.x, r21 = right.m2.y, r22 = right.m2.z, r23 = right.m2.w; - F32 r30 = right.m3.x, r31 = right.m3.y, r32 = right.m3.z, r33 = right.m3.w; + F32 r00, r01, r02, r03; + F32 r10, r11, r12, r13; + F32 r20, r21, r22, r23; + F32 r30, r31, r32, r33; - Mat4 r; + MAT4 r; + + l00 = left.m0.x; l01 = left.m0.y; l02 = left.m0.z; l03 = left.m0.w; + l10 = left.m1.x; l11 = left.m1.y; l12 = left.m1.z; l13 = left.m1.w; + l20 = left.m2.x; l21 = left.m2.y; l22 = left.m2.z; l23 = left.m2.w; + l30 = left.m3.x; l31 = left.m3.y; l32 = left.m3.z; l33 = left.m3.w; + + r00 = right.m0.x; r01 = right.m0.y; r02 = right.m0.z; r03 = right.m0.w; + r10 = right.m1.x; r11 = right.m1.y; r12 = right.m1.z; r13 = right.m1.w; + r20 = right.m2.x; r21 = right.m2.y; r22 = right.m2.z; r23 = right.m2.w; + r30 = right.m3.x; r31 = right.m3.y; r32 = right.m3.z; r33 = right.m3.w; r.m0.x = l00*r00+l10*r01+l20*r02+l30*r03; r.m0.y = l01*r00+l11*r01+l21*r02+l31*r03; @@ -308,28 +419,41 @@ Mat4 mat4mul(Mat4 left, Mat4 right) return r; } -Mat4 mat4translate(Mat4 m, V3 v) +MAT4 mat4transl(MAT4 m, V3 v) { - Mat4 t = MAT4_IDENTITY; - t.m3 = v4fromv3(v); - Mat4 r = mat4mul(t, m); + MAT4 t, r; + + t = MAT4_IDENTITY; + t.m3.x = v.x; + t.m3.y = v.y; + t.m3.z = v.z; + r = mat4mul(t, m); + return r; } -Mat4 mat4scale(Mat4 m, V3 v) +MAT4 mat4scale(MAT4 m, V3 v) { - Mat4 s = MAT4_IDENTITY; - s.m0 = v4fromv3(v); - Mat4 r = mat4mul(s, m); + MAT4 s, r; + + s = MAT4_IDENTITY; + s.m0.x = v.x; + s.m1.y = v.y; + s.m2.z = v.z; + r = mat4mul(s, m); + return r; } -Mat4 mat4_change_basis(V3 x, V3 y, V3 z) +MAT4 mat4_change_basis(V3 x, V3 y, V3 z) { - Mat4 r = MAT4_IDENTITY; + MAT4 r; + + r = MAT4_IDENTITY; r.m0 = v4(x.x, x.y, x.z, 0.0f); r.m1 = v4(y.x, y.y, y.z, 0.0f); r.m2 = v4(z.x, z.y, z.z, 0.0f); + return r; } @@ -339,11 +463,11 @@ Mat4 mat4_change_basis(V3 x, V3 y, V3 z) * | 0 cx -sx |*| 0 1 0 |*| sz cz 0 |=| sx*sy*cz+cx*sz -sx*sy*sz+cx*cz -sx*cy | * | 0 sx cx | | -sy 0 cy | | 0 0 1 | | -cx*sy*cz+sx*sz cx*sy*sz+sx*cz cx*cy | */ -Mat4 mat4rotate(Mat4 m, V3 angles) +MAT4 mat4rotate(MAT4 m, V3 angles) { - F32 angle, cx, sx, cy, sy, cz, sz; - Mat4 rotate, r; - V3 newx, newy, newz; + F32 angle, cx, sx, cy, sy, cz, sz; + MAT4 rotate, r; + V3 newx, newy, newz; angle = DEG2RAD*angles.x; cx = f32cos(angle); @@ -365,21 +489,22 @@ Mat4 mat4rotate(Mat4 m, V3 angles) return r; } -V4 mat4v4mul(Mat4 m, V4 v) +V4 mat4v4mul(MAT4 m, V4 v) { - V4 r = { - m.m0.x*v.x+m.m1.x*v.y+m.m2.x*v.z+m.m3.x*v.w, - m.m0.y*v.x+m.m1.y*v.y+m.m2.y*v.z+m.m3.y*v.w, - m.m0.z*v.x+m.m1.z*v.y+m.m2.z*v.z+m.m3.z*v.w, - m.m0.w*v.x+m.m1.w*v.y+m.m2.w*v.z+m.m3.w*v.w - }; + V4 r; + + r = v4(m.m0.x*v.x+m.m1.x*v.y+m.m2.x*v.z+m.m3.x*v.w, + m.m0.y*v.x+m.m1.y*v.y+m.m2.y*v.z+m.m3.y*v.w, + m.m0.z*v.x+m.m1.z*v.y+m.m2.z*v.z+m.m3.z*v.w, + m.m0.w*v.x+m.m1.w*v.y+m.m2.w*v.z+m.m3.w*v.w); + return r; } -void mat4print(Mat4 m) +void mat4print(MAT4 m) { - printf("| %.4f %.4f %.4f %.4f |\n", m.m0.x, m.m1.x, m.m2.x, m.m3.x); - printf("| %.4f %.4f %.4f %.4f |\n", m.m0.y, m.m1.y, m.m2.y, m.m3.y); - printf("| %.4f %.4f %.4f %.4f |\n", m.m0.z, m.m1.z, m.m2.z, m.m3.z); - printf("| %.4f %.4f %.4f %.4f |\n\n", m.m0.w, m.m1.w, m.m2.w, m.m3.w); + fprintf(stdout, "| %.4f %.4f %.4f %.4f |\n", m.m0.x, m.m1.x, m.m2.x, m.m3.x); + fprintf(stdout, "| %.4f %.4f %.4f %.4f |\n", m.m0.y, m.m1.y, m.m2.y, m.m3.y); + fprintf(stdout, "| %.4f %.4f %.4f %.4f |\n", m.m0.z, m.m1.z, m.m2.z, m.m3.z); + fprintf(stdout, "| %.4f %.4f %.4f %.4f |\n\n", m.m0.w, m.m1.w, m.m2.w, m.m3.w); } diff --git a/prb_math.h b/prb_math.h index efa0fab..f569100 100644 --- a/prb_math.h +++ b/prb_math.h @@ -7,58 +7,65 @@ #define DEG2RAD F32_PI/180.0f #define RAD2DEG 182.0f/F32_PI -F32 f32sin(F32 a); -F32 f32cos(F32 a); -F32 f32sqrt(F32 a); +F32 f32sin(F32 a); +F32 f32cos(F32 a); +F32 f32tan(F32 a); +F32 f32sqrt(F32 a); /* NOTE(pryazha): Vectors */ -V2 v2(F32 x, F32 y); -V2 v2a(F32 x); -V2 v2neg(V2 a); -V2 v2add(V2 a, V2 b); -V2 v2sub(V2 a, V2 b); -V2 v2scalef(V2 a, F32 s); -V2 v2scale(V2 a, V2 s); -F32 v2dot(V2 a, V2 b); -F32 v2len2(V2 a); -F32 v2len(V2 a); -void v2print(V2 a); +V2 v2(F32 x, F32 y); +V2 v2a(F32 x); +V2 v2inv(V2 a); +V2 v2add(V2 a, V2 b); +V2 v2sub(V2 a, V2 b); +V2 v2scalef(V2 a, F32 s); +V2 v2scale(V2 a, V2 s); +F32 v2dot(V2 a, V2 b); +F32 v2len2(V2 a); +F32 v2len(V2 a); +V2 v2norm(V2 a); +void v2print(V2 a); -V3 v3(F32 x, F32 y, F32 z); -V3 v3a(F32 x); -V3 v3neg(V3 a); -V3 v3add(V3 a, V3 b); -V3 v3sub(V3 a, V3 b); -V3 v3scalef(V3 a, F32 s); -V3 v3scale(V3 a, V3 s); -F32 v3dot(V3 a, V3 b); -V3 v3cross(V3 l, V3 r); -F32 v3len2(V3 a); -F32 v3len(V3 a); -void v3print(V3 a); +V3 v3(F32 x, F32 y, F32 z); +V3 v3a(F32 x); +V3 v3fromv2(V2 a); +V3 v3fromv4(V4 a); +V3 v3inv(V3 a); +V3 v3add(V3 a, V3 b); +V3 v3sub(V3 a, V3 b); +V3 v3scalef(V3 a, F32 s); +V3 v3scale(V3 a, V3 s); +F32 v3dot(V3 a, V3 b); +V3 v3cross(V3 l, V3 r); +F32 v3len2(V3 a); +F32 v3len(V3 a); +V3 v3norm(V3 a); +void v3print(V3 a); -V4 v4(F32 x, F32 y, F32 z, F32 w); -V4 v4a(F32 x); -V4 v4fromv3(V3 a); -V4 v4neg(V4 a); -V4 v4add(V4 a, V4 b); -V4 v4sub(V4 a, V4 b); -V4 v4scalef(V4 a, F32 s); -V4 v4scale(V4 a, V4 s); -F32 v4dot(V4 a, V4 b); -F32 v4len2(V4 a); -F32 v4len(V4 a); -void v4print(V4 a); +V4 v4(F32 x, F32 y, F32 z, F32 w); +V4 v4a(F32 x); +V4 v4fromv3(V3 a); +V4 v4inv(V4 a); +V4 v4add(V4 a, V4 b); +V4 v4sub(V4 a, V4 b); +V4 v4scalef(V4 a, F32 s); +V4 v4scale(V4 a, V4 s); +F32 v4dot(V4 a, V4 b); +F32 v4len2(V4 a); +F32 v4len(V4 a); +V4 v4norm(V4 a); +void v4print(V4 a); /* NOTE(pryazha): Matrices */ -F32 mat4det(Mat4 m); -Mat4 mat4transpose(Mat4 m); -Mat4 mat4mul(Mat4 left, Mat4 right); -Mat4 mat4translate(Mat4 m, V3 v); -Mat4 mat4scale(Mat4 m, V3 v); -Mat4 mat4_change_basis(V3 x, V3 y, V3 z); -Mat4 mat4rotate(Mat4 m, V3 angles); /* NOTE(pryazha): Angles in degrees */ -V4 mat4v4mul(Mat4 m, V4 v); -void mat4print(Mat4 m); +F32 mat4det(MAT4 m); +MAT4 mat4transp(MAT4 m); +MAT4 mat4mul(MAT4 left, MAT4 right); +MAT4 mat4transl(MAT4 m, V3 v); +MAT4 mat4scale(MAT4 m, V3 v); +MAT4 mat4_change_basis(V3 x, V3 y, V3 z); +/* NOTE(pryazha): Angles in degrees */ +MAT4 mat4rotate(MAT4 m, V3 angles); +V4 mat4v4mul(MAT4 m, V4 v); +void mat4print(MAT4 m); #endif /* PRB_MATH_H */ diff --git a/prb_memory.c b/prb_memory.c index 4bceccc..a160465 100644 --- a/prb_memory.c +++ b/prb_memory.c @@ -2,7 +2,7 @@ Arena *arena_alloc(U64 cap) { Arena *arena = 0; - /* TODO(pryazha): Use OS related memory allocator (like VirtualAlloc on Windows) */ + /* TODO(pryazha): Use OS specific memory allocator (like VirtualAlloc on Windows or mmap on Linux) */ arena = malloc(sizeof(Arena)); Assert(arena); diff --git a/prb_os_io.c b/prb_os_io.c index d35b73f..3ef4839 100644 --- a/prb_os_io.c +++ b/prb_os_io.c @@ -1,13 +1,13 @@ Str8 str8_read_entire_file(Arena *arena, Str8 filename) { - /* TODO(pryazha): Make it crossplatform already */ + /* TODO(pryazha): Make it crossplatform */ Assert(filename.ptr); Assert(filename.length); Str8 result = {0}; Arena *tmp = arena_alloc(0); - char *cfilename = str8_to_cstr(tmp, filename); + char *cfilename = str8tocstr(tmp, filename); FILE *f = fopen(cfilename, "rb"); if (!f) diff --git a/prb_os_io.h b/prb_os_io.h index 811778c..7138dec 100644 --- a/prb_os_io.h +++ b/prb_os_io.h @@ -3,4 +3,6 @@ Str8 str8_read_entire_file(Arena *arena, Str8 filename); +Str8 get_cwd(void); + #endif /* PRB_OS_IO_H */ diff --git a/prb_string.c b/prb_string.c index 061731c..bb235df 100644 --- a/prb_string.c +++ b/prb_string.c @@ -1,73 +1,70 @@ Str8 str8(U8 *ptr, U64 length) { Assert(ptr); - Str8 r = { ptr, length }; + + Str8 r; + + r.ptr = ptr; + r.length = length; + return r; } -Str8 str8_range(U8 *start, U8 *end) +Str8 str8range(U8 *start, U8 *end) { - Str8 r = { start, end-start }; + Str8 r; + + r.ptr = start; + r.length = end-start; + return r; } -Str8 str8_from_cstr(char *cstr) +Str8 str8fromcstr(char *cstr) { - U8 *ptr = (U8 *)cstr; + U8 *ptr; + Str8 r; + + ptr = (U8 *)cstr; for (; *ptr; ++ptr); - Str8 r = str8_range((U8 *)cstr, ptr); + r = str8range((U8 *)cstr, ptr); + return r; } -char *str8_to_cstr(Arena *arena, Str8 s) +char *str8tocstr(Arena *arena, Str8 s) { - U64 length = s.length+1; - char *r = arena_push(arena, length*sizeof(U8)); + U64 length; + char *r; + + length = s.length+1; + r = arena_push(arena, length*sizeof(U8)); MemoryCopy(r, s.ptr, length*sizeof(U8)); *(r+s.length) = 0; - return r; -} -Str8 str8_chop_end(Str8 s, U64 count) -{ - U64 length = s.length-ClampTop(count, s.length); - Str8 r = str8(s.ptr, length); return r; } -Str8 str8_chop_start(Str8 s, U64 count) +Str8 str8chopend(Str8 s, U64 count) { - U64 clamped = ClampTop(count, s.length); - U64 length = s.length-ClampTop(count, s.length); - Str8 r = str8(s.ptr+clamped, length); - return r; -} + U64 length; + Str8 r; -Str8 str8_pushfv(Arena *arena, char *fmt, va_list args) -{ - Str8 r = {0}; - - va_list args2; - va_copy(args2, args); - - U32 buf_size = 1024; - U8 *buf = arena_push(arena, buf_size); - - S32 n = vsnprintf((char *)buf, buf_size, fmt, args); - va_end(args2); - - if (n > 0) - r = str8(buf, n); + length = s.length-ClampTop(count, s.length); + r = str8(s.ptr, length); return r; } -Str8 str8_pushf(Arena *arena, char *fmt, ...) +Str8 str8chopstart(Str8 s, U64 count) { - va_list args; - va_start(args, fmt); - Str8 r = str8_pushfv(arena, fmt, args); - va_end(args); + U64 clamped, length; + Str8 r; + + clamped = ClampTop(count, s.length); + length = s.length-ClampTop(count, s.length); + r = str8(s.ptr+clamped, length); + return r; } @@ -77,17 +74,21 @@ void str8print(Str8 s) printf("%c", (char)(*(s.ptr+i))); } -Str8List *str8_list(Arena *arena) +Str8List *str8list(Arena *arena) { - Str8List *list = arena_push(arena, sizeof(Str8List)); + Str8List *list; + list = arena_push(arena, sizeof(Str8List)); MemoryZero(list, sizeof(Str8List)); return list; } -void str8_list_push(Arena *arena, Str8List *list, Str8 str, B32 to_front) +void str8listpush(Arena *arena, Str8List *list, Str8 str, B32 to_front) { Assert(arena && list); - Str8Node *n = arena_push(arena, sizeof(Str8Node)); + + Str8Node *n; + + n = arena_push(arena, sizeof(Str8Node)); n->str = str; if (to_front) DLLPushFront(list->first, list->last, n); @@ -97,19 +98,56 @@ void str8_list_push(Arena *arena, Str8List *list, Str8 str, B32 to_front) list->node_count++; } -void str8_list_pushf(Arena *arena, Str8List *list, B32 to_front, char *fmt, ...) +void str8listprint(Str8List *list) { - va_list args; + Str8Node *n; + for (n = list->first; n; n = n->next) + str8print(n->str); +} + +Str8 str8pushfv(Arena *arena, char *fmt, va_list args) +{ + va_list args2; + Str8 r; + S32 buf_size, n; + U8 *buf; + + va_copy(args2, args); + + buf_size = 1024; + buf = arena_push(arena, buf_size); + + n = vsnprintf((char *)buf, buf_size, fmt, args); + va_end(args2); + + MemoryZero(&r, sizeof(Str8)); + + if (n > 0) + r = str8(buf, n); + + return r; +} + +Str8 str8pushf(Arena *arena, char *fmt, ...) +{ + va_list args; + Str8 r; + va_start(args, fmt); - Str8 str = str8_pushfv(arena, fmt, args); + r = str8pushfv(arena, fmt, args); va_end(args); - str8_list_push(arena, list, str, to_front); + + return r; } -void str8_list_print(Str8List *list) +void str8listpushf(Arena *arena, Str8List *list, B32 to_front, char *fmt, ...) { - for (Str8Node *n = list->first; n; n = n->next) { - str8print(n->str); - printf("%s", (n->next) ? " -> " : "\n"); - } + va_list args; + Str8 str; + + va_start(args, fmt); + str = str8pushfv(arena, fmt, args); + va_end(args); + + str8listpush(arena, list, str, to_front); } diff --git a/prb_string.h b/prb_string.h index 85496cb..49c7d24 100644 --- a/prb_string.h +++ b/prb_string.h @@ -5,18 +5,19 @@ #define str8expand(s) (int)((s).length), ((s).ptr) Str8 str8(U8 *ptr, U64 length); -Str8 str8_range(U8 *start, U8 *end); -Str8 str8_from_cstr(char *cstr); -char *str8_to_cstr(Arena *arena, Str8 s); -Str8 str8_chop_end(Str8 s, U64 count); -Str8 str8_chop_start(Str8 s, U64 count); -Str8 str8_pushfv(Arena *arena, char *fmt, va_list args); -Str8 str8_pushf(Arena *arena, char *fmt, ...); +Str8 str8range(U8 *start, U8 *end); +Str8 str8fromcstr(char *cstr); +char *str8tocstr(Arena *arena, Str8 s); +Str8 str8chopend(Str8 s, U64 count); +Str8 str8chopstart(Str8 s, U64 count); void str8print(Str8 s); -Str8List *str8_list(Arena *arena); -void str8_list_push(Arena *arena, Str8List *list, Str8 str, B32 to_front); -void str8_list_pushf(Arena *arena, Str8List *list, B32 to_front, char *fmt, ...); -void str8_list_print(Str8List *list); +Str8List *str8list(Arena *arena); +void str8listpush(Arena *arena, Str8List *list, Str8 str, B32 to_front); +void str8listprint(Str8List *list); + +Str8 str8pushfv(Arena *arena, char *fmt, va_list args); +Str8 str8pushf(Arena *arena, char *fmt, ...); +void str8listpushf(Arena *arena, Str8List *list, B32 to_front, char *fmt, ...); #endif /* PRB_STRING_H */ diff --git a/prb_types.h b/prb_types.h index a1ac3ba..bfcafcc 100644 --- a/prb_types.h +++ b/prb_types.h @@ -1,84 +1,87 @@ #ifndef PRB_TYPES_H #define PRB_TYPES_H -typedef int8_t S8; -typedef int16_t S16; -typedef int32_t S32; -typedef int64_t S64; -typedef uint8_t U8; -typedef uint16_t U16; -typedef uint32_t U32; -typedef uint64_t U64; -typedef S32 B32; -typedef float F32; -typedef double F64; - -/* NOTE(pryazha): The library uses a right-handed coordiante system (for now) */ +typedef int8_t S8; +typedef int16_t S16; +typedef int32_t S32; +typedef int64_t S64; + +typedef uint8_t U8; +typedef uint16_t U16; +typedef uint32_t U32; +typedef uint64_t U64; + +typedef S32 B32; + +typedef float F32; +typedef double F64; + +/* NOTE(pryazha): The library only use right-handed coordiante system (for now) */ typedef struct { F32 x, y; } V2; -#define V2_ZERO (V2){ 0.0f, 0.0f } -#define V2_ONE (V2){ 1.0f, 1.0f } -#define V2_RIGHT (V2){ 1.0f, 0.0f } -#define V2_UP (V2){ 0.0f, 1.0f } -#define V2_LEFT (V2){-1.0f, 0.0f } -#define V2_DOWN (V2){ 0.0f, -1.0f } +#define V2_ZERO (V2){ 0.0f, 0.0f } +#define V2_ONE (V2){ 1.0f, 1.0f } +#define V2_RIGHT (V2){ 1.0f, 0.0f } +#define V2_UP (V2){ 0.0f, 1.0f } +#define V2_LEFT (V2){-1.0f, 0.0f } +#define V2_DOWN (V2){ 0.0f, -1.0f } typedef struct { F32 x, y, z; } V3; -#define V3_ZERO (V3){ 0.0f, 0.0f, 0.0f } -#define V3_ONE (V3){ 1.0f, 1.0f, 1.0f } -#define V3_RIGHT (V3){ 1.0f, 0.0f, 0.0f } -#define V3_UP (V3){ 0.0f, 1.0f, 0.0f } -#define V3_LEFT (V3){-1.0f, 0.0f, 0.0f } -#define V3_DOWN (V3){ 0.0f, -1.0f, 0.0f } -#define V3_FORWARD (V3){ 0.0f, 0.0f, 1.0f } -#define V3_BACKWARD (V3){ 0.0f, 0.0f, -1.0f } +#define V3_ZERO (V3){ 0.0f, 0.0f, 0.0f } +#define V3_ONE (V3){ 1.0f, 1.0f, 1.0f } +#define V3_RIGHT (V3){ 1.0f, 0.0f, 0.0f } +#define V3_UP (V3){ 0.0f, 1.0f, 0.0f } +#define V3_LEFT (V3){-1.0f, 0.0f, 0.0f } +#define V3_DOWN (V3){ 0.0f, -1.0f, 0.0f } +#define V3_FORWARD (V3){ 0.0f, 0.0f, 1.0f } +#define V3_BACKWARD (V3){ 0.0f, 0.0f, -1.0f } typedef struct { F32 x, y, z, w; } V4; -#define V4_ZERO (V4){ 0.0f, 0.0f, 0.0f, 0.0f } -#define V4_ONE (V4){ 1.0f, 1.0f, 1.0f, 1.0f } +#define V4_ZERO (V4){ 0.0f, 0.0f, 0.0f, 0.0f } +#define V4_ONE (V4){ 1.0f, 1.0f, 1.0f, 1.0f } typedef struct { V4 m0, m1, m2, m3; -} Mat4; +} MAT4; -#define MAT4_IDENTITY (Mat4) { \ +#define MAT4_IDENTITY (MAT4) { \ { 1.0f, 0.0f, 0.0f, 0.0f }, \ { 0.0f, 1.0f, 0.0f, 0.0f }, \ { 0.0f, 0.0f, 1.0f, 0.0f }, \ { 0.0f, 0.0f, 0.0f, 1.0f } } typedef struct { - U8 *mem; - U64 cap; - U64 used; + U8 *mem; + U64 cap; + U64 used; } Arena; /* NOTE(pryazha): Strings */ typedef struct { - U8 *ptr; - U64 length; + U8 *ptr; + U64 length; } Str8; typedef struct Str8Node { - Str8 str; - struct Str8Node *next; + Str8 str; + struct Str8Node *next; struct Str8Node *prev; } Str8Node; typedef struct { - Str8Node *first; - Str8Node *last; - U64 total_length; - U32 node_count; + Str8Node *first; + Str8Node *last; + U64 total_length; + U32 node_count; } Str8List; #endif /* PRB_TYPES_H */ -- cgit v1.2.3-70-g09d2