/*===========================================================================

  timer.h - Timing functions for games or demos.

  Copyright © 2005 Matthias Grumet. All rights reserved.

  http://www.embege.com/timing/

  Permission is hereby granted, free of charge, to any person obtaining
  a copy of this software and associated documentation files (the
  "Software"), to deal in the Software without restriction, including
  without limitation the rights to use, copy, modify, merge, publish,
  distribute, sublicense, and/or sell copies of the Software, and to
  permit persons to whom the Software is furnished to do so, subject to
  the following conditions:

  The above copyright notice and this permission notice shall be
  included in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

===========================================================================*/

#ifndef _TIMER_H
#define _TIMER_H

#include <math.h>

#define ACCURACY 20

LARGE_INTEGER curTime;
LARGE_INTEGER lastTime;
LARGE_INTEGER waitlastTime;
LARGE_INTEGER fpslastTime;
LARGE_INTEGER startTime;
LARGE_INTEGER frequency;
int fpscounter;
int fps;
double elapsedTime;
double timebuffer[ACCURACY];
double lastbuffer[ACCURACY];

//initialize timer variables
void timer_initialize()
{
	QueryPerformanceFrequency(&frequency);
	QueryPerformanceCounter(&lastTime);
	startTime.QuadPart = 0;
	fps = 0;
	fpscounter = 0;
	for (int i = 0; i < ACCURACY; i++)
		timebuffer[i] = 0.0;
}

//starts the timer
void timer_start()
{
	QueryPerformanceCounter(&startTime);
}

//waits for seconds
void timer_wait(float seconds)
{
	do
	{
		QueryPerformanceCounter(&curTime);
	}
	while ((double)(curTime.QuadPart-waitlastTime.QuadPart)/(double)frequency.QuadPart < seconds);

	QueryPerformanceCounter(&waitlastTime);
}

//returns the passed time
double timer_getTime()
{
	QueryPerformanceCounter(&curTime);
	return (double)((curTime.QuadPart-startTime.QuadPart)/(double)frequency.QuadPart);
}

//returns the time that has passed since the last call to this function
//good for measuring the exact time between frames
double timer_getelapsedTime()
{
	QueryPerformanceCounter(&curTime);
	elapsedTime = (double)(curTime.QuadPart-lastTime.QuadPart)/(double)frequency.QuadPart;
	lastTime = curTime;
	return elapsedTime;
}

//returns frames per seconds
int timer_getFPS()
{
	fpscounter++;
	QueryPerformanceCounter(&curTime);
	//check if one second has passed
	if( (double)(curTime.QuadPart-fpslastTime.QuadPart)/(double)frequency.QuadPart > 1.0f ) {
		fpslastTime = curTime;
		fps = fpscounter;
		fpscounter = 0;
	}
	return fps;
}

//returns an averaged time over the last ACCURACY frames
//good for avoiding outliers due to laggy frames
//you should provide the result of timer_getelapsedTime() as argument
double timer_getaveragedTime(double gt)
{
	double sum = 0.0;

	for (int i = 1; i<ACCURACY; i++) {
		timebuffer[i] = lastbuffer[i-1];
		sum += timebuffer[i];
	}
	timebuffer[0] = gt;
	sum += timebuffer[0];
	for (int i = 0; i<ACCURACY; i++)
		lastbuffer[i] = timebuffer[i];

	return sum/(double)ACCURACY;
}


#endif

