diff options
Diffstat (limited to 'src/Window.zig')
-rw-r--r-- | src/Window.zig | 186 |
1 files changed, 95 insertions, 91 deletions
diff --git a/src/Window.zig b/src/Window.zig index 5ff2de9..787fd84 100644 --- a/src/Window.zig +++ b/src/Window.zig @@ -16,13 +16,13 @@ // You should have received a copy of the GNU Lesser General Public License // along with gfz. If not, see <https://www.gnu.org/licenses/>. -usingnamespace @import("cimport.zig"); const Error = gfz.Error; const Key = input.Key; const KeyAction = input.KeyAction; const Mods = input.Mods; const Monitor = @import("Monitor.zig"); const MouseButton = input.MouseButton; +const c = @import("cimport.zig"); const checkError = gfz.checkError; const getError = gfz.getError; const gfz = @import("gfz.zig"); @@ -35,19 +35,19 @@ const KeyFun = fn (window: Self, key: Key, scancode: c_int, const MouseButtonFun = fn (window: Self, button: MouseButton, action: MouseButton.Action, mods: Mods) void; const SizeFun = fn (window: Self, width: c_int, height: c_int) void; -const Impl = GLFWwindow; +const Impl = c.GLFWwindow; pimpl: *Impl, /// Convert given function to GLFW callback. fn fnCast(comptime DestType: type, comptime fun: anytype) DestType { return switch (DestType) { - GLFWcursorposfun => struct { + c.GLFWcursorposfun => struct { pub fn callback(window: ?*Impl, xpos: f64, ypos: f64) callconv(.C) void { fun(@fieldParentPtr(Self, "pimpl", &window.?).*, xpos, ypos); } }, - GLFWkeyfun => struct { + c.GLFWkeyfun => 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.?).*, @@ -55,7 +55,7 @@ fn fnCast(comptime DestType: type, comptime fun: anytype) DestType { @intToEnum(KeyAction, action), Mods.fromInt(mods)); } }, - GLFWmousebuttonfun => struct { + c.GLFWmousebuttonfun => struct { pub fn callback(window: ?*Impl, button: c_int, action: c_int, mods: c_int) callconv(.C) void { fun(@fieldParentPtr(Self, "pimpl", &window.?).*, @@ -63,7 +63,7 @@ fn fnCast(comptime DestType: type, comptime fun: anytype) DestType { @intToEnum(MouseButton.Action, action), Mods.fromInt(mods)); } }, - GLFWwindowsizefun => struct { + c.GLFWwindowsizefun => struct { pub fn callback(window: ?*Impl, width: c_int, height: c_int) callconv(.C) void { fun(@fieldParentPtr(Self, "pimpl", &window.?).*, width, height); @@ -74,7 +74,7 @@ fn fnCast(comptime DestType: type, comptime fun: anytype) DestType { } pub const Hints = struct { - pub const dont_care = GLFW_DONT_CARE; + pub const dont_care = c.GLFW_DONT_CARE; resizable: bool = true, visible: bool = true, @@ -111,29 +111,29 @@ pub const Hints = struct { doublebuffer: bool = true, client_api: enum(c_int) { - opengl = GLFW_OPENGL_API, - opengl_es = GLFW_OPENGL_ES_API, - no = GLFW_NO_API, + opengl = c.GLFW_OPENGL_API, + opengl_es = c.GLFW_OPENGL_ES_API, + no = c.GLFW_NO_API, } = .opengl, context: struct { creation_api: enum(c_int) { - native = GLFW_NATIVE_CONTEXT_API, - egl = GLFW_EGL_CONTEXT_API, - osmesa = GLFW_OSMESA_CONTEXT_API, + native = c.GLFW_NATIVE_CONTEXT_API, + egl = c.GLFW_EGL_CONTEXT_API, + osmesa = c.GLFW_OSMESA_CONTEXT_API, } = .native, version: struct { major: c_int = 1, minor: c_int = 0, } = .{}, robustness: enum(c_int) { - no_robustness = GLFW_NO_ROBUSTNESS, - no_reset_notification = GLFW_NO_RESET_NOTIFICATION, - lose_context_on_reset = GLFW_LOSE_CONTEXT_ON_RESET, + no_robustness = c.GLFW_NO_ROBUSTNESS, + no_reset_notification = c.GLFW_NO_RESET_NOTIFICATION, + lose_context_on_reset = c.GLFW_LOSE_CONTEXT_ON_RESET, } = .no_robustness, release_behavior: enum(c_int) { - any = GLFW_ANY_RELEASE_BEHAVIOR, - flush = GLFW_RELEASE_BEHAVIOR_FLUSH, - none = GLFW_RELEASE_BEHAVIOR_NONE, + any = c.GLFW_ANY_RELEASE_BEHAVIOR, + flush = c.GLFW_RELEASE_BEHAVIOR_FLUSH, + none = c.GLFW_RELEASE_BEHAVIOR_NONE, } = .any, } = .{}, @@ -141,18 +141,18 @@ pub const Hints = struct { forward_compat: bool = false, debug_context: bool = false, profile: enum(c_int) { - any = GLFW_OPENGL_ANY_PROFILE, - compat = GLFW_OPENGL_COMPAT_PROFILE, - core = GLFW_OPENGL_CORE_PROFILE, + any = c.GLFW_OPENGL_ANY_PROFILE, + compat = c.GLFW_OPENGL_COMPAT_PROFILE, + core = c.GLFW_OPENGL_CORE_PROFILE, } = .any, } = .{}, }; /// Set the specified window hint to the desired value. fn setHint(hint: c_int, value: anytype) Error!void { - glfwWindowHint(hint, switch (@TypeOf(value)) { + c.glfwWindowHint(hint, switch (@TypeOf(value)) { c_int, comptime_int => value, - bool => if (value) GLFW_TRUE else GLFW_FALSE, + bool => if (value) c.GLFW_TRUE else c.GLFW_FALSE, else => @enumToInt(value), }); try checkError(); @@ -166,46 +166,47 @@ const Options = struct { /// 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!Self { - try setHint(GLFW_RESIZABLE, hints.resizable); - try setHint(GLFW_VISIBLE, hints.visible); - try setHint(GLFW_DECORATED, hints.decorated); - try setHint(GLFW_FOCUSED, hints.focused); - try setHint(GLFW_AUTO_ICONIFY, hints.auto_iconify); - try setHint(GLFW_FLOATING, hints.floating); - try setHint(GLFW_MAXIMIZED, hints.maximized); - try setHint(GLFW_CENTER_CURSOR, hints.center_cursor); - try setHint(GLFW_TRANSPARENT_FRAMEBUFFER, hints.transparent_framebuffer); - try setHint(GLFW_FOCUS_ON_SHOW, hints.focus_on_show); - try setHint(GLFW_SCALE_TO_MONITOR, hints.scale_to_monitor); - try setHint(GLFW_RED_BITS, hints.bits.red); - try setHint(GLFW_GREEN_BITS, hints.bits.green); - try setHint(GLFW_BLUE_BITS, hints.bits.blue); - try setHint(GLFW_ALPHA_BITS, hints.bits.alpha); - try setHint(GLFW_DEPTH_BITS, hints.bits.depth); - try setHint(GLFW_STENCIL_BITS, hints.bits.stencil); - try setHint(GLFW_ACCUM_RED_BITS, hints.accum_bits.red); - try setHint(GLFW_ACCUM_GREEN_BITS, hints.accum_bits.green); - try setHint(GLFW_ACCUM_BLUE_BITS, hints.accum_bits.blue); - try setHint(GLFW_ACCUM_ALPHA_BITS, hints.accum_bits.alpha); - try setHint(GLFW_AUX_BUFFERS, hints.aux_buffers); - try setHint(GLFW_SAMPLES, hints.samples); - try setHint(GLFW_REFRESH_RATE, hints.refresh_rate); - try setHint(GLFW_STEREO, hints.stereo); - try setHint(GLFW_SRGB_CAPABLE, hints.srgb_capable); - try setHint(GLFW_DOUBLEBUFFER, hints.doublebuffer); - try setHint(GLFW_CLIENT_API, hints.client_api); - try setHint(GLFW_CONTEXT_CREATION_API, hints.context.creation_api); - try setHint(GLFW_CONTEXT_VERSION_MAJOR, hints.context.version.major); - try setHint(GLFW_CONTEXT_VERSION_MINOR, hints.context.version.minor); - try setHint(GLFW_CONTEXT_ROBUSTNESS, hints.context.robustness); - try setHint(GLFW_CONTEXT_RELEASE_BEHAVIOR, hints.context.release_behavior); - try setHint(GLFW_OPENGL_FORWARD_COMPAT, hints.opengl.forward_compat); - try setHint(GLFW_OPENGL_DEBUG_CONTEXT, hints.opengl.debug_context); - try setHint(GLFW_OPENGL_PROFILE, hints.opengl.profile); + try setHint(c.GLFW_RESIZABLE, hints.resizable); + try setHint(c.GLFW_VISIBLE, hints.visible); + try setHint(c.GLFW_DECORATED, hints.decorated); + try setHint(c.GLFW_FOCUSED, hints.focused); + try setHint(c.GLFW_AUTO_ICONIFY, hints.auto_iconify); + try setHint(c.GLFW_FLOATING, hints.floating); + try setHint(c.GLFW_MAXIMIZED, hints.maximized); + try setHint(c.GLFW_CENTER_CURSOR, hints.center_cursor); + try setHint(c.GLFW_TRANSPARENT_FRAMEBUFFER, hints.transparent_framebuffer); + try setHint(c.GLFW_FOCUS_ON_SHOW, hints.focus_on_show); + try setHint(c.GLFW_SCALE_TO_MONITOR, hints.scale_to_monitor); + try setHint(c.GLFW_RED_BITS, hints.bits.red); + try setHint(c.GLFW_GREEN_BITS, hints.bits.green); + try setHint(c.GLFW_BLUE_BITS, hints.bits.blue); + try setHint(c.GLFW_ALPHA_BITS, hints.bits.alpha); + try setHint(c.GLFW_DEPTH_BITS, hints.bits.depth); + try setHint(c.GLFW_STENCIL_BITS, hints.bits.stencil); + try setHint(c.GLFW_ACCUM_RED_BITS, hints.accum_bits.red); + try setHint(c.GLFW_ACCUM_GREEN_BITS, hints.accum_bits.green); + try setHint(c.GLFW_ACCUM_BLUE_BITS, hints.accum_bits.blue); + try setHint(c.GLFW_ACCUM_ALPHA_BITS, hints.accum_bits.alpha); + try setHint(c.GLFW_AUX_BUFFERS, hints.aux_buffers); + try setHint(c.GLFW_SAMPLES, hints.samples); + try setHint(c.GLFW_REFRESH_RATE, hints.refresh_rate); + try setHint(c.GLFW_STEREO, hints.stereo); + try setHint(c.GLFW_SRGB_CAPABLE, hints.srgb_capable); + try setHint(c.GLFW_DOUBLEBUFFER, hints.doublebuffer); + try setHint(c.GLFW_CLIENT_API, hints.client_api); + try setHint(c.GLFW_CONTEXT_CREATION_API, hints.context.creation_api); + try setHint(c.GLFW_CONTEXT_VERSION_MAJOR, hints.context.version.major); + try setHint(c.GLFW_CONTEXT_VERSION_MINOR, hints.context.version.minor); + try setHint(c.GLFW_CONTEXT_ROBUSTNESS, hints.context.robustness); + try setHint(c.GLFW_CONTEXT_RELEASE_BEHAVIOR, + hints.context.release_behavior); + try setHint(c.GLFW_OPENGL_FORWARD_COMPAT, hints.opengl.forward_compat); + try setHint(c.GLFW_OPENGL_DEBUG_CONTEXT, hints.opengl.debug_context); + try setHint(c.GLFW_OPENGL_PROFILE, hints.opengl.profile); const monitor = if (options.fullscreen) |box| box.pimpl else null; const share = if (options.share) |box| box.pimpl else null; - const ptr = glfwCreateWindow(width, height, title.ptr, monitor, share); + const ptr = c.glfwCreateWindow(width, height, title.ptr, monitor, share); return if (ptr) |pimpl| Self{ .pimpl = pimpl } else @@ -214,7 +215,7 @@ pub fn create(width: c_int, height: c_int, title: [:0]const u8, /// Return the window whose context is current on the calling thread. pub fn getCurrent() Error!?Self { - const result = if (glfwGetCurrentContext()) |pimpl| + const result = if (c.glfwGetCurrentContext()) |pimpl| Self{ .pimpl = pimpl } else null; @@ -224,66 +225,67 @@ pub fn getCurrent() Error!?Self { /// Make the context current for the calling thread. pub fn makeCurrent(self: ?Self) Error!void { - glfwMakeContextCurrent(if (self) |window| window.pimpl else null); + c.glfwMakeContextCurrent(if (self) |window| window.pimpl else null); try checkError(); } /// Check the close flag. pub fn shouldClose(self: Self) Error!bool { - const flag = glfwWindowShouldClose(self.pimpl); + const flag = c.glfwWindowShouldClose(self.pimpl); try checkError(); - return flag == GLFW_TRUE; + return flag == c.GLFW_TRUE; } /// Set the close flag. pub fn setShouldClose(self: Self, value: bool) Error!void { - glfwSetWindowShouldClose(self.pimpl, if (value) GLFW_TRUE else GLFW_FALSE); + c.glfwSetWindowShouldClose(self.pimpl, + if (value) c.GLFW_TRUE else c.GLFW_FALSE); try checkError(); } /// Swap the front and back buffers. pub fn swapBuffers(self: Self) Error!void { - glfwSwapBuffers(self.pimpl); + c.glfwSwapBuffers(self.pimpl); try checkError(); } const InputMode = enum(c_int) { - sticky_keys = GLFW_STICKY_KEYS, - sticky_mouse_buttons = GLFW_STICKY_MOUSE_BUTTONS, - lock_key_mods = GLFW_LOCK_KEY_MODS, - raw_mouse_motion = GLFW_RAW_MOUSE_MOTION, + sticky_keys = c.GLFW_STICKY_KEYS, + sticky_mouse_buttons = c.GLFW_STICKY_MOUSE_BUTTONS, + lock_key_mods = c.GLFW_LOCK_KEY_MODS, + raw_mouse_motion = c.GLFW_RAW_MOUSE_MOTION, }; /// Return the value of an input option. pub fn getInputMode(self: Self, mode: InputMode) Error!bool { - const value = glfwGetInputMode(self.pimpl, @enumToInt(mode)); + const value = c.glfwGetInputMode(self.pimpl, @enumToInt(mode)); try checkError(); - return value == GLFW_TRUE; + return value == c.GLFW_TRUE; } /// Set an input option. 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); + const value = if (flag) c.GLFW_TRUE else c.GLFW_FALSE; + c.glfwSetInputMode(self.pimpl, @enumToInt(mode), value); try checkError(); } const CursorMode = enum(c_int) { - normal = GLFW_CURSOR_NORMAL, - hidden = GLFW_CURSOR_HIDDEN, - disabled = GLFW_CURSOR_DISABLED, + normal = c.GLFW_CURSOR_NORMAL, + hidden = c.GLFW_CURSOR_HIDDEN, + disabled = c.GLFW_CURSOR_DISABLED, }; /// Return the cursor mode. pub fn getCursorMode(self: Self) Error!CursorMode { - const value = glfwGetInputMode(self.pimpl, GLFW_CURSOR); + const value = c.glfwGetInputMode(self.pimpl, c.GLFW_CURSOR); try checkError(); return @intToEnum(CursorMode, value); } /// Set the cursor mode. pub fn setCursorMode(self: Self, value: CursorMode) Error!void { - glfwSetInputMode(self.pimpl, GLFW_CURSOR, @enumToInt(value)); + c.glfwSetInputMode(self.pimpl, c.GLFW_CURSOR, @enumToInt(value)); try checkError(); } @@ -295,31 +297,31 @@ const Pos = struct { /// Retrieve the position of the cursor relative to the window's content area. pub fn getCursorPos(self: Self) Error!Pos { var pos: Pos = undefined; - glfwGetCursorPos(self.pimpl, &pos.x, &pos.y); + c.glfwGetCursorPos(self.pimpl, &pos.x, &pos.y); try checkError(); return pos; } /// Set the position of the cursor, relative to the window's content area. pub fn setCursorPos(self: Self, xpos: f64, ypos: f64) Error!void { - glfwSetCursorPos(self.pimpl, xpos, ypos); + c.glfwSetCursorPos(self.pimpl, xpos, ypos); try checkError(); } /// Set the cursor position callback. pub fn setCursorPosCallback(self: Self, comptime fun: CursorPosFun) Error!void { - _ = glfwSetCursorPosCallback(self.pimpl, fnCast(GLFWcursorposfun, fun)); + _ = c.glfwSetCursorPosCallback(self.pimpl, fnCast(c.GLFWcursorposfun, fun)); try checkError(); } const State = enum(c_int) { - press = GLFW_PRESS, - release = GLFW_RELEASE, + press = c.GLFW_PRESS, + release = c.GLFW_RELEASE, }; /// Return the last reported state of a mouse button. pub fn getMouseButton(self: Self, button: MouseButton) Error!State { - const state = glfwGetMouseButton(self.pimpl, @enumToInt(button)); + const state = c.glfwGetMouseButton(self.pimpl, @enumToInt(button)); try checkError(); return @intToEnum(State, state); } @@ -327,31 +329,33 @@ pub fn getMouseButton(self: Self, button: MouseButton) Error!State { /// Set the mouse button callback. pub fn setMouseButtonCallback(self: Self, comptime fun: MouseButtonFun) Error!void { - _ = glfwSetMouseButtonCallback(self.pimpl, fnCast(GLFWmousebuttonfun, fun)); + _ = c.glfwSetMouseButtonCallback(self.pimpl, + fnCast(c.GLFWmousebuttonfun, fun)); try checkError(); } /// Return the last reported state of a keyboard key. pub fn getKey(self: Self, key: Key) Error!State { - const state = glfwGetKey(self.pimpl, @enumToInt(key)); + const state = c.glfwGetKey(self.pimpl, @enumToInt(key)); try checkError(); return @intToEnum(State, state); } /// Set the key callback. pub fn setKeyCallback(self: Self, comptime fun: KeyFun) Error!void { - _ = glfwSetKeyCallback(self.pimpl, fnCast(GLFWkeyfun, fun)); + _ = c.glfwSetKeyCallback(self.pimpl, fnCast(c.GLFWkeyfun, fun)); try checkError(); } /// Set the size of the content area. pub fn setSize(self: Self, width: c_int, height: c_int) Error!void { - glfwSetWindowSize(self.pimpl, width, height); + c.glfwSetWindowSize(self.pimpl, width, height); try checkError(); } /// Set the size callback. pub fn setSizeCallback(self: Self, comptime fun: SizeFun) Error!void { - _ = glfwSetWindowSizeCallback(self.pimpl, fnCast(GLFWwindowsizefun, fun)); + _ = c.glfwSetWindowSizeCallback(self.pimpl, + fnCast(c.GLFWwindowsizefun, fun)); try checkError(); } |