Skip to content

Commit 7a27d25

Browse files
committed
fix(input): Use unsigned subtraction for keyboard repeat timing
1 parent 2d14d5b commit 7a27d25

File tree

2 files changed

+10
-4
lines changed
  • GeneralsMD/Code/GameEngine/Source/GameClient/Input
  • Generals/Code/GameEngine/Source/GameClient/Input

2 files changed

+10
-4
lines changed

Generals/Code/GameEngine/Source/GameClient/Input/Keyboard.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,13 @@ Bool Keyboard::checkKeyRepeat( void )
225225
if( BitIsSet( m_keyStatus[ key ].state, KEY_STATE_DOWN ) )
226226
{
227227

228-
UnsignedInt now = timeGetTime();
228+
const UnsignedInt now = timeGetTime();
229+
const UnsignedInt keyDownTime = m_keyStatus[ key ].keyDownTimeMsec;
229230

230-
if( now - m_keyStatus[ key ].keyDownTimeMsec > Keyboard::KEY_REPEAT_DELAY_MSEC )
231+
// Unsigned subtraction handles wraparound correctly
232+
const UnsignedInt elapsedMsec = now - keyDownTime;
233+
234+
if( elapsedMsec > Keyboard::KEY_REPEAT_DELAY_MSEC )
231235
{
232236
// Add key to this frame
233237
m_keys[ index ].key = (UnsignedByte)key;

GeneralsMD/Code/GameEngine/Source/GameClient/Input/Keyboard.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,11 @@ Bool Keyboard::checkKeyRepeat( void )
225225
if( BitIsSet( m_keyStatus[ key ].state, KEY_STATE_DOWN ) )
226226
{
227227

228-
UnsignedInt now = timeGetTime();
228+
const UnsignedInt now = timeGetTime();
229+
const UnsignedInt keyDownTime = m_keyStatus[ key ].keyDownTimeMsec;
230+
const UnsignedInt elapsedMsec = now - keyDownTime;
229231

230-
if( now - m_keyStatus[ key ].keyDownTimeMsec > Keyboard::KEY_REPEAT_DELAY_MSEC )
232+
if( elapsedMsec > Keyboard::KEY_REPEAT_DELAY_MSEC )
231233
{
232234
// Add key to this frame
233235
m_keys[ index ].key = (UnsignedByte)key;

0 commit comments

Comments
 (0)