summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authors-ol <s-ol@users.noreply.github.com>2019-10-20 18:11:41 +0000
committers-ol <s-ol@users.noreply.github.com>2019-10-20 18:11:41 +0000
commit883688f3009291b9e5d2baa121cf8a3da8ac9d0e (patch)
tree97cf4aae9c7be3333b0f2430b7e8bda85ffb5179 /src
parentinitial commit (diff)
downloadzig-imgui-883688f3009291b9e5d2baa121cf8a3da8ac9d0e.tar.gz
zig-imgui-883688f3009291b9e5d2baa121cf8a3da8ac9d0e.zip
finish GLFW impl
Diffstat (limited to 'src')
-rw-r--r--src/c.zig7
-rw-r--r--src/glfw_impl.zig91
-rw-r--r--src/main.zig38
3 files changed, 108 insertions, 28 deletions
diff --git a/src/c.zig b/src/c.zig
index eeea35f..a4e20cc 100644
--- a/src/c.zig
+++ b/src/c.zig
@@ -1,5 +1,6 @@
pub usingnamespace @cImport({
- @cDefine("CIMGUI_DEFINE_ENUMS_AND_STRUCTS", "1");
- @cInclude("cimgui.h");
- @cInclude("GLFW/glfw3.h");
+ @cInclude("epoxy/gl.h");
+ @cDefine("CIMGUI_DEFINE_ENUMS_AND_STRUCTS", "1");
+ @cInclude("cimgui.h");
+ @cInclude("GLFW/glfw3.h");
});
diff --git a/src/glfw_impl.zig b/src/glfw_impl.zig
index 7ef2f63..9b79374 100644
--- a/src/glfw_impl.zig
+++ b/src/glfw_impl.zig
@@ -10,16 +10,16 @@ pub const ClientApi = enum {
var g_Window: ?*c.GLFWwindow = null;
var g_ClientApi: ClientApi = .Unknown;
var g_Time: f32 = 0.0;
-var g_MouseJustPressed = [5]bool;
-var g_MouseCursors = [_]?*c.GLFWcursor{null} ** @enumToInt(c.ImGuiMouseCursor_COUNT);
+var g_MouseJustPressed = [_]bool{ false } ** 5;
+var g_MouseCursors = [_]?*c.GLFWcursor{ null } ** @enumToInt(c.ImGuiMouseCursor_COUNT);
var g_WantUpdateMonitors = true;
// Chain GLFW callbacks for main viewport:
// our callbacks will call the user's previously installed callbacks, if any.
-var g_PrevUserCallbackMousebutton: ?*c.GLFWmousebuttonfun = null;
-var g_PrevUserCallbackScroll: ?*c.GLFWscrollfun = null;
-var g_PrevUserCallbackKey: ?*c.GLFWkeyfun = null;
-var g_PrevUserCallbackChar: ?*c.GLFWcharfun = null;
+var g_PrevUserCallbackMousebutton: ?c.GLFWmousebuttonfun = null;
+var g_PrevUserCallbackScroll: ?c.GLFWscrollfun = null;
+var g_PrevUserCallbackKey: ?c.GLFWkeyfun = null;
+var g_PrevUserCallbackChar: ?c.GLFWcharfun = null;
pub fn Init(window: *c.GLFWwindow, install_callbacks: bool, client_api: ClientApi) void {
g_Window = window;
@@ -30,10 +30,10 @@ pub fn Init(window: *c.GLFWwindow, install_callbacks: bool, client_api: ClientAp
io.*.BackendFlags |= @enumToInt(c.ImGuiBackendFlags_HasMouseCursors); // We can honor GetMouseCursor() values (optional)
io.*.BackendFlags |= @enumToInt(c.ImGuiBackendFlags_HasSetMousePos); // We can honor io.WantSetMousePos requests (optional, rarely used)
io.*.BackendFlags |= @enumToInt(c.ImGuiBackendFlags_PlatformHasViewports); // We can create multi-viewports on the Platform side (optional)
- if (false and c.GLFW_HAS_GLFW_HOVERED and builtin.os == builtin.Os.windows) {
+ if (@hasField(c, "GLFW_HAS_GLFW_HOVERED") and builtin.os == builtin.Os.windows) {
io.*.BackendFlags |= @enumToInt(ImGuiBackendFlags_HasMouseHoveredViewport); // We can set io.MouseHoveredViewport correctly (optional, not easy)
}
- io.*.BackendPlatformName = c"imgui_impl_glfw";
+ io.*.BackendPlatformName = c"imgui_impl_glfw.zig";
// Keyboard mapping. ImGui will use those indices to peek into the io.KeysDown[] array.
io.*.KeyMap[@enumToInt(c.ImGuiKey_Tab)] = c.GLFW_KEY_TAB;
@@ -59,6 +59,7 @@ pub fn Init(window: *c.GLFWwindow, install_callbacks: bool, client_api: ClientAp
io.*.KeyMap[@enumToInt(c.ImGuiKey_Y)] = c.GLFW_KEY_Y;
io.*.KeyMap[@enumToInt(c.ImGuiKey_Z)] = c.GLFW_KEY_Z;
+ // @TODO: Clipboard
// io.SetClipboardTextFn = ImGui_ImplGlfw_SetClipboardText;
// io.GetClipboardTextFn = ImGui_ImplGlfw_GetClipboardText;
// io.ClipboardUserData = g_Window;
@@ -77,11 +78,11 @@ pub fn Init(window: *c.GLFWwindow, install_callbacks: bool, client_api: ClientAp
g_PrevUserCallbackScroll = null;
g_PrevUserCallbackKey = null;
g_PrevUserCallbackChar = null;
- if (install_callbacks and false) {
- // g_PrevUserCallbackMousebutton = glfwSetMouseButtonCallback(window, ImGui_ImplGlfw_MouseButtonCallback);
- // g_PrevUserCallbackScroll = glfwSetScrollCallback(window, ImGui_ImplGlfw_ScrollCallback);
- // g_PrevUserCallbackKey = glfwSetKeyCallback(window, ImGui_ImplGlfw_KeyCallback);
- // g_PrevUserCallbackChar = glfwSetCharCallback(window, ImGui_ImplGlfw_CharCallback);
+ if (install_callbacks) {
+ g_PrevUserCallbackMousebutton = c.glfwSetMouseButtonCallback(window, Callback_MouseButton);
+ g_PrevUserCallbackScroll = c.glfwSetScrollCallback(window, Callback_Scroll);
+ g_PrevUserCallbackKey = c.glfwSetKeyCallback(window, Callback_Key);
+ g_PrevUserCallbackChar = c.glfwSetCharCallback(window, Callback_Char);
}
// Our mouse update function expect PlatformHandle to be filled for the main viewport
@@ -91,6 +92,7 @@ pub fn Init(window: *c.GLFWwindow, install_callbacks: bool, client_api: ClientAp
main_viewport.*.PlatformHandleRaw = c.glfwGetWin32Window(g_Window);
}
+ // @TODO: Platform Interface (Viewport)
// if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable != 0)
// ImGui_ImplGlfw_InitPlatformInterface();
@@ -98,10 +100,73 @@ pub fn Init(window: *c.GLFWwindow, install_callbacks: bool, client_api: ClientAp
}
pub fn Shutdown() void {
+ // @TODO: Platform Interface (Viewport)
// ImGui_ImplGlfw_ShutdownPlatformInterface();
+
for (g_MouseCursors) |*cursor| {
c.glfwDestroyCursor(cursor.*);
cursor.* = null;
}
g_ClientApi = .Unknown;
}
+
+pub fn NewFrame() void {
+ const io = c.igGetIO();
+ // @TODO: assert font atlas
+
+ var w : c_int = undefined;
+ var h : c_int = undefined;
+ var display_w : c_int = undefined;
+ var display_h : c_int = undefined;
+ c.glfwGetWindowSize(g_Window, &w, &h);
+ c.glfwGetFramebufferSize(g_Window, &display_w, &display_h);
+ io.*.DisplaySize = c.ImVec2{ .x = @intToFloat(f32, w), .y = @intToFloat(f32, h) };
+}
+
+// GLFW Callbacks
+extern fn Callback_MouseButton(window: ?*c.GLFWwindow, button: c_int, action: c_int, mods: c_int) void {
+ // @TODO: delegate up
+
+ if (button < 0)
+ return;
+
+ const button_u = @intCast(usize, button);
+ if (action == c.GLFW_PRESS and button_u < g_MouseJustPressed.len)
+ g_MouseJustPressed[button_u] = true;
+}
+
+extern fn Callback_Scroll(window: ?*c.GLFWwindow, dx: f64, dy: f64) void {
+ // @TODO: delegate up
+
+ const io = c.igGetIO();
+ io.*.MouseWheelH += @floatCast(f32, dx);
+ io.*.MouseWheel += @floatCast(f32, dy);
+}
+
+extern fn Callback_Key(window: ?*c.GLFWwindow, key: c_int, scancode: c_int, action: c_int, modifiers: c_int) void {
+ // @TODO: delegate up
+
+ if (key < 0)
+ unreachable;
+
+ const key_u = @intCast(usize, key);
+
+ const io = c.igGetIO();
+ if (action == c.GLFW_PRESS)
+ io.*.KeysDown[key_u] = true;
+ if (action == c.GLFW_RELEASE)
+ io.*.KeysDown[key_u] = false;
+
+ // Modifiers are not reliable across systems
+ io.*.KeyCtrl = io.*.KeysDown[c.GLFW_KEY_LEFT_CONTROL] or io.*.KeysDown[c.GLFW_KEY_RIGHT_CONTROL];
+ io.*.KeyShift = io.*.KeysDown[c.GLFW_KEY_LEFT_SHIFT] or io.*.KeysDown[c.GLFW_KEY_RIGHT_SHIFT];
+ io.*.KeyAlt = io.*.KeysDown[c.GLFW_KEY_LEFT_ALT] or io.*.KeysDown[c.GLFW_KEY_RIGHT_ALT];
+ io.*.KeySuper = io.*.KeysDown[c.GLFW_KEY_LEFT_SUPER] or io.*.KeysDown[c.GLFW_KEY_RIGHT_SUPER];
+}
+
+extern fn Callback_Char(window: ?*c.GLFWwindow, char: c_uint) void {
+ // @TODO: delegate up
+
+ const io = c.igGetIO();
+ c.ImGuiIO_AddInputCharacter(io, char);
+}
diff --git a/src/main.zig b/src/main.zig
index 42fa152..08f5bea 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -4,7 +4,7 @@ const panic = std.debug.panic;
const glfw_impl = @import("glfw_impl.zig");
extern fn errorCallback(err: c_int, description: [*c]const u8) void {
- panic("Error: {}\n", description);
+ panic("Error: {}\n", description);
}
var window: *c.GLFWwindow = undefined;
@@ -13,7 +13,7 @@ pub fn main() void {
_ = c.glfwSetErrorCallback(errorCallback);
if (c.glfwInit() == c.GL_FALSE) {
- panic("GLFW init failure\n");
+ panic("GLFW init failure\n");
}
defer c.glfwTerminate();
@@ -30,7 +30,7 @@ pub fn main() void {
const window_width = 640;
const window_height = 480;
window = c.glfwCreateWindow(window_width, window_height, c"ImGUI Test", null, null) orelse {
- panic("unable to create window\n");
+ panic("unable to create window\n");
};
defer c.glfwDestroyWindow(window);
@@ -54,24 +54,38 @@ pub fn main() void {
}
glfw_impl.Init(window, true, glfw_impl.ClientApi.OpenGL);
- // ImGui_InitForOpenGL(window, true);
+ defer glfw_impl.Shutdown();
+
// ImGui_ImplOpenGL3_Init(glsl_version);
const start_time = c.glfwGetTime();
var prev_time = start_time;
while (c.glfwWindowShouldClose(window) == c.GL_FALSE) {
- // c.glClear(c.GL_COLOR_BUFFER_BIT | c.GL_DEPTH_BUFFER_BIT | c.GL_STENCIL_BUFFER_BIT);
-
- const now_time = c.glfwGetTime();
- const elapsed = now_time - prev_time;
- prev_time = now_time;
+ c.glfwPollEvents();
+ glfw_impl.NewFrame();
+ // gl3_impl.NewFrame();
+ c.igNewFrame();
+
+ // main part
+ c.igShowDemoWindow(null);
+
+ c.igRender();
+ var w: c_int = undefined;
+ var h: c_int = undefined;
+ c.glfwGetFramebufferSize(window, &w, &h);
+ c.glViewport(0, 0, w, h);
+ c.glClearColor(0.0, 0.0, 0.0, 0.0);
+ c.glClear(c.GL_COLOR_BUFFER_BIT);
+ // gl3_impl.RenderDrawData(c.igGetDrawData());
+
+ // const now_time = c.glfwGetTime();
+ // const elapsed = now_time - prev_time;
+ // prev_time = now_time;
// nextFrame(t, elapsed);
-
// draw(t, @This());
+
c.glfwSwapBuffers(window);
-
- c.glfwPollEvents();
}
}