aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorNguyễn Gia Phong <cnx@loang.net>2023-08-14 12:50:47 +0900
committerNguyễn Gia Phong <cnx@loang.net>2023-08-14 12:53:11 +0900
commitd30ae1d4a46ad4b1f7b3c45c59dee268ab95c6b8 (patch)
tree36184e6023324e0fb7b5e9207cbbc0027881aa34 /src
parent407f889cf60ecea2709b449cb9939168d0b795c8 (diff)
downloadgfz-d30ae1d4a46ad4b1f7b3c45c59dee268ab95c6b8.tar.gz
Port to Zig 0.11
Diffstat (limited to 'src')
-rw-r--r--src/Window.zig28
-rw-r--r--src/gfz.zig8
-rw-r--r--src/input.zig45
3 files changed, 39 insertions, 42 deletions
diff --git a/src/Window.zig b/src/Window.zig
index 6f4a313..d5bb8e7 100644
--- a/src/Window.zig
+++ b/src/Window.zig
@@ -1,5 +1,5 @@
// Window manipulation
-// Copyright (C) 2021-2022 Nguyễn Gia Phong
+// Copyright (C) 2021-2023 Nguyễn Gia Phong
//
// This file is part of gfz.
//
@@ -51,16 +51,16 @@ fn fnCast(comptime DestType: type, comptime fun: anytype) DestType {
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.?).*,
- @intToEnum(Key, key), scancode,
- @intToEnum(KeyAction, action), Mods.fromInt(mods));
+ @enumFromInt(key), scancode,
+ @enumFromInt(action), Mods.fromInt(mods));
}
},
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.?).*,
- @intToEnum(MouseButton, button),
- @intToEnum(MouseButton.Action, action), Mods.fromInt(mods));
+ @enumFromInt(button),
+ @enumFromInt(action), Mods.fromInt(mods));
}
},
c.GLFWwindowsizefun => struct {
@@ -153,7 +153,7 @@ fn setHint(hint: c_int, value: anytype) Error!void {
c.glfwWindowHint(hint, switch (@TypeOf(value)) {
c_int, comptime_int => value,
bool => if (value) c.GLFW_TRUE else c.GLFW_FALSE,
- else => @enumToInt(value),
+ else => @intFromEnum(value),
});
try checkError();
}
@@ -258,7 +258,7 @@ const InputMode = enum(c_int) {
/// Return the value of an input option.
pub fn getInputMode(self: Self, mode: InputMode) Error!bool {
- const value = c.glfwGetInputMode(self.pimpl, @enumToInt(mode));
+ const value = c.glfwGetInputMode(self.pimpl, @intFromEnum(mode));
try checkError();
return value == c.GLFW_TRUE;
}
@@ -266,7 +266,7 @@ pub fn getInputMode(self: Self, mode: InputMode) Error!bool {
/// Set an input option.
pub fn setInputMode(self: Self, mode: InputMode, flag: bool) Error!void {
const value = if (flag) c.GLFW_TRUE else c.GLFW_FALSE;
- c.glfwSetInputMode(self.pimpl, @enumToInt(mode), value);
+ c.glfwSetInputMode(self.pimpl, @intFromEnum(mode), value);
try checkError();
}
@@ -280,12 +280,12 @@ const CursorMode = enum(c_int) {
pub fn getCursorMode(self: Self) Error!CursorMode {
const value = c.glfwGetInputMode(self.pimpl, c.GLFW_CURSOR);
try checkError();
- return @intToEnum(CursorMode, value);
+ return @enumFromInt(value);
}
/// Set the cursor mode.
pub fn setCursorMode(self: Self, value: CursorMode) Error!void {
- c.glfwSetInputMode(self.pimpl, c.GLFW_CURSOR, @enumToInt(value));
+ c.glfwSetInputMode(self.pimpl, c.GLFW_CURSOR, @intFromEnum(value));
try checkError();
}
@@ -321,9 +321,9 @@ const State = enum(c_int) {
/// Return the last reported state of a mouse button.
pub fn getMouseButton(self: Self, button: MouseButton) Error!State {
- const state = c.glfwGetMouseButton(self.pimpl, @enumToInt(button));
+ const state = c.glfwGetMouseButton(self.pimpl, @intFromEnum(button));
try checkError();
- return @intToEnum(State, state);
+ return @enumFromInt(state);
}
/// Set the mouse button callback.
@@ -336,9 +336,9 @@ pub fn setMouseButtonCallback(self: Self,
/// Return the last reported state of a keyboard key.
pub fn getKey(self: Self, key: Key) Error!State {
- const state = c.glfwGetKey(self.pimpl, @enumToInt(key));
+ const state = c.glfwGetKey(self.pimpl, @intFromEnum(key));
try checkError();
- return @intToEnum(State, state);
+ return @enumFromInt(state);
}
/// Set the key callback.
diff --git a/src/gfz.zig b/src/gfz.zig
index eeb78dc..041632d 100644
--- a/src/gfz.zig
+++ b/src/gfz.zig
@@ -1,5 +1,5 @@
// Graphics Framework for Zig
-// Copyright (C) 2021-2022 Nguyễn Gia Phong
+// Copyright (C) 2021-2023 Nguyễn Gia Phong
//
// This file is part of gfz.
//
@@ -19,7 +19,7 @@
const c = @import("cimport.zig");
const input = @import("input.zig");
-pub const Error = error {
+pub const Error = error{
/// GLFW has not been initialized.
NotInitialized,
/// No context is current for this thread.
@@ -88,11 +88,11 @@ pub fn init() Error!void {
///
/// Once this function is called, you must again call `init` successfully
/// before you will be able to use most GLFW functions.
-///
+///
/// If GLFW has been successfully initialized, this function should be called
/// before the application exits. If initialization fails, there is no need
/// to call this function, as it is called by `init` before it returns failure.
-///
+///
/// This function has no effect if GLFW is not initialized.
pub fn deinit() Error!void {
c.glfwTerminate();
diff --git a/src/input.zig b/src/input.zig
index 9c2396c..31fda2e 100644
--- a/src/input.zig
+++ b/src/input.zig
@@ -45,23 +45,20 @@ const keys = [_][]const u8{
};
/// Keyboard key enumeration: https://www.glfw.org/docs/latest/group__keys.html
-pub const Key = @Type(.{
- .Enum = .{
- .layout = .Auto,
- .tag_type = c_int,
- .fields = blk: {
- var fields: [keys.len]EnumField = undefined;
- for (keys) |name, i|
- fields[i] = .{
- .name = name,
- .value = @field(c, "GLFW_KEY_" ++ name),
- };
- break :blk fields[0..];
- },
- .decls = &.{},
- .is_exhaustive = true,
- }
-});
+pub const Key = @Type(.{ .Enum = .{
+ .tag_type = c_int,
+ .fields = blk: {
+ var fields: [keys.len]EnumField = undefined;
+ for (keys, 0..) |name, i|
+ fields[i] = .{
+ .name = name,
+ .value = @field(c, "GLFW_KEY_" ++ name),
+ };
+ break :blk fields[0..];
+ },
+ .decls = &.{},
+ .is_exhaustive = true,
+} });
/// Modifier key flags: https://www.glfw.org/docs/latest/group__mods.html
pub const Mods = packed struct {
@@ -85,8 +82,8 @@ pub const Mods = packed struct {
/// Convert c_int to Mods.
pub fn fromInt(mask: c_int) Mods {
- const integer = @intCast(MaskInt, mask);
- return @bitCast(Mods, Mask{
+ const integer: MaskInt = @intCast(mask);
+ return @bitCast(Mask{
.integer = switch (endian) {
.Big => @bitReverse(integer),
.Little => integer,
@@ -96,8 +93,8 @@ pub const Mods = packed struct {
/// Convert Mods to c_int.
pub fn toInt(self: Mods) c_int {
- const integer = @bitCast(Mask, self).integer;
- return @intCast(c_int, switch (endian) {
+ const integer = @as(Mask, @bitCast(self)).integer;
+ return @intCast(switch (endian) {
.Big => @bitReverse(integer),
.Little => integer,
});
@@ -123,9 +120,9 @@ pub const MouseButton = enum(c_int) {
@"7" = c.GLFW_MOUSE_BUTTON_7,
@"8" = c.GLFW_MOUSE_BUTTON_8,
- pub const left = @intToEnum(MouseButton, c.GLFW_MOUSE_BUTTON_LEFT);
- pub const right = @intToEnum(MouseButton, c.GLFW_MOUSE_BUTTON_RIGHT);
- pub const middle = @intToEnum(MouseButton, c.GLFW_MOUSE_BUTTON_MIDDLE);
+ pub const left: MouseButton = @enumFromInt(c.GLFW_MOUSE_BUTTON_LEFT);
+ pub const right: MouseButton = @enumFromInt(c.GLFW_MOUSE_BUTTON_RIGHT);
+ pub const middle: MouseButton = @enumFromInt(c.GLFW_MOUSE_BUTTON_MIDDLE);
/// Mouse button input action:
/// https://www.glfw.org/docs/latest/input_guide.html#input_mouse_button