/// Copyright (c) 2010 Ruben Penalva. All rights reserved.
/// Redistribution and use in source and binary forms, with or without modification, 
/// are permitted provided that the following conditions are met:
///    1. Redistributions of source code must retain the above copyright notice, this 
///    list of conditions and the following disclaimer.
///    2. Redistributions in binary form must reproduce the above copyright notice,
///    this list of conditions and the following disclaimer in the documentation and/or
///    other materials provided with the distribution.
///    3. The name of the author may not be used to endorse or promote products derived
///    from this software without specific prior written permission.
///
/// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
/// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
/// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 
/// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
/// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
/// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
/// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 
/// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "Timer.h"

#include "Exception.h"

using namespace TestFrameWork;

Timer::Timer(int samplesCount)	:	m_samplesCount(samplesCount),
									m_elapsedTimeSamples(new float[samplesCount]), 
									m_samplesInsertionIndex(0),
									m_timeFactor(ConfigureTimeCounter()),
									m_lastTime(0)
{
	assert(samplesCount > 1);

	memset(m_elapsedTimeSamples.get(), 0, sizeof(float) * m_samplesCount);

	if (QueryPerformanceCounter((LARGE_INTEGER *) &m_lastTime) != TRUE)
	{
		throw Exception(L"QueryPerformanceCounter Error");
	}
}

Timer::~Timer(void)
{
}

float Timer::ElapsedTime(void)
{
	LONGLONG currentTime;
	QueryPerformanceCounter((LARGE_INTEGER *) &currentTime);		
	
	float elapsedTime = (currentTime - m_lastTime) * m_timeFactor;

	m_lastTime = currentTime;

	m_elapsedTimeSamples[m_samplesInsertionIndex] = elapsedTime;

	m_samplesInsertionIndex++;
	m_samplesInsertionIndex %= m_samplesCount;

	return elapsedTime;
}

float Timer::MeanElapsedTime(void) const
{
	float meanElapsedTime = 0.0f;

	for (int i = 0; i < m_samplesCount; ++i)
	{
		meanElapsedTime += m_elapsedTimeSamples[i];
	}

	return meanElapsedTime / (m_samplesCount * 1.0f);
}

float Timer::ConfigureTimeCounter(void) const
{	
	LONGLONG performanceFrequency;      

	/// is there a performance counter available?
	if (QueryPerformanceFrequency((LARGE_INTEGER *) &performanceFrequency) != TRUE) 
	{
		throw Exception(L"QueryPerformanceFrequency Error. High Performance Counter is not available");
	}

	return 1.0f / performanceFrequency;
}