Page 1 of 2

Very N00b Question

Posted: Wed Feb 06, 2008 10:13 am
by VintigeUK
Hi how would i go about adding a timer to my game. Id like it to be like mario so its a countdown timer and the remaining time goes on to the score.
Thankyou

Posted: Wed Feb 06, 2008 10:37 am
by FuzzYspo0N
Device->getTimer()->

that returns a bunch of things regarding time. getTime from that gives u a number, divide the number by a thousand and u get seconds.

Im sure u can figure out how to use the seconds the timer has been running... or make a new timer object per application of timer

Posted: Tue Feb 12, 2008 1:28 pm
by declan07
Hi, im very new to irrlicht. Im akso trying to do a timer. is it ok if you post some example code.
Appreciate anyhelp :wink:

Posted: Tue Feb 12, 2008 1:42 pm
by MasterGod
I'm not sure if the tutorials has ITimer class examples but looking through the ITimer class should pretty help you.
Searching the forums will get you code examples.

Posted: Tue Feb 12, 2008 1:48 pm
by declan07
Ive searched the forum and i havent found a countown timer that works for me

Posted: Tue Feb 12, 2008 2:02 pm
by JP
If you actually think about how to do the timer yourself then you should be able to figure out how one would work using the value returned by the ITimer.

The value you get is either the time your system has been running or the time since a specific date many years ago (not sure which the irrlicht timer uses but it doesn't matter for this example).

So you've basically got some random time there, not the real time or anything, but it's all you need.

When you want to start your time you grab the current time from the timer and store it somewhere.

Then, you can compare the current time, each frame, with that time stamp you took and you'll know how many seconds have passed.

Posted: Tue Feb 12, 2008 2:08 pm
by Vsk
This will do what you want.
You can reset, pause the timer. And give you the time bewten resetets or if you pause it give you the add of all the time that has been not paused.
(kinda crhonometer).
How the irrlikcht device is acceded is your choice.

Code: Select all

class MyTimer{

	ITimer* timer;
	unsigned long start;
	unsigned long seconds;
	bool stopped;
	public:
		MyTimer(){
			timer = device->getTimer; //if you want, pass the device to the constructor
			start = timer->getRealTime();
			seconds = 0;
                        stopped = true;
		};
		void reset(){
			seconds = 0;
			start = timer->getRealTime();
			stopped = true;
		};
		void pause(){
			if(!stopped){
				seconds = timer->getRealTime() - start + seconds;	
				stopped = true;
			}
		}
		void play(){
			if(stopped){
				start = timer->getRealTime();
				stopped = false;
			};
		}
		unsigned long getTime(){
			if (stopped)
				return seconds;				
			return seconds+ timer->getRealTime()- start;
		}

};
You also should clean the code ;).
And divide betwen 1000 to find the seconds.

Posted: Tue Feb 12, 2008 2:14 pm
by Vsk
By the way, this not count down. That added will be your task :). Is prety easy.

Posted: Tue Feb 12, 2008 3:34 pm
by declan07
Thankyou for the code, ive got two errors though.

Error 1 error C3867: 'irr::IrrlichtDevice::getTimer': function call missing argument list; use '&irr::IrrlichtDevice::getTimer' to create a pointer to member

Error 2 error C2440: '=' : cannot convert from 'irr::ITimer *(__thiscall irr::IrrlichtDevice::* )(void)' to 'irr::ITimer *'

Posted: Tue Feb 12, 2008 4:22 pm
by JP
You can't see anything wrong with the line that the error points to?

There should be brackets on getTimer...

Posted: Tue Feb 12, 2008 4:46 pm
by declan07
Cheers i noticed that when i had a proper look through I on have any errors but the timer is not outputting on the screen

Posted: Tue Feb 12, 2008 4:48 pm
by JP
Show us your code for rendering the timer and we might be able to see a problem with it.

Posted: Tue Feb 12, 2008 4:51 pm
by Vsk
By the way. I think anyway what he want can be done in a accuracy way in irlicht.
Because what he would want is a function callbacked for dx time.
I used this in allegro library(2d). You could attach some function callback to a timer.Then that function wuoul be invocated each dx time.

How can be this resolved?.
If I undertand correcly( If I remember correctly :?) in allegro the timer gerated a interruption that were handled for the function that you use as callback. But realy I don't know how to to this in irrlicht.
This nooby question has grow up :P.
Any ideas?

Posted: Tue Feb 12, 2008 5:04 pm
by JP
Well you could just put a check into an update function of the timer so if it's time to call the callback it's called. So all you'd have to do is add into your while (device->run()) loop the line 'timer->update();'

And of course declare the function ;)

Posted: Tue Feb 12, 2008 5:13 pm
by declan07
What would be the rendering code?