From 2b7f054590419f2b39339ac1dd7c64baecad1431 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Fri, 21 Apr 2023 18:31:11 +0200 Subject: [PATCH] Use const reference to pass `CEvent` objects The size of the `CEvent` class is 140 bytes, so it's more efficient to pass objects by reference. --- src/engine/client/input.h | 2 +- src/engine/input.h | 4 +-- src/game/client/component.h | 4 +-- src/game/client/components/binds.cpp | 28 +++++++++---------- src/game/client/components/binds.h | 4 +-- src/game/client/components/chat.cpp | 2 +- src/game/client/components/chat.h | 2 +- src/game/client/components/console.cpp | 4 +-- src/game/client/components/console.h | 4 +-- src/game/client/components/menus.cpp | 2 +- src/game/client/components/menus.h | 4 +-- src/game/client/components/menus_settings.cpp | 2 +- src/game/client/components/motd.cpp | 2 +- src/game/client/components/motd.h | 2 +- src/game/client/gameclient.cpp | 6 ++-- 15 files changed, 36 insertions(+), 36 deletions(-) diff --git a/src/engine/client/input.h b/src/engine/client/input.h index c5ecc7cf4..3ee21de12 100644 --- a/src/engine/client/input.h +++ b/src/engine/client/input.h @@ -77,7 +77,7 @@ private: void AddEvent(char *pText, int Key, int Flags); void Clear() override; - bool IsEventValid(CEvent *pEvent) const override { return pEvent->m_InputCount == m_InputCounter; } + bool IsEventValid(const CEvent &Event) const override { return Event.m_InputCount == m_InputCounter; } // quick access to input unsigned short m_aInputCount[g_MaxKeys]; // tw-KEY diff --git a/src/engine/input.h b/src/engine/input.h index 2b8e08f45..f5b4ab703 100644 --- a/src/engine/input.h +++ b/src/engine/input.h @@ -55,8 +55,8 @@ public: // events int NumEvents() const { return m_NumEvents; } - virtual bool IsEventValid(CEvent *pEvent) const = 0; - CEvent GetEvent(int Index) const + virtual bool IsEventValid(const CEvent &Event) const = 0; + const CEvent &GetEvent(int Index) const { dbg_assert(Index >= 0 && Index < m_NumEvents, "Index invalid"); return m_aInputEvents[Index]; diff --git a/src/game/client/component.h b/src/game/client/component.h index af8c8543b..fb0f731b0 100644 --- a/src/game/client/component.h +++ b/src/game/client/component.h @@ -209,9 +209,9 @@ public: virtual bool OnCursorMove(float x, float y, IInput::ECursorType CursorType) { return false; } /** * Called on a input event. - * @param e The input event. + * @param Event The input event. */ - virtual bool OnInput(IInput::CEvent e) { return false; } + virtual bool OnInput(const IInput::CEvent &Event) { return false; } }; #endif diff --git a/src/game/client/components/binds.cpp b/src/game/client/components/binds.cpp index 736e79fa2..97ec13dd2 100644 --- a/src/game/client/components/binds.cpp +++ b/src/game/client/components/binds.cpp @@ -8,7 +8,7 @@ static const ColorRGBA gs_BindPrintColor{1.0f, 1.0f, 0.8f, 1.0f}; -bool CBinds::CBindsSpecial::OnInput(IInput::CEvent Event) +bool CBinds::CBindsSpecial::OnInput(const IInput::CEvent &Event) { // only handle F and composed F binds if(((Event.m_Key >= KEY_F1 && Event.m_Key <= KEY_F12) || (Event.m_Key >= KEY_F13 && Event.m_Key <= KEY_F24)) && (Event.m_Key != KEY_F5 || !m_pClient->m_Menus.IsActive())) @@ -123,33 +123,33 @@ int CBinds::GetModifierMaskOfKey(int Key) } } -bool CBinds::OnInput(IInput::CEvent e) +bool CBinds::OnInput(const IInput::CEvent &Event) { // don't handle invalid events - if(e.m_Key <= 0 || e.m_Key >= KEY_LAST) + if(Event.m_Key <= 0 || Event.m_Key >= KEY_LAST) return false; int Mask = GetModifierMask(Input()); - int KeyModifierMask = GetModifierMaskOfKey(e.m_Key); + int KeyModifierMask = GetModifierMaskOfKey(Event.m_Key); Mask &= ~KeyModifierMask; bool ret = false; - if(m_aapKeyBindings[Mask][e.m_Key]) + if(m_aapKeyBindings[Mask][Event.m_Key]) { - if(e.m_Flags & IInput::FLAG_PRESS) - Console()->ExecuteLineStroked(1, m_aapKeyBindings[Mask][e.m_Key]); - if(e.m_Flags & IInput::FLAG_RELEASE) - Console()->ExecuteLineStroked(0, m_aapKeyBindings[Mask][e.m_Key]); + if(Event.m_Flags & IInput::FLAG_PRESS) + Console()->ExecuteLineStroked(1, m_aapKeyBindings[Mask][Event.m_Key]); + if(Event.m_Flags & IInput::FLAG_RELEASE) + Console()->ExecuteLineStroked(0, m_aapKeyBindings[Mask][Event.m_Key]); ret = true; } - if(m_aapKeyBindings[0][e.m_Key] && !ret) + if(m_aapKeyBindings[0][Event.m_Key] && !ret) { // When ctrl+shift are pressed (ctrl+shift binds and also the hard-coded ctrl+shift+d, ctrl+shift+g, ctrl+shift+e), ignore other +xxx binds - if(e.m_Flags & IInput::FLAG_PRESS && Mask != ((1 << MODIFIER_CTRL) | (1 << MODIFIER_SHIFT)) && Mask != ((1 << MODIFIER_GUI) | (1 << MODIFIER_SHIFT))) - Console()->ExecuteLineStroked(1, m_aapKeyBindings[0][e.m_Key]); - if(e.m_Flags & IInput::FLAG_RELEASE) - Console()->ExecuteLineStroked(0, m_aapKeyBindings[0][e.m_Key]); + if(Event.m_Flags & IInput::FLAG_PRESS && Mask != ((1 << MODIFIER_CTRL) | (1 << MODIFIER_SHIFT)) && Mask != ((1 << MODIFIER_GUI) | (1 << MODIFIER_SHIFT))) + Console()->ExecuteLineStroked(1, m_aapKeyBindings[0][Event.m_Key]); + if(Event.m_Flags & IInput::FLAG_RELEASE) + Console()->ExecuteLineStroked(0, m_aapKeyBindings[0][Event.m_Key]); ret = true; } diff --git a/src/game/client/components/binds.h b/src/game/client/components/binds.h index 0cd55a360..b420e0e6f 100644 --- a/src/game/client/components/binds.h +++ b/src/game/client/components/binds.h @@ -32,7 +32,7 @@ public: public: CBinds *m_pBinds; virtual int Sizeof() const override { return sizeof(*this); } - virtual bool OnInput(IInput::CEvent Event) override; + virtual bool OnInput(const IInput::CEvent &Event) override; }; enum @@ -60,7 +60,7 @@ public: static const char *GetKeyBindModifiersName(int ModifierCombination); virtual void OnConsoleInit() override; - virtual bool OnInput(IInput::CEvent Event) override; + virtual bool OnInput(const IInput::CEvent &Event) override; // DDRace diff --git a/src/game/client/components/chat.cpp b/src/game/client/components/chat.cpp index 0b0e11f18..8c9e922b0 100644 --- a/src/game/client/components/chat.cpp +++ b/src/game/client/components/chat.cpp @@ -165,7 +165,7 @@ void CChat::OnInit() Console()->Chain("cl_chat_old", ConchainChatOld, this); } -bool CChat::OnInput(IInput::CEvent Event) +bool CChat::OnInput(const IInput::CEvent &Event) { if(m_Mode == MODE_NONE) return false; diff --git a/src/game/client/components/chat.h b/src/game/client/components/chat.h index a077413c4..84d3b27fb 100644 --- a/src/game/client/components/chat.h +++ b/src/game/client/components/chat.h @@ -166,7 +166,7 @@ public: void Reset(); void OnRelease() override; void OnMessage(int MsgType, void *pRawMsg) override; - bool OnInput(IInput::CEvent Event) override; + bool OnInput(const IInput::CEvent &Event) override; void OnInit() override; void RebuildChat(); diff --git a/src/game/client/components/console.cpp b/src/game/client/components/console.cpp index ce1262a83..0e5da06f7 100644 --- a/src/game/client/components/console.cpp +++ b/src/game/client/components/console.cpp @@ -203,7 +203,7 @@ void CGameConsole::CInstance::PossibleArgumentsCompleteCallback(int Index, const } } -void CGameConsole::CInstance::OnInput(IInput::CEvent Event) +void CGameConsole::CInstance::OnInput(const IInput::CEvent &Event) { bool Handled = false; @@ -939,7 +939,7 @@ void CGameConsole::OnMessage(int MsgType, void *pRawMsg) { } -bool CGameConsole::OnInput(IInput::CEvent Event) +bool CGameConsole::OnInput(const IInput::CEvent &Event) { // accept input when opening, but not at first frame to discard the input that caused the console to open if(m_ConsoleState != CONSOLE_OPEN && (m_ConsoleState != CONSOLE_OPENING || m_StateChangeEnd == TimeNow() + m_StateChangeDuration)) diff --git a/src/game/client/components/console.h b/src/game/client/components/console.h index 17f52d940..aba19a24e 100644 --- a/src/game/client/components/console.h +++ b/src/game/client/components/console.h @@ -71,7 +71,7 @@ class CGameConsole : public CComponent void ExecuteLine(const char *pLine); - void OnInput(IInput::CEvent Event); + void OnInput(const IInput::CEvent &Event); void PrintLine(const char *pLine, int Len, ColorRGBA PrintColor); const char *GetString() const { return m_Input.GetString(); } @@ -138,7 +138,7 @@ public: virtual void OnReset() override; virtual void OnRender() override; virtual void OnMessage(int MsgType, void *pRawMsg) override; - virtual bool OnInput(IInput::CEvent Events) override; + virtual bool OnInput(const IInput::CEvent &Event) override; bool IsClosed() { return m_ConsoleState == CONSOLE_CLOSED; } }; diff --git a/src/game/client/components/menus.cpp b/src/game/client/components/menus.cpp index 031eba30d..9be3b19a2 100644 --- a/src/game/client/components/menus.cpp +++ b/src/game/client/components/menus.cpp @@ -2297,7 +2297,7 @@ bool CMenus::OnCursorMove(float x, float y, IInput::ECursorType CursorType) return true; } -bool CMenus::OnInput(IInput::CEvent Event) +bool CMenus::OnInput(const IInput::CEvent &Event) { // special handle esc and enter for popup purposes if(Event.m_Flags & IInput::FLAG_PRESS && Event.m_Key == KEY_ESCAPE) diff --git a/src/game/client/components/menus.h b/src/game/client/components/menus.h index 886e02358..742758611 100644 --- a/src/game/client/components/menus.h +++ b/src/game/client/components/menus.h @@ -55,7 +55,7 @@ public: int m_ModifierCombination; CMenusKeyBinder(); virtual int Sizeof() const override { return sizeof(*this); } - virtual bool OnInput(IInput::CEvent Event) override; + virtual bool OnInput(const IInput::CEvent &Event) override; }; class CMenus : public CComponent @@ -593,7 +593,7 @@ public: virtual void OnWindowResize() override; virtual void OnReset() override; virtual void OnRender() override; - virtual bool OnInput(IInput::CEvent Event) override; + virtual bool OnInput(const IInput::CEvent &Event) override; virtual bool OnCursorMove(float x, float y, IInput::ECursorType CursorType) override; virtual void OnShutdown() override; diff --git a/src/game/client/components/menus_settings.cpp b/src/game/client/components/menus_settings.cpp index c20f05c9c..b42891e26 100644 --- a/src/game/client/components/menus_settings.cpp +++ b/src/game/client/components/menus_settings.cpp @@ -48,7 +48,7 @@ CMenusKeyBinder::CMenusKeyBinder() m_ModifierCombination = 0; } -bool CMenusKeyBinder::OnInput(IInput::CEvent Event) +bool CMenusKeyBinder::OnInput(const IInput::CEvent &Event) { if(m_TakeKey) { diff --git a/src/game/client/components/motd.cpp b/src/game/client/components/motd.cpp index f64052cdb..766e376c0 100644 --- a/src/game/client/components/motd.cpp +++ b/src/game/client/components/motd.cpp @@ -131,7 +131,7 @@ void CMotd::OnMessage(int MsgType, void *pRawMsg) } } -bool CMotd::OnInput(IInput::CEvent Event) +bool CMotd::OnInput(const IInput::CEvent &Event) { if(IsActive() && Event.m_Flags & IInput::FLAG_PRESS && Event.m_Key == KEY_ESCAPE) { diff --git a/src/game/client/components/motd.h b/src/game/client/components/motd.h index f6f43dd33..c251904bc 100644 --- a/src/game/client/components/motd.h +++ b/src/game/client/components/motd.h @@ -28,7 +28,7 @@ public: virtual void OnStateChange(int NewState, int OldState) override; virtual void OnWindowResize() override; virtual void OnMessage(int MsgType, void *pRawMsg) override; - virtual bool OnInput(IInput::CEvent Event) override; + virtual bool OnInput(const IInput::CEvent &Event) override; }; #endif diff --git a/src/game/client/gameclient.cpp b/src/game/client/gameclient.cpp index aa1a5a60a..2b0c8f6f8 100644 --- a/src/game/client/gameclient.cpp +++ b/src/game/client/gameclient.cpp @@ -394,13 +394,13 @@ void CGameClient::OnUpdate() // handle key presses for(int i = 0; i < Input()->NumEvents(); i++) { - IInput::CEvent e = Input()->GetEvent(i); - if(!Input()->IsEventValid(&e)) + const IInput::CEvent &Event = Input()->GetEvent(i); + if(!Input()->IsEventValid(Event)) continue; for(auto &pComponent : m_vpInput) { - if(pComponent->OnInput(e)) + if(pComponent->OnInput(Event)) break; } }