From 8b26fa0bc337158d7e959cc1bd69fa093c696819 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Fri, 21 Apr 2023 18:29:31 +0200 Subject: [PATCH 1/8] Assert instead of returning empty event on invalid index --- src/engine/input.h | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/engine/input.h b/src/engine/input.h index 180bcb5d5..2b8e08f45 100644 --- a/src/engine/input.h +++ b/src/engine/input.h @@ -58,11 +58,7 @@ public: virtual bool IsEventValid(CEvent *pEvent) const = 0; CEvent GetEvent(int Index) const { - if(Index < 0 || Index >= m_NumEvents) - { - IInput::CEvent e = {0, 0}; - return e; - } + dbg_assert(Index >= 0 && Index < m_NumEvents, "Index invalid"); return m_aInputEvents[Index]; } CEvent *GetEventsRaw() { return m_aInputEvents; } 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 2/8] 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; } } From ecc49a699f74bd3367b94c8dd3f0682c8e2bc645 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Fri, 21 Apr 2023 18:34:28 +0200 Subject: [PATCH 3/8] Use `size_t` for number of input events --- src/engine/input.h | 10 +++++----- src/game/client/components/menus.cpp | 2 +- src/game/client/components/menus.h | 2 +- src/game/client/gameclient.cpp | 2 +- src/game/client/ui.cpp | 4 ++-- src/game/client/ui.h | 4 ++-- src/game/editor/editor.cpp | 2 +- 7 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/engine/input.h b/src/engine/input.h index f5b4ab703..911dcd26c 100644 --- a/src/engine/input.h +++ b/src/engine/input.h @@ -33,7 +33,7 @@ protected: }; // quick access to events - int m_NumEvents; + size_t m_NumEvents; IInput::CEvent m_aInputEvents[INPUT_BUFFER_SIZE]; int64_t m_LastUpdate; float m_UpdateTime; @@ -54,15 +54,15 @@ public: }; // events - int NumEvents() const { return m_NumEvents; } + size_t NumEvents() const { return m_NumEvents; } virtual bool IsEventValid(const CEvent &Event) const = 0; - const CEvent &GetEvent(int Index) const + const CEvent &GetEvent(size_t Index) const { - dbg_assert(Index >= 0 && Index < m_NumEvents, "Index invalid"); + dbg_assert(Index < m_NumEvents, "Index invalid"); return m_aInputEvents[Index]; } CEvent *GetEventsRaw() { return m_aInputEvents; } - int *GetEventCountRaw() { return &m_NumEvents; } + size_t *GetEventCountRaw() { return &m_NumEvents; } /** * @return Rolling average of the time in seconds between diff --git a/src/game/client/components/menus.cpp b/src/game/client/components/menus.cpp index 9be3b19a2..c848d5929 100644 --- a/src/game/client/components/menus.cpp +++ b/src/game/client/components/menus.cpp @@ -58,7 +58,7 @@ float CMenus::ms_ButtonHeight = 25.0f; float CMenus::ms_ListheaderHeight = 17.0f; IInput::CEvent CMenus::m_aInputEvents[MAX_INPUTEVENTS]; -int CMenus::m_NumInputEvents; +size_t CMenus::m_NumInputEvents; CMenus::CMenus() { diff --git a/src/game/client/components/menus.h b/src/game/client/components/menus.h index 742758611..3a5a81433 100644 --- a/src/game/client/components/menus.h +++ b/src/game/client/components/menus.h @@ -340,7 +340,7 @@ protected: MAX_INPUTEVENTS = 32 }; static IInput::CEvent m_aInputEvents[MAX_INPUTEVENTS]; - static int m_NumInputEvents; + static size_t m_NumInputEvents; // some settings static float ms_ButtonHeight; diff --git a/src/game/client/gameclient.cpp b/src/game/client/gameclient.cpp index 2b0c8f6f8..ed75dedc1 100644 --- a/src/game/client/gameclient.cpp +++ b/src/game/client/gameclient.cpp @@ -392,7 +392,7 @@ void CGameClient::OnUpdate() } // handle key presses - for(int i = 0; i < Input()->NumEvents(); i++) + for(size_t i = 0; i < Input()->NumEvents(); i++) { const IInput::CEvent &Event = Input()->GetEvent(i); if(!Input()->IsEventValid(Event)) diff --git a/src/game/client/ui.cpp b/src/game/client/ui.cpp index 711cad4d8..bf1c129b1 100644 --- a/src/game/client/ui.cpp +++ b/src/game/client/ui.cpp @@ -101,7 +101,7 @@ void CUI::Init(IKernel *pKernel) CUIElementBase::Init(this); } -void CUI::InitInputs(IInput::CEvent *pInputEventsArray, int *pInputEventCount) +void CUI::InitInputs(IInput::CEvent *pInputEventsArray, size_t *pInputEventCount) { m_pInputEventsArray = pInputEventsArray; m_pInputEventCount = pInputEventCount; @@ -812,7 +812,7 @@ bool CUI::DoEditBox(const void *pID, const CUIRect *pRect, char *pStr, unsigned ReturnValue = true; } - for(int i = 0; i < *m_pInputEventCount; i++) + for(size_t i = 0; i < *m_pInputEventCount; i++) { int LastCursor = m_CurCursor; int Len, NumChars; diff --git a/src/game/client/ui.h b/src/game/client/ui.h index 5abb62f79..eabd2e601 100644 --- a/src/game/client/ui.h +++ b/src/game/client/ui.h @@ -243,7 +243,7 @@ private: bool m_MouseSlow = false; IInput::CEvent *m_pInputEventsArray; - int *m_pInputEventCount; + size_t *m_pInputEventCount; unsigned m_HotkeysPressed = 0; bool m_MouseIsPress = false; @@ -297,7 +297,7 @@ public: static float ms_FontmodHeight; void Init(IKernel *pKernel); - void InitInputs(IInput::CEvent *pInputEventsArray, int *pInputEventCount); + void InitInputs(IInput::CEvent *pInputEventsArray, size_t *pInputEventCount); IClient *Client() const { return m_pClient; } IGraphics *Graphics() const { return m_pGraphics; } IInput *Input() const { return m_pInput; } diff --git a/src/game/editor/editor.cpp b/src/game/editor/editor.cpp index 26bcf9c03..dfc2f553e 100644 --- a/src/game/editor/editor.cpp +++ b/src/game/editor/editor.cpp @@ -6855,7 +6855,7 @@ void CEditor::OnUpdate() Reset(); } - for(int i = 0; i < Input()->NumEvents(); i++) + for(size_t i = 0; i < Input()->NumEvents(); i++) UI()->OnInput(Input()->GetEvent(i)); // handle cursor movement From adc813493a86417267a9173acc82e174644e3017 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Fri, 21 Apr 2023 18:35:15 +0200 Subject: [PATCH 4/8] Remove unused `FLAG_REPEAT` --- src/engine/input.h | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/engine/input.h b/src/engine/input.h index 911dcd26c..19ab452f3 100644 --- a/src/engine/input.h +++ b/src/engine/input.h @@ -41,10 +41,9 @@ protected: public: enum { - FLAG_PRESS = 1, - FLAG_RELEASE = 2, - FLAG_REPEAT = 4, - FLAG_TEXT = 8, + FLAG_PRESS = 1 << 0, + FLAG_RELEASE = 1 << 1, + FLAG_TEXT = 1 << 2, }; enum ECursorType { From c959665226644c64ccb33655d2189ce6407c3660 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Fri, 21 Apr 2023 18:36:49 +0200 Subject: [PATCH 5/8] Use `nullptr` instead of `0`, `0x0` and `NULL` --- src/engine/client/input.cpp | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/engine/client/input.cpp b/src/engine/client/input.cpp index a14276029..be20cf022 100644 --- a/src/engine/client/input.cpp +++ b/src/engine/client/input.cpp @@ -32,7 +32,7 @@ void CInput::AddEvent(char *pText, int Key, int Flags) { m_aInputEvents[m_NumEvents].m_Key = Key; m_aInputEvents[m_NumEvents].m_Flags = Flags; - if(!pText) + if(pText == nullptr) m_aInputEvents[m_NumEvents].m_aText[0] = 0; else str_copy(m_aInputEvents[m_NumEvents].m_aText, pText); @@ -57,7 +57,7 @@ CInput::CInput() m_NumEvents = 0; m_MouseFocus = true; - m_pClipboardText = NULL; + m_pClipboardText = nullptr; m_NumTextInputInstances = 0; m_EditingTextLen = -1; @@ -290,7 +290,7 @@ void CInput::MouseModeRelative() #endif Graphics()->SetWindowGrab(true); // Clear pending relative mouse motion - SDL_GetRelativeMouseState(0x0, 0x0); + SDL_GetRelativeMouseState(nullptr, nullptr); } void CInput::NativeMousePos(int *pX, int *pY) const @@ -300,7 +300,7 @@ void CInput::NativeMousePos(int *pX, int *pY) const bool CInput::NativeMousePressed(int Index) { - int i = SDL_GetMouseState(NULL, NULL); + int i = SDL_GetMouseState(nullptr, nullptr); return (i & SDL_BUTTON(Index)) != 0; } @@ -342,7 +342,7 @@ bool CInput::KeyState(int Key) const void CInput::UpdateMouseState() { - const int MouseState = SDL_GetMouseState(NULL, NULL); + const int MouseState = SDL_GetMouseState(nullptr, nullptr); if(MouseState & SDL_BUTTON(SDL_BUTTON_LEFT)) m_aInputState[KEY_MOUSE_1] = 1; if(MouseState & SDL_BUTTON(SDL_BUTTON_RIGHT)) @@ -408,24 +408,24 @@ void CInput::HandleJoystickAxisMotionEvent(const SDL_JoyAxisEvent &Event) { m_aInputState[LeftKey] = true; m_aInputCount[LeftKey] = m_InputCounter; - AddEvent(0, LeftKey, IInput::FLAG_PRESS); + AddEvent(nullptr, LeftKey, IInput::FLAG_PRESS); } else if(Event.value > SDL_JOYSTICK_AXIS_MIN * DeadZone && m_aInputState[LeftKey]) { m_aInputState[LeftKey] = false; - AddEvent(0, LeftKey, IInput::FLAG_RELEASE); + AddEvent(nullptr, LeftKey, IInput::FLAG_RELEASE); } if(Event.value >= SDL_JOYSTICK_AXIS_MAX * DeadZone && !m_aInputState[RightKey]) { m_aInputState[RightKey] = true; m_aInputCount[RightKey] = m_InputCounter; - AddEvent(0, RightKey, IInput::FLAG_PRESS); + AddEvent(nullptr, RightKey, IInput::FLAG_PRESS); } else if(Event.value < SDL_JOYSTICK_AXIS_MAX * DeadZone && m_aInputState[RightKey]) { m_aInputState[RightKey] = false; - AddEvent(0, RightKey, IInput::FLAG_RELEASE); + AddEvent(nullptr, RightKey, IInput::FLAG_RELEASE); } } @@ -445,12 +445,12 @@ void CInput::HandleJoystickButtonEvent(const SDL_JoyButtonEvent &Event) { m_aInputState[Key] = true; m_aInputCount[Key] = m_InputCounter; - AddEvent(0, Key, IInput::FLAG_PRESS); + AddEvent(nullptr, Key, IInput::FLAG_PRESS); } else if(Event.type == SDL_JOYBUTTONUP) { m_aInputState[Key] = false; - AddEvent(0, Key, IInput::FLAG_RELEASE); + AddEvent(nullptr, Key, IInput::FLAG_RELEASE); } } @@ -472,7 +472,7 @@ void CInput::HandleJoystickHatMotionEvent(const SDL_JoyHatEvent &Event) if(Key != HatKeys[0] && Key != HatKeys[1] && m_aInputState[Key]) { m_aInputState[Key] = false; - AddEvent(0, Key, IInput::FLAG_RELEASE); + AddEvent(nullptr, Key, IInput::FLAG_RELEASE); } } @@ -482,7 +482,7 @@ void CInput::HandleJoystickHatMotionEvent(const SDL_JoyHatEvent &Event) { m_aInputState[CurrentKey] = true; m_aInputCount[CurrentKey] = m_InputCounter; - AddEvent(0, CurrentKey, IInput::FLAG_PRESS); + AddEvent(nullptr, CurrentKey, IInput::FLAG_PRESS); } } } @@ -731,7 +731,7 @@ int CInput::Update() // Enable this in case SDL 2.0.16 has major bugs or 2.0.18 still doesn't fix tabbing out with relative mouse // MouseModeRelative(); // Clear pending relative mouse motion - SDL_GetRelativeMouseState(0x0, 0x0); + SDL_GetRelativeMouseState(nullptr, nullptr); } m_MouseFocus = true; IgnoreKeys = true; @@ -780,7 +780,7 @@ int CInput::Update() m_aInputState[Scancode] = 1; m_aInputCount[Scancode] = m_InputCounter; } - AddEvent(0, Scancode, Action); + AddEvent(nullptr, Scancode, Action); } } From 09462cff679ab75558a48c43a666dc8d6004edc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Fri, 21 Apr 2023 18:38:46 +0200 Subject: [PATCH 6/8] Use `KEY_FIRST` and `KEY_UNKNOWN` instead of `0` --- src/engine/client/input.cpp | 4 ++-- src/game/client/components/binds.cpp | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/engine/client/input.cpp b/src/engine/client/input.cpp index be20cf022..72931a86b 100644 --- a/src/engine/client/input.cpp +++ b/src/engine/client/input.cpp @@ -335,7 +335,7 @@ void CInput::Clear() bool CInput::KeyState(int Key) const { - if(Key < 0 || Key >= KEY_LAST) + if(Key < KEY_FIRST || Key >= KEY_LAST) return false; return m_aInputState[Key]; } @@ -618,7 +618,7 @@ int CInput::Update() } case SDL_TEXTINPUT: m_EditingTextLen = -1; - AddEvent(Event.text.text, 0, IInput::FLAG_TEXT); + AddEvent(Event.text.text, KEY_UNKNOWN, IInput::FLAG_TEXT); break; // handle keys case SDL_KEYDOWN: diff --git a/src/game/client/components/binds.cpp b/src/game/client/components/binds.cpp index 97ec13dd2..3080fa710 100644 --- a/src/game/client/components/binds.cpp +++ b/src/game/client/components/binds.cpp @@ -126,7 +126,7 @@ int CBinds::GetModifierMaskOfKey(int Key) bool CBinds::OnInput(const IInput::CEvent &Event) { // don't handle invalid events - if(Event.m_Key <= 0 || Event.m_Key >= KEY_LAST) + if(Event.m_Key <= KEY_FIRST || Event.m_Key >= KEY_LAST) return false; int Mask = GetModifierMask(Input()); From 6f033e41ba774a924c7e801166115816a60e5c5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Fri, 21 Apr 2023 18:40:03 +0200 Subject: [PATCH 7/8] Use `'\0'` instead of `0` for `char`s --- src/engine/client/input.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/engine/client/input.cpp b/src/engine/client/input.cpp index 72931a86b..0fb04366d 100644 --- a/src/engine/client/input.cpp +++ b/src/engine/client/input.cpp @@ -33,7 +33,7 @@ void CInput::AddEvent(char *pText, int Key, int Flags) m_aInputEvents[m_NumEvents].m_Key = Key; m_aInputEvents[m_NumEvents].m_Flags = Flags; if(pText == nullptr) - m_aInputEvents[m_NumEvents].m_aText[0] = 0; + m_aInputEvents[m_NumEvents].m_aText[0] = '\0'; else str_copy(m_aInputEvents[m_NumEvents].m_aText, pText); m_aInputEvents[m_NumEvents].m_InputCount = m_InputCounter; @@ -61,9 +61,9 @@ CInput::CInput() m_NumTextInputInstances = 0; m_EditingTextLen = -1; - m_aEditingText[0] = 0; + m_aEditingText[0] = '\0'; - m_aDropFile[0] = 0; + m_aDropFile[0] = '\0'; } void CInput::Init() @@ -612,7 +612,7 @@ int CInput::Update() } else { - m_aEditingText[0] = 0; + m_aEditingText[0] = '\0'; } break; } From c9baa5f0d6d129ace30da151d32ca9adf4d613a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Fri, 21 Apr 2023 18:40:19 +0200 Subject: [PATCH 8/8] Remove dead code --- src/engine/client/input.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/engine/client/input.cpp b/src/engine/client/input.cpp index 0fb04366d..0edecb466 100644 --- a/src/engine/client/input.cpp +++ b/src/engine/client/input.cpp @@ -11,8 +11,6 @@ #include "input.h" -//print >>f, "int inp_key_code(const char *key_name) { int i; if (!strcmp(key_name, \"-?-\")) return -1; else for (i = 0; i < 512; i++) if (!strcmp(key_strings[i], key_name)) return i; return -1; }" - // this header is protected so you don't include it from anywhere #define KEYS_INCLUDE #include "keynames.h"