summaryrefslogtreecommitdiff
path: root/source/calcify.h
diff options
context:
space:
mode:
authortalha <talha@talhaamir.xyz>2025-11-27 13:18:57 +0500
committertalha <talha@talhaamir.xyz>2025-11-27 13:18:57 +0500
commit0b3d69976819219e71350b6a988d1704fd5f0746 (patch)
tree3511564cb2cb3b10697dc997260479a14540d706 /source/calcify.h
Added files to git.HEADmain
Current State: - hot reloading - math library (calcify) - triangle rendering (unbatched) - orthographic projection (no camera) - layer isolation setup (platform vs game)
Diffstat (limited to 'source/calcify.h')
-rw-r--r--source/calcify.h146
1 files changed, 146 insertions, 0 deletions
diff --git a/source/calcify.h b/source/calcify.h
new file mode 100644
index 0000000..d0c3499
--- /dev/null
+++ b/source/calcify.h
@@ -0,0 +1,146 @@
+#pragma once
+
+#include <math.h>
+#include <string.h>
+
+#if !AMR_MATH_USE_TYPES
+#include <stdint.h>
+
+typedef int32_t i32;
+typedef int64_t i64;
+
+typedef float r32;
+typedef double r64;
+
+typedef int8_t b8;
+#endif
+
+#define PI 3.14159265358979323846264338327950288f
+#define SQUARE(x) ((x)*(x))
+#define TO_RAD(x) ((x)*PI/180.0f)
+#define TO_DEG(x) ((x)*180.0f/PI)
+#define ABS(x) ((x) > 0 ? (x) : -(x))
+#define MIN(x, y) ((x) < (y) ? (y) : (x))
+#define MAX(x, y) ((x) > (y) ? (y) : (x))
+
+union Vec3 {
+ struct {
+ r32 x;
+ r32 y;
+ r32 z;
+ };
+ struct {
+ r32 r;
+ r32 g;
+ r32 b;
+ };
+ r32 buffer[3];
+};
+
+union Vec4 {
+ struct {
+ r32 x;
+ r32 y;
+ r32 z;
+ r32 w;
+ };
+ r32 buffer[4];
+};
+
+union Mat4 {
+ Vec4 row[4];
+ r32 array[4][4];
+ r32 buffer[16];
+};
+
+Mat4 calcify_init4m(void);
+Mat4 calcify_ident4m(void);
+Mat4 calcify_multiply4mv(Mat4 a, Mat4 b);
+Mat4 calcify_multiply4m(Mat4 a, Mat4 b);
+Mat4 calcify_translation_matrix4m(r32 x, r32 y, r32 z);
+Mat4 calcify_ortho4m(r32 left, r32 right, r32 top, r32 bot, r32 near, r32 far);
+
+Mat4 calcify_init4m(void) {
+ Mat4 res;
+ memset(&res, 0, sizeof(Mat4));
+ return res;
+}
+
+Mat4 calcify_ident4m(void) {
+ Mat4 res = calcify_init4m();
+ res.array[0][0] = 1.0f;
+ res.array[1][1] = 1.0f;
+ res.array[2][2] = 1.0f;
+ res.array[3][3] = 1.0f;
+
+ return res;
+}
+
+Vec4 calcify_multiply4mv(Mat4 m, Vec4 v) {
+ Vec4 res = {0};
+ res.x = 0;
+ res.y = 0;
+ res.z = 0;
+ res.w = 0;
+
+ res.x = v.x*m.array[0][0];
+ res.y = v.x*m.array[0][1];
+ res.z = v.x*m.array[0][2];
+ res.w = v.x*m.array[0][3];
+
+ res.x += v.y*m.array[1][0];
+ res.y += v.y*m.array[1][1];
+ res.z += v.y*m.array[1][2];
+ res.w += v.y*m.array[1][3];
+
+ res.x += v.z*m.array[2][0];
+ res.y += v.z*m.array[2][1];
+ res.z += v.z*m.array[2][2];
+ res.w += v.z*m.array[2][3];
+
+ res.x += v.w*m.array[3][0];
+ res.y += v.w*m.array[3][1];
+ res.z += v.w*m.array[3][2];
+ res.w += v.w*m.array[3][3];
+
+ return res;
+};
+
+Mat4 calcify_multiply4m(Mat4 a, Mat4 b) {
+ Mat4 res = calcify_init4m();
+
+ res.row[0] = calcify_multiply4mv(a, b.row[0]);
+ res.row[1] = calcify_multiply4mv(a, b.row[1]);
+ res.row[2] = calcify_multiply4mv(a, b.row[2]);
+ res.row[3] = calcify_multiply4mv(a, b.row[3]);
+
+ return res;
+}
+
+Mat4 calcify_translation_matrix4m(r32 x, r32 y, r32 z) {
+ Mat4 trans = calcify_ident4m();
+ trans.row[3] = Vec4{x, y, z, 1.0f};
+ return trans;
+};
+
+Mat4 calcify_scaling_matrix4m(r32 x, r32 y) {
+ Mat4 scale = calcify_ident4m();
+ scale.array[0][0] = x;
+ scale.array[1][1] = y;
+ scale.array[2][2] = 0;
+
+ return scale;
+}
+
+Mat4 calcify_ortho4m(r32 left, r32 right, r32 bottom, r32 top, r32 near, r32 far) {
+ Mat4 res = calcify_init4m();
+ res.array[0][0] = 2.0f/(right - left);
+ res.array[1][1] = 2.0f/(top - bottom);
+ res.array[2][2] = 2.0f/(near - far);
+ res.array[3][0] = (right + left)/(left - right);
+ res.array[3][1] = (top + bottom)/(bottom - top);
+ res.array[3][2] = (far + near)/(near - far);
+ res.array[3][3] = 1.0f;
+
+ return(res);
+}