all setting gba and gbc bios

This commit is contained in:
Matthew Berry 2021-06-27 17:02:34 -10:00
parent bd7e588fdd
commit 2a4c7f9ee5
5 changed files with 80 additions and 19 deletions

View file

@ -11,16 +11,22 @@ class Config
include YAML::Serializable
property explorer_dir : String?
property gba : GBA?
property gb : GB?
property gbc : GBC?
class GBA
include YAML::Serializable
property bios : String?
def initialize
end
end
class GB
class GBC
include YAML::Serializable
property bios : String?
def initialize
end
end
end
@ -34,6 +40,24 @@ private def write(& : Config ->)
File.write(CONFIG_FILE, new_config.to_yaml)
end
private def write_gba(& : Config::GBA ->)
new_gba = config.gba || Config::GBA.new
yield new_gba
write { |config| config.gba = new_gba }
end
private def write_gbc(& : Config::GBC ->)
new_gbc = config.gbc || Config::GBC.new
yield new_gbc
write { |config| config.gbc = new_gbc }
end
private def write(& : Config ->)
new_config = config
yield new_config
File.write(CONFIG_FILE, new_config.to_yaml)
end
def explorer_dir : String
config.explorer_dir || Dir.current
end
@ -42,6 +66,18 @@ def set_explorer_dir(dir : String) : Nil
write { |config| config.explorer_dir = dir }
end
def gbc_bios : String
config.gbc.try(&.bios) || "bios.bin"
end
def set_gbc_bios(bios : String) : Nil
write_gbc { |gbc| gbc.bios = bios }
end
def gba_bios : String
config.gba.try(&.bios) || "bios.bin"
end
def set_gba_bios(bios : String) : Nil
write_gba { |gba| gba.bios = bios }
end

View file

@ -9,7 +9,7 @@ class GBController < Controller
getter name : String = "Game Boy (Color)"
def initialize(bios : String?, rom : String)
@emu = GB::GB.new(bios, rom, true, false)
@emu = GB::GB.new(bios || gbc_bios, rom, true, false)
@emu.post_init
end

View file

@ -9,7 +9,7 @@ class GBAController < Controller
getter name : String = "Game Boy Advance"
def initialize(bios : String?, rom : String)
@emu = GBA::GBA.new(gba_bios, rom)
@emu = GBA::GBA.new(bios || gba_bios, rom)
@emu.post_init
end

View file

@ -59,7 +59,20 @@ class SDLOpenGLImGuiFrontend < Frontend
render_imgui
LibSDL.gl_swap_window(@window)
update_draw_count
load_new_rom(@file_explorer.selection.not_nil!.to_s) if @file_explorer.selection
handle_file_selection
end
end
private def handle_file_selection : Nil
if selection = @file_explorer.selection
file, symbol = selection
if symbol == :ROM
load_new_rom(file.to_s)
elsif symbol == :BIOS
load_new_bios(file.to_s)
else
abort "Internal error: Unexpected file explorer symbol #{symbol}"
end
end
end
@ -80,6 +93,17 @@ class SDLOpenGLImGuiFrontend < Frontend
@sync = true
end
private def load_new_bios(bios : String) : Nil
if @controller.class == GBController
set_gbc_bios(bios)
elsif @controller.class == GBAController
set_gba_bios(bios)
else
abort "Internal error: Cannot set bios #{bios} for controller #{@controller}"
end
@file_explorer.clear_selection
end
private def handle_input : Nil
while event = SDL::Event.poll
ImGui::SDL2.process_event(event)
@ -129,7 +153,8 @@ class SDLOpenGLImGuiFrontend < Frontend
ImGui.new_frame
overlay_height = 10.0
open_file_explorer = false
open_rom_selection = false
open_bios_selection = false
if LibSDL.get_mouse_focus || @open_first_frame
if ImGui.begin_main_menu_bar
@ -138,7 +163,8 @@ class SDLOpenGLImGuiFrontend < Frontend
previously_paused = @pause
previously_synced = @sync
open_file_explorer = ImGui.menu_item "Open ROM"
open_rom_selection = ImGui.menu_item "Open ROM"
open_bios_selection = ImGui.menu_item "Select BIOS"
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)
@ -161,7 +187,8 @@ class SDLOpenGLImGuiFrontend < Frontend
end
end
@file_explorer.render(open_file_explorer, ROM_EXTENSIONS)
@file_explorer.render(:ROM, open_rom_selection, ROM_EXTENSIONS)
@file_explorer.render(:BIOS, open_bios_selection)
if @enable_overlay
ImGui.set_next_window_pos(ImGui::ImVec2.new 10, overlay_height)

View file

@ -1,23 +1,21 @@
module ImGui
class FileExplorer
@path : Path
@matched_entries = [] of Entry
@selected_entry_idx = 0
@match_hidden = false
@path : Path
@name = "File Explorer"
getter selection : Path? = nil
getter selection : Tuple(Path, Symbol)? = nil
def initialize(@path = Path[explorer_dir].expand(home: true))
gather_entries
end
def render(open_popup : Bool, extensions : Array(String)?) : Nil
ImGui.open_popup(@name) if open_popup
def render(name : Symbol, open_popup : Bool, extensions : Array(String)? = nil) : Nil
ImGui.open_popup(name.to_s) if open_popup
center = ImGui.get_main_viewport.get_center
ImGui.set_next_window_pos(center, ImGui::ImGuiCond::Appearing, ImGui::ImVec2.new(0.5, 0.5))
if ImGui.begin_popup_modal(@name, flags: ImGui::ImGuiWindowFlags::AlwaysAutoResize)
if ImGui.begin_popup_modal(name.to_s, flags: ImGui::ImGuiWindowFlags::AlwaysAutoResize)
parts = @path.parts
parts.each_with_index do |part, idx|
ImGui.same_line unless idx == 0
@ -41,7 +39,7 @@ module ImGui
if ImGui.selectable("[#{letter}] #{entry[:name]}#{'/' unless entry[:file?]}", is_selected, flags)
if entry[:file?]
@selected_entry_idx = idx
open_file if ImGui.is_mouse_double_clicked(ImGui::ImGuiMouseButton::Left)
open_file(name) if ImGui.is_mouse_double_clicked(ImGui::ImGuiMouseButton::Left)
elsif ImGui.is_mouse_double_clicked(ImGui::ImGuiMouseButton::Left)
change_dir entry[:name]
end
@ -51,7 +49,7 @@ module ImGui
ImGui.end_list_box
end
ImGui.begin_group
open_file if ImGui.button "Open"
open_file(name) if ImGui.button "Open"
ImGui.same_line
ImGui.close_current_popup if ImGui.button "Cancel"
ImGui.same_line(spacing: 10)
@ -65,8 +63,8 @@ module ImGui
@selection = nil
end
private def open_file : Nil
@selection = (@path / @matched_entries[@selected_entry_idx][:name]).normalize
private def open_file(name : Symbol) : Nil
@selection = {(@path / @matched_entries[@selected_entry_idx][:name]).normalize, name}
ImGui.close_current_popup
set_explorer_dir @path.to_s
end