From 5c9a73c5300346f65849018fc09fa7f0181290e8 Mon Sep 17 00:00:00 2001 From: thrust26 Date: Sun, 26 Jul 2020 10:52:38 +0200 Subject: [PATCH] added emulation speed hotkeys --- docs/index.html | 12 +++++- src/common/PKeyboardHandler.cxx | 2 + src/emucore/Console.cxx | 67 ++++++++++++++++++++++++++++++++- src/emucore/Console.hxx | 6 +++ src/emucore/Event.hxx | 1 + src/emucore/EventHandler.cxx | 14 ++++++- src/emucore/EventHandler.hxx | 2 +- 7 files changed, 98 insertions(+), 6 deletions(-) diff --git a/docs/index.html b/docs/index.html index 49e4c50bc..38b3da174 100644 --- a/docs/index.html +++ b/docs/index.html @@ -1767,7 +1767,17 @@ Backspace - Toggle 'Turbo' mode + Decrease emulation speed (disables 'Turbo' mode) + Shift-Control + s + Shift-Control + s + + + Increase emulation speed (disables 'Turbo' mode) + Control + s + Control + s + + + Toggle 'Turbo' mode (maximum emulation speed) Control + t Control + t diff --git a/src/common/PKeyboardHandler.cxx b/src/common/PKeyboardHandler.cxx index 6d726f3fc..7532a1632 100644 --- a/src/common/PKeyboardHandler.cxx +++ b/src/common/PKeyboardHandler.cxx @@ -525,6 +525,8 @@ PhysicalKeyboardHandler::EventMappingArray PhysicalKeyboardHandler::DefaultCommo {Event::SettingIncrease, KBDK_KP_9, KBDM_CTRL}, {Event::ToggleInter, KBDK_I, KBDM_CTRL}, + {Event::DecreaseSpeed, KBDK_S, KBDM_SHIFT | KBDM_CTRL}, + {Event::IncreaseSpeed, KBDK_S, KBDM_CTRL }, {Event::ToggleTurbo, KBDK_T, KBDM_CTRL}, {Event::ToggleJitter, KBDK_J, MOD3}, {Event::ToggleFrameStats, KBDK_L, MOD3}, diff --git a/src/emucore/Console.cxx b/src/emucore/Console.cxx index b8d6231a6..9cb5c5087 100644 --- a/src/emucore/Console.cxx +++ b/src/emucore/Console.cxx @@ -73,6 +73,45 @@ #include "Console.hxx" +namespace { + // Emulation speed is a positive float that multiplies the framerate. However, the UI controls + // adjust speed in terms of a speedup factor (1/10, 1/9 .. 1/2, 1, 2, 3, .., 10). The following + // mapping and formatting functions implement this conversion. The speedup factor is represented + // by an integer value between -900 and 900 (0 means no speedup). + + constexpr int MAX_SPEED = 900; + constexpr int MIN_SPEED = -900; + constexpr int SPEED_STEP = 10; + + int mapSpeed(float speed) + { + speed = std::abs(speed); + + return BSPF::clamp( + static_cast(round(100 * (speed >= 1 ? speed - 1 : -1 / speed + 1))), + MIN_SPEED, MAX_SPEED + ); + } + + float unmapSpeed(int speed) + { + float f_speed = static_cast(speed) / 100; + + return speed < 0 ? -1 / (f_speed - 1) : 1 + f_speed; + } + + string formatSpeed(int speed) { + stringstream ss; + + ss + << std::setw(3) << std::fixed << std::setprecision(0) + << (unmapSpeed(speed) * 100); + + return ss.str(); + } +} + + // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Console::Console(OSystem& osystem, unique_ptr& cart, const Properties& props, AudioSettings& audioSettings) @@ -503,6 +542,31 @@ void Console::toggleTurbo() myOSystem.frameBuffer().showMessage(ss.str()); } +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +void Console::changeSpeed(int direction) +{ + int speed = mapSpeed(myOSystem.settings().getFloat("speed")); + bool turbo = myOSystem.settings().getBool("turbo"); + + speed = BSPF::clamp(speed + direction * SPEED_STEP, MIN_SPEED, MAX_SPEED); + myOSystem.settings().setValue("speed", unmapSpeed(speed)); + + // update rate + initializeAudio(); + + if(turbo) + { + myOSystem.settings().setValue("turbo", false); + // update VSync + initializeVideo(); + } + + ostringstream val; + + val << formatSpeed(speed) << "%"; + myOSystem.frameBuffer().showMessage("Emulation speed", val.str(), speed, MIN_SPEED, MAX_SPEED); +} + // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void Console::togglePhosphor() { @@ -890,9 +954,8 @@ void Console::changeAutoFireRate(int direction) if(rate) val << rate << " Hz"; else - { val << "Off"; - } + myOSystem.frameBuffer().showMessage("Autofire rate", val.str(), rate, 0, isNTSC ? 30 : 25); } diff --git a/src/emucore/Console.hxx b/src/emucore/Console.hxx index 86595da9f..366e606a0 100644 --- a/src/emucore/Console.hxx +++ b/src/emucore/Console.hxx @@ -213,6 +213,12 @@ class Console : public Serializable, public ConsoleIO */ void toggleTurbo(); + /** + Change emulation speed + + @param direction +1 indicates increase, -1 indicates decrease. + */ + void changeSpeed(int direction = +1); /** Toggles phosphor effect. diff --git a/src/emucore/Event.hxx b/src/emucore/Event.hxx index b9a301e7f..52808d77a 100644 --- a/src/emucore/Event.hxx +++ b/src/emucore/Event.hxx @@ -127,6 +127,7 @@ class Event PreviousSettingGroup, NextSettingGroup, TogglePlayBackMode, DecreaseAutoFire, IncreaseAutoFire, + DecreaseSpeed, IncreaseSpeed, LastType }; diff --git a/src/emucore/EventHandler.cxx b/src/emucore/EventHandler.cxx index 24159b369..75a38b97c 100644 --- a/src/emucore/EventHandler.cxx +++ b/src/emucore/EventHandler.cxx @@ -1220,6 +1220,14 @@ void EventHandler::handleEvent(Event::Type event, Int32 value, bool repeated) /////////////////////////////////////////////////////////////////////////// // Misc events + case Event::DecreaseSpeed: + if(pressed) myOSystem.console().changeSpeed(-1); + return; + + case Event::IncreaseSpeed: + if(pressed) myOSystem.console().changeSpeed(+1); + return; + case Event::ToggleTurbo: if (pressed && !repeated) myOSystem.console().toggleTurbo(); return; @@ -2403,7 +2411,9 @@ EventHandler::EmulActionList EventHandler::ourEmulActionList = { { { Event::TogglePauseMode, "Toggle Pause mode", "" }, { Event::StartPauseMode, "Start Pause mode", "" }, { Event::Fry, "Fry cartridge", "" }, - { Event::ToggleTurbo, "Toggle Turbo mode", "" }, + { Event::DecreaseSpeed, "Decrease emulation speed", "" }, + { Event::IncreaseSpeed, "Increase emulation speed", "" }, + { Event::ToggleTurbo, "Toggle 'Turbo' mode", "" }, { Event::DebuggerMode, "Toggle Debugger mode", "" }, { Event::ConsoleSelect, "Select", "" }, @@ -2634,7 +2644,7 @@ EventHandler::MenuActionList EventHandler::ourMenuActionList = { { const Event::EventSet EventHandler::MiscEvents = { Event::Quit, Event::ReloadConsole, Event::Fry, Event::StartPauseMode, Event::TogglePauseMode, Event::OptionsMenuMode, Event::CmdMenuMode, Event::ExitMode, - Event::ToggleTurbo, + Event::ToggleTurbo, Event::DecreaseSpeed, Event::IncreaseSpeed, Event::TakeSnapshot, Event::ToggleContSnapshots, Event::ToggleContSnapshotsFrame, // Event::MouseAxisXMove, Event::MouseAxisYMove, // Event::MouseButtonLeftValue, Event::MouseButtonRightValue, diff --git a/src/emucore/EventHandler.hxx b/src/emucore/EventHandler.hxx index 055d56d64..93e167282 100644 --- a/src/emucore/EventHandler.hxx +++ b/src/emucore/EventHandler.hxx @@ -558,7 +558,7 @@ class EventHandler #else REFRESH_SIZE = 0, #endif - EMUL_ACTIONLIST_SIZE = 162 + PNG_SIZE + COMBO_SIZE + REFRESH_SIZE, + EMUL_ACTIONLIST_SIZE = 164 + PNG_SIZE + COMBO_SIZE + REFRESH_SIZE, MENU_ACTIONLIST_SIZE = 18 ;