From f6cd1be6044d205f0855b72eeaa92c87d67e845a Mon Sep 17 00:00:00 2001 From: phosit Date: Thu, 17 Apr 2025 22:10:38 +0200 Subject: [PATCH] Move the profileViewer handler to CProfileViewer `CProfileViewer::InputThunk` isn't global anymore. That makes the interface simpler and might make the compilation and the runtime faster. --- source/ps/GameSetup/GameSetup.cpp | 2 - source/ps/ProfileViewer.cpp | 86 ++++++++++++++++++------------- source/ps/ProfileViewer.h | 24 --------- 3 files changed, 50 insertions(+), 62 deletions(-) diff --git a/source/ps/GameSetup/GameSetup.cpp b/source/ps/GameSetup/GameSetup.cpp index 1ef5f8b770..4da2802d02 100644 --- a/source/ps/GameSetup/GameSetup.cpp +++ b/source/ps/GameSetup/GameSetup.cpp @@ -260,8 +260,6 @@ static void InitPs(bool setup_gui, const CStrW& gui_page, ScriptInterface* srcSc std::unique_ptr handlers{std::make_unique()}; handlers->emplace(g_VideoMode.m_InputManager, Input::Slot::GAME_VIEW, game_view_handler); - handlers->emplace(g_VideoMode.m_InputManager, Input::Slot::PROFILE_VIEWER, CProfileViewer::InputThunk); - handlers->emplace(g_VideoMode.m_InputManager, Input::Slot::HOTKEY_INPUT, HotkeyInputActualHandler); handlers->emplace(g_VideoMode.m_InputManager, Input::Slot::TOUCH_INPUT, touch_input_handler); diff --git a/source/ps/ProfileViewer.cpp b/source/ps/ProfileViewer.cpp index 057d4eb4f3..0aa39c4d0e 100644 --- a/source/ps/ProfileViewer.cpp +++ b/source/ps/ProfileViewer.cpp @@ -35,6 +35,7 @@ #include "ps/Hotkey.h" #include "ps/Profile.h" #include "ps/Pyrogenesis.h" +#include "ps/VideoMode.h" #include "scriptinterface/Object.h" #include "scriptinterface/ScriptRequest.h" @@ -69,8 +70,25 @@ public: void TableIsDeleted(AbstractProfileTable* table); void NavigateTree(int id); + void SaveToFile(); + /// File for saved profile output (reset when the game is restarted) std::ofstream outputStream; + + /** + * Input: Filter and handle any input events that the profile display is + * interested in. + * + * In particular, this function handles enable/disable of the profile + * display as well as navigating the information tree. + */ + struct InputHandler + { + CProfileViewerInternals& profileViewer; + Input::Reaction operator()(const SDL_Event& ev); + }; + Input::Handler inputHandler{g_VideoMode.m_InputManager, Input::Slot::PROFILE_VIEWER, + {*this}}; }; @@ -271,62 +289,62 @@ void CProfileViewer::RenderProfile(CCanvas2D& canvas) // Handle input -Input::Reaction CProfileViewer::Input(const SDL_Event& ev) +Input::Reaction CProfileViewerInternals::InputHandler::operator()(const SDL_Event& ev) { switch(ev.type) { case SDL_KEYDOWN: { - if (!m->profileVisible) + if (!profileViewer.profileVisible) break; int k = ev.key.keysym.sym; if (k >= SDLK_0 && k <= SDLK_9) { - m->NavigateTree(k - SDLK_0); + profileViewer.NavigateTree(k - SDLK_0); return Input::Reaction::HANDLED; } break; } case SDL_HOTKEYPRESS: - std::string hotkey = static_cast(ev.user.data1); + std::string_view hotkey = static_cast(ev.user.data1); - if( hotkey == "profile.toggle" ) + if (hotkey == "profile.toggle") { - if (!m->profileVisible) + if (!profileViewer.profileVisible) { - if (m->rootTables.size()) + if (profileViewer.rootTables.size()) { - m->profileVisible = true; - m->path.push_back(m->rootTables[0]); + profileViewer.profileVisible = true; + profileViewer.path.push_back(profileViewer.rootTables[0]); } } else { size_t i; - for(i = 0; i < m->rootTables.size(); ++i) + for(i = 0; i < profileViewer.rootTables.size(); ++i) { - if (m->rootTables[i] == m->path[0]) + if (profileViewer.rootTables[i] == profileViewer.path[0]) break; } i++; - m->path.clear(); - if (i < m->rootTables.size()) + profileViewer.path.clear(); + if (i < profileViewer.rootTables.size()) { - m->path.push_back(m->rootTables[i]); + profileViewer.path.push_back(profileViewer.rootTables[i]); } else { - m->profileVisible = false; + profileViewer.profileVisible = false; } } return Input::Reaction::HANDLED; } else if( hotkey == "profile.save" ) { - SaveToFile(); + profileViewer.SaveToFile(); return Input::Reaction::HANDLED; } break; @@ -334,15 +352,6 @@ Input::Reaction CProfileViewer::Input(const SDL_Event& ev) return Input::Reaction::PASS; } -Input::Reaction CProfileViewer::InputThunk(const SDL_Event& ev) -{ - if (CProfileViewer::IsInitialised()) - return g_ProfileViewer.Input(ev); - - return Input::Reaction::PASS; -} - - // Add a table to the list of roots void CProfileViewer::AddRootTable(AbstractProfileTable* table, bool front) { @@ -527,39 +536,44 @@ namespace } void CProfileViewer::SaveToFile() +{ + m->SaveToFile(); +} + +void CProfileViewerInternals::SaveToFile() { // Open the file, if necessary. If this method is called several times, // the profile results will be appended to the previous ones from the same // run. - if (! m->outputStream.is_open()) + if (!outputStream.is_open()) { // Open the file. (It will be closed when the CProfileViewer // destructor is called.) - OsPath path = psLogDir()/"profile.txt"; - m->outputStream.open(OsString(path), std::ofstream::out | std::ofstream::trunc); + const OsPath filePath{psLogDir()/"profile.txt"}; + outputStream.open(OsString(filePath), std::ofstream::out | std::ofstream::trunc); - if (m->outputStream.fail()) + if (outputStream.fail()) { LOGERROR("Failed to open profile log file"); return; } else { - LOGMESSAGERENDER("Profiler snapshot saved to '%s'", path.string8()); + LOGMESSAGERENDER("Profiler snapshot saved to '%s'", filePath.string8()); } } time_t t; time(&t); - m->outputStream << "================================================================\n\n"; - m->outputStream << "PS profiler snapshot - " << asctime(localtime(&t)); + outputStream << "================================================================\n\n"; + outputStream << "PS profiler snapshot - " << asctime(localtime(&t)); - std::vector tables = m->rootTables; + std::vector tables = rootTables; sort(tables.begin(), tables.end(), SortByName); - for_each(tables.begin(), tables.end(), WriteTable(m->outputStream)); + for_each(tables.begin(), tables.end(), WriteTable(outputStream)); - m->outputStream << "\n\n================================================================\n"; - m->outputStream.flush(); + outputStream << "\n\n================================================================\n"; + outputStream.flush(); } void CProfileViewer::ShowTable(const CStr& table) diff --git a/source/ps/ProfileViewer.h b/source/ps/ProfileViewer.h index a29a881aa4..39339abfb6 100644 --- a/source/ps/ProfileViewer.h +++ b/source/ps/ProfileViewer.h @@ -23,7 +23,6 @@ #define INCLUDED_PROFILE_VIEWER #include "ps/CStr.h" -#include "ps/Input.h" #include "ps/Singleton.h" #include @@ -143,20 +142,6 @@ public: */ void RenderProfile(CCanvas2D& canvas); - /** - * Input: Filter and handle any input events that the profile display - * is interested in. - * - * In particular, this function handles enable/disable of the profile - * display as well as navigating the information tree. - * - * @param ev The incoming event. - * - * @return IN_PASS or IN_HANDLED depending on whether the event relates - * to the profiling display. - */ - Input::Reaction Input(const SDL_Event& ev); - /** * AddRootTable: Add a new profile table as a root table (i.e. the * tables that you cycle through via the profile hotkey). @@ -170,15 +155,6 @@ public: */ void AddRootTable(AbstractProfileTable* table, bool front = false); - /** - * InputThunk: Delegate to the singleton's Input() member function - * if the singleton has been initialized. - * - * This allows our input handler to be installed via in_add_handler - * like a normal, global function input handler. - */ - static Input::Reaction InputThunk(const SDL_Event& ev); - /** * SaveToFile: Save the current profiler data (for all profile tables) * to a file in the 'logs' directory.