From b86aabd8e098febfe1bccd17627419ced53ffce2 Mon Sep 17 00:00:00 2001 From: Matthew Berry Date: Tue, 20 Jul 2021 21:29:12 -0700 Subject: [PATCH] redesign the structure of the menu bar --- src/crab/common/frontend/controller.cr | 34 +++++++++---- .../frontend/controllers/gb_controller.cr | 9 ++-- .../frontend/controllers/gba_controller.cr | 16 ++++--- .../controllers/stubbed_controller.cr | 25 ++++++---- .../frontend/sdl_opengl_imgui_frontend.cr | 48 +++++++++---------- 5 files changed, 79 insertions(+), 53 deletions(-) diff --git a/src/crab/common/frontend/controller.cr b/src/crab/common/frontend/controller.cr index f445370..662ce2f 100644 --- a/src/crab/common/frontend/controller.cr +++ b/src/crab/common/frontend/controller.cr @@ -1,8 +1,10 @@ abstract class Controller - alias Action = Tuple(String, Proc(Nil), Bool) + class_getter shader : String? = nil abstract def emu : Emu + # Window Config + abstract def width : Int32 abstract def height : Int32 @@ -12,8 +14,6 @@ abstract class Controller def render_windows : Nil end - getter actions = [] of Action - def window_width : Int32 width end @@ -22,15 +22,27 @@ abstract class Controller height end - class_getter shader : String? = nil + # Control + + def run_until_frame : Nil + emu.run_until_frame + end + + # Audio + + abstract def sync? : Bool + + def toggle_sync : Nil + emu.toggle_sync + end + + # Video def get_framebuffer : Slice(UInt16) emu.ppu.framebuffer end - def run_until_frame : Nil - emu.run_until_frame - end + # Input def handle_controller_event(event : SDL::Event::JoyHat | SDL::Event::JoyButton) : Nil emu.handle_controller_event(event) @@ -40,7 +52,11 @@ abstract class Controller emu.handle_input(input, pressed) end - def toggle_sync : Nil - emu.toggle_sync + # Debug + + def render_debug_items : Nil + end + + def render_windows : Nil end end diff --git a/src/crab/common/frontend/controllers/gb_controller.cr b/src/crab/common/frontend/controllers/gb_controller.cr index b30130a..3e9e016 100644 --- a/src/crab/common/frontend/controllers/gb_controller.cr +++ b/src/crab/common/frontend/controllers/gb_controller.cr @@ -11,10 +11,9 @@ class GBController < Controller @emu.post_init end - def render_menu : Nil - if ImGui.begin_menu "Game Boy (Color)" - @emu.toggle_sync if ImGui.menu_item("Audio Sync", "", emu.apu.sync) - ImGui.end_menu - end + # Audio + + def sync? : Bool + @emu.apu.sync end end diff --git a/src/crab/common/frontend/controllers/gba_controller.cr b/src/crab/common/frontend/controllers/gba_controller.cr index e57958e..d814db9 100644 --- a/src/crab/common/frontend/controllers/gba_controller.cr +++ b/src/crab/common/frontend/controllers/gba_controller.cr @@ -13,12 +13,16 @@ class GBAController < Controller @emu.post_init end - def render_menu : Nil - if ImGui.begin_menu "Game Boy Advance" - @emu.toggle_sync if ImGui.menu_item("Audio Sync", "", emu.apu.sync) - ImGui.menu_item("Debug", "", pointerof(@debug_window)) - ImGui.end_menu - end + # Audio + + def sync? : Bool + @emu.apu.sync + end + + # Debug + + def render_debug_items : Nil + ImGui.menu_item("Debug", "", pointerof(@debug_window)) end def render_windows : Nil diff --git a/src/crab/common/frontend/controllers/stubbed_controller.cr b/src/crab/common/frontend/controllers/stubbed_controller.cr index 15a897c..f585ade 100644 --- a/src/crab/common/frontend/controllers/stubbed_controller.cr +++ b/src/crab/common/frontend/controllers/stubbed_controller.cr @@ -12,22 +12,31 @@ class StubbedController < Controller def initialize(*args, **kwargs) end + # Control + + def run_until_frame : Nil + end + + # Audio + + def sync? : Bool + true + end + + def toggle_sync : Nil + end + + # Video + def get_framebuffer : Slice(UInt16) Slice(UInt16).new 0 end - def run_until_frame : Nil - end + # Input def handle_controller_event(event : SDL::Event::JoyHat | SDL::Event::JoyButton) : Nil end def handle_input(input : Input, pressed : Bool) : Nil end - - def toggle_sync : Nil - end - - def actions(& : Action ->) - end end diff --git a/src/crab/common/frontend/sdl_opengl_imgui_frontend.cr b/src/crab/common/frontend/sdl_opengl_imgui_frontend.cr index 4ab43ce..7bd3d89 100644 --- a/src/crab/common/frontend/sdl_opengl_imgui_frontend.cr +++ b/src/crab/common/frontend/sdl_opengl_imgui_frontend.cr @@ -22,8 +22,6 @@ class SDLOpenGLImGuiFrontend < Frontend @last_time = Time.utc @seconds : Int32 = Time.utc.second - @enable_blend = false - @blending = false @enable_overlay = false @pause = false @recents : Array(String) = recents @@ -49,7 +47,7 @@ class SDLOpenGLImGuiFrontend < Frontend LibGL.use_program(@shader_programs[@controller.class]) @opengl_info = OpenGLInfo.new @io = setup_imgui - LibSDL.gl_set_swap_interval(1) if @controller.class == StubbedController + LibSDL.gl_set_swap_interval(1) if stubbed? @file_explorer = ImGui::FileExplorer.new @keybindings = ImGui::Keybindings.new @@ -98,8 +96,6 @@ class SDLOpenGLImGuiFrontend < Frontend LibGL.use_program(@shader_programs[@controller.class]) LibSDL.gl_set_swap_interval(0) - @enable_blend = false - @blending = false @enable_overlay = false @pause = false end @@ -137,15 +133,6 @@ class SDLOpenGLImGuiFrontend < Frontend end end - private def toggle_blending : Nil - if @blending - LibGL.disable(LibGL::BLEND) - else - LibGL.enable(LibGL::BLEND) - end - @blending = @enable_blend = !@blending - end - private def render_game : Nil LibGL.tex_image_2d( LibGL::TEXTURE_2D, @@ -161,6 +148,10 @@ class SDLOpenGLImGuiFrontend < Frontend LibGL.draw_arrays(LibGL::TRIANGLE_STRIP, 0, 4) end + private def stubbed? : Bool + @controller.class == StubbedController + end + private def render_imgui : Nil ImGui::OpenGL3.new_frame ImGui::SDL2.new_frame(@window) @@ -171,13 +162,11 @@ class SDLOpenGLImGuiFrontend < Frontend open_bios_selection = false open_keybindings = false - if (LibSDL.get_mouse_focus || @controller.class == StubbedController) && !@file_explorer.open? && !@keybindings.open? + if (LibSDL.get_mouse_focus || stubbed?) && !@file_explorer.open? && !@keybindings.open? if ImGui.begin_main_menu_bar if ImGui.begin_menu "File" - previously_paused = @pause - open_rom_selection = ImGui.menu_item "Open ROM" - open_bios_selection = ImGui.menu_item "Select BIOS" unless @controller.class == StubbedController + open_bios_selection = ImGui.menu_item "Select BIOS" unless stubbed? if ImGui.begin_menu "Recent", @recents.size > 0 @recents.each do |recent| load_new_rom(recent) if ImGui.menu_item recent @@ -190,19 +179,28 @@ class SDLOpenGLImGuiFrontend < Frontend ImGui.end_menu end ImGui.separator - ImGui.menu_item "Overlay", "", pointerof(@enable_overlay) - # ImGui.menu_item "Blend", "", pointerof(@enable_blend) todo: re-implement blending now that frames are cleared - ImGui.menu_item "Pause", "", pointerof(@pause) open_keybindings = ImGui.menu_item "Keybindings" ImGui.separator exit if ImGui.menu_item "Exit", "Ctrl+Q" ImGui.end_menu - - toggle_blending if @enable_blend ^ @blending - LibSDL.gl_set_swap_interval(@pause.to_unsafe) if previously_paused ^ @pause end - @controller.render_menu + if ImGui.begin_menu "Emulation" + previously_paused = @pause + + ImGui.menu_item "Pause", "", pointerof(@pause) + @controller.toggle_sync if ImGui.menu_item "Audio Sync", selected: @controller.sync?, enabled: !stubbed? + + LibSDL.gl_set_swap_interval(@pause.to_unsafe) if previously_paused ^ @pause + ImGui.end_menu + end + + if ImGui.begin_menu "Debug" + ImGui.menu_item "Overlay", "", pointerof(@enable_overlay) + ImGui.separator + @controller.render_debug_items + ImGui.end_menu + end overlay_height += ImGui.get_window_size.y ImGui.end_main_menu_bar