diff options
Diffstat (limited to 'src/zeal.zig')
-rw-r--r-- | src/zeal.zig | 55 |
1 files changed, 22 insertions, 33 deletions
diff --git a/src/zeal.zig b/src/zeal.zig index af9fd85..d470fbb 100644 --- a/src/zeal.zig +++ b/src/zeal.zig @@ -59,36 +59,34 @@ pub const Context = struct { try alc.makeContextCurrent(null); try alc.destroyContext(self.pimpl); } -}; -pub fn useContext(context: ?Context) alc.Error!void { - try alc.makeContextCurrent(if (context == null) null else context.?.pimpl); -} + pub fn makeCurrent(self: ?Context) alc.Error!void { + try alc.makeContextCurrent(if (self) |context| context.pimpl else null); + } -pub fn currentContext() ?alc.Error!Context { - if (alc.getCurrentContext()) |pimpl| - return Context { - .pimpl = pimpl, - .device = Device { .pimpl = try alc.getContextsDevice(pimpl) }, - }; - return null; -} + pub fn getCurrent() ?alc.Error!Context { + if (alc.getCurrentContext()) |pimpl| + return Context { + .pimpl = pimpl, + .device = Device { .pimpl = try alc.getContextsDevice(pimpl) }, + }; + return null; + } -fn checkContext(context: Context) !void { - if (context.pimpl != alc.getCurrentContext()) - return error.UncurrentContext; -} + pub fn checkCurrent(self: Context) !void { + if (self.pimpl != alc.getCurrentContext()) + return error.UncurrentContext; + } +}; pub const Listener = struct { context: Context, pub fn setPosition(self: Listener, position: [3]f32) Error!void { - try checkContext(self.context); try al.listener.set(al.POSITION, position); } pub fn setOrientation(self: Listener, at: [3]f32, up: [3]f32) Error!void { - try checkContext(self.context); const orientation = [_]f32{ at[0], at[1], at[2], up[0], up[1], up[2] }; try al.listener.set(al.ORIENTATION, orientation); } @@ -129,14 +127,12 @@ pub const Buffer = struct { reference: u32, pub fn init(context: Context, audio: Audio) Error!Buffer { - try checkContext(context); const reference = try al.buffer.create(); try al.buffer.fill(reference, audio.data, audio.frequency); return Buffer{ .context = context, .reference = reference }; } pub fn deinit(self: Buffer) Error!void { - try checkContext(self.context); try al.buffer.destroy(&self.reference); } }; @@ -146,13 +142,11 @@ pub const Source = struct { reference: u32, pub fn init(context: Context) Error!Source { - try checkContext(context); const reference = try al.source.create(); return Source{ .context = context, .reference = reference }; } pub fn deinit(self: Source) Error!void { - try checkContext(self.context); try al.source.destroy(&self.reference); } @@ -160,28 +154,23 @@ pub const Source = struct { /// never has 3D spatialization features (al.OFF), or if spatialization /// is enabled based on playing a mono sound or not (al.AUTO, default). pub fn setSpatialize(self: Source, value: i32) Error!void { - try checkContext(self.context); try al.source.set(self.reference, al.SOURCE_SPATIALIZE, value); } pub fn setPosition(self: Source, position: [3]f32) Error!void { - try checkContext(self.context); try al.source.set(self.reference, al.POSITION, position); } pub fn isPlaying(self: Source) Error!bool { - try checkContext(self.context); const state = try al.source.get(i32, self.reference, al.SOURCE_STATE); return state == al.PLAYING; } pub fn getSecOffset(self: Source) Error!f32 { - try checkContext(self.context); return try al.source.get(f32, self.reference, al.SEC_OFFSET); } pub fn play(self: Source, buffer: Buffer) Error!void { - try checkContext(self.context); try al.source.set(self.reference, al.BUFFER, @intCast(i32, buffer.reference)); try al.source.play(self.reference); @@ -201,16 +190,16 @@ test "Context" { const context = try Context.init(device, &.{ alc.HRTF, alc.TRUE }); defer context.deinit() catch unreachable; - try expectEqual(currentContext(), null); - if (checkContext(context)) unreachable else |err| switch (err) { + try expectEqual(Context.getCurrent(), null); + if (context.checkCurrent()) unreachable else |err| switch (err) { error.UncurrentContext => {}, } - try useContext(context); - const current_context = try currentContext().?; + try context.makeCurrent(); + const current_context = try Context.getCurrent().?; try expectEqual(current_context, context); try expectEqual(current_context.device, device); - try checkContext(context); + try context.checkCurrent(); } test "Listener" { @@ -218,7 +207,7 @@ test "Listener" { defer context.device.deinit() catch unreachable; defer context.deinit() catch unreachable; - try useContext(context); + try context.makeCurrent(); const listener = Listener{ .context = context }; try listener.setPosition(.{ 1, 2, 3 }); try listener.setOrientation(.{ 4, 5, 6 }, .{ 7, 8, 9 }); |