diff options
Diffstat (limited to 'src/Window.zig')
-rw-r--r-- | src/Window.zig | 89 |
1 files changed, 60 insertions, 29 deletions
diff --git a/src/Window.zig b/src/Window.zig index 5a4424b..d947977 100644 --- a/src/Window.zig +++ b/src/Window.zig @@ -23,13 +23,13 @@ const checkError = gfz.checkError; const getError = gfz.getError; const gfz = @import("gfz.zig"); -const Window = @This(); +const Self = @This(); pub const Impl = GLFWwindow; pimpl: *Impl, const Options = struct { fullscreen: ?Monitor = null, - share: ?Window = null, + share: ?Self = null, }; pub const Hints = struct { @@ -119,7 +119,7 @@ fn setHint(hint: c_int, value: anytype) Error!void { /// Create a window and its associated context. pub fn create(width: c_int, height: c_int, title: [:0]const u8, - options: Options, hints: Hints) Error!Window { + options: Options, hints: Hints) Error!Self { try setHint(GLFW_RESIZABLE, hints.resizable); try setHint(GLFW_VISIBLE, hints.visible); try setHint(GLFW_DECORATED, hints.decorated); @@ -161,15 +161,15 @@ pub fn create(width: c_int, height: c_int, title: [:0]const u8, const share = if (options.share) |box| box.pimpl else null; const ptr = glfwCreateWindow(width, height, title.ptr, monitor, share); return if (ptr) |pimpl| - Window{ .pimpl = pimpl } + Self{ .pimpl = pimpl } else getError(); } /// Return the window whose context is current on the calling thread. -pub fn getCurrent() Error!?Window { +pub fn getCurrent() Error!?Self { const result = if (glfwGetCurrentContext()) |pimpl| - Window{ .pimpl = pimpl } + Self{ .pimpl = pimpl } else null; try checkError(); @@ -177,26 +177,26 @@ pub fn getCurrent() Error!?Window { } /// Make the context current for the calling thread. -pub fn makeCurrent(self: ?Window) Error!void { +pub fn makeCurrent(self: ?Self) Error!void { glfwMakeContextCurrent(if (self) |window| window.pimpl else null); try checkError(); } /// Check the close flag. -pub fn shouldClose(self: Window) Error!bool { +pub fn shouldClose(self: Self) Error!bool { const flag = glfwWindowShouldClose(self.pimpl); try checkError(); return flag == GLFW_TRUE; } /// Set the close flag. -pub fn setShouldClose(self: Window, value: bool) Error!void { +pub fn setShouldClose(self: Self, value: bool) Error!void { glfwSetWindowShouldClose(self.pimpl, if (value) GLFW_TRUE else GLFW_FALSE); try checkError(); } /// Swap the front and back buffers. -pub fn swapBuffers(self: Window) Error!void { +pub fn swapBuffers(self: Self) Error!void { glfwSwapBuffers(self.pimpl); try checkError(); } @@ -209,14 +209,14 @@ pub const InputMode = enum(c_int) { }; /// Return the value of an input option. -pub fn getInputMode(self: Window, mode: InputMode) Error!bool { +pub fn getInputMode(self: Self, mode: InputMode) Error!bool { const value = glfwGetInputMode(self.pimpl, @enumToInt(mode)); try checkError(); return value == GLFW_TRUE; } /// Set an input option. -pub fn setInputMode(self: Window, mode: InputMode, flag: bool) Error!void { +pub fn setInputMode(self: Self, mode: InputMode, flag: bool) Error!void { const value = if (flag) GLFW_TRUE else GLFW_FALSE; glfwSetInputMode(self.pimpl, @enumToInt(mode), value); try checkError(); @@ -229,14 +229,14 @@ pub const CursorMode = enum(c_int) { }; /// Return the cursor mode. -pub fn getCursorMode(self: Window) Error!CursorMode { +pub fn getCursorMode(self: Self) Error!CursorMode { const value = glfwGetInputMode(self.pimpl, GLFW_CURSOR); try checkError(); return @intToEnum(CursorMode, value); } /// Set the cursor mode. -pub fn setCursorMode(self: Window, value: CursorMode) Error!void { +pub fn setCursorMode(self: Self, value: CursorMode) Error!void { glfwSetInputMode(self.pimpl, GLFW_CURSOR, @enumToInt(value)); try checkError(); } @@ -247,7 +247,7 @@ const Pos = struct { }; /// Retrieve the position of the cursor relative to the window's content area. -pub fn getCursorPos(self: Window) Error!Pos { +pub fn getCursorPos(self: Self) Error!Pos { var pos: Pos = undefined; glfwGetCursorPos(self.pimpl, &pos.x, &pos.y); try checkError(); @@ -255,15 +255,21 @@ pub fn getCursorPos(self: Window) Error!Pos { } /// Set the position of the cursor, relative to the window's content area. -pub fn setCursorPos(self: Window, xpos: f64, ypos: f64) Error!void { +pub fn setCursorPos(self: Self, xpos: f64, ypos: f64) Error!void { glfwSetCursorPos(self.pimpl, xpos, ypos); try checkError(); } +const CursorPosFun = fn (window: Self, xpos: f64, ypos: f64) void; + /// Set the cursor position callback. -pub fn setCursorPosCallback(self: Window, - callback: GLFWcursorposfun) Error!void { - _ = glfwSetCursorPosCallback(self.pimpl, callback); +pub fn setCursorPosCallback(self: Self, comptime fun: CursorPosFun) Error!void { + _ = glfwSetCursorPosCallback(self.pimpl, struct { + pub fn callback(window: ?*Impl, + xpos: f64, ypos: f64) callconv(.C) void { + fun(@fieldParentPtr(Self, "pimpl", &window.?).*, xpos, ypos); + } + }.callback); try checkError(); } @@ -273,40 +279,65 @@ const State = enum(c_int) { }; /// Return the last reported state of a mouse button. -pub fn getMouseButton(self: Window, button: c_int) Error!State { +pub fn getMouseButton(self: Self, button: c_int) Error!State { const state = glfwGetMouseButton(self.pimpl, button); try checkError(); return @intToEnum(State, state); } +const MouseButtonFun = fn (window: Self, button: c_int, + action: c_int, mods: c_int) void; + /// Set the mouse button callback. -pub fn setMouseButtonCallback(self: Window, - callback: GLFWmousebuttonfun) Error!void { - _ = glfwSetMouseButtonCallback(self.pimpl, callback); +pub fn setMouseButtonCallback(self: Self, + comptime fun: MouseButtonFun) Error!void { + _ = glfwSetMouseButtonCallback(self.pimpl, struct { + pub fn callback(window: ?*Impl, button: c_int, + action: c_int, mods: c_int) callconv(.C) void { + fun(@fieldParentPtr(Self, "pimpl", &window.?).*, + button, action, mods); + } + }.callback); try checkError(); } /// Return the last reported state of a keyboard key. -pub fn getKey(self: Window, key: c_int) Error!State { +pub fn getKey(self: Self, key: c_int) Error!State { const state = glfwGetKey(self.pimpl, key); try checkError(); return @intToEnum(State, state); } +const KeyFun = fn (window: Self, key: c_int, scancode: c_int, + action: c_int, mods: c_int) void; + /// Set the key callback. -pub fn setKeyCallback(self: Window, callback: GLFWkeyfun) Error!void { - _ = glfwSetKeyCallback(self.pimpl, callback); +pub fn setKeyCallback(self: Self, comptime fun: KeyFun) Error!void { + _ = glfwSetKeyCallback(self.pimpl, struct { + pub fn callback(window: ?*Impl, key: c_int, scancode: c_int, + action: c_int, mods: c_int) callconv(.C) void { + fun(@fieldParentPtr(Self, "pimpl", &window.?).*, + key, scancode, action, mods); + } + }.callback); try checkError(); } /// Set the size of the content area. -pub fn setSize(self: Window, width: c_int, height: c_int) Error!void { +pub fn setSize(self: Self, width: c_int, height: c_int) Error!void { glfwSetWindowSize(self.pimpl, width, height); try checkError(); } +const SizeFun = fn (window: Self, width: c_int, height: c_int) void; + /// Set the size callback. -pub fn setSizeCallback(self: Window, callback: GLFWwindowsizefun) Error!void { - _ = glfwSetWindowSizeCallback(self.pimpl, callback); +pub fn setSizeCallback(self: Self, comptime fun: SizeFun) Error!void { + _ = glfwSetWindowSizeCallback(self.pimpl, struct { + pub fn callback(window: ?*Impl, + width: c_int, height: c_int) callconv(.C) void { + fun(@fieldParentPtr(Self, "pimpl", &window.?).*, width, height); + } + }.callback); try checkError(); } |