2005-07-13 00:45:58 +00:00
|
|
|
#include "global.h"
|
|
|
|
|
#include "GameLoop.h"
|
|
|
|
|
#include "RageLog.h"
|
|
|
|
|
#include "RageTextureManager.h"
|
|
|
|
|
#include "RageSoundManager.h"
|
|
|
|
|
#include "PrefsManager.h"
|
|
|
|
|
#include "RageDisplay.h"
|
|
|
|
|
|
|
|
|
|
#include "arch/ArchHooks/ArchHooks.h"
|
|
|
|
|
|
|
|
|
|
#include "GameSoundManager.h"
|
|
|
|
|
#include "ThemeManager.h"
|
|
|
|
|
#include "SongManager.h"
|
|
|
|
|
#include "GameState.h"
|
|
|
|
|
#include "MemoryCardManager.h"
|
|
|
|
|
#include "ScreenManager.h"
|
|
|
|
|
#include "InputFilter.h"
|
2005-12-19 07:22:36 +00:00
|
|
|
#include "InputMapper.h"
|
2005-07-13 00:45:58 +00:00
|
|
|
#include "RageFileManager.h"
|
|
|
|
|
#include "LightsManager.h"
|
|
|
|
|
#include "NetworkSyncManager.h"
|
|
|
|
|
#include "RageTimer.h"
|
2005-12-18 03:08:12 +00:00
|
|
|
#include "RageInput.h"
|
2005-07-13 00:45:58 +00:00
|
|
|
|
|
|
|
|
static RageTimer g_GameplayTimer;
|
|
|
|
|
|
2006-10-07 08:04:02 +00:00
|
|
|
static Preference<bool> g_bNeverBoostAppPriority( "NeverBoostAppPriority", false );
|
2005-12-30 04:19:55 +00:00
|
|
|
|
|
|
|
|
/* experimental: force a specific update rate. This prevents big
|
|
|
|
|
* animation jumps on frame skips. 0 to disable. */
|
|
|
|
|
static Preference<float> g_fConstantUpdateDeltaSeconds( "ConstantUpdateDeltaSeconds", 0 );
|
|
|
|
|
|
2005-07-13 00:45:58 +00:00
|
|
|
void HandleInputEvents( float fDeltaTime );
|
|
|
|
|
|
2006-01-24 04:10:21 +00:00
|
|
|
static float g_fUpdateRate = 1;
|
|
|
|
|
void GameLoop::SetUpdateRate( float fUpdateRate )
|
|
|
|
|
{
|
|
|
|
|
g_fUpdateRate = fUpdateRate;
|
|
|
|
|
}
|
|
|
|
|
|
2005-07-13 00:45:58 +00:00
|
|
|
static void CheckGameLoopTimerSkips( float fDeltaTime )
|
|
|
|
|
{
|
|
|
|
|
if( !PREFSMAN->m_bLogSkips )
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
static int iLastFPS = 0;
|
|
|
|
|
int iThisFPS = DISPLAY->GetFPS();
|
|
|
|
|
|
|
|
|
|
/* If vsync is on, and we have a solid framerate (vsync == refresh and we've sustained this
|
|
|
|
|
* for at least one second), we expect the amount of time for the last frame to be 1/FPS. */
|
2005-11-11 21:16:48 +00:00
|
|
|
if( iThisFPS != DISPLAY->GetActualVideoModeParams().rate || iThisFPS != iLastFPS )
|
2005-07-13 00:45:58 +00:00
|
|
|
{
|
|
|
|
|
iLastFPS = iThisFPS;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const float fExpectedTime = 1.0f / iThisFPS;
|
|
|
|
|
const float fDifference = fDeltaTime - fExpectedTime;
|
|
|
|
|
if( fabsf(fDifference) > 0.002f && fabsf(fDifference) < 0.100f )
|
|
|
|
|
LOG->Trace( "GameLoop timer skip: %i FPS, expected %.3f, got %.3f (%.3f difference)",
|
|
|
|
|
iThisFPS, fExpectedTime, fDeltaTime, fDifference );
|
|
|
|
|
}
|
|
|
|
|
|
2005-12-30 04:00:05 +00:00
|
|
|
static bool ChangeAppPri()
|
|
|
|
|
{
|
2006-10-07 08:04:02 +00:00
|
|
|
if( g_bNeverBoostAppPriority.Get() )
|
2005-12-30 04:00:05 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
// if using NTPAD don't boost or else input is laggy
|
|
|
|
|
#if defined(_WINDOWS)
|
|
|
|
|
{
|
2006-02-27 17:51:27 +00:00
|
|
|
vector<InputDeviceInfo> vDevices;
|
2005-12-30 04:00:05 +00:00
|
|
|
|
|
|
|
|
// This can get called before INPUTMAN is constructed.
|
|
|
|
|
if( INPUTMAN )
|
|
|
|
|
{
|
2006-02-27 17:51:27 +00:00
|
|
|
INPUTMAN->GetDevicesAndDescriptions(vDevices);
|
|
|
|
|
FOREACH_CONST( InputDeviceInfo, vDevices, d )
|
2005-12-30 04:00:05 +00:00
|
|
|
{
|
2006-02-27 17:51:27 +00:00
|
|
|
if( d->sDesc.find("NTPAD") != string::npos )
|
|
|
|
|
{
|
|
|
|
|
LOG->Trace( "Using NTPAD. Don't boost priority." );
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2005-12-30 04:00:05 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2006-10-07 08:04:02 +00:00
|
|
|
/* If this is a debug build, don't. It makes the VC debugger sluggish. */
|
|
|
|
|
#if defined(WIN32) && defined(DEBUG)
|
|
|
|
|
return false;
|
|
|
|
|
#else
|
2005-12-30 04:00:05 +00:00
|
|
|
return true;
|
2006-10-07 08:04:02 +00:00
|
|
|
#endif
|
2005-12-30 04:00:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void CheckFocus()
|
|
|
|
|
{
|
2006-09-15 23:34:24 +00:00
|
|
|
if( !HOOKS->AppFocusChanged() )
|
2005-12-30 04:00:05 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
/* If we lose focus, we may lose input events, especially key releases. */
|
|
|
|
|
INPUTFILTER->Reset();
|
|
|
|
|
|
|
|
|
|
if( ChangeAppPri() )
|
|
|
|
|
{
|
2006-09-15 23:36:05 +00:00
|
|
|
if( HOOKS->AppHasFocus() )
|
2005-12-30 04:00:05 +00:00
|
|
|
HOOKS->BoostPriority();
|
|
|
|
|
else
|
|
|
|
|
HOOKS->UnBoostPriority();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2006-09-20 23:45:38 +00:00
|
|
|
/* On the next update, change themes, and load sNewScreen. */
|
|
|
|
|
static RString g_sNewTheme;
|
|
|
|
|
static RString g_sNewScreen;
|
2007-02-11 01:52:41 +00:00
|
|
|
static bool g_bForceThemeReload;
|
|
|
|
|
void GameLoop::ChangeTheme( const RString &sNewTheme, const RString &sNewScreen, bool bForced )
|
2006-09-20 23:45:38 +00:00
|
|
|
{
|
|
|
|
|
g_sNewTheme = sNewTheme;
|
|
|
|
|
g_sNewScreen = sNewScreen;
|
2007-02-11 01:52:41 +00:00
|
|
|
g_bForceThemeReload = bForced;
|
2006-09-20 23:45:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#include "StepMania.h" // XXX
|
|
|
|
|
namespace
|
|
|
|
|
{
|
|
|
|
|
void DoChangeTheme()
|
|
|
|
|
{
|
|
|
|
|
SAFE_DELETE( SCREENMAN );
|
|
|
|
|
TEXTUREMAN->DoDelayedDelete();
|
|
|
|
|
|
2006-09-21 04:58:41 +00:00
|
|
|
/* In case the previous theme overloaded class bindings, reinitialize them. */
|
|
|
|
|
LUA->RegisterTypes();
|
|
|
|
|
|
2007-02-11 01:52:41 +00:00
|
|
|
THEME->SwitchThemeAndLanguage( g_sNewTheme, THEME->GetCurLanguage(), PREFSMAN->m_bPseudoLocalize, g_bForceThemeReload );
|
2006-09-20 23:45:38 +00:00
|
|
|
PREFSMAN->m_sTheme.Set( g_sNewTheme );
|
|
|
|
|
|
|
|
|
|
/* Apply the new window title, icon and aspect ratio. */
|
|
|
|
|
StepMania::ApplyGraphicOptions();
|
|
|
|
|
|
|
|
|
|
SCREENMAN = new ScreenManager();
|
|
|
|
|
|
|
|
|
|
StepMania::ResetGame();
|
|
|
|
|
SCREENMAN->ThemeChanged();
|
|
|
|
|
SCREENMAN->SetNewScreen( g_sNewScreen );
|
|
|
|
|
|
|
|
|
|
g_sNewTheme = RString();
|
|
|
|
|
g_sNewScreen = RString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-26 04:49:45 +00:00
|
|
|
void GameLoop::RunGameLoop()
|
2005-07-13 00:45:58 +00:00
|
|
|
{
|
2005-12-30 04:00:05 +00:00
|
|
|
/* People may want to do something else while songs are loading, so do
|
|
|
|
|
* this after loading songs. */
|
|
|
|
|
if( ChangeAppPri() )
|
|
|
|
|
HOOKS->BoostPriority();
|
|
|
|
|
|
2006-09-16 00:14:20 +00:00
|
|
|
while( !ArchHooks::UserQuit() )
|
2005-07-13 00:45:58 +00:00
|
|
|
{
|
2006-09-20 23:45:38 +00:00
|
|
|
if( !g_sNewTheme.empty() )
|
|
|
|
|
DoChangeTheme();
|
|
|
|
|
|
2005-07-13 00:45:58 +00:00
|
|
|
/*
|
|
|
|
|
* Update
|
|
|
|
|
*/
|
|
|
|
|
float fDeltaTime = g_GameplayTimer.GetDeltaTime();
|
|
|
|
|
|
2005-12-30 04:19:55 +00:00
|
|
|
if( g_fConstantUpdateDeltaSeconds > 0 )
|
|
|
|
|
fDeltaTime = g_fConstantUpdateDeltaSeconds;
|
2005-07-13 00:45:58 +00:00
|
|
|
|
|
|
|
|
CheckGameLoopTimerSkips( fDeltaTime );
|
|
|
|
|
|
2006-01-24 04:10:21 +00:00
|
|
|
fDeltaTime *= g_fUpdateRate;
|
2005-07-13 00:45:58 +00:00
|
|
|
|
2005-12-30 04:00:05 +00:00
|
|
|
CheckFocus();
|
|
|
|
|
|
2005-07-13 00:45:58 +00:00
|
|
|
/* Update SOUNDMAN early (before any RageSound::GetPosition calls), to flush position data. */
|
2006-08-21 00:20:13 +00:00
|
|
|
SOUNDMAN->Update();
|
2005-07-13 00:45:58 +00:00
|
|
|
|
|
|
|
|
/* Update song beat information -before- calling update on all the classes that
|
|
|
|
|
* depend on it. If you don't do this first, the classes are all acting on old
|
|
|
|
|
* information and will lag. (but no longer fatally, due to timestamping -glenn) */
|
|
|
|
|
SOUND->Update( fDeltaTime );
|
|
|
|
|
TEXTUREMAN->Update( fDeltaTime );
|
|
|
|
|
GAMESTATE->Update( fDeltaTime );
|
|
|
|
|
SCREENMAN->Update( fDeltaTime );
|
2007-04-16 19:09:40 +00:00
|
|
|
MEMCARDMAN->Update();
|
2005-07-13 00:45:58 +00:00
|
|
|
NSMAN->Update( fDeltaTime );
|
|
|
|
|
|
|
|
|
|
/* Important: Process input AFTER updating game logic, or input will be acting on song beat from last frame */
|
|
|
|
|
HandleInputEvents( fDeltaTime );
|
|
|
|
|
|
2005-12-18 03:08:12 +00:00
|
|
|
if( INPUTMAN->DevicesChanged() )
|
|
|
|
|
{
|
2005-12-19 10:00:29 +00:00
|
|
|
INPUTFILTER->Reset(); // fix "buttons stuck" if button held while unplugged
|
2005-12-18 21:24:33 +00:00
|
|
|
INPUTMAN->LoadDrivers();
|
2006-01-22 01:00:06 +00:00
|
|
|
RString sMessage;
|
2005-12-19 07:22:36 +00:00
|
|
|
if( INPUTMAPPER->CheckForChangedInputDevicesAndRemap(sMessage) )
|
|
|
|
|
SCREENMAN->SystemMessage( sMessage );
|
2005-12-18 03:08:12 +00:00
|
|
|
}
|
|
|
|
|
|
2005-07-13 00:45:58 +00:00
|
|
|
LIGHTSMAN->Update( fDeltaTime );
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Render
|
|
|
|
|
*/
|
|
|
|
|
SCREENMAN->Draw();
|
|
|
|
|
}
|
2005-12-30 04:10:43 +00:00
|
|
|
|
2006-09-21 05:08:42 +00:00
|
|
|
/* If we ended mid-game, finish up. */
|
2007-04-19 13:59:38 +00:00
|
|
|
GAMESTATE->SaveLocalData();
|
2006-09-21 05:08:42 +00:00
|
|
|
|
2005-12-30 04:10:43 +00:00
|
|
|
if( ChangeAppPri() )
|
|
|
|
|
HOOKS->UnBoostPriority();
|
2005-07-13 00:45:58 +00:00
|
|
|
}
|
|
|
|
|
|
2005-07-13 06:41:27 +00:00
|
|
|
class ConcurrentRenderer
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
ConcurrentRenderer();
|
|
|
|
|
~ConcurrentRenderer();
|
|
|
|
|
|
2006-01-27 09:15:21 +00:00
|
|
|
void Start();
|
|
|
|
|
void Stop();
|
|
|
|
|
|
2005-07-13 06:41:27 +00:00
|
|
|
private:
|
|
|
|
|
RageThread m_Thread;
|
2006-01-27 09:15:21 +00:00
|
|
|
RageEvent m_Event;
|
2005-07-13 06:41:27 +00:00
|
|
|
bool m_bShutdown;
|
|
|
|
|
void RenderThread();
|
|
|
|
|
static int StartRenderThread( void *p );
|
2006-01-27 09:15:21 +00:00
|
|
|
|
|
|
|
|
enum State { RENDERING_IDLE, RENDERING_START, RENDERING_ACTIVE, RENDERING_END };
|
|
|
|
|
State m_State;
|
2005-07-13 06:41:27 +00:00
|
|
|
};
|
|
|
|
|
static ConcurrentRenderer *g_pConcurrentRenderer = NULL;
|
|
|
|
|
|
2006-01-27 09:15:21 +00:00
|
|
|
ConcurrentRenderer::ConcurrentRenderer():
|
|
|
|
|
m_Event("ConcurrentRenderer")
|
2005-07-13 06:41:27 +00:00
|
|
|
{
|
|
|
|
|
m_bShutdown = false;
|
2006-01-27 09:15:21 +00:00
|
|
|
m_State = RENDERING_IDLE;
|
2005-07-13 06:41:27 +00:00
|
|
|
|
|
|
|
|
m_Thread.SetName( "ConcurrentRenderer" );
|
|
|
|
|
m_Thread.Create( StartRenderThread, this );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ConcurrentRenderer::~ConcurrentRenderer()
|
|
|
|
|
{
|
2006-01-27 09:15:21 +00:00
|
|
|
ASSERT( m_State == RENDERING_IDLE );
|
2005-07-13 06:41:27 +00:00
|
|
|
m_bShutdown = true;
|
|
|
|
|
m_Thread.Wait();
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-27 09:15:21 +00:00
|
|
|
void ConcurrentRenderer::Start()
|
2005-07-13 06:41:27 +00:00
|
|
|
{
|
2006-01-27 09:15:21 +00:00
|
|
|
DISPLAY->BeginConcurrentRenderingMainThread();
|
|
|
|
|
|
|
|
|
|
m_Event.Lock();
|
|
|
|
|
ASSERT( m_State == RENDERING_IDLE );
|
|
|
|
|
m_State = RENDERING_START;
|
|
|
|
|
m_Event.Signal();
|
|
|
|
|
while( m_State != RENDERING_ACTIVE )
|
|
|
|
|
m_Event.Wait();
|
|
|
|
|
m_Event.Unlock();
|
|
|
|
|
}
|
2005-07-13 06:41:27 +00:00
|
|
|
|
2006-01-27 09:15:21 +00:00
|
|
|
void ConcurrentRenderer::Stop()
|
|
|
|
|
{
|
|
|
|
|
m_Event.Lock();
|
|
|
|
|
ASSERT( m_State == RENDERING_ACTIVE );
|
|
|
|
|
m_State = RENDERING_END;
|
|
|
|
|
m_Event.Signal();
|
|
|
|
|
while( m_State != RENDERING_IDLE )
|
|
|
|
|
m_Event.Wait();
|
|
|
|
|
m_Event.Unlock();
|
|
|
|
|
|
|
|
|
|
DISPLAY->EndConcurrentRenderingMainThread();
|
|
|
|
|
}
|
2005-07-13 06:41:27 +00:00
|
|
|
|
2006-01-27 09:15:21 +00:00
|
|
|
void ConcurrentRenderer::RenderThread()
|
|
|
|
|
{
|
|
|
|
|
ASSERT( SCREENMAN != NULL );
|
2005-07-13 06:41:27 +00:00
|
|
|
|
|
|
|
|
while( !m_bShutdown )
|
|
|
|
|
{
|
2006-01-27 09:15:21 +00:00
|
|
|
m_Event.Lock();
|
|
|
|
|
while( m_State == RENDERING_IDLE && !m_bShutdown )
|
|
|
|
|
m_Event.Wait();
|
|
|
|
|
m_Event.Unlock();
|
2005-07-13 06:41:27 +00:00
|
|
|
|
2006-01-27 09:15:21 +00:00
|
|
|
if( m_State == RENDERING_START )
|
|
|
|
|
{
|
|
|
|
|
/* We're starting to render. Set up, and then kick the event to wake
|
|
|
|
|
* up the calling thread. */
|
|
|
|
|
DISPLAY->BeginConcurrentRendering();
|
|
|
|
|
HOOKS->SetupConcurrentRenderingThread();
|
|
|
|
|
|
|
|
|
|
LOG->Trace( "ConcurrentRenderer::RenderThread start" );
|
|
|
|
|
|
|
|
|
|
m_Event.Lock();
|
|
|
|
|
m_State = RENDERING_ACTIVE;
|
|
|
|
|
m_Event.Signal();
|
|
|
|
|
m_Event.Unlock();
|
|
|
|
|
}
|
2005-07-13 06:41:27 +00:00
|
|
|
|
2006-01-27 09:15:21 +00:00
|
|
|
/* This is started during Update(). The next thing the game loop
|
|
|
|
|
* will do is Draw, so shift operations around to put Draw at the
|
|
|
|
|
* top. This makes sure updates are seamless. */
|
|
|
|
|
if( m_State == RENDERING_ACTIVE )
|
|
|
|
|
{
|
|
|
|
|
SCREENMAN->Draw();
|
|
|
|
|
|
|
|
|
|
float fDeltaTime = g_GameplayTimer.GetDeltaTime();
|
|
|
|
|
SCREENMAN->Update( fDeltaTime );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if( m_State == RENDERING_END )
|
|
|
|
|
{
|
|
|
|
|
LOG->Trace( "ConcurrentRenderer::RenderThread done" );
|
|
|
|
|
|
|
|
|
|
DISPLAY->EndConcurrentRendering();
|
|
|
|
|
|
|
|
|
|
m_Event.Lock();
|
|
|
|
|
m_State = RENDERING_IDLE;
|
|
|
|
|
m_Event.Signal();
|
|
|
|
|
m_Event.Unlock();
|
|
|
|
|
}
|
|
|
|
|
}
|
2005-07-13 06:41:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int ConcurrentRenderer::StartRenderThread( void *p )
|
|
|
|
|
{
|
|
|
|
|
((ConcurrentRenderer *) p)->RenderThread();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-24 03:53:17 +00:00
|
|
|
void GameLoop::StartConcurrentRendering()
|
2005-07-13 06:41:27 +00:00
|
|
|
{
|
2006-01-27 09:15:21 +00:00
|
|
|
if( g_pConcurrentRenderer == NULL )
|
|
|
|
|
g_pConcurrentRenderer = new ConcurrentRenderer;
|
|
|
|
|
g_pConcurrentRenderer->Start();
|
2005-07-13 06:41:27 +00:00
|
|
|
}
|
|
|
|
|
|
2006-01-24 03:53:17 +00:00
|
|
|
void GameLoop::FinishConcurrentRendering()
|
2005-07-13 06:41:27 +00:00
|
|
|
{
|
2006-01-27 09:15:21 +00:00
|
|
|
g_pConcurrentRenderer->Stop();
|
2005-07-13 06:41:27 +00:00
|
|
|
}
|
|
|
|
|
|
2005-07-13 00:45:58 +00:00
|
|
|
/*
|
|
|
|
|
* (c) 2001-2005 Chris Danford, Glenn Maynard
|
|
|
|
|
* All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
|
* copy of this software and associated documentation files (the
|
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
|
* distribute, and/or sell copies of the Software, and to permit persons to
|
|
|
|
|
* whom the Software is furnished to do so, provided that the above
|
|
|
|
|
* copyright notice(s) and this permission notice appear in all copies of
|
|
|
|
|
* the Software and that both the above copyright notice(s) and this
|
|
|
|
|
* permission notice appear in supporting documentation.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
|
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
|
|
|
|
|
* THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
|
|
|
|
|
* INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
|
|
|
|
|
* OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
|
|
|
|
|
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
|
|
|
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
|
|
|
* PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
|
*/
|