diff options
| author | tslil clingman <> | 2021-10-27 15:37:28 -0400 |
|---|---|---|
| committer | tslil clingman <> | 2021-10-27 15:37:28 -0400 |
| commit | 48dee0817f5fe8c09a085f0150e0c6b4a699ab7c (patch) | |
| tree | d6ab0a54f2260cb73c79e00ca1489c034f555f09 /src/sfml/system | |
| parent | 486935299e4b7e097754611d9f8dd40003351ae4 (diff) | |
Update SFML wrapper
Diffstat (limited to 'src/sfml/system')
| -rw-r--r-- | src/sfml/system/Clock.zig | 14 | ||||
| -rw-r--r-- | src/sfml/system/Time.zig | 18 | ||||
| -rw-r--r-- | src/sfml/system/vector.zig | 89 |
3 files changed, 79 insertions, 42 deletions
diff --git a/src/sfml/system/Clock.zig b/src/sfml/system/Clock.zig index d8b174e..74b73ab 100644 --- a/src/sfml/system/Clock.zig +++ b/src/sfml/system/Clock.zig @@ -17,29 +17,29 @@ pub fn create() !Clock { if (clock == null) return sf.Error.nullptrUnknownReason; - return Clock{ .ptr = clock.? }; + return Clock{ ._ptr = clock.? }; } /// Destroys this clock -pub fn destroy(self: Clock) void { - sf.c.sfClock_destroy(self.ptr); +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; + 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; +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, +_ptr: *sf.c.sfClock, test "clock: sleep test" { const tst = @import("std").testing; diff --git a/src/sfml/system/Time.zig b/src/sfml/system/Time.zig index 2e8a34c..ae5152a 100644 --- a/src/sfml/system/Time.zig +++ b/src/sfml/system/Time.zig @@ -8,13 +8,13 @@ const Time = @This(); /// Converts a time from a csfml object /// For inner workings -pub fn fromCSFML(time: sf.c.sfTime) Time { +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 { +pub fn _toCSFML(self: Time) sf.c.sfTime { return sf.c.sfTime{ .microseconds = self.us }; } @@ -54,7 +54,7 @@ pub fn asSeconds(self: Time) f32 { /// Sleeps the amount of time specified pub fn sleep(time: Time) void { - sf.c.sfSleep(time.toCSFML()); + sf.c.sfSleep(time._toCSFML()); } /// A time of zero @@ -75,19 +75,19 @@ pub const TimeSpan = struct { /// Converts a timespan from a csfml object /// For inner workings - pub fn fromCSFML(span: sf.c.sfTimeSpan) TimeSpan { + pub fn _fromCSFML(span: sf.c.sfTimeSpan) TimeSpan { return TimeSpan{ - .offset = Time.fromCSFML(span.offset), - .length = Time.fromCSFML(span.length), + .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 { + pub fn _toCSFML(self: TimeSpan) sf.c.sfTimeSpan { return sf.c.sfTimeSpan{ - .offset = self.offset.toCSFML(), - .length = self.length.toCSFML(), + .offset = self.offset._toCSFML(), + .length = self.length._toCSFML(), }; } 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); } |
