aboutsummaryrefslogtreecommitdiff
path: root/src/sfml/system/Time.zig
blob: 2e8a34cef582a477cf71f65065d594dced02aec2 (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
105
106
107
108
109
110
111
112
113
114
//! Represents a time value.

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

const Time = @This();

// Constructors

/// Converts a time from a csfml object
/// For inner workings
pub fn fromCSFML(time: sf.c.sfTime) Time {
    return Time{ .us = time.microseconds };
}

/// Converts a time to a csfml object
/// For inner workings
pub fn toCSFML(self: Time) sf.c.sfTime {
    return sf.c.sfTime{ .microseconds = self.us };
}

/// Creates a time object from a seconds count
pub fn seconds(s: f32) Time {
    return Time{ .us = @floatToInt(i64, s * 1_000) * 1_000 };
}

/// Creates a time object from milliseconds
pub fn milliseconds(ms: i32) Time {
    return Time{ .us = @intCast(i64, ms) * 1_000 };
}

/// Creates a time object from microseconds
pub fn microseconds(us: i64) Time {
    return Time{ .us = us };
}

// Getters

/// Gets this time measurement as microseconds
pub fn asMicroseconds(self: Time) i64 {
    return self.us;
}

/// Gets this time measurement as milliseconds
pub fn asMilliseconds(self: Time) i32 {
    return @truncate(i32, @divFloor(self.us, 1_000));
}

/// Gets this time measurement as seconds (as a float)
pub fn asSeconds(self: Time) f32 {
    return @intToFloat(f32, self.us) / 1_000_000;
}

// Misc

/// Sleeps the amount of time specified
pub fn sleep(time: Time) void {
    sf.c.sfSleep(time.toCSFML());
}

/// A time of zero
pub const Zero = microseconds(0);

us: i64,

pub const TimeSpan = struct {
    // Constructors

    /// Construcs a time span
    pub fn init(begin: Time, length: Time) TimeSpan {
        return TimeSpan{
            .offset = begin,
            .length = length,
        };
    }

    /// Converts a timespan from a csfml object
    /// For inner workings
    pub fn fromCSFML(span: sf.c.sfTimeSpan) TimeSpan {
        return TimeSpan{
            .offset = Time.fromCSFML(span.offset),
            .length = Time.fromCSFML(span.length),
        };
    }

    /// Converts a timespan to a csfml object
    /// For inner workings
    pub fn toCSFML(self: TimeSpan) sf.c.sfTimeSpan {
        return sf.c.sfTimeSpan{
            .offset = self.offset.toCSFML(),
            .length = self.length.toCSFML(),
        };
    }

    /// The beginning of this span
    offset: Time,
    /// The length of this time span
    length: Time,
};

test "time: conversion" {
    const tst = @import("std").testing;

    var t = Time.microseconds(5_120_000);

    try tst.expectEqual(@as(i32, 5_120), t.asMilliseconds());
    try tst.expectApproxEqAbs(@as(f32, 5.12), t.asSeconds(), 0.0001);

    t = Time.seconds(12);

    try tst.expectApproxEqAbs(@as(f32, 12), t.asSeconds(), 0.0001);

    t = Time.microseconds(800);
    try tst.expectApproxEqAbs(@as(f32, 0.0008), t.asSeconds(), 0.0001);
}