/*
	Lan Wars.
	Copyright (c) 2009, Darragh Coy.
	All rights reserved.
	Original work(s), source code & author(s) details available at: http://www.darraghcoy.com

	Redistribution and use in source and binary forms, with or without modification, are permitted 
	provided that the following conditions are met:

		* 	Redistributions of source code must retain the above copyright notice, this list of 
			conditions and the following disclaimer.
		* 	Redistributions in binary form must reproduce the above copyright notice, this list of 
			conditions and the following disclaimer in the documentation.
		* 	Neither the name of the author nor the names of contributors may be used to endorse or 
			promote products derived from this software without specific prior written permission.
		* 	The use of any third party libraries, modules or data files provided with the source 
			form of the program must follow and respect the individual licensing terms of these 
			components. You do not have permission to modify or circumvent the licensing terms for 
			provided third party components unless the third party licenses specifically allow this. 
			If you do not agree to the terms of provided third party components then you do not have 
			permission to use them in your derived work(s).
		* 	The terms of this license do not apply to separate (but related) distributions if supplied 
			by the author(s), only to the contents of this source distribution, derived works, and the 
			binaries produced by either modified or unmodified versions of the source code. The terms 
			of this license do not also apply to any third party libraries or components provided with 
			the source distribution. Such third party elements are licensed under their own separate 
			agreements.

	THIS SOFTWARE IS PROVIDED BY DARRAGH COY ''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 DARRAGH COY 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.
*/

#ifndef GAME_FRAME_TIMER_H
#define GAME_FRAME_TIMER_H

//=====================================================================================================================
//=====================================================================================================================

namespace Game
{
	//=================================================================================================================
	/// @brief		Simple class that handles frame timing logic and frame rate control for the game.
	///				This class will make the application sleep if we are getting ahead of real time also, to save
	///				on CPU usage.
	//=================================================================================================================

	class GameFrameTimer
	{
		public:

			//=========================================================================================================
			// Enums
			//=========================================================================================================

				/// These are the action the game should take after timing has been updated

				enum ActionFlags
				{
					/// Do nothing for this iteration of the game loop

					ACTION_IDLE = 0x00,

					/// Just do an update

					ACTION_UPDATE = 0x01,

					/// Just do a render

					ACTION_RENDER = 0x02,

					/// Do some network updates

					ACTION_NETWORK = 0x04
				};

			//=========================================================================================================
			// Constants
			//=========================================================================================================
								
				/// Target refresh interval in between game ticks - in seconds.
				
				static const float FRAME_INTERVAL;
				
				/// Target refresh interval in between game ticks - in milliseconds
				
				static const unsigned FRAME_INTERVAL_MS;
				
				/// Maximum number of frames that we can try to make up from frame skipping if we are behind real time
				
				static const unsigned MAX_CATCHUP_FRAMES;
			
			//=========================================================================================================
			// Functions
			//=========================================================================================================
		
				/// Creates the timer

				GameFrameTimer();

				/// Resets the timer: forcing the next update to be both a render and update

				void Reset();

				//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
				/// Discounts all time passed between the last call to DoUpdate() or object creation. This time is 
				/// regarded as not having passed.
				//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

				void DiscountTimeDelta();

				/// Does an update of the timer and returns what action the game should take

				ActionFlags Update();

		private:

			//=========================================================================================================
			// Variables
			//=========================================================================================================
			
				/// Time since the last update
				
				float m_time_since_update;
				
				/// Was the last frame rendered ? 
				
				bool m_rendered_last_frame;
	};
};

#endif
