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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
|
// Window manipulation
// 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");
const Error = gfz.Error;
const Monitor = @import("Monitor.zig");
const checkError = gfz.checkError;
const getError = gfz.getError;
const gfz = @import("gfz.zig");
const Window = @This();
pub const Impl = GLFWwindow;
pimpl: *Impl,
const Options = struct {
fullscreen: ?Monitor = null,
share: ?Window = null,
};
pub const Hints = struct {
pub const dont_care = GLFW_DONT_CARE;
resizable: bool = true,
visible: bool = true,
decorated: bool = true,
focused: bool = true,
auto_iconify: bool = true,
floating: bool = false,
maximized: bool = false,
center_cursor: bool = true,
transparent_framebuffer: bool = false,
focus_on_show: bool = true,
scale_to_monitor: bool = false,
bits: struct {
red: c_int = 8,
green: c_int = 8,
blue: c_int = 8,
alpha: c_int = 8,
depth: c_int = 24,
stencil: c_int = 8,
} = .{},
accum_bits: struct {
red: c_int = 0,
green: c_int = 0,
blue: c_int = 0,
alpha: c_int = 0,
} = .{},
aux_buffers: c_int = 0,
samples: c_int = 0,
refresh_rate: c_int = dont_care,
stereo: bool = false,
srgb_capable: bool = false,
doublebuffer: bool = true,
client_api: enum(c_int) {
opengl = GLFW_OPENGL_API,
opengl_es = GLFW_OPENGL_ES_API,
no = 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,
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,
release_behavior: enum(c_int) {
any = GLFW_ANY_RELEASE_BEHAVIOR,
flush = GLFW_RELEASE_BEHAVIOR_FLUSH,
none = GLFW_RELEASE_BEHAVIOR_NONE,
} = .any,
} = .{},
opengl: 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,
} = .{},
};
/// Set the specified window hint to the desired value.
fn setHint(hint: c_int, value: anytype) Error!void {
glfwWindowHint(hint, switch (@TypeOf(value)) {
c_int, comptime_int => value,
bool => if (value) GLFW_TRUE else GLFW_FALSE,
else => @enumToInt(value),
});
try checkError();
}
/// 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!Window {
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);
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);
return if (ptr) |pimpl|
Window{ .pimpl = pimpl }
else
getError();
}
/// Make the context current for the calling thread.
pub fn makeCurrent(self: Window) Error!void {
glfwMakeContextCurrent(self.pimpl);
try checkError();
}
/// Check the close flag.
pub fn shouldClose(self: Window) Error!bool {
const flag = glfwWindowShouldClose(self.pimpl);
try checkError();
return flag == GLFW_TRUE;
}
/// Set the close flag.
pub fn setShouldClose(self: Window, value: bool) Error!void {
glfwSetWindowShouldClose(self.pimpl, if (value) GLFW_TRUE else GLFW_FALSE);
try checkError();
}
/// Swap the front and back buffers.
pub fn swapBuffers(self: Window) Error!void {
glfwSwapBuffers(self.pimpl);
try checkError();
}
pub 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,
};
/// Return the value of an input option.
pub fn getInputMode(self: Window, mode: InputMode) Error!bool {
const value = glfwGetInputMode(self.pimpl, @enumToInt(mode));
try checkError();
return value == GLFW_TRUE;
}
/// Set an input option.
pub fn setInputMode(self: Window, mode: InputMode, flag: bool) Error!void {
const value = if (flag) GLFW_TRUE else GLFW_FALSE;
glfwSetInputMode(self.pimpl, @enumToInt(mode), value);
try checkError();
}
pub const CursorMode = enum(c_int) {
normal = GLFW_CURSOR_NORMAL,
hidden = GLFW_CURSOR_HIDDEN,
disabled = GLFW_CURSOR_DISABLED,
};
/// Return the cursor mode.
pub fn getCursorMode(self: Window) Error!CursorMode {
const value = glfwGetInputMode(self.pimpl, GLFW_CURSOR);
try checkError();
return @intToEnum(CursorMode, value);
}
/// Set the cursor mode.
pub fn setCursorMode(self: Window, value: CursorMode) Error!void {
glfwSetInputMode(self.pimpl, GLFW_CURSOR, @enumToInt(value));
try checkError();
}
/// Set the size callback.
pub fn setSizeCallback(self: Window, callback: GLFWwindowsizefun) Error!void {
_ = glfwSetWindowSizeCallback(self.pimpl, callback);
try checkError();
}
/// Set the key callback.
pub fn setKeyCallback(self: Window, callback: GLFWkeyfun) Error!void {
_ = glfwSetKeyCallback(self.pimpl, callback);
try checkError();
}
|