//////////////////////////////////////////////////////////////////////////////
//
// Asura_Platform_Timers.cpp
//
// Copyright (c) 2006 Rebellion. All rights reserved.
//
//////////////////////////////////////////////////////////////////////////////

/******************************************************************************/
/* Included Files                                                             */
/******************************************************************************/

#include "Core/Asura.h"

#include "Core/Asura_Timers.h"

// Misc
#include <xtl.h>

/******************************************************************************/
/* Constants                                                                  */
/******************************************************************************/

// Timer query always returns 50 MHz on 360 (slightly inaccurate). See docs for
// QueryPerformanceFrequency()
const float fFREQUENCY = 50000000.0f;

/******************************************************************************/
/* Global Variables                                                           */
/******************************************************************************/

static Asura_TickCount g_xLastTickCount;

/******************************************************************************/
/* Asura_Timers Static Class Functions                                        */
/******************************************************************************/

Asura_TickCount Asura_Timers::Platform_GetCurTicks()
{
	return __mftb();
}

float Asura_Timers::Platform_GetDefaultFrameRate()
{
	return 60.0f;
}

Asura_TickCount Asura_Timers::Platform_GetElapsedTicks( Asura_TickCount xStartTicks )
{
	return __mftb() - xStartTicks;
}

float Asura_Timers::Platform_GetTickFrequency()
{
	return fFREQUENCY;
}

u_int Asura_Timers::Platform_GetTicksInFrame()
{
	return static_cast< u_int >( __mftb() - g_xLastTickCount );
}

u_int Asura_Timers::Platform_GetTicksThisFrame()
{
	const unsigned __int64 xNewTickCount = __mftb();
	const unsigned __int64 xTicksThisFrame = xNewTickCount - g_xLastTickCount;

	g_xLastTickCount = xNewTickCount;

	return static_cast< u_int >( xTicksThisFrame );
}

void Asura_Timers::Platform_Initialise()
{
}

void Asura_Timers::Platform_Reset()
{
	g_xLastTickCount = __mftb();
}

void Asura_Timers::Platform_Shutdown()
{
}

Asura_TickCount Asura_Timers::Platform_SecondsToTicks( float fSeconds )
{
	return static_cast< Asura_TickCount >( fSeconds * fFREQUENCY );
}

float Asura_Timers::Platform_TicksToSeconds( Asura_TickCount xTicks )
{
	return static_cast< float >( xTicks ) / fFREQUENCY;
}

void Asura_Timers::Platform_Update()
{
}
