From ceabcec7a33cea3a86ca666f2d8554d93a9f01ea Mon Sep 17 00:00:00 2001 From: Stephen Gutekanst Date: Sat, 17 Jul 2021 21:29:51 -0700 Subject: [PATCH] glfw: add GammaRamp type Signed-off-by: Stephen Gutekanst --- glfw/src/GammaRamp.zig | 74 ++++++++++++++++++++++++++++++++++++++++++ glfw/src/main.zig | 2 ++ 2 files changed, 76 insertions(+) create mode 100644 glfw/src/GammaRamp.zig diff --git a/glfw/src/GammaRamp.zig b/glfw/src/GammaRamp.zig new file mode 100644 index 00000000..1bd4888c --- /dev/null +++ b/glfw/src/GammaRamp.zig @@ -0,0 +1,74 @@ +//! Gamma ramp for monitors and related functions. +//! +//! It may be .owned (e.g. in the case of a gamma ramp initialized by you for passing into +//! glfw.Monitor.setGammaRamp) or not .owned (e.g. in the case of one gotten via +//! glfw.Monitor.getGammaRamp.) If it is .owned, deinit should be called to free the memory. It is +//! safe to call deinit even if not .owned. +//! +//! see also: monitor_gamma, glfw.Monitor.getGammaRamp + +const std = @import("std"); +const testing = std.testing; +const mem = std.mem; +const c = @import("c.zig").c; + +const GammaRamp = @This(); + +red: []u16, +green: []u16, +blue: []u16, +owned: bool, + +/// Initializes a new owned gamma ramp with the given array size and undefined values. +/// +/// see also: glfw.Monitor.getGammaRamp +pub inline fn init(allocator: *mem.Allocator, size: usize) !GammaRamp { + const buf = try allocator.alloc(u16, size * 3); + return GammaRamp{ + .red = buf[size * 0 .. (size * 0) + size], + .green = buf[size * 1 .. (size * 1) + size], + .blue = buf[size * 2 .. (size * 2) + size], + .owned = true, + }; +} + +/// Turns a GLFW / C gamma ramp into the nicer Zig type, and sets `.owned = false`. +/// +/// The returned memory is valid for as long as the GLFW C memory is valid. +pub inline fn fromC(native: c.GLFWgammaramp) GammaRamp { + return GammaRamp{ + .red = native.red[0..native.size], + .green = native.green[0..native.size], + .blue = native.blue[0..native.size], + .owned = false, + }; +} + +/// Turns the nicer Zig type into a GLFW / C gamma ramp, for passing into GLFW C functions. +/// +/// The returned memory is valid for as long as the Zig memory is valid. +pub inline fn toC(self: GammaRamp) c.GLFWgammaramp { + std.debug.assert(self.red.len == self.green.len); + std.debug.assert(self.red.len == self.blue.len); + return c.GLFWgammaramp{ + .red = &self.red[0], + .green = &self.green[0], + .blue = &self.blue[0], + .size = @intCast(c_uint, self.red.len), + }; +} + +/// Deinitializes the memory using the allocator iff `.owned = true`. +pub inline fn deinit(self: GammaRamp, allocator: *mem.Allocator) void { + if (self.owned) allocator.free(self.red); +} + +test "conversion" { + const allocator = testing.allocator; + + const ramp = try GammaRamp.init(allocator, 256); + defer ramp.deinit(allocator); + + const glfw = ramp.toC(); + _ = GammaRamp.fromC(glfw); +} diff --git a/glfw/src/main.zig b/glfw/src/main.zig index 35a290be..dfbcb01f 100644 --- a/glfw/src/main.zig +++ b/glfw/src/main.zig @@ -10,6 +10,7 @@ const getError = @import("errors.zig").getError; pub const action = @import("action.zig"); pub const gamepad_axis = @import("gamepad_axis.zig"); pub const gamepad_button = @import("gamepad_button.zig"); +pub const GammaRamp = @import("GammaRamp.zig"); pub const hat = @import("hat.zig"); pub const joystick = @import("joystick.zig"); pub const key = @import("key.zig"); @@ -143,6 +144,7 @@ test "version" { test "basic" { _ = Monitor; + _ = GammaRamp; _ = VideoMode; basicTest() catch unreachable; }