/*
 * DeltaTime.h
 *
 *  Created on: 2009-07-07
 *      Author: wasyl
 */

#ifndef DELTATIME_H_
#define DELTATIME_H_

#include <allegro5/allegro.h>
#include <vector>

/****************************************/

struct DeltaInterval
{
	double dt;
	bool skipRendering;

	DeltaInterval(double dt, bool skipRendering) : dt(dt), skipRendering(skipRendering) {};

	static const DeltaInterval NONE;
};

/****************************************/

class DeltaTime
{
	private:
		double minDT, maxDT;
		int maxFS;
		double globalTimerFreq;

		bool running;

		double referenceTime;
		double spareTime, halfAvgSleepTime;

		std::vector<DeltaInterval> pendingIntervals;

	public:

		/** Constructors **/
		DeltaTime(double minDT, double maxDT, int maxFS);
		DeltaTime();

		/**
		 * Start the delta time handler.
		 */
		void start();

		/**
		 * Stop the delta time handler.
		 */
		void stop();

		/**
		 * Restart the delta time handler.
		 */
		void restart();

	private:

		/**
		 * Retrieves raw delta time since last reference time.
		 */
		double getRawDT();

	public:

		/**
		 * Returns the next delta interval to be processed.
		 */
		DeltaInterval delta();
};


#endif /* DELTATIME_H_ */
