// SpectatorTimer.cpp ***********************************************//
// Created     Owner     Modification                                //
// Copyright Microsoft Corporation, 2001                             //
// --------    ------    -------------                               //
// 12/12/2001  sdemar    definition of timer object for state engine //
//*******************************************************************//

#include "SpectatorTimer.hpp"
#include "MW4.hpp"  // gos_GetElapsedTime()


#define SMALL_TIME_VALUE 0.00001

CSpectatorTimer::CSpectatorTimer() :
	m_expireTime(gos_GetElapsedTime()), 
	m_startTime(m_expireTime),
	m_fIsExpired(true)
{
}


CSpectatorTimer::~CSpectatorTimer() 
{
	// stubbed
}

void CSpectatorTimer::Initialize(const double duration)
{
	// gos_GetElapsedTime() returns a formatted double as seconds elapsed
	
	m_startTime = gos_GetElapsedTime();
	m_expireTime = duration + m_startTime/*gos_GetElapsedTime() don't use this...*/;

	// insure that an expired duration was not used
#if 1
    CheckExpired();
#else
	if (m_expireTime > gos_GetElapsedTime()) {
		m_fIsExpired = false;
	} else {
		m_fIsExpired = true;
	}
#endif
}

bool CSpectatorTimer::CheckExpired(double& dblElapsed/* = *(double*)0*/)
{
    if (&dblElapsed)
        dblElapsed = GetTimeRemaining();
    else
        GetTimeRemaining();

    return m_fIsExpired;
}

double CSpectatorTimer::GetTimeRemaining()
{
	double elapsedTime;
	elapsedTime = m_expireTime - gos_GetElapsedTime();

	if (elapsedTime <= SMALL_TIME_VALUE)
	{
        elapsedTime = -SMALL_TIME_VALUE;
		m_fIsExpired = true;
	}

	return elapsedTime;
}


void CSpectatorTimer::Expire(void) 
{ 
	m_startTime = m_expireTime = gos_GetElapsedTime(); 
	m_fIsExpired = true;
}