glfw: fix and test Monitor.getVideoModes

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
Stephen Gutekanst 2021-07-17 20:15:40 -07:00
parent b76e8d02f2
commit c6e8bce219
2 changed files with 43 additions and 31 deletions

View file

@ -5,8 +5,9 @@ const mem = std.mem;
const testing = std.testing; const testing = std.testing;
const c = @import("c.zig").c; const c = @import("c.zig").c;
pub const Error = @import("errors.zig").Error; const Error = @import("errors.zig").Error;
const getError = @import("errors.zig").getError; const getError = @import("errors.zig").getError;
const VideoMode = @import("VideoMode.zig");
const Monitor = @This(); const Monitor = @This();
@ -182,6 +183,32 @@ pub inline fn getUserPointer(self: Monitor, comptime T: type) Error!?*T {
return @ptrCast(*T, @alignCast(@alignOf(T), ptr.?)); return @ptrCast(*T, @alignCast(@alignOf(T), ptr.?));
} }
/// Returns the available video modes for the specified monitor.
///
/// This function returns an array of all video modes supported by the monitor. The returned slice
/// is sorted in ascending order, first by color bit depth (the sum of all channel depths) and
/// then by resolution area (the product of width and height).
///
/// Possible errors include glfw.Error.NotInitialized and glfw.Error.PlatformError.
///
/// The returned slice memory is owned by the caller.
///
/// @thread_safety This function must only be called from the main thread.
///
/// see also: monitor_modes, glfw.Monitor.getVideoMode
pub inline fn getVideoModes(self: Monitor, allocator: *mem.Allocator) ![]VideoMode {
var count: c_int = 0;
const modes = c.glfwGetVideoModes(self.handle, &count);
try getError();
const slice = try allocator.alloc(VideoMode, @intCast(usize, count));
var i: usize = 0;
while (i < count) : (i += 1) {
slice[i] = VideoMode{ .handle = modes[i] };
}
return slice;
}
/// Returns the currently connected monitors. /// Returns the currently connected monitors.
/// ///
/// This function returns a slice of all currently connected monitors. The primary monitor is /// This function returns a slice of all currently connected monitors. The primary monitor is
@ -273,34 +300,6 @@ pub inline fn setCallback(comptime Data: type, data: *Data, f: ?*const fn (monit
try getError(); try getError();
} }
/// Returns the available video modes for the specified monitor.
///
/// This function returns an array of all video modes supported by the monitor. The returned slice
/// is sorted in ascending order, first by color bit depth (the sum of all channel depths) and
/// then by resolution area (the product of width and height).
///
/// Possible errors include glfw.Error.NotInitialized and glfw.Error.PlatformError.
///
/// The returned slice memory is owned by the caller. The underlying handles are owned by GLFW, and
/// are valid until the monitor is disconnected, this function is called again, or `glfw.terminate`
/// is called.
///
/// @thread_safety This function must only be called from the main thread.
///
/// see also: monitor_modes, glfw.Monitor.getVideoMode
pub inline fn getVideoModes(allocator: *mem.Allocator) ![]VideoMode {
var count: c_int = 0;
const modes = c.glfwGetVideoModes(&count);
try getError();
const slice = try allocator.alloc(VideoMode, @intCast(usize, count));
var i: usize = 0;
while (i < count) : (i += 1) {
slice[i] = VideoMode{ .handle = modes[i].? };
}
return slice;
}
test "getAll" { test "getAll" {
const glfw = @import("main.zig"); const glfw = @import("main.zig");
try glfw.init(); try glfw.init();
@ -404,3 +403,16 @@ test "setCallback" {
} }
}).callback); }).callback);
} }
test "getVideoModes" {
const glfw = @import("main.zig");
try glfw.init();
defer glfw.terminate();
const monitor = try getPrimary();
if (monitor) |m| {
const allocator = testing.allocator;
const modes = try m.getVideoModes(allocator);
defer allocator.free(modes);
}
}

View file

@ -7,10 +7,10 @@ const mem = std.mem;
const testing = std.testing; const testing = std.testing;
const c = @import("c.zig").c; const c = @import("c.zig").c;
pub const Error = @import("errors.zig").Error; const Error = @import("errors.zig").Error;
const getError = @import("errors.zig").getError; const getError = @import("errors.zig").getError;
handle: *c.GLFWvidmode, handle: c.GLFWvidmode,
/// Returns the width of the video mode, in screen coordinates. /// Returns the width of the video mode, in screen coordinates.
pub inline fn getWidth(self: VideoMode) usize { pub inline fn getWidth(self: VideoMode) usize {