stdlib: relocate modules under library/
- examples/modules/ -> library/modules/ (top-level, no more symlink hacks in consumer projects) - compiler discovers stdlib via _NSGetExecutablePath / readlink /proc/self/exe; searches dev layout (../../library), install layout (../library), and alongside-binary fallback - SX_STDLIB_PATH env var overrides for tests / dev convenience - SX_DEBUG_STDLIB env var dumps the discovery results - build.zig installs library/ alongside the binary - Compilation gains stdlib_paths field threaded through resolveImports - 50 tests pass; consumer projects can now build from any cwd
This commit is contained in:
37
library/modules/math/math.sx
Normal file
37
library/modules/math/math.sx
Normal file
@@ -0,0 +1,37 @@
|
||||
PI :f32: 3.14159265;
|
||||
TAU :f32: 6.28318530;
|
||||
DEG2RAD :f32: 0.01745329;
|
||||
RAD2DEG :f32: 57.2957795;
|
||||
|
||||
sqrt :: (x: $T) -> T #builtin;
|
||||
sin :: (x: $T) -> T #builtin;
|
||||
cos :: (x: $T) -> T #builtin;
|
||||
floor :: (x: $T) -> T #builtin;
|
||||
|
||||
min :: (a: $T, b: T) -> T {
|
||||
if a < b then a else b;
|
||||
}
|
||||
|
||||
max :: (a: $T, b: T) -> T {
|
||||
if a > b then a else b;
|
||||
}
|
||||
|
||||
clamp :: (val: $T, lo: T, hi: T) -> T {
|
||||
if val < lo then lo
|
||||
else if val > hi then hi
|
||||
else val;
|
||||
}
|
||||
|
||||
abs :: (x: $T) -> T {
|
||||
if x < 0 then 0 - x else x;
|
||||
}
|
||||
|
||||
lerp :: (a: f32, b: f32, t: f32) -> f32 {
|
||||
a + (b - a) * t;
|
||||
}
|
||||
|
||||
sign :: (x: $T) -> T {
|
||||
if x > 0 then 1
|
||||
else if x < 0 then 0 - 1
|
||||
else 0;
|
||||
}
|
||||
117
library/modules/math/matrix44.sx
Normal file
117
library/modules/math/matrix44.sx
Normal file
@@ -0,0 +1,117 @@
|
||||
// Column-major 4x4 float matrix (OpenGL convention)
|
||||
// data[col * 4 + row]
|
||||
|
||||
Mat4 :: struct {
|
||||
data: [16]f32;
|
||||
|
||||
identity :: () -> Mat4 {
|
||||
Mat4.{ data = .[
|
||||
1.0, 0.0, 0.0, 0.0,
|
||||
0.0, 1.0, 0.0, 0.0,
|
||||
0.0, 0.0, 1.0, 0.0,
|
||||
0.0, 0.0, 0.0, 1.0
|
||||
]};
|
||||
}
|
||||
|
||||
zero :: () -> Mat4 {
|
||||
Mat4.{ data = .[
|
||||
0.0, 0.0, 0.0, 0.0,
|
||||
0.0, 0.0, 0.0, 0.0,
|
||||
0.0, 0.0, 0.0, 0.0,
|
||||
0.0, 0.0, 0.0, 0.0
|
||||
]};
|
||||
}
|
||||
|
||||
mul :: (self: Mat4, b: Mat4) -> Mat4 {
|
||||
r := Mat4.zero();
|
||||
col := 0;
|
||||
while col < 4 {
|
||||
row := 0;
|
||||
while row < 4 {
|
||||
sum : f32 = 0.0;
|
||||
k := 0;
|
||||
while k < 4 {
|
||||
sum = sum + self.data[k * 4 + row] * b.data[col * 4 + k];
|
||||
k += 1;
|
||||
}
|
||||
r.data[col * 4 + row] = sum;
|
||||
row += 1;
|
||||
}
|
||||
col += 1;
|
||||
}
|
||||
r;
|
||||
}
|
||||
|
||||
translate :: (x: f32, y: f32, z: f32) -> Mat4 {
|
||||
m := Mat4.identity();
|
||||
m.data[12] = x;
|
||||
m.data[13] = y;
|
||||
m.data[14] = z;
|
||||
m;
|
||||
}
|
||||
|
||||
scale :: (x: f32, y: f32, z: f32) -> Mat4 {
|
||||
m := Mat4.zero();
|
||||
m.data[0] = x;
|
||||
m.data[5] = y;
|
||||
m.data[10] = z;
|
||||
m.data[15] = 1.0;
|
||||
m;
|
||||
}
|
||||
|
||||
rotate_x :: (angle: f32) -> Mat4 {
|
||||
c := cos(angle);
|
||||
s := sin(angle);
|
||||
m := Mat4.identity();
|
||||
m.data[5] = c;
|
||||
m.data[6] = s;
|
||||
m.data[9] = 0.0 - s;
|
||||
m.data[10] = c;
|
||||
m;
|
||||
}
|
||||
|
||||
rotate_y :: (angle: f32) -> Mat4 {
|
||||
c := cos(angle);
|
||||
s := sin(angle);
|
||||
m := Mat4.identity();
|
||||
m.data[0] = c;
|
||||
m.data[2] = 0.0 - s;
|
||||
m.data[8] = s;
|
||||
m.data[10] = c;
|
||||
m;
|
||||
}
|
||||
|
||||
rotate_z :: (angle: f32) -> Mat4 {
|
||||
c := cos(angle);
|
||||
s := sin(angle);
|
||||
m := Mat4.identity();
|
||||
m.data[0] = c;
|
||||
m.data[1] = s;
|
||||
m.data[4] = 0.0 - s;
|
||||
m.data[5] = c;
|
||||
m;
|
||||
}
|
||||
|
||||
ortho :: (left: f32, right: f32, bottom: f32, top: f32, near: f32, far: f32) -> Mat4 {
|
||||
m := Mat4.zero();
|
||||
m.data[0] = 2.0 / (right - left);
|
||||
m.data[5] = 2.0 / (top - bottom);
|
||||
m.data[10] = 0.0 - 2.0 / (far - near);
|
||||
m.data[12] = 0.0 - (right + left) / (right - left);
|
||||
m.data[13] = 0.0 - (top + bottom) / (top - bottom);
|
||||
m.data[14] = 0.0 - (far + near) / (far - near);
|
||||
m.data[15] = 1.0;
|
||||
m;
|
||||
}
|
||||
|
||||
perspective :: (fov: f32, aspect: f32, near: f32, far: f32) -> Mat4 {
|
||||
half_tan := sin(fov * 0.5) / cos(fov * 0.5);
|
||||
m := Mat4.zero();
|
||||
m.data[0] = 1.0 / (aspect * half_tan);
|
||||
m.data[5] = 1.0 / half_tan;
|
||||
m.data[10] = 0.0 - (far + near) / (far - near);
|
||||
m.data[11] = 0.0 - 1.0;
|
||||
m.data[14] = 0.0 - 2.0 * far * near / (far - near);
|
||||
m;
|
||||
}
|
||||
}
|
||||
49
library/modules/math/vector2.sx
Normal file
49
library/modules/math/vector2.sx
Normal file
@@ -0,0 +1,49 @@
|
||||
Vec2 :: struct {
|
||||
x, y: f32;
|
||||
|
||||
zero :: () -> Vec2 { Vec2.{ x = 0.0, y = 0.0 }; }
|
||||
|
||||
add :: (self: Vec2, b: Vec2) -> Vec2 {
|
||||
Vec2.{ x = self.x + b.x, y = self.y + b.y };
|
||||
}
|
||||
|
||||
sub :: (self: Vec2, b: Vec2) -> Vec2 {
|
||||
Vec2.{ x = self.x - b.x, y = self.y - b.y };
|
||||
}
|
||||
|
||||
scale :: (self: Vec2, s: f32) -> Vec2 {
|
||||
Vec2.{ x = self.x * s, y = self.y * s };
|
||||
}
|
||||
|
||||
dot :: (self: Vec2, b: Vec2) -> f32 {
|
||||
self.x * b.x + self.y * b.y;
|
||||
}
|
||||
|
||||
length :: (self: Vec2) -> f32 {
|
||||
sqrt(self.x * self.x + self.y * self.y);
|
||||
}
|
||||
|
||||
normalize :: (self: Vec2) -> Vec2 {
|
||||
len := self.length();
|
||||
if len > 0.0 {
|
||||
return Vec2.{ x = self.x / len, y = self.y / len };
|
||||
}
|
||||
Vec2.zero();
|
||||
}
|
||||
|
||||
lerp :: (self: Vec2, b: Vec2, t: f32) -> Vec2 {
|
||||
Vec2.{ x = self.x + (b.x - self.x) * t, y = self.y + (b.y - self.y) * t };
|
||||
}
|
||||
|
||||
distance :: (self: Vec2, b: Vec2) -> f32 {
|
||||
self.sub(b).length();
|
||||
}
|
||||
|
||||
negate :: (self: Vec2) -> Vec2 {
|
||||
Vec2.{ x = 0.0 - self.x, y = 0.0 - self.y };
|
||||
}
|
||||
|
||||
equals :: (self: Vec2, b: Vec2) -> bool {
|
||||
self.x == b.x and self.y == b.y;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user