Very N00b Question

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
VintigeUK
Posts: 3
Joined: Thu Nov 08, 2007 9:31 pm

Very N00b Question

Post 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
FuzzYspo0N
Posts: 914
Joined: Fri Aug 03, 2007 12:43 pm
Location: South Africa
Contact:

Post 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
declan07
Posts: 6
Joined: Tue Feb 12, 2008 1:22 pm

Post 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:
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

Post 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.
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
declan07
Posts: 6
Joined: Tue Feb 12, 2008 1:22 pm

Post by declan07 »

Ive searched the forum and i havent found a countown timer that works for me
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post 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.
Image Image Image
Vsk
Posts: 343
Joined: Thu Sep 27, 2007 4:43 pm

Post 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.
Vsk
Posts: 343
Joined: Thu Sep 27, 2007 4:43 pm

Post by Vsk »

By the way, this not count down. That added will be your task :). Is prety easy.
declan07
Posts: 6
Joined: Tue Feb 12, 2008 1:22 pm

Post 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 *'
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

You can't see anything wrong with the line that the error points to?

There should be brackets on getTimer...
Image Image Image
declan07
Posts: 6
Joined: Tue Feb 12, 2008 1:22 pm

Post 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
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

Show us your code for rendering the timer and we might be able to see a problem with it.
Image Image Image
Vsk
Posts: 343
Joined: Thu Sep 27, 2007 4:43 pm

Post 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?
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post 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 ;)
Image Image Image
declan07
Posts: 6
Joined: Tue Feb 12, 2008 1:22 pm

Post by declan07 »

What would be the rendering code?
Post Reply