From 66a30e72b8090c597ca94cdbfbc4b93d21238228 Mon Sep 17 00:00:00 2001 From: Chris Danford Date: Mon, 21 Mar 2005 21:40:07 +0000 Subject: [PATCH] optimize: Add GetTimeSinceStartFast() that caches the time for archs where GetMicrosecondsSinceStart() is slow --- stepmania/src/ArrowEffects.cpp | 10 +++++----- stepmania/src/BGAnimationLayer.cpp | 2 +- stepmania/src/Background.cpp | 2 +- stepmania/src/BitmapText.cpp | 2 +- stepmania/src/Inventory.cpp | 4 ++-- stepmania/src/LifeMeterBar.cpp | 2 +- stepmania/src/LightsManager.cpp | 6 +++--- stepmania/src/NoteField.cpp | 8 ++++---- stepmania/src/RageTimer.cpp | 16 ++++++++++------ stepmania/src/RageTimer.h | 3 ++- stepmania/src/ScreenEz2SelectMusic.cpp | 14 +++++++------- stepmania/src/ScreenSystemLayer.cpp | 4 ++-- stepmania/src/ScreenTestLights.cpp | 2 +- stepmania/src/WheelNotifyIcon.cpp | 2 +- 14 files changed, 41 insertions(+), 36 deletions(-) diff --git a/stepmania/src/ArrowEffects.cpp b/stepmania/src/ArrowEffects.cpp index a81e6df7fc..fddefb680c 100644 --- a/stepmania/src/ArrowEffects.cpp +++ b/stepmania/src/ArrowEffects.cpp @@ -153,7 +153,7 @@ float ArrowEffects::GetYPos( const PlayerState* pPlayerState, int iCol, float fY const float* fEffects = pPlayerState->m_CurrentPlayerOptions.m_fEffects; if( fEffects[PlayerOptions::EFFECT_TIPSY] > 0 ) - f += fEffects[PlayerOptions::EFFECT_TIPSY] * ( cosf( RageTimer::GetTimeSinceStart()*1.2f + iCol*1.8f) * ARROW_SIZE*0.4f ); + f += fEffects[PlayerOptions::EFFECT_TIPSY] * ( cosf( RageTimer::GetTimeSinceStartFast()*1.2f + iCol*1.8f) * ARROW_SIZE*0.4f ); return f; } @@ -163,7 +163,7 @@ float ArrowEffects::GetYOffsetFromYPos( const PlayerState* pPlayerState, int iCo const float* fEffects = pPlayerState->m_CurrentPlayerOptions.m_fEffects; if( fEffects[PlayerOptions::EFFECT_TIPSY] > 0 ) - f -= fEffects[PlayerOptions::EFFECT_TIPSY] * ( cosf( RageTimer::GetTimeSinceStart()*1.2f + iCol*2.f) * ARROW_SIZE*0.4f ); + f -= fEffects[PlayerOptions::EFFECT_TIPSY] * ( cosf( RageTimer::GetTimeSinceStartFast()*1.2f + iCol*2.f) * ARROW_SIZE*0.4f ); float fShift, fScale; ArrowGetReverseShiftAndScale( pPlayerState, iCol, fYReverseOffsetPixels, fShift, fScale ); @@ -218,7 +218,7 @@ float ArrowEffects::GetXPos( const PlayerState* pPlayerState, int iColNum, float } if( fEffects[PlayerOptions::EFFECT_DRUNK] > 0 ) - fPixelOffsetFromCenter += fEffects[PlayerOptions::EFFECT_DRUNK] * ( cosf( RageTimer::GetTimeSinceStart() + iColNum*0.2f + fYOffset*10/SCREEN_HEIGHT) * ARROW_SIZE*0.5f ); + fPixelOffsetFromCenter += fEffects[PlayerOptions::EFFECT_DRUNK] * ( cosf( RageTimer::GetTimeSinceStartFast() + iColNum*0.2f + fYOffset*10/SCREEN_HEIGHT) * ARROW_SIZE*0.5f ); if( fEffects[PlayerOptions::EFFECT_FLIP] > 0 ) { // TODO: Don't index by PlayerNumber. @@ -377,8 +377,8 @@ float ArrowGetPercentVisible( const PlayerState* pPlayerState, int iCol, float f fVisibleAdjust -= fAppearances[PlayerOptions::APPEARANCE_STEALTH]; if( fAppearances[PlayerOptions::APPEARANCE_BLINK] > 0 ) { - float f = sinf(RageTimer::GetTimeSinceStart()*10); - f = Quantize( f, 0.3333f ); + float f = sinf(RageTimer::GetTimeSinceStartFast()*10); + f = Quantize( f, 0.3333f ); fVisibleAdjust += SCALE( f, 0, 1, -1, 0 ); } if( fAppearances[PlayerOptions::APPEARANCE_RANDOMVANISH] > 0) diff --git a/stepmania/src/BGAnimationLayer.cpp b/stepmania/src/BGAnimationLayer.cpp index 2b86d851d9..1f6eda44c4 100644 --- a/stepmania/src/BGAnimationLayer.cpp +++ b/stepmania/src/BGAnimationLayer.cpp @@ -627,7 +627,7 @@ void BGAnimationLayer::Update( float fDeltaTime ) break; case TYPE_TILES: { - float fSecs = RageTimer::GetTimeSinceStart(); + float fSecs = RageTimer::GetTimeSinceStartFast(); float fTotalWidth = m_iNumTilesWide * m_fTilesSpacingX; float fTotalHeight = m_iNumTilesHigh * m_fTilesSpacingY; diff --git a/stepmania/src/Background.cpp b/stepmania/src/Background.cpp index 1c4ed92e2a..9c977a418b 100644 --- a/stepmania/src/Background.cpp +++ b/stepmania/src/Background.cpp @@ -681,7 +681,7 @@ bool Background::IsDangerAllVisible() return false; if( BLINK_DANGER_ALL ) - return (RageTimer::GetTimeSinceStart() - (int)RageTimer::GetTimeSinceStart()) < 0.5f; + return (RageTimer::GetTimeSinceStartFast() - (int)RageTimer::GetTimeSinceStartFast()) < 0.5f; else return true; } diff --git a/stepmania/src/BitmapText.cpp b/stepmania/src/BitmapText.cpp index 68f27c70b1..64a21e8b41 100644 --- a/stepmania/src/BitmapText.cpp +++ b/stepmania/src/BitmapText.cpp @@ -517,7 +517,7 @@ void BitmapText::DrawPrimitives() ////////////////////// if( m_bRainbow ) { - int color_index = int(RageTimer::GetTimeSinceStart() / 0.200) % NUM_RAINBOW_COLORS; + int color_index = int(RageTimer::GetTimeSinceStartFast() / 0.200) % NUM_RAINBOW_COLORS; for( unsigned i=0; im_fSongBeat < GAMESTATE->m_pCurSong->m_fLastBeat ) { // every 1 seconds, try to use an item - int iLastSecond = (int)(RageTimer::GetTimeSinceStart() - fDelta); - int iThisSecond = (int)RageTimer::GetTimeSinceStart(); + int iLastSecond = (int)(RageTimer::GetTimeSinceStartFast() - fDelta); + int iThisSecond = (int)RageTimer::GetTimeSinceStartFast(); if( iLastSecond != iThisSecond ) { for( int s=0; sm_fPassingAlpha = m_fPassingAlpha; m_pStream->m_fHotAlpha = m_fHotAlpha; - float fPercentRed = (m_fTrailingLifePercentageGetCurrentGame()->GetNumGameplayButtons(); diff --git a/stepmania/src/NoteField.cpp b/stepmania/src/NoteField.cpp index 3871e98d56..bac3b843b8 100644 --- a/stepmania/src/NoteField.cpp +++ b/stepmania/src/NoteField.cpp @@ -284,7 +284,7 @@ void NoteField::DrawBPMText( const float fBeat, const float fBPM ) m_textMeasureNumber.SetHorizAlign( Actor::align_right ); m_textMeasureNumber.SetDiffuse( RageColor(1,0,0,1) ); - m_textMeasureNumber.SetGlow( RageColor(1,1,1,cosf(RageTimer::GetTimeSinceStart()*2)/2+0.5f) ); + m_textMeasureNumber.SetGlow( RageColor(1,1,1,cosf(RageTimer::GetTimeSinceStartFast()*2)/2+0.5f) ); m_textMeasureNumber.SetText( ssprintf("%.2f", fBPM) ); m_textMeasureNumber.SetXY( -GetWidth()/2.f - 60, fYPos ); m_textMeasureNumber.Draw(); @@ -297,7 +297,7 @@ void NoteField::DrawFreezeText( const float fBeat, const float fSecs ) m_textMeasureNumber.SetHorizAlign( Actor::align_right ); m_textMeasureNumber.SetDiffuse( RageColor(0.8f,0.8f,0,1) ); - m_textMeasureNumber.SetGlow( RageColor(1,1,1,cosf(RageTimer::GetTimeSinceStart()*2)/2+0.5f) ); + m_textMeasureNumber.SetGlow( RageColor(1,1,1,cosf(RageTimer::GetTimeSinceStartFast()*2)/2+0.5f) ); m_textMeasureNumber.SetText( ssprintf("%.2f", fSecs) ); m_textMeasureNumber.SetXY( -GetWidth()/2.f - 10, fYPos ); m_textMeasureNumber.Draw(); @@ -310,7 +310,7 @@ void NoteField::DrawBGChangeText( const float fBeat, const CString sNewBGName ) m_textMeasureNumber.SetHorizAlign( Actor::align_left ); m_textMeasureNumber.SetDiffuse( RageColor(0,1,0,1) ); - m_textMeasureNumber.SetGlow( RageColor(1,1,1,cosf(RageTimer::GetTimeSinceStart()*2)/2+0.5f) ); + m_textMeasureNumber.SetGlow( RageColor(1,1,1,cosf(RageTimer::GetTimeSinceStartFast()*2)/2+0.5f) ); m_textMeasureNumber.SetText( sNewBGName ); m_textMeasureNumber.SetXY( +GetWidth()/2.f, fYPos ); m_textMeasureNumber.Draw(); @@ -525,7 +525,7 @@ void NoteField::DrawPrimitives() // draw in big batches. // - float fSelectedRangeGlow = SCALE( cosf(RageTimer::GetTimeSinceStart()*2), -1, 1, 0.1f, 0.3f ); + float fSelectedRangeGlow = SCALE( cosf(RageTimer::GetTimeSinceStartFast()*2), -1, 1, 0.1f, 0.3f ); for( int c=0; cGetNumTracks(); c++ ) // for each arrow column { diff --git a/stepmania/src/RageTimer.cpp b/stepmania/src/RageTimer.cpp index 4591fa19a8..876b39b275 100644 --- a/stepmania/src/RageTimer.cpp +++ b/stepmania/src/RageTimer.cpp @@ -30,24 +30,28 @@ const RageTimer RageZeroTimer(0,0); -static void GetTime( unsigned &secs, unsigned &us ) +static void GetTime( unsigned &secs, unsigned &us, bool bAccurate ) { - uint64_t usecs = ArchHooks::GetMicrosecondsSinceStart( true ); + // if !bAccurate, then don't call ArchHooks to find the current time. Just return the + // last calculated time. GetMicrosecondsSinceStart is slow on some archs. + static uint64_t usecs = 0; + if( bAccurate ) + usecs = ArchHooks::GetMicrosecondsSinceStart( true ); secs = unsigned(usecs / 1000000); us = unsigned(usecs % 1000000); } -float RageTimer::GetTimeSinceStart() +float RageTimer::GetTimeSinceStart( bool bAccurate ) { unsigned secs, us; - GetTime( secs, us ); - return secs + us / 1000000.0f; + GetTime( secs, us, bAccurate ); + return secs + (us / 1000000.0f); } void RageTimer::Touch() { - GetTime( this->m_secs, this->m_us ); + GetTime( this->m_secs, this->m_us, true ); } float RageTimer::Ago() const diff --git a/stepmania/src/RageTimer.h b/stepmania/src/RageTimer.h index 81647c18da..81b7ed25ee 100644 --- a/stepmania/src/RageTimer.h +++ b/stepmania/src/RageTimer.h @@ -21,7 +21,8 @@ public: float PeekDeltaTime() const { return Ago(); } /* deprecated: */ - static float GetTimeSinceStart(); // seconds since the program was started + static float GetTimeSinceStart( bool bAccurate = true ); // seconds since the program was started + static float GetTimeSinceStartFast() { return GetTimeSinceStart(false); } /* Get a timer representing half of the time ago as this one. */ RageTimer Half() const; diff --git a/stepmania/src/ScreenEz2SelectMusic.cpp b/stepmania/src/ScreenEz2SelectMusic.cpp index 48ef9696a2..ea7fd20fd9 100644 --- a/stepmania/src/ScreenEz2SelectMusic.cpp +++ b/stepmania/src/ScreenEz2SelectMusic.cpp @@ -285,15 +285,15 @@ void ScreenEz2SelectMusic::Input( const DeviceInput& DeviceI, const InputEventTy if(ScrollStartTime == 0) { i_SkipAheadOffset = 0; - ScrollStartTime = RageTimer::GetTimeSinceStart(); + ScrollStartTime = RageTimer::GetTimeSinceStartFast(); } else { - if(RageTimer::GetTimeSinceStart() - ScrollStartTime > 5) // been cycling for more than 5 seconds + if(RageTimer::GetTimeSinceStartFast() - ScrollStartTime > 5) // been cycling for more than 5 seconds { i_SkipAheadOffset = 2; // start skipping ahead in twos } - else if (RageTimer::GetTimeSinceStart() - ScrollStartTime > 10) // been cycling for more than 10 seconds + else if (RageTimer::GetTimeSinceStartFast() - ScrollStartTime > 10) // been cycling for more than 10 seconds { i_SkipAheadOffset = 3; // start skipping ahead 3 at a time } @@ -307,7 +307,7 @@ void ScreenEz2SelectMusic::Input( const DeviceInput& DeviceI, const InputEventTy // m_soundMusicChange.Play(); m_soundButtonPress.Play(); } - LastInputTime = RageTimer::GetTimeSinceStart(); + LastInputTime = RageTimer::GetTimeSinceStartFast(); Screen::Input( DeviceI, type, GameI, MenuI, StyleI ); } @@ -468,7 +468,7 @@ void ScreenEz2SelectMusic::MenuStart( PlayerNumber pn ) } m_bMadeChoice = true; - m_fRemainingWaitTime = RageTimer::GetTimeSinceStart(); + m_fRemainingWaitTime = RageTimer::GetTimeSinceStartFast(); TweenOffScreen(); @@ -488,7 +488,7 @@ void ScreenEz2SelectMusic::Update( float fDeltaTime ) { m_DifficultyRating.Update(fDeltaTime); - if(i_SkipAheadOffset > 0 && RageTimer::GetTimeSinceStart() - LastInputTime < 0.5) + if(i_SkipAheadOffset > 0 && RageTimer::GetTimeSinceStartFast() - LastInputTime < 0.5) { m_MusicBannerWheel.SetScanMode(true); } @@ -504,7 +504,7 @@ void ScreenEz2SelectMusic::Update( float fDeltaTime ) this->PostScreenMessage( SM_NoSongs, 5.5f ); // timeout incase the user decides to do nothing :D } - if(m_bMadeChoice && RageTimer::GetTimeSinceStart() > m_fRemainingWaitTime + 2 && !m_bTransitioning) + if(m_bMadeChoice && RageTimer::GetTimeSinceStartFast() > m_fRemainingWaitTime + 2 && !m_bTransitioning) { m_bTransitioning = true; StartTransitioning( SM_GoToNextScreen ); diff --git a/stepmania/src/ScreenSystemLayer.cpp b/stepmania/src/ScreenSystemLayer.cpp index f75668422f..e58005ab13 100644 --- a/stepmania/src/ScreenSystemLayer.cpp +++ b/stepmania/src/ScreenSystemLayer.cpp @@ -273,7 +273,7 @@ void ScreenSystemLayer::UpdateSkips() if( skip ) { - CString time(SecondsToMMSSMsMs(RageTimer::GetTimeSinceStart())); + CString time(SecondsToMMSSMsMs(RageTimer::GetTimeSinceStartFast())); static const RageColor colors[] = { @@ -307,7 +307,7 @@ void ScreenSystemLayer::Update( float fDeltaTime ) UpdateSkips(); if( PREFSMAN->m_bTimestamping ) - m_textTime.SetText( SecondsToMMSSMsMs(RageTimer::GetTimeSinceStart()) ); + m_textTime.SetText( SecondsToMMSSMsMs(RageTimer::GetTimeSinceStartFast()) ); } /* diff --git a/stepmania/src/ScreenTestLights.cpp b/stepmania/src/ScreenTestLights.cpp index 9f762fcd5e..f9d52d927b 100644 --- a/stepmania/src/ScreenTestLights.cpp +++ b/stepmania/src/ScreenTestLights.cpp @@ -45,7 +45,7 @@ void ScreenTestLights::Update( float fDeltaTime ) { Screen::Update( fDeltaTime ); - int iSec = (int)RageTimer::GetTimeSinceStart(); + int iSec = (int)RageTimer::GetTimeSinceStartFast(); CabinetLight cl = (CabinetLight)(iSec%NUM_CABINET_LIGHTS); int iNumGameButtonsToShow = GAMESTATE->GetCurrentGame()->GetNumGameplayButtons(); diff --git a/stepmania/src/WheelNotifyIcon.cpp b/stepmania/src/WheelNotifyIcon.cpp index a557ca8740..305ffc3d5f 100644 --- a/stepmania/src/WheelNotifyIcon.cpp +++ b/stepmania/src/WheelNotifyIcon.cpp @@ -69,7 +69,7 @@ void WheelNotifyIcon::Update( float fDeltaTime ) { if( m_vIconsToShow.size() > 0 ) { - const float fSecondFraction = fmodf( RageTimer::GetTimeSinceStart(), 1 ); + const float fSecondFraction = fmodf( RageTimer::GetTimeSinceStartFast(), 1 ); const int index = (int)(fSecondFraction*m_vIconsToShow.size()); Sprite::SetState( m_vIconsToShow[index] ); }