/*  $Id: frameratecounter.cpp,v 1.2 2003/10/10 23:11:19 sphair Exp $
**
**  ClanLib Game SDK
**  Copyright (C) 2003  The ClanLib Team
**  For a total list of contributers see the file CREDITS.
**
**  This library is free software; you can redistribute it and/or
**  modify it under the terms of the GNU Lesser General Public
**  License as published by the Free Software Foundation; either
**  version 2.1 of the License, or (at your option) any later version.
**
**  This library is distributed in the hope that it will be useful,
**  but WITHOUT ANY WARRANTY; without even the implied warranty of
**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
**  Lesser General Public License for more details.
**
**  You should have received a copy of the GNU Lesser General Public
**  License along with this library; if not, write to the Free Software
**  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
*/

#include "Display/display_precomp.h"
#include "API/Display/frameratecounter.h"
#include "API/Core/System/system.h"
#include "frameratecounter_generic.h"

/////////////////////////////////////////////////////////////////////////////
// CL_FramerateCounter construction:

CL_FramerateCounter::CL_FramerateCounter()
: impl(new CL_FramerateCounter_Generic)
{
	impl->start_time = CL_System::get_time();
	impl->total_time = 0;
	impl->fps_result = 0;
	impl->fps_counter = 0;
}

/////////////////////////////////////////////////////////////////////////////
// CL_FramerateCounter attributes:

int CL_FramerateCounter::get_fps()
{
	return impl->fps_result;
}

/////////////////////////////////////////////////////////////////////////////
// CL_FramerateCounter implementation:

void CL_FramerateCounter::keep_alive()
{
	int cur_time = CL_System::get_time();
	int delta_time = cur_time - impl->start_time;
	impl->start_time = cur_time;

	impl->total_time += delta_time;
	if(impl->total_time >= 1000)	// One second has passed
	{
		impl->fps_result = impl->fps_counter + 1;
		impl->fps_counter = impl->total_time = 0;
	}
	impl->fps_counter++;	// Increase fps
}
