Timer Class

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
monkeycracks
Posts: 1029
Joined: Thu Apr 06, 2006 12:45 am
Location: Tennesee, USA
Contact:

Timer Class

Post by monkeycracks »

I don't know if this will be useful to anyone, or if it's even optimal, but here goes :

(Main.h is just the Irrlicht includes)

Timer.h

Code: Select all

#ifndef TIMER_H_INCLUDED_
#define TIMER_H_INCLUDED_

#include "Main.h"

class Timer
{
public:
	Timer();
	~Timer();

	s32 getSecondsElapsed();
	f32 getMinutesElapsed();
	f32 getHoursElapsed();
	f32 getDaysElapsed();
	f32 getDeltaTime();

	void init(ITimer* t);
	void update();

private:
	u32 NewTime;
	u32 OldTime;
	u32 Counter;

	s32 Seconds;

	f32 Minutes;
	f32 Hours;
	f32 Days;
	f32 Delta;
   f32 Elapsed;


	ITimer* timer;
};

#endif
Timer.cpp

Code: Select all

Timer::Timer() : Seconds(0), Minutes(0), Hours(0), Days(0), Delta(0), NewTime(0), OldTime(0), Elapsed(0), Counter(0)
{
}

Timer::~Timer()
{
}

void Timer::init(ITimer* t)
{
	timer = t;
	OldTime = timer->getRealTime();
}

void Timer::update()
{
	NewTime = timer->getRealTime();
	Elapsed = (NewTime - OldTime);
	OldTime = NewTime;

	Counter += Elapsed;
	if(Counter >= 1000)
	{
		Counter -= 1000;
		Seconds+=1;
	} 
}

s32 Timer::getSecondsElapsed()
{
	return Seconds;
}

f32 Timer::getMinutesElapsed()
{
	return Seconds/60.0f;
}

f32 Timer::getHoursElapsed()
{
	return (Seconds/60.0f)/60.0f;
}

f32 Timer::getDaysElapsed()
{
	return ((Seconds/60.0f)/60.0f)/24.0f;
}

f32 Timer::getDeltaTime()
{
	return Elapsed/1000.0f;
}
If anyone has any optimization tips feel free to post them, I just coded this in 10-15 minutes :P
Last edited by monkeycracks on Tue Apr 08, 2008 5:39 pm, edited 3 times in total.
FuzzYspo0N
Posts: 914
Joined: Fri Aug 03, 2007 12:43 pm
Location: South Africa
Contact:

Post by FuzzYspo0N »

DOH, i just pasted in ur other thread. heres what i said, it seems better imo (comparnigly im my brain and iv just woken up)

http://irrlicht.sourceforge.net/phpBB2/ ... 819#153819
monkeycracks
Posts: 1029
Joined: Thu Apr 06, 2006 12:45 am
Location: Tennesee, USA
Contact:

Post by monkeycracks »

Ah I missed your post. I think it's being done the same way and just has different variable names. Also thanks to Ion Dude and Fuzzy for the Timer helps :D
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

Post by MasterGod »

Maybe propose a patch for the Irrlicht timer that extends it with those features.
(Who know, I might even do it myself cause this sounds pretty useful though I have very little free time)

P.S
I've also posted in the other thread, check it out.
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

Post by MasterGod »

I believe it should be if(Counter >= 1000) .
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
monkeycracks
Posts: 1029
Joined: Thu Apr 06, 2006 12:45 am
Location: Tennesee, USA
Contact:

Post by monkeycracks »

That would give the extra millisecond accuracy. Fixed.
Dorth
Posts: 931
Joined: Sat May 26, 2007 11:03 pm

Post by Dorth »

I'd rather not have it in Irr, if nothing else than the overhead it produces. What it could do is keep track of only the millisecond and calculate on the spot the other ones, since there is no gain having them each update.
monkeycracks
Posts: 1029
Joined: Thu Apr 06, 2006 12:45 am
Location: Tennesee, USA
Contact:

Post by monkeycracks »

I didn't even consider that before.
Fixed according to Dorth's post.
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

Post by MasterGod »

I didn't mean that exact code.
I meant extending the ITimer class a bit further with the required thinking and consideration.

I just thought that monkeycracks might find that fun and if he's already into working on timer classes he can do that too, it's just a suggestion.

P.S
I find it fun but I'm under pressure cause I need to finish my project by next week and I've got a lot of documentation to do so I can't do it now.
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
Dorth
Posts: 931
Joined: Sat May 26, 2007 11:03 pm

Post by Dorth »

Much better ^^
But I'm sure it could be simplified further ^^ Nevertheless, it's starting to get cleaned up :)
}{ermanssoN
Posts: 37
Joined: Mon May 26, 2008 7:39 pm

Post by }{ermanssoN »

I used the code that ypu posten, and It didn't quite work.


If i changed from device->GetRealTime() to just getTime() the second counter started to work, but deltaTime still fails.


Has anyone else had the same problem?

now i cal my update timer once per frame, and deltatime just stays 0
}{ermanssoN
Posts: 37
Joined: Mon May 26, 2008 7:39 pm

Post by }{ermanssoN »

Well after looking around for other examples I made a mix that seems to work quite well.

If you create a timer in main application and update it once per frame you will get a correct delta time, seconds and hours:


Code: Select all

#ifndef TIMER_H_
#define TIMER_H

#include "globals.h"

class Timer
{
public:
   Timer();
   ~Timer();

   s32 GetSecondsElapsed		( void );
   s32 GetMinutesElapsed		( void );
   s32 GetHoursElapsed			( void );
   f32 GetDeltaTime				( void );

   void Initialize				( void );
   void Update					( void );

private:

   u32							m_NewTime;
   u32							m_OldTime;
   u32							m_RealNewTime;
   u32							m_RealOldTime;
   f32							m_DeltaTime;
   
   u32							m_DeltaCount;
   u32							m_SecondCount;
   u32							m_Seconds;
   f32							m_TimeElapsed;
};

#endif

Code: Select all

#ifndef TIMER_CPP
#define TIMER_CPP
#include "globals.h"

Timer::Timer( void)
{
	
	
	m_NewTime		= 0;
	m_OldTime		= 0;
	m_RealNewTime		= 0;
	m_RealOldTime		= 0;
	m_DeltaTime		= 0.0f;
	m_DeltaCount	= 0;
	m_SecondCount	= 0;
	m_TimeElapsed	= 0.0f;
	m_Seconds		= 0;

}	

Timer::~Timer()
{
}


void 
Timer::Update()
{
	ITimer* pTimer = g_pApp->GetIrrDevice()->getTimer(); 

	//Calulate DeltaTime
	if( pTimer->getTime() > m_NewTime + 10)
	{
		m_OldTime = m_NewTime;
		m_NewTime = pTimer->getTime();

		m_DeltaTime = (f32)(m_NewTime - m_OldTime) / m_DeltaCount;
		m_DeltaCount = 1;
	}
	else m_DeltaCount++;

	//calculate seconds
	m_RealNewTime = pTimer->getTime();
	m_TimeElapsed = (m_RealNewTime - m_RealOldTime);
	m_RealOldTime = m_RealNewTime;

	m_SecondCount += m_TimeElapsed;
	if( m_SecondCount >= 1000 )
	{
		m_SecondCount -= 1000;
		m_Seconds+=1;
	} 
	  
}

f32 
Timer::GetDeltaTime()
{
	return m_DeltaTime / 1000.0f;
}
s32
Timer::GetSecondsElapsed()
{
	return m_Seconds;
}
s32
Timer::GetMinutesElapsed( void )
{
	return m_Seconds / 60;
}
s32
Timer::GetHoursElapsed( void )
{
	return ( m_Seconds / 60 ) / 60;
}
#endif //TIMER_CPP
Post Reply