Skip to content

Commit 2021def

Browse files
committed
refactor(input): Simplify timeGetTime calls
1 parent 797ac20 commit 2021def

File tree

2 files changed

+6
-18
lines changed
  • GeneralsMD/Code/GameEngine/Source/GameClient/Input
  • Generals/Code/GameEngine/Source/GameClient/Input

2 files changed

+6
-18
lines changed

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

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,6 @@ void Keyboard::updateKeys( void )
130130
}
131131
while( m_keys[ index++ ].key != KEY_NONE );
132132

133-
// Get current time once for all key updates
134-
UnsignedInt now = 0;
135-
136133
// update keyboard status array
137134
index = 0;
138135
while( m_keys[ index ].key != KEY_NONE )
@@ -147,9 +144,7 @@ void Keyboard::updateKeys( void )
147144
// Update key down time for new key presses
148145
if( BitIsSet( m_keys[ index ].state, KEY_STATE_DOWN ) )
149146
{
150-
if( !now )
151-
now = timeGetTime();
152-
m_keyStatus[ m_keys[ index ].key ].keyDownTimeMsec = now;
147+
m_keyStatus[ m_keys[ index ].key ].keyDownTimeMsec = timeGetTime();
153148
}
154149

155150
// prevent ALT-TAB from causing a TAB event
@@ -225,14 +220,13 @@ Bool Keyboard::checkKeyRepeat( void )
225220
// Scan Keyboard status array for first key down
226221
// long enough to repeat
227222
const UnsignedInt KEY_REPEAT_INTERVAL_MSEC = 67; // ~2 frames at 30 FPS
228-
UnsignedInt now = timeGetTime();
229223
for( key = 0; key < ARRAY_SIZE(m_keyStatus); key++ )
230224
{
231225

232226
if( BitIsSet( m_keyStatus[ key ].state, KEY_STATE_DOWN ) )
233227
{
234228

235-
if( now - m_keyStatus[ key ].keyDownTimeMsec > Keyboard::KEY_REPEAT_DELAY_MSEC )
229+
if( timeGetTime() - m_keyStatus[ key ].keyDownTimeMsec > Keyboard::KEY_REPEAT_DELAY_MSEC )
236230
{
237231
// Add key to this frame
238232
m_keys[ index ].key = (UnsignedByte)key;
@@ -243,7 +237,7 @@ Bool Keyboard::checkKeyRepeat( void )
243237
m_keys[ ++index ].key = KEY_NONE;
244238

245239
// Set repeated key so it will repeat again after the interval
246-
m_keyStatus[ key ].keyDownTimeMsec = now - (Keyboard::KEY_REPEAT_DELAY_MSEC + KEY_REPEAT_INTERVAL_MSEC);
240+
m_keyStatus[ key ].keyDownTimeMsec = timeGetTime() - (Keyboard::KEY_REPEAT_DELAY_MSEC + KEY_REPEAT_INTERVAL_MSEC);
247241

248242
retVal = TRUE;
249243
break; // exit for key

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

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,6 @@ void Keyboard::updateKeys( void )
130130
}
131131
while( m_keys[ index++ ].key != KEY_NONE );
132132

133-
// Get current time once for all key updates
134-
UnsignedInt now = 0;
135-
136133
// update keyboard status array
137134
index = 0;
138135
while( m_keys[ index ].key != KEY_NONE )
@@ -147,9 +144,7 @@ void Keyboard::updateKeys( void )
147144
// Update key down time for new key presses
148145
if( BitIsSet( m_keys[ index ].state, KEY_STATE_DOWN ) )
149146
{
150-
if( !now )
151-
now = timeGetTime();
152-
m_keyStatus[ m_keys[ index ].key ].keyDownTimeMsec = now;
147+
m_keyStatus[ m_keys[ index ].key ].keyDownTimeMsec = timeGetTime();
153148
}
154149

155150
// prevent ALT-TAB from causing a TAB event
@@ -225,14 +220,13 @@ Bool Keyboard::checkKeyRepeat( void )
225220
// Scan Keyboard status array for first key down
226221
// long enough to repeat
227222
const UnsignedInt KEY_REPEAT_INTERVAL_MSEC = 67; // ~2 frames at 30 FPS
228-
UnsignedInt now = timeGetTime();
229223
for( key = 0; key < ARRAY_SIZE(m_keyStatus); key++ )
230224
{
231225

232226
if( BitIsSet( m_keyStatus[ key ].state, KEY_STATE_DOWN ) )
233227
{
234228

235-
if( now - m_keyStatus[ key ].keyDownTimeMsec > Keyboard::KEY_REPEAT_DELAY_MSEC )
229+
if( timeGetTime() - m_keyStatus[ key ].keyDownTimeMsec > Keyboard::KEY_REPEAT_DELAY_MSEC )
236230
{
237231
// Add key to this frame
238232
m_keys[ index ].key = (UnsignedByte)key;
@@ -243,7 +237,7 @@ Bool Keyboard::checkKeyRepeat( void )
243237
m_keys[ ++index ].key = KEY_NONE;
244238

245239
// Set repeated key so it will repeat again after the interval
246-
m_keyStatus[ key ].keyDownTimeMsec = now - (Keyboard::KEY_REPEAT_DELAY_MSEC + KEY_REPEAT_INTERVAL_MSEC);
240+
m_keyStatus[ key ].keyDownTimeMsec = timeGetTime() - (Keyboard::KEY_REPEAT_DELAY_MSEC + KEY_REPEAT_INTERVAL_MSEC);
247241

248242
retVal = TRUE;
249243
break; // exit for key

0 commit comments

Comments
 (0)