1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
// Graphics Framework for Zig
// Copyright (C) 2021 Nguyễn Gia Phong
//
// This file is part of gfz.
//
// gfz is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// gfz is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// 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");
pub const Error = error {
/// GLFW has not been initialized.
NotInitialized,
/// No context is current for this thread.
NoCurrentContext,
/// One of the arguments to the function was an invalid enum value.
InvalidEnum,
/// One of the arguments to the function was an invalid value.
InvalidValue,
/// A memory allocation failed.
OutOfMemory,
/// GLFW could not find support for the requested API on the system.
ApiUnavailable,
/// The requested OpenGL or OpenGL ES version is not available.
VersionUnavailable,
/// A platform-specific error occurred
/// that does not match any of the more specific categories.
PlatformError,
/// The requested format is not supported or available.
FormatUnavailable,
/// The specified window does not have an OpenGL or OpenGL ES context.
NoWindowContext,
};
/// Return and clear the error code of the last error
/// that occurred on the calling thread if any.
pub fn checkError() Error!void {
return switch (glfwGetError(null)) {
GLFW_NO_ERROR => {},
GLFW_NOT_INITIALIZED => Error.NotInitialized,
GLFW_NO_CURRENT_CONTEXT => Error.NoCurrentContext,
GLFW_INVALID_ENUM => Error.InvalidEnum,
GLFW_INVALID_VALUE => Error.InvalidValue,
GLFW_OUT_OF_MEMORY => Error.OutOfMemory,
GLFW_API_UNAVAILABLE => Error.ApiUnavailable,
GLFW_VERSION_UNAVAILABLE => Error.VersionUnavailable,
GLFW_PLATFORM_ERROR => Error.PlatformError,
GLFW_FORMAT_UNAVAILABLE => Error.FormatUnavailable,
GLFW_NO_WINDOW_CONTEXT => Error.NoWindowContext,
else => unreachable,
};
}
/// Return and clear the last error for the calling thread.
pub fn getError() Error {
return if (checkError()) |_| unreachable else |err| err;
}
/// Initialize the GLFW library.
///
/// Before most GLFW functions can be used, GLFW must be initialized,
/// and before an application terminates GLFW should be terminated
/// in order to free any resources allocated during or after initialization.
///
/// If this function fails, it calls `glfwTerminate` before returning.
/// If it succeeds, you should call `deinit` before the application exits.
///
/// Additional calls to this function after successful initialization
/// but before termination will return immediately.
pub fn init() Error!void {
if (glfwInit() != GLFW_TRUE)
return getError();
}
/// Destroy all remaining windows and cursors, restore any modified gamma ramps
/// and frees any other allocated resources.
///
/// 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 {
glfwTerminate();
try checkError();
}
/// Process all pending events.
pub fn pollEvents() Error!void {
glfwPollEvents();
try checkError();
}
/// Return whether raw mouse motion is supported.
pub fn rawMouseMotionSupported() Error!bool {
const result = glfwRawMouseMotionSupported();
try checkError();
return result == GLFW_TRUE;
}
pub const Window = @import("Window.zig");
test {
try init();
defer deinit() catch unreachable;
const window = try Window.create(800, 600, "Hello, World!", .{}, .{});
try window.makeCurrent();
while (!try window.shouldClose()) {
try window.swapBuffers();
try pollEvents();
}
}
|