void Game::MainLoop()
{
    ITimeSystem *pTimeSystem = m_pEnv->sys->pTimeSystem;

    // Time in userspace, ignoring frametime and whether we are paused, compiling, etc.
    // That seems most appropriate to the filechangenotifier
    double fSessionTimeNow = pTimeSystem->GetSessionTimeNow();
    float fSessionTimeDelta = (float)(fSessionTimeNow - m_fLastUpdateSessionTime);
    float fClampedDelta = (std::min)( fSessionTimeDelta*m_GameSpeed, 0.1f ); // used for IObject updates
    m_fLastUpdateSessionTime = fSessionTimeNow;

    float fFrameTimeDelta = (float)pTimeSystem->GetSmoothFrameDuration();

    m_pEnv->sys->pFileChangeNotifier->Update(fSessionTimeDelta);

    //check status of any compile
    bool bLoadModule = false;
    if( m_pEnv->sys->pRuntimeObjectSystem->GetIsCompiledComplete() )
    {
        bLoadModule = true; //we load module after update/display, to get notification on screen correct
    }

    pTimeSystem->StartFrame();

    if( !m_bHaveProgramError )
    {
        AUDynArray<AUEntityId> entities;
        IEntitySystem* pEntitySystem = m_pEnv->sys->pEntitySystem;
        pEntitySystem->GetAll(entities);

        if (!ProtectedUpdate(entities, fClampedDelta))
        {
            m_bHaveProgramError = true;
            m_pEnv->sys->pLogSystem->Log(eLV_ERRORS, "Have caught an exception in main entity Update loop, code will not be run until new compile - please fix.\n");
        }
    }

    if (!m_pCameraControl || !m_pLightingControl)
    {
        if (!m_bRenderError)
        {
            m_bRenderError = true;
            m_pEnv->sys->pLogSystem->Log(eLV_ERRORS, "Missing Camera and/or Lighting control. Can't render world.\n");
        }
    }
    else
    {
        m_bRenderError = false;
        RenderWorld();
    }

    RocketLibUpdate();

    if( bLoadModule )
    {
        // load module when compile complete, and notify console - TODO replace with event system
        bool bSuccess = m_pEnv->sys->pRuntimeObjectSystem->LoadCompiledModule();
        m_pConsole->OnCompileDone(bSuccess);
        if( bSuccess )
        {
            // reset program error status
            m_bHaveProgramError = false;
        }
    }

    // Limit frame rate
    double dTimeTaken = pTimeSystem->GetFrameTimeNow();
    const double dIdealTime = 1.0 / 60.0;
    if ( dTimeTaken < dIdealTime)
    {
        Sleep( (DWORD) ((dIdealTime - dTimeTaken) * 1000.0) );
    }

    pTimeSystem->EndFrame();
}
Beispiel #2
0
void Game::MainLoop()
{
	ITimeSystem *pTimeSystem = m_pEnv->sys->pTimeSystem;

	// Time in userspace, ignoring frametime and whether we are paused, compiling, etc.
	// That seems most appropriate to the filechangenotifier
	double fSessionTimeNow = pTimeSystem->GetSessionTimeNow();
	float fSessionTimeDelta = (float)(fSessionTimeNow - m_fLastUpdateSessionTime);
	float fClampedDelta = (std::min)( fSessionTimeDelta*m_GameSpeed, 0.1f ); // used for IObject updates
	m_fLastUpdateSessionTime = fSessionTimeNow;

	m_pEnv->sys->pFileChangeNotifier->Update(fSessionTimeDelta);

	if( m_pEnv->sys->pRuntimeObjectSystem->GetIsCompiling() && m_CompileStartedTime == 0.0 )
	{
		m_CompileStartedTime = pTimeSystem->GetSessionTimeNow();
	}

	//check status of any compile
	bool bLoadModule = false;
	if( m_pEnv->sys->pRuntimeObjectSystem->GetIsCompiledComplete() )
	{
		bLoadModule = true; //we load module after update/display, to get notification on screen correct
	}

	pTimeSystem->StartFrame();

	AUDynArray<AUEntityId> entities;
	m_EntityUpdateProtector.pEntitySystem->GetAll(m_EntityUpdateProtector.entities);
    m_EntityUpdateProtector.fDeltaTime = fClampedDelta;
		
    if (!m_pEnv->sys->pRuntimeObjectSystem->TryProtectedFunction( &m_EntityUpdateProtector ) )
	{
		m_pEnv->sys->pLogSystem->Log(eLV_ERRORS, "Have caught an exception in main entity Update loop, code will not be run until new compile - please fix.\n");
	}


	if (!m_pCameraControl || !m_pLightingControl)
	{
		if (!m_bRenderError)
		{
			m_bRenderError = true;
			m_pEnv->sys->pLogSystem->Log(eLV_ERRORS, "Missing Camera and/or Lighting control. Can't render world.\n");
		}	
	}
	else
	{
		m_bRenderError = false;
		RenderWorld();
	}

	RocketLibUpdate();

	if( bLoadModule )
	{
		// load module when compile complete, and notify console - TODO replace with event system 
		bool bSuccess = m_pEnv->sys->pRuntimeObjectSystem->LoadCompiledModule();
		m_pConsole->OnCompileDone(bSuccess);
		if( bSuccess )
		{
			float compileAndLoadTime = (float)( pTimeSystem->GetSessionTimeNow() - m_CompileStartedTime );
			m_pEnv->sys->pLogSystem->Log(eLV_COMMENTS, "Compile and Module Reload Time: %.1f s\n", compileAndLoadTime);

		}
		m_CompileStartedTime = 0.0;
	}

	// Limit frame rate
	double dTimeTaken = pTimeSystem->GetFrameTimeNow();
	const double dIdealTime = 1.0 / 70.0; //ideal time is actually 1/60, but we want some leeway 
	if ( dTimeTaken < dIdealTime)
	{
        glfwSleep( dIdealTime - dTimeTaken );
	}

	pTimeSystem->EndFrame();
}