glfw: refactor getError and related functions

`getError()` now returns a struct `Error` containing `error_code` and
`description`. Rationale: retrieving the error code with the previous
implementation of `getError()` caused `getErrorString()` to return
null (the reverse is also true). The new implementation allows both
values to be retrieved at once.

The previous `getError()` function has been renamed to
`getErrorCode()` to reflect the fact that it returns a simple Zig
error rather than the `Error` struct. The error set returned by
`getErrorCode()` is now named `ErrorCode` rather than `Error`.

The behavior of the `getError()` family of functions clearing the
stored error is unchanged. However, since all code that used
`defer glfw.getError() catch {}` to explicitly clear errors had to be
refactored, a new `glfw.clearError()` function that returns void is
now available to make this operation more explicit.

Additionally, `mustGetError()` is now `mustGetErrorCode()`, and new
functions `mustGetError()` and `mustGetErrorString()` have been added
which wrap `getError()` and `getErrorString()` but panic if no error
is actually available.

Tests and API documentation had to be refactored across all of
`mach/glfw`. This commit also takes the opportunity to skip tests
involving window creation on CI so that other tests may still execute
normally.
This commit is contained in:
Lue 2023-01-10 23:25:00 +00:00 committed by Stephen Gutekanst
parent 779359a519
commit eed2be4591
14 changed files with 512 additions and 449 deletions

View file

@ -54,8 +54,9 @@ pub const Platform = struct {
const options = core.options;
const backend_type = try util.detectBackendType(allocator);
defer glfw.clearError();
glfw.setErrorCallback(Platform.errorCallback);
if (!glfw.init(.{})) try glfw.getError();
if (!glfw.init(.{})) try glfw.getErrorCode();
// Create the test window and discover adapters using it (esp. for OpenGL)
var hints = util.glfwWindowHintsForBackend(backend_type);
@ -67,13 +68,13 @@ pub const Platform = struct {
null,
null,
hints,
) orelse return glfw.mustGetError();
) orelse return glfw.mustGetErrorCode();
if (backend_type == .opengl) glfw.makeContextCurrent(window);
if (backend_type == .opengles) glfw.makeContextCurrent(window);
const window_size = window.getSize();
const framebuffer_size = window.getFramebufferSize();
try glfw.getError();
try glfw.getErrorCode();
const instance = gpu.createInstance(null);
if (instance == null) {
@ -144,7 +145,7 @@ pub const Platform = struct {
core.target_desc = descriptor;
core.swap_chain = null;
const cursor_pos = window.getCursorPos();
try glfw.getError();
try glfw.getErrorCode();
return Platform{
.window = window,
@ -324,7 +325,7 @@ pub const Platform = struct {
if (options.borderless_window) {
glfw.Window.setAttrib(platform.window, .decorated, false);
try glfw.getError();
try glfw.getErrorCode();
}
if (options.fullscreen) {
@ -650,7 +651,7 @@ pub fn coreUpdate(core: *Core, resize: ?CoreResizeCallback) !void {
// Don't wait for events
glfw.pollEvents();
}
try glfw.getError();
try glfw.getErrorCode();
core.delta_time_ns = core.timer.lapPrecise();
core.delta_time = @intToFloat(f32, core.delta_time_ns) / @intToFloat(f32, std.time.ns_per_s);