Skip to content

Commit

Permalink
Upgrade to glfw 3.3.7
Browse files Browse the repository at this point in the history
  • Loading branch information
SpexGuy committed Jul 7, 2022
1 parent fcea66b commit 0a2cfca
Show file tree
Hide file tree
Showing 4 changed files with 538 additions and 381 deletions.
7 changes: 3 additions & 4 deletions examples/example_glfw_opengl3.zig
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@ pub fn main() !void {
}

// Create window with graphics context
const window = glfw.glfwCreateWindow(1280, 720, "Dear ImGui GLFW+OpenGL3 example", null, null);
if (window == null)
return error.GlfwCreateWindowFailed;
const window = glfw.glfwCreateWindow(1280, 720, "Dear ImGui GLFW+OpenGL3 example", null, null)
orelse return error.GlfwCreateWindowFailed;
glfw.glfwMakeContextCurrent(window);
glfw.glfwSwapInterval(1); // Enable vsync

Expand All @@ -61,7 +60,7 @@ pub fn main() !void {
//imgui.StyleColorsClassic();

// Setup Platform/Renderer bindings
_ = impl_glfw.InitForOpenGL(window.?, true);
_ = impl_glfw.InitForOpenGL(window, true);
_ = impl_gl3.Init(glsl_version);

// Load Fonts
Expand Down
5 changes: 3 additions & 2 deletions examples/example_glfw_vulkan.zig
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,9 @@ pub fn main() !void {
return error.VulkanNotSupported;
}
var extensions_count: u32 = 0;
var extensions = glfw.glfwGetRequiredInstanceExtensions(&extensions_count);
try SetupVulkan(extensions[0..extensions_count], allocator);
var extensions_ptr = glfw.glfwGetRequiredInstanceExtensions(&extensions_count);
const extensions = if (extensions_count > 0) extensions_ptr.?[0..extensions_count] else &[_][*:0]const u8{};
try SetupVulkan(extensions, allocator);

// Create Window Surface
var surface: vk.SurfaceKHR = undefined;
Expand Down
68 changes: 32 additions & 36 deletions examples/imgui_impl_glfw.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,10 @@ const imgui = @import("imgui");
const glfw = @import("include/glfw.zig");
const assert = std.debug.assert;

const GLFW_HAS_WINDOW_TOPMOST = (glfw.GLFW_VERSION_MAJOR * 1000 + glfw.GLFW_VERSION_MINOR * 100 >= 3200); // 3.2+ GLFW_FLOATING
const GLFW_HAS_WINDOW_HOVERED = (glfw.GLFW_VERSION_MAJOR * 1000 + glfw.GLFW_VERSION_MINOR * 100 >= 3300); // 3.3+ GLFW_HOVERED
const GLFW_HAS_WINDOW_ALPHA = (glfw.GLFW_VERSION_MAJOR * 1000 + glfw.GLFW_VERSION_MINOR * 100 >= 3300); // 3.3+ glfwSetWindowOpacity
const GLFW_HAS_PER_MONITOR_DPI = (glfw.GLFW_VERSION_MAJOR * 1000 + glfw.GLFW_VERSION_MINOR * 100 >= 3300); // 3.3+ glfwGetMonitorContentScale
const GLFW_HAS_VULKAN = (glfw.GLFW_VERSION_MAJOR * 1000 + glfw.GLFW_VERSION_MINOR * 100 >= 3200); // 3.2+ glfwCreateWindowSurface
const GLFW_HAS_NEW_CURSORS = @hasDecl(glfw, "GLFW_RESIZE_NESW_CURSOR") and (glfw.GLFW_VERSION_MAJOR * 1000 + glfw.GLFW_VERSION_MINOR * 100 >= 3400); // 3.4+ GLFW_RESIZE_ALL_CURSOR, GLFW_RESIZE_NESW_CURSOR, GLFW_RESIZE_NWSE_CURSOR, GLFW_NOT_ALLOWED_CURSOR
const GLFW_HAS_GAMEPAD_API = (glfw.GLFW_VERSION_MAJOR * 1000 + glfw.GLFW_VERSION_MINOR * 100 >= 3300); // 3.3+ glfwGetGamepadState() new api
const GLFW_HAS_GET_KEY_NAME = (glfw.GLFW_VERSION_MAJOR * 1000 + glfw.GLFW_VERSION_MINOR * 100 >= 3200); // 3.2+ glfwGetKeyName()
const GLFW_HEADER_VERSION = glfw.GLFW_VERSION_MAJOR * 1000 + glfw.GLFW_VERSION_MINOR * 100;
const GLFW_HAS_NEW_CURSORS = @hasDecl(glfw, "GLFW_RESIZE_NESW_CURSOR") and (GLFW_HEADER_VERSION >= 3400); // 3.4+ GLFW_RESIZE_ALL_CURSOR, GLFW_RESIZE_NESW_CURSOR, GLFW_RESIZE_NWSE_CURSOR, GLFW_NOT_ALLOWED_CURSOR
const GLFW_HAS_GAMEPAD_API = (GLFW_HEADER_VERSION >= 3300); // 3.3+ glfwGetGamepadState() new api
const GLFW_HAS_GET_KEY_NAME = (GLFW_HEADER_VERSION >= 3200); // 3.2+ glfwGetKeyName()

const IS_EMSCRIPTEN = false;

Expand Down Expand Up @@ -56,11 +52,11 @@ fn GetBackendData() ?*Data {

// Functions
fn GetClipboardText(user_data: ?*anyopaque) callconv(.C) ?[*:0]const u8 {
return glfw.glfwGetClipboardString(@ptrCast(?*glfw.GLFWwindow, user_data));
return glfw.glfwGetClipboardString(@ptrCast(*glfw.GLFWwindow, user_data));
}

fn SetClipboardText(user_data: ?*anyopaque, text: ?[*:0]const u8) callconv(.C) void {
glfw.glfwSetClipboardString(@ptrCast(?*glfw.GLFWwindow, user_data), text);
glfw.glfwSetClipboardString(@ptrCast(*glfw.GLFWwindow, user_data), text.?);
}

fn KeyToImGuiKey(key: i32) imgui.Key {
Expand Down Expand Up @@ -194,7 +190,7 @@ fn UpdateKeyModifiers(mods: i32) void {
io.AddKeyEvent(.ModSuper, (mods & glfw.GLFW_MOD_SUPER) != 0);
}

pub fn MouseButtonCallback(window: ?*glfw.GLFWwindow, button: i32, action: i32, mods: i32) callconv(.C) void {
pub fn MouseButtonCallback(window: *glfw.GLFWwindow, button: i32, action: i32, mods: i32) callconv(.C) void {
const bd = GetBackendData().?;
if (bd.PrevUserCallbackMousebutton != null and window == bd.Window)
bd.PrevUserCallbackMousebutton.?(window, button, action, mods);
Expand All @@ -206,7 +202,7 @@ pub fn MouseButtonCallback(window: ?*glfw.GLFWwindow, button: i32, action: i32,
io.AddMouseButtonEvent(button, action == glfw.GLFW_PRESS);
}

pub fn ScrollCallback(window: ?*glfw.GLFWwindow, xoffset: f64, yoffset: f64) callconv(.C) void {
pub fn ScrollCallback(window: *glfw.GLFWwindow, xoffset: f64, yoffset: f64) callconv(.C) void {
const bd = GetBackendData().?;
if (bd.PrevUserCallbackScroll != null and window == bd.Window)
bd.PrevUserCallbackScroll.?(window, xoffset, yoffset);
Expand Down Expand Up @@ -240,7 +236,7 @@ fn TranslateUntranslatedKey(raw_key: i32, scancode: i32) i32 {
return raw_key;
}

pub fn KeyCallback(window: ?*glfw.GLFWwindow, raw_keycode: i32, scancode: i32, action: i32, mods: i32) callconv(.C) void {
pub fn KeyCallback(window: *glfw.GLFWwindow, raw_keycode: i32, scancode: i32, action: i32, mods: i32) callconv(.C) void {
const bd = GetBackendData().?;
if (bd.PrevUserCallbackKey != null and window == bd.Window)
bd.PrevUserCallbackKey.?(window, raw_keycode, scancode, action, mods);
Expand All @@ -262,7 +258,7 @@ pub fn KeyCallback(window: ?*glfw.GLFWwindow, raw_keycode: i32, scancode: i32, a
io.SetKeyEventNativeData(imgui_key, keycode, scancode); // To support legacy indexing (<1.87 user code)
}

pub fn WindowFocusCallback(window: ?*glfw.GLFWwindow, focused: i32) callconv(.C) void {
pub fn WindowFocusCallback(window: *glfw.GLFWwindow, focused: i32) callconv(.C) void {
const bd = GetBackendData().?;
if (bd.PrevUserCallbackWindowFocus != null and window == bd.Window)
bd.PrevUserCallbackWindowFocus.?(window, focused);
Expand All @@ -271,7 +267,7 @@ pub fn WindowFocusCallback(window: ?*glfw.GLFWwindow, focused: i32) callconv(.C)
io.AddFocusEvent(focused != 0);
}

pub fn CursorPosCallback(window: ?*glfw.GLFWwindow, x: f64, y: f64) callconv(.C) void {
pub fn CursorPosCallback(window: *glfw.GLFWwindow, x: f64, y: f64) callconv(.C) void {
const bd = GetBackendData().?;
if (bd.PrevUserCallbackCursorPos != null and window == bd.Window)
bd.PrevUserCallbackCursorPos.?(window, x, y);
Expand All @@ -283,7 +279,7 @@ pub fn CursorPosCallback(window: ?*glfw.GLFWwindow, x: f64, y: f64) callconv(.C)

// Workaround: X11 seems to send spurious Leave/Enter events which would make us lose our position,
// so we back it up and restore on Leave/Enter (see https://github.com/ocornut/imgui/issues/4984)
pub fn CursorEnterCallback(window: ?*glfw.GLFWwindow, entered: i32) callconv(.C) void {
pub fn CursorEnterCallback(window: *glfw.GLFWwindow, entered: i32) callconv(.C) void {
const bd = GetBackendData().?;
if (bd.PrevUserCallbackCursorEnter != null and window == bd.Window)
bd.PrevUserCallbackCursorEnter.?(window, entered);
Expand All @@ -299,7 +295,7 @@ pub fn CursorEnterCallback(window: ?*glfw.GLFWwindow, entered: i32) callconv(.C)
}
}

pub fn CharCallback(window: ?*glfw.GLFWwindow, c: u32) callconv(.C) void {
pub fn CharCallback(window: *glfw.GLFWwindow, c: u32) callconv(.C) void {
const bd = GetBackendData().?;
if (bd.PrevUserCallbackChar != null and window == bd.Window)
bd.PrevUserCallbackChar.?(window, c);
Expand All @@ -308,15 +304,15 @@ pub fn CharCallback(window: ?*glfw.GLFWwindow, c: u32) callconv(.C) void {
io.AddInputCharacter(c);
}

pub fn MonitorCallback(monitor: ?*glfw.GLFWmonitor, event: i32) callconv(.C) void {
pub fn MonitorCallback(monitor: *glfw.GLFWmonitor, event: i32) callconv(.C) void {
const bd = GetBackendData().?;
if (bd.PrevUserCallbackMonitor != null)
bd.PrevUserCallbackMonitor.?(monitor, event);

// Unused in 'master' branch but 'docking' branch will use this, so we declare it ahead of it so if you have to install callbacks you can install this one too.
}

pub fn InstallCallbacks(window: ?*glfw.GLFWwindow) void {
pub fn InstallCallbacks(window: *glfw.GLFWwindow) void {
const bd = GetBackendData().?;
assert(bd.InstalledCallbacks == false); // Callbacks already installed!
assert(bd.Window == window);
Expand All @@ -332,7 +328,7 @@ pub fn InstallCallbacks(window: ?*glfw.GLFWwindow) void {
bd.InstalledCallbacks = true;
}

pub fn RestoreCallbacks(window: ?*glfw.GLFWwindow) void {
pub fn RestoreCallbacks(window: *glfw.GLFWwindow) void {
const bd = GetBackendData().?;
assert(bd.InstalledCallbacks == true); // Callbacks not installed!
assert(bd.Window == window);
Expand All @@ -356,7 +352,7 @@ pub fn RestoreCallbacks(window: ?*glfw.GLFWwindow) void {
bd.PrevUserCallbackMonitor = null;
}

fn Init(window: ?*glfw.GLFWwindow, install_callbacks: bool, client_api: GlfwClientApi) bool {
fn Init(window: *glfw.GLFWwindow, install_callbacks: bool, client_api: GlfwClientApi) bool {
const io = imgui.GetIO();
assert(io.BackendPlatformUserData == null); // Already initialized a platform backend!

Expand Down Expand Up @@ -412,15 +408,15 @@ fn Init(window: ?*glfw.GLFWwindow, install_callbacks: bool, client_api: GlfwClie
return true;
}

pub fn InitForOpenGL(window: ?*glfw.GLFWwindow, install_callbacks: bool) bool {
pub fn InitForOpenGL(window: *glfw.GLFWwindow, install_callbacks: bool) bool {
return Init(window, install_callbacks, .OpenGL);
}

pub fn InitForVulkan(window: ?*glfw.GLFWwindow, install_callbacks: bool) bool {
pub fn InitForVulkan(window: *glfw.GLFWwindow, install_callbacks: bool) bool {
return Init(window, install_callbacks, .Vulkan);
}

pub fn InitForOther(window: ?*glfw.GLFWwindow, install_callbacks: bool) bool {
pub fn InitForOther(window: *glfw.GLFWwindow, install_callbacks: bool) bool {
return Init(window, install_callbacks, .Unknown);
}

Expand All @@ -430,10 +426,10 @@ pub fn Shutdown() void {
const io = imgui.GetIO();

if (bd.?.InstalledCallbacks)
RestoreCallbacks(bd.?.Window);
RestoreCallbacks(bd.?.Window.?);

for (bd.?.MouseCursors) |cursor|
glfw.glfwDestroyCursor(cursor);
if (cursor) |c| glfw.glfwDestroyCursor(c);

io.BackendPlatformName = null;
io.BackendPlatformUserData = null;
Expand All @@ -445,17 +441,17 @@ fn UpdateMouseData() void {
const io = imgui.GetIO();

const is_app_focused = if (IS_EMSCRIPTEN) true
else (glfw.glfwGetWindowAttrib(bd.Window, glfw.GLFW_FOCUSED) != 0);
else (glfw.glfwGetWindowAttrib(bd.Window.?, glfw.GLFW_FOCUSED) != 0);
if (is_app_focused) {
// (Optional) Set OS mouse position from Dear ImGui if requested (rarely used, only when ImGuiConfigFlags_NavEnableSetMousePos is enabled by user)
if (io.WantSetMousePos)
glfw.glfwSetCursorPos(bd.Window, io.MousePos.x, io.MousePos.y);
glfw.glfwSetCursorPos(bd.Window.?, io.MousePos.x, io.MousePos.y);

// (Optional) Fallback to provide mouse position when focused (ImGui_ImplGlfw_CursorPosCallback already provides this when hovered or captured)
if (is_app_focused and bd.MouseWindow == null) {
var mouse_x: f64 = 0;
var mouse_y: f64 = 0;
glfw.glfwGetCursorPos(bd.Window, &mouse_x, &mouse_y);
glfw.glfwGetCursorPos(bd.Window.?, &mouse_x, &mouse_y);
io.AddMousePosEvent(@floatCast(f32, mouse_x), @floatCast(f32, mouse_y));
bd.LastValidMousePos = .{ .x = @floatCast(f32, mouse_x), .y = @floatCast(f32, mouse_y) };
}
Expand All @@ -465,18 +461,18 @@ fn UpdateMouseData() void {
fn UpdateMouseCursor() void {
const bd = GetBackendData().?;
const io = imgui.GetIO();
if ((io.ConfigFlags.NoMouseCursorChange) or glfw.glfwGetInputMode(bd.Window, glfw.GLFW_CURSOR) == glfw.GLFW_CURSOR_DISABLED)
if ((io.ConfigFlags.NoMouseCursorChange) or glfw.glfwGetInputMode(bd.Window.?, glfw.GLFW_CURSOR) == glfw.GLFW_CURSOR_DISABLED)
return;

const imgui_cursor = imgui.GetMouseCursor();
if (imgui_cursor == .None or io.MouseDrawCursor) {
// Hide OS mouse cursor if imgui is drawing it or if it wants no cursor
glfw.glfwSetInputMode(bd.Window, glfw.GLFW_CURSOR, glfw.GLFW_CURSOR_HIDDEN);
glfw.glfwSetInputMode(bd.Window.?, glfw.GLFW_CURSOR, glfw.GLFW_CURSOR_HIDDEN);
} else {
// Show OS mouse cursor
// FIXME-PLATFORM: Unfocused windows seems to fail changing the mouse cursor with GLFW 3.2, but 3.3 works here.
glfw.glfwSetCursor(bd.Window, bd.MouseCursors[@intCast(usize, @enumToInt(imgui_cursor))] orelse bd.MouseCursors[@intCast(usize, @enumToInt(imgui.MouseCursor.Arrow))]);
glfw.glfwSetInputMode(bd.Window, glfw.GLFW_CURSOR, glfw.GLFW_CURSOR_NORMAL);
glfw.glfwSetCursor(bd.Window.?, bd.MouseCursors[@intCast(usize, @enumToInt(imgui_cursor))] orelse bd.MouseCursors[@intCast(usize, @enumToInt(imgui.MouseCursor.Arrow))]);
glfw.glfwSetInputMode(bd.Window.?, glfw.GLFW_CURSOR, glfw.GLFW_CURSOR_NORMAL);
}
}

Expand Down Expand Up @@ -520,7 +516,7 @@ fn UpdateGamepads() void {
io.BackendFlags.HasGamepad = false;
if (GLFW_HAS_GAMEPAD_API) {
var gamepad: glfw.GLFWgamepadstate = undefined;
if (!glfw.glfwGetGamepadState(glfw.GLFW_JOYSTICK_1, &gamepad))
if (glfw.glfwGetGamepadState(glfw.GLFW_JOYSTICK_1, &gamepad) == 0)
return;
inline for (mappings) |m| switch (m.kind) {
.Button => io.AddKeyEvent(m.key, gamepad.buttons[m.btn] != 0),
Expand Down Expand Up @@ -559,8 +555,8 @@ pub fn NewFrame() void {
var h: c_int = 0;
var display_w: c_int = 0;
var display_h: c_int = 0;
glfw.glfwGetWindowSize(bd.Window, &w, &h);
glfw.glfwGetFramebufferSize(bd.Window, &display_w, &display_h);
glfw.glfwGetWindowSize(bd.Window.?, &w, &h);
glfw.glfwGetFramebufferSize(bd.Window.?, &display_w, &display_h);
io.DisplaySize = .{ .x = @intToFloat(f32, w), .y = @intToFloat(f32, h) };
if (w > 0 and h > 0) {
io.DisplayFramebufferScale = .{
Expand Down
Loading

0 comments on commit 0a2cfca

Please sign in to comment.