diff options
| author | tslil clingman <> | 2021-10-30 16:54:03 -0400 |
|---|---|---|
| committer | tslil clingman <> | 2021-10-30 16:56:41 -0400 |
| commit | 6ead90c0f0e9b9a44c7c1f7137f437faa29fa750 (patch) | |
| tree | 38cbaff4f606689a149865d5708dadac47ee43db /src/sfml/system | |
| parent | 48dee0817f5fe8c09a085f0150e0c6b4a699ab7c (diff) | |
Switched to tracking zig-sfml-wrapper as submodule
Diffstat (limited to 'src/sfml/system')
| -rw-r--r-- | src/sfml/system/Clock.zig | 59 | ||||
| -rw-r--r-- | src/sfml/system/Time.zig | 114 | ||||
| -rw-r--r-- | src/sfml/system/vector.zig | 141 |
3 files changed, 0 insertions, 314 deletions
diff --git a/src/sfml/system/Clock.zig b/src/sfml/system/Clock.zig deleted file mode 100644 index 74b73ab..0000000 --- a/src/sfml/system/Clock.zig +++ /dev/null @@ -1,59 +0,0 @@ -//! Utility class that measures the elapsed time. - -const sf = struct { - pub usingnamespace @import("../sfml.zig"); - pub usingnamespace sf.system; - pub usingnamespace sf.graphics; -}; - -const Clock = @This(); - -// Constructor/destructor - -/// Inits a clock. The clock will have its time set at 0 at this point, and automatically starts -/// Std.time timer also is a good alternative -pub fn create() !Clock { - var clock = sf.c.sfClock_create(); - if (clock == null) - return sf.Error.nullptrUnknownReason; - - return Clock{ ._ptr = clock.? }; -} - -/// Destroys this clock -pub fn destroy(self: *Clock) void { - sf.c.sfClock_destroy(self._ptr); -} - -// Clock control -/// Gets the elapsed seconds -pub fn getElapsedTime(self: Clock) sf.Time { - var time = sf.c.sfClock_getElapsedTime(self._ptr).microseconds; - return sf.Time{ .us = time }; -} - -/// Gets the elapsed seconds and restarts the timer -pub fn restart(self: *Clock) sf.Time { - var time = sf.c.sfClock_restart(self._ptr).microseconds; - return sf.Time{ .us = time }; -} - -/// Pointer to the csfml structure -_ptr: *sf.c.sfClock, - -test "clock: sleep test" { - const tst = @import("std").testing; - - // This tests just sleeps and check what the timer measured (not very accurate but eh) - var clk = try Clock.create(); - defer clk.destroy(); - - sf.Time.milliseconds(500).sleep(); - - try tst.expectApproxEqAbs(@as(f32, 0.5), clk.getElapsedTime().asSeconds(), 0.1); - - sf.Time.sleep(sf.Time.seconds(0.2)); - - try tst.expectApproxEqAbs(@as(f32, 0.7), clk.restart().asSeconds(), 0.1); - try tst.expectApproxEqAbs(@as(f32, 0), clk.getElapsedTime().asSeconds(), 0.01); -} diff --git a/src/sfml/system/Time.zig b/src/sfml/system/Time.zig deleted file mode 100644 index ae5152a..0000000 --- a/src/sfml/system/Time.zig +++ /dev/null @@ -1,114 +0,0 @@ -//! 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); -} diff --git a/src/sfml/system/vector.zig b/src/sfml/system/vector.zig deleted file mode 100644 index 3190d59..0000000 --- a/src/sfml/system/vector.zig +++ /dev/null @@ -1,141 +0,0 @@ -//! Utility struct for manipulating 2-dimensional vectors. - -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(); - - /// 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 - }; -} - -/// 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 { - 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 { - 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 - }; -} - -/// 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; - - 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 = 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 = Vector3(f32)._fromCSFML(cvec); - - try tst.expectEqual(vec, vec2); - } -} |
