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.
This commit is contained in:
Robert Müller 2023-04-21 18:31:11 +02:00
parent 8b26fa0bc3
commit 2b7f054590
15 changed files with 36 additions and 36 deletions

View file

@ -77,7 +77,7 @@ private:
void AddEvent(char *pText, int Key, int Flags); void AddEvent(char *pText, int Key, int Flags);
void Clear() override; 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 // quick access to input
unsigned short m_aInputCount[g_MaxKeys]; // tw-KEY unsigned short m_aInputCount[g_MaxKeys]; // tw-KEY

View file

@ -55,8 +55,8 @@ public:
// events // events
int NumEvents() const { return m_NumEvents; } int NumEvents() const { return m_NumEvents; }
virtual bool IsEventValid(CEvent *pEvent) const = 0; virtual bool IsEventValid(const CEvent &Event) const = 0;
CEvent GetEvent(int Index) const const CEvent &GetEvent(int Index) const
{ {
dbg_assert(Index >= 0 && Index < m_NumEvents, "Index invalid"); dbg_assert(Index >= 0 && Index < m_NumEvents, "Index invalid");
return m_aInputEvents[Index]; return m_aInputEvents[Index];

View file

@ -209,9 +209,9 @@ public:
virtual bool OnCursorMove(float x, float y, IInput::ECursorType CursorType) { return false; } virtual bool OnCursorMove(float x, float y, IInput::ECursorType CursorType) { return false; }
/** /**
* Called on a input event. * 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 #endif

View file

@ -8,7 +8,7 @@
static const ColorRGBA gs_BindPrintColor{1.0f, 1.0f, 0.8f, 1.0f}; 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 // 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())) 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 // 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; return false;
int Mask = GetModifierMask(Input()); int Mask = GetModifierMask(Input());
int KeyModifierMask = GetModifierMaskOfKey(e.m_Key); int KeyModifierMask = GetModifierMaskOfKey(Event.m_Key);
Mask &= ~KeyModifierMask; Mask &= ~KeyModifierMask;
bool ret = false; bool ret = false;
if(m_aapKeyBindings[Mask][e.m_Key]) if(m_aapKeyBindings[Mask][Event.m_Key])
{ {
if(e.m_Flags & IInput::FLAG_PRESS) if(Event.m_Flags & IInput::FLAG_PRESS)
Console()->ExecuteLineStroked(1, m_aapKeyBindings[Mask][e.m_Key]); Console()->ExecuteLineStroked(1, m_aapKeyBindings[Mask][Event.m_Key]);
if(e.m_Flags & IInput::FLAG_RELEASE) if(Event.m_Flags & IInput::FLAG_RELEASE)
Console()->ExecuteLineStroked(0, m_aapKeyBindings[Mask][e.m_Key]); Console()->ExecuteLineStroked(0, m_aapKeyBindings[Mask][Event.m_Key]);
ret = true; 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 // 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))) 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][e.m_Key]); Console()->ExecuteLineStroked(1, m_aapKeyBindings[0][Event.m_Key]);
if(e.m_Flags & IInput::FLAG_RELEASE) if(Event.m_Flags & IInput::FLAG_RELEASE)
Console()->ExecuteLineStroked(0, m_aapKeyBindings[0][e.m_Key]); Console()->ExecuteLineStroked(0, m_aapKeyBindings[0][Event.m_Key]);
ret = true; ret = true;
} }

View file

@ -32,7 +32,7 @@ public:
public: public:
CBinds *m_pBinds; CBinds *m_pBinds;
virtual int Sizeof() const override { return sizeof(*this); } virtual int Sizeof() const override { return sizeof(*this); }
virtual bool OnInput(IInput::CEvent Event) override; virtual bool OnInput(const IInput::CEvent &Event) override;
}; };
enum enum
@ -60,7 +60,7 @@ public:
static const char *GetKeyBindModifiersName(int ModifierCombination); static const char *GetKeyBindModifiersName(int ModifierCombination);
virtual void OnConsoleInit() override; virtual void OnConsoleInit() override;
virtual bool OnInput(IInput::CEvent Event) override; virtual bool OnInput(const IInput::CEvent &Event) override;
// DDRace // DDRace

View file

@ -165,7 +165,7 @@ void CChat::OnInit()
Console()->Chain("cl_chat_old", ConchainChatOld, this); 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) if(m_Mode == MODE_NONE)
return false; return false;

View file

@ -166,7 +166,7 @@ public:
void Reset(); void Reset();
void OnRelease() override; void OnRelease() override;
void OnMessage(int MsgType, void *pRawMsg) 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 OnInit() override;
void RebuildChat(); void RebuildChat();

View file

@ -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; 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 // 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)) if(m_ConsoleState != CONSOLE_OPEN && (m_ConsoleState != CONSOLE_OPENING || m_StateChangeEnd == TimeNow() + m_StateChangeDuration))

View file

@ -71,7 +71,7 @@ class CGameConsole : public CComponent
void ExecuteLine(const char *pLine); 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); void PrintLine(const char *pLine, int Len, ColorRGBA PrintColor);
const char *GetString() const { return m_Input.GetString(); } const char *GetString() const { return m_Input.GetString(); }
@ -138,7 +138,7 @@ public:
virtual void OnReset() override; virtual void OnReset() override;
virtual void OnRender() override; virtual void OnRender() override;
virtual void OnMessage(int MsgType, void *pRawMsg) 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; } bool IsClosed() { return m_ConsoleState == CONSOLE_CLOSED; }
}; };

View file

@ -2297,7 +2297,7 @@ bool CMenus::OnCursorMove(float x, float y, IInput::ECursorType CursorType)
return true; return true;
} }
bool CMenus::OnInput(IInput::CEvent Event) bool CMenus::OnInput(const IInput::CEvent &Event)
{ {
// special handle esc and enter for popup purposes // special handle esc and enter for popup purposes
if(Event.m_Flags & IInput::FLAG_PRESS && Event.m_Key == KEY_ESCAPE) if(Event.m_Flags & IInput::FLAG_PRESS && Event.m_Key == KEY_ESCAPE)

View file

@ -55,7 +55,7 @@ public:
int m_ModifierCombination; int m_ModifierCombination;
CMenusKeyBinder(); CMenusKeyBinder();
virtual int Sizeof() const override { return sizeof(*this); } 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 class CMenus : public CComponent
@ -593,7 +593,7 @@ public:
virtual void OnWindowResize() override; virtual void OnWindowResize() override;
virtual void OnReset() override; virtual void OnReset() override;
virtual void OnRender() 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 bool OnCursorMove(float x, float y, IInput::ECursorType CursorType) override;
virtual void OnShutdown() override; virtual void OnShutdown() override;

View file

@ -48,7 +48,7 @@ CMenusKeyBinder::CMenusKeyBinder()
m_ModifierCombination = 0; m_ModifierCombination = 0;
} }
bool CMenusKeyBinder::OnInput(IInput::CEvent Event) bool CMenusKeyBinder::OnInput(const IInput::CEvent &Event)
{ {
if(m_TakeKey) if(m_TakeKey)
{ {

View file

@ -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) if(IsActive() && Event.m_Flags & IInput::FLAG_PRESS && Event.m_Key == KEY_ESCAPE)
{ {

View file

@ -28,7 +28,7 @@ public:
virtual void OnStateChange(int NewState, int OldState) override; virtual void OnStateChange(int NewState, int OldState) override;
virtual void OnWindowResize() override; virtual void OnWindowResize() override;
virtual void OnMessage(int MsgType, void *pRawMsg) 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 #endif

View file

@ -394,13 +394,13 @@ void CGameClient::OnUpdate()
// handle key presses // handle key presses
for(int i = 0; i < Input()->NumEvents(); i++) for(int i = 0; i < Input()->NumEvents(); i++)
{ {
IInput::CEvent e = Input()->GetEvent(i); const IInput::CEvent &Event = Input()->GetEvent(i);
if(!Input()->IsEventValid(&e)) if(!Input()->IsEventValid(Event))
continue; continue;
for(auto &pComponent : m_vpInput) for(auto &pComponent : m_vpInput)
{ {
if(pComponent->OnInput(e)) if(pComponent->OnInput(Event))
break; break;
} }
} }