Advertisement

im having weird problems with timers

Started by January 16, 2003 02:11 PM
4 comments, last by deal 21 years, 10 months ago
i need a timer ro use in my first game, so i can trigger events at regular intervals, i googled time.h and wrote this little program...
      
#include <iostream>
using namespace std;

#include <time.h>

int main ()
{
	int seconds =5; 
	clock_t end = clock()+(seconds*CLK_TCK);

	while(1)
	{
		if (end < clock()) // seems like this statement is evaluating to true everytime ? why

		{
			end = clock()+(seconds*CLK_TCK);
			cout << "around 5 seconds passes\n";
		}
	}
	return 0;
}

   

//so i tried something else


      
#include <iostream>
using namespace std;

#include <time.h>

int main ()
{
	time_t end; 

	end = (time(NULL)+5);

	while(1)
	{
		if(end > time(NULL)) // again evaluating to true every time

		{
			end = end+5;
			cout << "5 seconds passed\n";
		}
	}
	return 0;
}

      
[edited by - deal on January 16, 2003 3:14:29 PM]
I think this is what you need to do ...
- Set the timeout time to some time in the future by using clock() plus some amount of time.
- Then you run your commands when clock() has gotten to that time.

Here is a simple example ...
void pause(int seconds) {        clock_t runEventTime = clock() + seconds * CLK_TCK;        while (clock() < runEventTime);           // Loops this till the clock() gets to runEventTime} 


- Go not to the elves for counsel, for they will say both yes and no.

[edited by - WhtRbt on January 16, 2003 3:29:31 PM]
- Go not to the elves for counsel, for they will say both yes and no.
Advertisement
i dont think you understand what im trying to do...probably because my explaination wasnt very good

i want to be able to call a function, from inside my gameloop, if it has not been called for a period of time. i dont want to add a few seconds pause into my gameloop i want to be able to check if a period of time has passed NOT wait until the time has passed.

im just using a console app to test the timer before i implement it into my game.
i''ve copied your first example program in my compiler and it worked. I don''t know what''s the problem, but look your settings.
now that is weird, it now works

thanx guys. very strange.
you should always initialize things to 0 before you start using them... you may get garbage values, which explains the oddities

Sabonis
"Only two things are infinite, the universe and human stupidity, and I'm not sure about the former." - Albert Einstein (1879-1955) That is so very true...

This topic is closed to new replies.

Advertisement