Advertisement

timers

Started by May 29, 2001 10:45 PM
3 comments, last by sysFA1L 23 years, 8 months ago
how in c++ would i make a function that incremented a value by 1 at a set amount of seconds... say .001 seconds it increments x by 1 i dont know of any functions that do this, and im just learning to program.... any help is appreciated
-sysFA1L
if you''re in dos you would have to use the timer interrupt istself
i don''t come up with any other accurate ideas
and if you''re in windows
you can use SetTimer
or multimedia timer which are better

Arkon
[QSoft Systems]
Advertisement
Timing is a piece of cake in C++. Here''s an example:

// Example of timing using Win32 APIclass Timer{public:	Timer(DWORD Rate, void (* OnTime)())	~Timer();	bool Update();private:	DWORD Rate;	void (* OnTimePtr)();	};Timer::Timer(DWORD Rate, void (* OnTime)()) : Rate(Rate){	OnTimePtr = OnTime;	}Timer::~Timer(){}bool Timer::Update(){	static DWORD time = timeGetTime();	if (timeGetTime() - time >= Rate)	{		time = timeGetTime();		if (OnTimePtr)			OnTimePtr();		return true;	}	return false;}// OnTimer function void IcrementOnOneSecond(){	++somevar;}// Then in a class or somewhere:Timer timer(1000, IncrementOnOneSecond); // Create a new timer for 1 second// Then in the game/app loop or update area:timer.Update(); // Which checks time state and calls our init function 
Oh and BTW, this was a pretty rough timer I just wrote up for this reply, if something doesn''t work or it''s not efficient enough for your work, tell me.
oh i forgot to mention that way....

Arkon
[QSoft Systems]

This topic is closed to new replies.

Advertisement