
///////////////////////////////////////////////////////////////////////////////
//
// File   : AVP_CoreState_LoadingFrontend.cpp
// Author : Anthony Gregson
// Date   : 21/11/07
//
// Copyright (C) 2007 Rebellion, All Rights Reserved.
//
///////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
// Include files...
////////////////////////////////////////////////////////////////////////////////

#include "Core/Asura.h"

#if defined(AVP_USE_GAME_FRONTEND)

// This file's header...
#include "Core/States/Core/AVP_CoreState_LoadingFrontend.h"

// Asura independent header-files...
#include "ChunkSystem/Asura_Chunk_Loaders.h"
#include "Core/Asura_Core.h"
#include "Core/Asura_Multithread.h"
#include "Core/Resources/Asura_Resource_System.h"
#include "Debugging/Asura_DebugText.h"
#ifdef ASURA_SUPPORT_DOWNLOADABLE_CONTENT
#include "Network/Asura_DLC_System.h"
#endif //#ifdef ASURA_SUPPORT_DOWNLOADABLE_CONTENT

// Game independent header-files...
#include "Core/AVP_Core.h"
#include "Core/States/Core/AVP_StateMachine_GameFrontend.h"
#include "GUIMenu/AVP_GUIMenu_System.h"


////////////////////////////////////////////////////////////////////////////////
// Constant declarations...
////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////
// Static variable declarations...
////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////
// Private functions...
////////////////////////////////////////////////////////////////////////////////

Asura_TickCount	AVP_CoreState_LoadingFrontend::s_xStartTicks	= 0;
volatile bool	AVP_CoreState_LoadingFrontend::s_bLoadCompleted	= false;


////////////////////////////////////////////////////////////////////////////////
// Protected functions...
////////////////////////////////////////////////////////////////////////////////

u_int	AVP_CoreState_LoadingFrontend::m_uStatePostLoad = AVP_CoreState_LoadingFrontend::STATE_POSTLOAD_FRONTEND;


////////////////////////////////////////////////////////////////////////////////
// Function     : AVP_CoreState_LoadingFrontend::PerformLoad
// Author       : Andy Tate
// Date         : 11-Feb-2009
////////////////////////////////////////////////////////////////////////////////

void AVP_CoreState_LoadingFrontend::PerformLoad(const void* pUserData)
{
#if defined(AVP_USE_DEV_LOBBY)
	AVP_Core::LoadFile("GUIMenu\\DevLobby." ASURA_GUIMENU_STANDARD_FILE_EXTENSION);
#endif	//defined(AVP_USE_DEV_LOBBY)
	AVP_Core::LoadFile("GUIMenu\\FrontEnd." ASURA_GUIMENU_STANDARD_FILE_EXTENSION);
	AVP_Core::LoadFile("GUIMenu\\FE_Marine." ASURA_GUIMENU_STANDARD_FILE_EXTENSION);
#if defined(AVP_USE_GAME_LOBBY)
	AVP_Core::LoadFile("GUIMenu\\GameLobby." ASURA_GUIMENU_STANDARD_FILE_EXTENSION);
#endif	//defined(AVP_USE_GAME_LOBBY)

#ifdef ASURA_SUPPORT_DOWNLOADABLE_CONTENT
	if (AVP_Core::GetDLCRequiresFilesUpdate())
	{
		AVP_Core::SetDLCRequiresFilesUpdate(false);
		Asura_DLC_System::UpdateDownloadedFiles();
	}
#endif //#ifdef ASURA_SUPPORT_DOWNLOADABLE_CONTENT

	// We're finished...
	s_bLoadCompleted	= true;
}

/// End of function 'AVP_CoreState_LoadingFrontend::PerformLoad'. ///



////////////////////////////////////////////////////////////////////////////////
// Public functions...
////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
// Function     : AVP_CoreState_LoadingFrontend::Enter
// Author       : Andy Tate
// Date         : 11-Feb-2009
////////////////////////////////////////////////////////////////////////////////

void AVP_CoreState_LoadingFrontend::Enter(const u_int uPreviousState)
{
	AVP_Core::InitialiseEffects();

	// Time to begin loading the frontend data...
	s_xStartTicks		= Asura_Timers::GetCurTicks();
	s_bLoadCompleted	= false;

#if defined(AVP_ASYNCHRONOUS_LOADING)
	Asura_Thread_System::CreateThread("AvP_LoadingFrontend", PerformLoad, NULL);
#endif	// defined(AVP_ASYNCHRONOUS_LOADING)	
}

/// End of function 'AVP_CoreState_LoadingFrontend::Enter'. ///



////////////////////////////////////////////////////////////////////////////////
// Function     : AVP_CoreState_LoadingFrontend::Update
// Author       : Andy Tate
// Date         : 12-Mar-2007
////////////////////////////////////////////////////////////////////////////////

void AVP_CoreState_LoadingFrontend::Update()
{
#if !defined(AVP_ASYNCHRONOUS_LOADING)
	const float	fTimeInState	= GetParentMachine().GetTimeInCurrentState();

	if(fTimeInState > 1.0f)
	{
		PerformLoad(NULL);
	}
#endif	// !defined(AVP_ASYNCHRONOUS_LOADING)

	// Have we finished loading?
	if(s_bLoadCompleted)
	{
		// Decide where to go from here, as may wish to goto to FrontEnd or MP-Lobby
		if ( m_uStatePostLoad == STATE_POSTLOAD_FRONTEND )
		{
			GetParentMachine().ChangeState( AVP_StateMachine_GameFrontend::STATE_FRONTEND );
		}
#if defined(ASURA_SUPPORT_LOBBY)
		else if ( m_uStatePostLoad == STATE_POSTLOAD_MPLOBBY )
		{
			GetParentMachine().ChangeState( AVP_StateMachine_GameFrontend::STATE_MULTIPLAYERLOBBY );
		}
#endif	//defined(ASURA_SUPPORT_LOBBY)
		m_uStatePostLoad = STATE_POSTLOAD_FRONTEND;	//	Reset
	}
	else
	{
#ifdef AVP_ASYNCHRONOUS_LOADING // Probably irrelevant, since we almost certainly have to be loading asynchronously to hit this code.
		Asura_Thread_System::YieldCurrentThread();
#endif //#ifdef AVP_ASYNCHRONOUS_LOADING
	}
}

/// End of function 'AVP_CoreState_LoadingFrontend::Update'. ///



////////////////////////////////////////////////////////////////////////////////
// Function     : AVP_CoreState_LoadingFrontend::Render
// Author       : Andy Tate
// Date         : 22-Jan-2009
////////////////////////////////////////////////////////////////////////////////

void AVP_CoreState_LoadingFrontend::Render(void) const
{
#if !defined(ASURA_TOOLS)
	AVP_Core::BeginRender(false);
	{
		AVP_GUIMenu_System::Render(true);

#if defined(ASURA_USE_DEBUG_TEXT)
		{
			const Asura_TickCount	xElapsedTicks	= Asura_Timers::GetElapsedTicks(s_xStartTicks);
			const float				fElapsedSecs	= Asura_Timers::TicksToSeconds(xElapsedTicks);

			Asura_DebugText::AddTextBlock(50, 20, 0xFFFFFFFF, 0, "Loading Frontend: %.02f secs", fElapsedSecs);
		}
#endif	// defined(ASURA_USE_DEBUG_TEXT)
	}
	AVP_Core::EndRender();
#endif	// defined(ASURA_TOOLS)
}

/// End of function 'AVP_CoreState_LoadingFrontend::Render'. ///



////////////////////////////////////////////////////////////////////////////////
// Function     : AVP_CoreState_LoadingFrontend::Exit
// Author       : Andy Tate
// Date         : 11-Feb-2009
////////////////////////////////////////////////////////////////////////////////

void AVP_CoreState_LoadingFrontend::Exit(const u_int uNextState)
{
	AVP_GUIMenu_System::Deactivate(AVP_GUIMenu_System::GUIMENU_ID_LOADING);
}

/// End of function 'AVP_CoreState_LoadingFrontend::Exit'. ///

#endif	//defined(AVP_USE_GAME_FRONTEND)


/// End of file. ///
