From e3dd781f7164296b66ed56f8b81f01bc545250eb Mon Sep 17 00:00:00 2001 From: Matthew Berry Date: Sun, 1 Aug 2021 21:54:20 -0700 Subject: [PATCH] add menu to change window size or fullscreen --- .../frontend/sdl_opengl_imgui_frontend.cr | 35 ++++++++++++++----- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/src/crab/common/frontend/sdl_opengl_imgui_frontend.cr b/src/crab/common/frontend/sdl_opengl_imgui_frontend.cr index 91bb361..ce57eda 100644 --- a/src/crab/common/frontend/sdl_opengl_imgui_frontend.cr +++ b/src/crab/common/frontend/sdl_opengl_imgui_frontend.cr @@ -10,7 +10,6 @@ require "./shaders/*" class SDLOpenGLImGuiFrontend < Frontend CONTROLLERS = [StubbedController, GBController, GBAController] ROM_EXTENSIONS = CONTROLLERS.reduce([] of String) { |acc, controller| acc + controller.extensions } - SCALE = 3 SHADERS = Path["#{__DIR__}/shaders"] @controller : Controller @@ -31,6 +30,8 @@ class SDLOpenGLImGuiFrontend < Frontend @enable_overlay = false @pause = false @recents : Array(String) = recents + @scale = 3 + @fullscreen = false @opengl_info : OpenGLInfo @@ -45,8 +46,8 @@ class SDLOpenGLImGuiFrontend < Frontend @controller = init_controller(bios, rom) @window = SDL::Window.new( window_title(59.7), - @controller.window_width * SCALE, - @controller.window_height * SCALE, + @controller.window_width * @scale, + @controller.window_height * @scale, x: LibSDL::WindowPosition::CENTERED, y: LibSDL::WindowPosition::CENTERED, flags: SDL::Window::Flags::OPENGL | SDL::Window::Flags::RESIZABLE @@ -124,9 +125,8 @@ class SDLOpenGLImGuiFrontend < Frontend @recents.pop(@recents.size - 8) if @recents.size > 8 set_recents @recents - LibSDL.set_window_size(@window, @controller.window_width * SCALE, @controller.window_height * SCALE) + LibSDL.set_window_size(@window, @controller.window_width * @scale, @controller.window_height * @scale) LibSDL.set_window_position(@window, LibSDL::WindowPosition::CENTERED, LibSDL::WindowPosition::CENTERED) - LibGL.viewport(0, 0, @window.width, @window.height) @shader_programs[@controller.class].use LibGL.disable(LibGL::BLEND) @@ -151,14 +151,16 @@ class SDLOpenGLImGuiFrontend < Frontend when SDL::Event::Keyboard if @keybindings.wants_input? @keybindings.key_released(event.sym) unless event.pressed? # pass on key release + elsif event.mod.includes?(LibSDL::Keymod::LCTRL) + case event.sym + when LibSDL::Keycode::P then pause(!@pause) unless event.pressed? + when LibSDL::Keycode::F then @window.fullscreen = (@fullscreen = !@fullscreen) unless event.pressed? + when LibSDL::Keycode::Q then exit + end elsif input = @keybindings[event.sym]? @controller.handle_input(input, event.pressed?) elsif event.sym == LibSDL::Keycode::TAB @controller.toggle_sync if event.pressed? - elsif event.sym == LibSDL::Keycode::Q && event.mod.includes?(LibSDL::Keymod::LCTRL) - exit - elsif event.sym == LibSDL::Keycode::P && event.mod.includes?(LibSDL::Keymod::LCTRL) - pause(!@pause) unless event.pressed? # toggle pause on key release end when SDL::Event::JoyHat, SDL::Event::JoyButton then @controller.handle_controller_event(event) when SDL::Event::Window @@ -242,6 +244,21 @@ class SDLOpenGLImGuiFrontend < Frontend ImGui.end_menu end + if ImGui.begin_menu "Audio/Video" + if ImGui.begin_menu "Frame size" + (1..8).each do |scale| + if ImGui.menu_item "#{scale}x", selected: scale == @scale + @scale = scale + LibSDL.set_window_size(@window, @controller.window_width * @scale, @controller.window_height * @scale) + end + end + ImGui.separator + @window.fullscreen = @fullscreen if ImGui.menu_item "Fullscreen", "Ctrl+F", pointerof(@fullscreen) + ImGui.end_menu + end + ImGui.end_menu + end + if ImGui.begin_menu "Debug" ImGui.menu_item "Overlay", "", pointerof(@enable_overlay) ImGui.separator