glfw: add Window.setMonitor

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
Stephen Gutekanst 2021-10-17 22:42:16 -07:00 committed by Stephen Gutekanst
parent 06e114c2dd
commit ceec340d95

View file

@ -951,56 +951,62 @@ pub inline fn swapBuffers(self: Window) Error!void {
// ///
// GLFWAPI GLFWmonitor* glfwGetWindowMonitor(GLFWwindow* window);
// /// Sets the mode, monitor, video mode and placement of a window.
// ///
// /// This function sets the monitor that the window uses for full screen mode or,
// /// if the monitor is null, makes it windowed mode.
// ///
// /// When setting a monitor, this function updates the width, height and refresh
// /// rate of the desired video mode and switches to the video mode closest to it.
// /// The window position is ignored when setting a monitor.
// ///
// /// When the monitor is null, the position, width and height are used to
// /// place the window content area. The refresh rate is ignored when no monitor
// /// is specified.
// ///
// /// If you only wish to update the resolution of a full screen window or the
// /// size of a windowed mode window, see @ref glfwSetWindowSize.
// ///
// /// When a window transitions from full screen to windowed mode, this function
// /// restores any previous window settings such as whether it is decorated,
// /// floating, resizable, has size or aspect ratio limits, etc.
// ///
// /// @param[in] window The window whose monitor, size or video mode to set.
// /// @param[in] monitor The desired monitor, or null to set windowed mode.
// /// @param[in] xpos The desired x-coordinate of the upper-left corner of the
// /// content area.
// /// @param[in] ypos The desired y-coordinate of the upper-left corner of the
// /// content area.
// /// @param[in] width The desired with, in screen coordinates, of the content
// /// area or video mode.
// /// @param[in] height The desired height, in screen coordinates, of the content
// /// area or video mode.
// /// @param[in] refreshRate The desired refresh rate, in Hz, of the video mode,
// /// or `GLFW_DONT_CARE`.
// ///
// /// Possible errors include glfw.Error.NotInitialized and glfw.Error.PlatformError.
// ///
// /// The OpenGL or OpenGL ES context will not be destroyed or otherwise
// /// affected by any resizing or mode switching, although you may need to update
// /// your viewport if the framebuffer size has changed.
// ///
// /// wayland: The desired window position is ignored, as there is no way
// /// for an application to set this property.
// ///
// /// wayland: Setting the window to full screen will not attempt to
// /// change the mode, no matter what the requested size or refresh rate.
// ///
// /// @thread_safety This function must only be called from the main thread.
// ///
// /// see also: window_monitor, window_full_screen, glfw.Window.getMonitor, glfw.Window.setSize
// ///
// GLFWAPI void glfwSetWindowMonitor(GLFWwindow* window, GLFWmonitor* monitor, int xpos, int ypos, int width, int height, int refreshRate);
/// Sets the mode, monitor, video mode and placement of a window.
///
/// This function sets the monitor that the window uses for full screen mode or, if the monitor is
/// null, makes it windowed mode.
///
/// When setting a monitor, this function updates the width, height and refresh rate of the desired
/// video mode and switches to the video mode closest to it. The window position is ignored when
/// setting a monitor.
///
/// When the monitor is null, the position, width and height are used to place the window content
/// area. The refresh rate is ignored when no monitor is specified.
///
/// If you only wish to update the resolution of a full screen window or the size of a windowed
/// mode window, see @ref glfwSetWindowSize.
///
/// When a window transitions from full screen to windowed mode, this function restores any
/// previous window settings such as whether it is decorated, floating, resizable, has size or
/// aspect ratio limits, etc.
///
/// @param[in] window The window whose monitor, size or video mode to set.
/// @param[in] monitor The desired monitor, or null to set windowed mode.
/// @param[in] xpos The desired x-coordinate of the upper-left corner of the content area.
/// @param[in] ypos The desired y-coordinate of the upper-left corner of the content area.
/// @param[in] width The desired with, in screen coordinates, of the content area or video mode.
/// @param[in] height The desired height, in screen coordinates, of the content area or video mode.
/// @param[in] refreshRate The desired refresh rate, in Hz, of the video mode, or `glfw.dont_care`.
///
/// Possible errors include glfw.Error.NotInitialized and glfw.Error.PlatformError.
///
/// The OpenGL or OpenGL ES context will not be destroyed or otherwise affected by any resizing or
/// mode switching, although you may need to update your viewport if the framebuffer size has
/// changed.
///
/// wayland: The desired window position is ignored, as there is no way for an application to set
/// this property.
///
/// wayland: Setting the window to full screen will not attempt to change the mode, no matter what
/// the requested size or refresh rate.
///
/// @thread_safety This function must only be called from the main thread.
///
/// see also: window_monitor, window_full_screen, glfw.Window.getMonitor, glfw.Window.setSize
pub inline fn setMonitor(self: Window, monitor: ?Monitor, xpos: isize, ypos: isize, width: isize, height: isize, refresh_rate: isize) Error!void {
c.glfwSetWindowMonitor(
self.handle,
if (monitor) |m| m.handle else null,
@intCast(c_int, xpos),
@intCast(c_int, ypos),
@intCast(c_int, width),
@intCast(c_int, height),
@intCast(c_int, refresh_rate),
);
try getError();
}
// TODO(window):
// /// Returns an attribute of the specified window.
// ///
@ -1811,3 +1817,19 @@ test "swapBuffers" {
_ = try window.swapBuffers();
}
test "setMonitor" {
const glfw = @import("main.zig");
try glfw.init();
defer glfw.terminate();
const window = glfw.Window.create(640, 480, "Hello, Zig!", null, null) catch |err| {
// return without fail, because most of our CI environments are headless / we cannot open
// windows on them.
std.debug.print("note: failed to create window: {}\n", .{err});
return;
};
defer window.destroy();
window.setMonitor(null, 10, 10, 640, 480, 60) catch |err| std.debug.print("can't set monitor, not supported by OS maybe? error={}\n", .{err});
}