glfw: force init in key.zig

This commit is contained in:
InKryption 2021-11-21 18:21:08 +01:00 committed by Stephen Gutekanst
parent c6310dc377
commit c90f879709

View file

@ -20,6 +20,8 @@ const cc = @import("c.zig").c;
const Error = @import("errors.zig").Error;
const getError = @import("errors.zig").getError;
const internal_debug = @import("internal_debug.zig");
/// enum containing all glfw keys
pub const Key = enum(c_int) {
/// The unknown key
@ -212,9 +214,13 @@ pub const Key = enum(c_int) {
/// @thread_safety This function must only be called from the main thread.
///
/// see also: input_key_name
pub inline fn getName(self: Key, scancode: isize) Error!?[:0]const u8 {
pub inline fn getName(self: Key, scancode: isize) error{ PlatformError }!?[:0]const u8 {
internal_debug.assertInitialized();
const name_opt = cc.glfwGetKeyName(@enumToInt(self), @intCast(c_int, scancode));
try getError();
getError() catch |err| return switch (err) {
Error.PlatformError => err,
else => unreachable,
};
return if (name_opt) |name|
std.mem.span(name)
else
@ -233,9 +239,14 @@ pub const Key = enum(c_int) {
/// Possible errors include glfw.Error.NotInitialized, glfw.Error.InvalidEnum and glfw.Error.PlatformError.
///
/// @thread_safety This function may be called from any thread.
pub inline fn getScancode(self: Key) Error!isize {
pub inline fn getScancode(self: Key) error{ PlatformError }!isize {
internal_debug.assertInitialized();
const scancode = cc.glfwGetKeyScancode(@enumToInt(self));
try getError();
getError() catch |err| return switch (err) {
Error.InvalidEnum => unreachable, // Should be unreachable for any valid 'Key' value.
Error.PlatformError => err,
else => unreachable,
};
return scancode;
}
};