From c39baef28f03c00689606b5ac62d88c5f4f51382 Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Tue, 17 Jan 2023 17:55:45 -0600 Subject: [PATCH] feat: implement menu bar + add file picker dep --- .gitmodules | 3 +++ build.zig | 6 +++++- lib/nfd-zig | 1 + src/platform.zig | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 43 insertions(+), 1 deletion(-) create mode 160000 lib/nfd-zig diff --git a/.gitmodules b/.gitmodules index 9ce0f12..9d0cf56 100644 --- a/.gitmodules +++ b/.gitmodules @@ -19,3 +19,6 @@ [submodule "lib/zgui"] path = lib/zgui url = https://git.musuka.dev/paoda/zgui +[submodule "lib/nfd-zig"] + path = lib/nfd-zig + url = https://github.com/fabioarnold/nfd-zig diff --git a/build.zig b/build.zig index b2df81a..51b7d55 100644 --- a/build.zig +++ b/build.zig @@ -4,6 +4,7 @@ const builtin = @import("builtin"); const Sdk = @import("lib/SDL.zig/Sdk.zig"); const gdbstub = @import("lib/zba-gdbstub/build.zig"); const zgui = @import("lib/zgui/build.zig"); +const nfd = @import("lib/nfd-zig/build.zig"); pub fn build(b: *std.build.Builder) void { // Minimum Zig Version @@ -45,6 +46,9 @@ pub fn build(b: *std.build.Builder) void { // gdbstub gdbstub.link(exe); + // NativeFileDialog(ue) Bindings + exe.linkLibrary(nfd.makeLib(b, mode, target)); + exe.addPackage(nfd.getPackage("nfd")); // Zig SDL Bindings: https://github.com/MasterQ32/SDL.zig const sdk = Sdk.init(b, null); @@ -54,8 +58,8 @@ pub fn build(b: *std.build.Builder) void { // Dear ImGui bindings const zgui_options = zgui.BuildOptionsStep.init(b, .{ .backend = .sdl2_opengl3 }); const zgui_pkg = zgui.getPkg(&.{zgui_options.getPkg()}); - exe.addPackage(zgui_pkg); zgui.link(exe, zgui_options); + exe.addPackage(zgui_pkg); exe.setBuildMode(mode); exe.install(); diff --git a/lib/nfd-zig b/lib/nfd-zig new file mode 160000 index 0000000..75acefd --- /dev/null +++ b/lib/nfd-zig @@ -0,0 +1 @@ +Subproject commit 75acefd571ede5361514b3a8ae9c8b36c6908d36 diff --git a/src/platform.zig b/src/platform.zig index 132895d..49aaded 100644 --- a/src/platform.zig +++ b/src/platform.zig @@ -2,6 +2,7 @@ const std = @import("std"); const SDL = @import("sdl2"); const gl = @import("gl"); const zgui = @import("zgui"); +const nfd = @import("nfd"); const emu = @import("core/emu.zig"); const config = @import("config.zig"); @@ -34,6 +35,7 @@ pub const Gui = struct { const State = struct { fps_hist: RingBuffer(u32), + should_quit: bool = false, pub fn init(allocator: Allocator) !@This() { const history = try allocator.alloc(u32, 0x400); @@ -261,6 +263,36 @@ pub const Gui = struct { fn draw(self: *Self, tex_id: GLuint, cpu: *const Arm7tdmi) void { const win_scale = config.config().host.win_scale; + { + _ = zgui.beginMainMenuBar(); + defer zgui.endMainMenuBar(); + + if (zgui.beginMenu("File", true)) { + defer zgui.endMenu(); + + if (zgui.menuItem("Quit", .{})) self.state.should_quit = true; + if (zgui.menuItem("Insert ROM", .{})) blk: { + const maybe_path = nfd.openFileDialog("gba", null) catch |e| { + log.err("failed to open file dialog: {?}", .{e}); + break :blk; + }; + + if (maybe_path) |file_path| { + defer nfd.freePath(file_path); + log.info("user chose: \"{s}\"", .{file_path}); + + // emu.loadRom(cpu, file_path); + } + } + } + + if (zgui.beginMenu("Emulation", true)) { + defer zgui.endMenu(); + + if (zgui.menuItem("Restart", .{})) log.warn("TODO: Restart Emulator", .{}); + } + } + { const w = @intToFloat(f32, gba_width * win_scale); const h = @intToFloat(f32, gba_height * win_scale); @@ -422,6 +454,8 @@ pub const Gui = struct { // This might be true if the emu is running via a gdbstub server // and the gdb stub exits first if (quit.load(.Monotonic)) break :emu_loop; + // Quit Signal from Dear Imgui + if (self.state.should_quit) break :emu_loop; while (SDL.SDL_PollEvent(&event) != 0) { _ = zgui.backend.processEvent(&event);