Skip to main content
GameDev.net gamedev.net
🔒 Locked

Timing?

Started by Zmurf Aug 22, 2005 at 10:14 PM 8 replies 850+ views
Original Post
Zmurf
Zmurf
I'm making a Bomberan clone in C++/OpenGL. So far so good. But now when my bomberman drops a bomb i want to make a timer function to make the bomb go off after a specified time, i've looked into using but I can't figure out how to use it. Any help?
If it's possible for us to conceive it, than it must be possible.
Drazgal
Drazgal
I don't want to sound unhelpful but this topic has been done to death on these forums a quick search of timer will give you everything from ready made timer classes to countless forum questions on how to implement every different type avaliable to you.

heres a very simple example for you

DWORD Timer; // Global, class member or what ever

When you drop a bomb use: Timer = GetTickCount();

Then to find out how much time has passed

Time = GetTickCount() - Timer;

Will place the time in milliseconds into variable time. Its not great or even accurate but is a quick dirty way to do it. Use the search to find something a bit better.
lubby
lubby
Best option is to make the bomb update itself every so often (every frame/ every second, however you feel most suitable for the precision you require.)

Your bomb has an update function;
Your bomb knows the time when it was dropped
Your bomb has a delay
Your bomb knows or can poll the current time

Class Bomb{public:   void DropBomb();   void Update();   void Detonate();private:   Time m_DroppedTime;   float m_Delay;   };// drop the bombvoid Bomb::DropBomb(float delay){  m_DroppedTime = Time::GetCurrentTime();  m_Delay = delay;}// update the bomb every n framesvoid Bomb::Update(){  if (Time::GetCurrentTime() - m_DroppedTime > m_Delay)    Detonate();}void Bomb::Detonate(){  // do some funky stuff here}


Cacks
Cacks
If your trying to use the old c header for timing, take a look at this site, it might help you for your timer

http://www.cplusplus.com/ref/ctime/clock.html

Hope this helps you!
Reject the basic asumption of civialisation especially the importance of material possessions
Zmurf
Zmurf
thanks, i'll try that
If it's possible for us to conceive it, than it must be possible.
simon10k
simon10k
Here is a timer class I made...

class CTimer {	int counter;public:	CTimer(): counter(0) {}	bool Timer(int ms)	{	        int tick_count = (int)GetTickCount();		if((tick_count - counter) > ms)		{					counter = tick_count;			return true;		}		return false;	}};
-----------------------------Language: C++API: Win32, DirectXCompiler: VC++ 2003
Skeleton_V@T
Skeleton_V@T
Why aren't you thinkin' of using a counter variables (I often name it as 'unsigned char m_ucPowerLevel') to make the bomb explode when its power level reaches the limit ?
This way is very fast 'cause your object doesn't have to call GetTickCount () or somethin' like that ev'ry frame. And... It is totally accurate, think of your player has a low-speed computer. If you use any of timing function, he/she will notice that the bomb explode slower/faster.
I always use this way because it make the object more real (your bomb need time to burn itself, but time in such computer game varies between computers).
Isn't that right ?
--> The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones <--
perfectly_dark
perfectly_dark
Quote:
Original post by Skeleton_V@T
Why aren't you thinkin' of using a counter variables (I often name it as 'unsigned char m_ucPowerLevel') to make the bomb explode when its power level reaches the limit ?
This way is very fast 'cause your object doesn't have to call GetTickCount () or somethin' like that ev'ry frame. And... It is totally accurate, think of your player has a low-speed computer. If you use any of timing function, he/she will notice that the bomb explode slower/faster.
I always use this way because it make the object more real (your bomb need time to burn itself, but time in such computer game varies between computers).
Isn't that right ?


If you update the counter function every frame then it becomes dependant on computer speed. Using the timer functions (like GetTickCount()) will be independant of computer speed since it's based on the elapsed time. GetTickCount is fairly accurate to several ms if I'm not wrong so it should be sufficient for the OPs task.
blizzard999
blizzard999
However I think you can spend 0.02 milliseconds every frame (cost: +0.2% @ 100fps ) and get the time. [smile]
simon10k
simon10k
Your game will run at different speeds on different computers though.
-----------------------------Language: C++API: Win32, DirectXCompiler: VC++ 2003

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.