aboutsummaryrefslogtreecommitdiff
path: root/src/sfml/system/vector.zig
blob: 7e3e500ba86ce1e5f126119312e779ab04a4ef2d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
//! Utility struct for manipulating 2-dimensional vectors.

const sf = @import("../sfml_import.zig");

pub fn Vector2(comptime T: type) type {
    return packed struct {
        const Self = @This();

        /// The CSFML vector type equivalent
        const CsfmlEquivalent = switch (T) {
            c_uint => sf.c.sfVector2u,
            c_int => sf.c.sfVector2i,
            f32 => sf.c.sfVector2f,
            else => void,
        };

        /// 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 {
            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 {
            if (CsfmlEquivalent == void) @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 };
        }

        /// Substracts two vectors
        pub fn substract(self: Self, other: Self) Self {
            return Self{ .x = self.x - other.x, .y = self.y - other.y };
        }

        /// Scales a vector
        pub fn scale(self: Self, scalar: T) Self {
            return Self{ .x = self.x * scalar, .y = self.y * scalar };
        }

        /// x component of the vector
        x: T,
        /// y component of the vector
        y: T
    };
}

pub const Vector3f = 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);
    }

    /// 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);
    }

    /// x component of the vector
    x: f32,
    /// y component of the vector
    y: f32,
    /// z component of the vector
    z: f32,
};

test "vector: sane from/to CSFML vectors" {
    const tst = @import("std").testing;

    inline for ([_]type{ c_int, c_uint, f32 }) |T| {
        const VecT = Vector2(T);
        const vec = VecT{ .x = 1, .y = 3 };
        const cvec = vec.toCSFML();

        try tst.expectEqual(vec.x, cvec.x);
        try tst.expectEqual(vec.y, cvec.y);

        const vec2 = VecT.fromCSFML(cvec);

        try tst.expectEqual(vec, vec2);
    }

    {
        const vec = Vector3f{ .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);

        try tst.expectEqual(vec, vec2);
    }
}