aboutsummaryrefslogtreecommitdiff
path: root/src/sfml/system/vector.zig
diff options
context:
space:
mode:
authortslil clingman <>2021-10-27 15:37:28 -0400
committertslil clingman <>2021-10-27 15:37:28 -0400
commit48dee0817f5fe8c09a085f0150e0c6b4a699ab7c (patch)
treed6ab0a54f2260cb73c79e00ca1489c034f555f09 /src/sfml/system/vector.zig
parent486935299e4b7e097754611d9f8dd40003351ae4 (diff)
Update SFML wrapper
Diffstat (limited to 'src/sfml/system/vector.zig')
-rw-r--r--src/sfml/system/vector.zig89
1 files changed, 63 insertions, 26 deletions
diff --git a/src/sfml/system/vector.zig b/src/sfml/system/vector.zig
index 7e3e500..3190d59 100644
--- a/src/sfml/system/vector.zig
+++ b/src/sfml/system/vector.zig
@@ -2,6 +2,7 @@
const sf = @import("../sfml_import.zig");
+/// Template for a 2 dimensional vector
pub fn Vector2(comptime T: type) type {
return packed struct {
const Self = @This();
@@ -16,14 +17,14 @@ pub fn Vector2(comptime T: type) type {
/// Makes a CSFML vector with this vector (only if the corresponding type exists)
/// This is mainly for the inner workings of this wrapper
- pub fn toCSFML(self: Self) CsfmlEquivalent {
+ pub fn _toCSFML(self: Self) CsfmlEquivalent {
if (CsfmlEquivalent == void) @compileError("This vector type doesn't have a CSFML equivalent.");
return @bitCast(CsfmlEquivalent, self);
}
/// Creates a vector from a CSFML one (only if the corresponding type exists)
/// This is mainly for the inner workings of this wrapper
- pub fn fromCSFML(vec: CsfmlEquivalent) Self {
+ pub fn _fromCSFML(vec: CsfmlEquivalent) Self {
if (CsfmlEquivalent == void) @compileError("This vector type doesn't have a CSFML equivalent.");
return @bitCast(Self, vec);
}
@@ -50,28 +51,64 @@ pub fn Vector2(comptime T: type) type {
};
}
-pub const Vector3f = struct {
- const Self = @This();
+/// Template for a 3 dimensional vector
+pub fn Vector3(comptime T: type) type {
+ return packed struct {
+ const Self = @This();
- /// Makes a CSFML vector with this vector (only if the corresponding type exists)
- /// This is mainly for the inner workings of this wrapper
- pub fn toCSFML(self: Self) sf.c.sfVector3f {
- return @bitCast(sf.c.sfVector3f, self);
- }
+ /// Makes a CSFML vector with this vector (only if the corresponding type exists)
+ /// This is mainly for the inner workings of this wrapper
+ pub fn _toCSFML(self: Self) sf.c.sfVector3f {
+ if (T != f32) @compileError("This vector type doesn't have a CSFML equivalent.");
+ return @bitCast(sf.c.sfVector3f, self);
+ }
- /// Creates a vector from a CSFML one (only if the corresponding type exists)
- /// This is mainly for the inner workings of this wrapper
- pub fn fromCSFML(vec: sf.c.sfVector3f) Self {
- return @bitCast(Self, vec);
- }
+ /// Creates a vector from a CSFML one (only if the corresponding type exists)
+ /// This is mainly for the inner workings of this wrapper
+ pub fn _fromCSFML(vec: sf.c.sfVector3f) Self {
+ if (T != f32) @compileError("This vector type doesn't have a CSFML equivalent.");
+ return @bitCast(Self, vec);
+ }
+
+ /// Adds two vectors
+ pub fn add(self: Self, other: Self) Self {
+ return Self{ .x = self.x + other.x, .y = self.y + other.y, .z = self.z + other.z };
+ }
+
+ /// Substracts two vectors
+ pub fn substract(self: Self, other: Self) Self {
+ return Self{ .x = self.x - other.x, .y = self.y - other.y, .z = self.z - other.z };
+ }
+
+ /// Scales a vector
+ pub fn scale(self: Self, scalar: T) Self {
+ return Self{ .x = self.x * scalar, .y = self.y * scalar, .z = self.z * scalar };
+ }
+
+ /// x component of the vector
+ x: T,
+ /// y component of the vector
+ y: T,
+ /// z component of the vector
+ z: T
+ };
+}
- /// x component of the vector
- x: f32,
- /// y component of the vector
- y: f32,
- /// z component of the vector
- z: f32,
-};
+/// Template for a 4 dimensional vector
+/// SFML doesn't really have this but as shaders use such vectors it can be here anyways
+pub fn Vector4(comptime T: type) type {
+ return packed struct {
+ const Self = @This();
+ /// x component of the vector
+ x: T,
+ /// y component of the vector
+ y: T,
+ /// z component of the vector
+ z: T,
+ /// w component of the vector
+ w: T
+ };
+}
test "vector: sane from/to CSFML vectors" {
const tst = @import("std").testing;
@@ -79,25 +116,25 @@ test "vector: sane from/to CSFML vectors" {
inline for ([_]type{ c_int, c_uint, f32 }) |T| {
const VecT = Vector2(T);
const vec = VecT{ .x = 1, .y = 3 };
- const cvec = vec.toCSFML();
+ const cvec = vec._toCSFML();
try tst.expectEqual(vec.x, cvec.x);
try tst.expectEqual(vec.y, cvec.y);
- const vec2 = VecT.fromCSFML(cvec);
+ const vec2 = VecT._fromCSFML(cvec);
try tst.expectEqual(vec, vec2);
}
{
- const vec = Vector3f{ .x = 1, .y = 3.5, .z = -12 };
- const cvec = vec.toCSFML();
+ const vec = Vector3(f32){ .x = 1, .y = 3.5, .z = -12 };
+ const cvec = vec._toCSFML();
try tst.expectEqual(vec.x, cvec.x);
try tst.expectEqual(vec.y, cvec.y);
try tst.expectEqual(vec.z, cvec.z);
- const vec2 = Vector3f.fromCSFML(cvec);
+ const vec2 = Vector3(f32)._fromCSFML(cvec);
try tst.expectEqual(vec, vec2);
}