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

timing problems

Started by cptrnet Sep 20, 2005 at 10:43 AM 5 replies 450+ views
Original Post
cptrnet
cptrnet
Hi, I am having some timings problems, I am using c++ and gdi. What I want to do is every 30 seconds have a powerup come up. Then when you use the powerup, then have it active for a certain amount of time. I don't know too much about timing, I've looked around google and saw some High Performance Timers, but I don't know how to use them. I am currently using the timeGetTime() function and then comparing start and end times. But I guess the way I was doing it is really messed up because its giving me weird numbers, now I put it in a thread and its doing the same thing. Here is my timer class. It inherits from my thread class that works fine. The timer_func() is what it call once it hits the interval. It seems to call the function even without hitting the interval.

UTimer::UTimer(void)
{
	timer_func = def_timer_func;
	m_bIsRunning = false;
	interval = 0;
}

void UTimer::OnStart(void)
{
	m_TempTimeStart = m_TimeStart = m_TimeEnd = timeGetTime();
	m_Time			= 0;
	m_bIsRunning    = true;
}

void UTimer::OnFinish(void)
{
	m_bIsRunning = false;
}

DWORD UTimer::Run(LPVOID)
{
	while(m_bIsRunning)
	{
		m_TimeEnd = timeGetTime();
		m_Time += m_TimeEnd - m_TimeStart;
		m_TempInterval += m_Time;
		m_TimeStart = m_TimeEnd;    
		if(m_TempInterval >= interval)
		{
			m_TempInterval = 0;
			timer_func();
		}
	}
	return GetLastError();
}

double UTimer::GetTime(void)
{
	return m_Time;
}

If you insist on saying "DUH", try to make a post that makes a little more sense
cptrnet
cptrnet
Does anyone know what I'm talking about? Maybe I don't even understand the problem enough to give a better description.
If you insist on saying "DUH", try to make a post that makes a little more sense
perfectly_dark
perfectly_dark
DWORD UTimer::Run(LPVOID){	while(m_bIsRunning)	{		m_TimeEnd = timeGetTime();		m_Time += m_TimeEnd - m_TimeStart;		m_TempInterval += m_Time;		m_TimeStart = m_TimeEnd;    		if(m_TempInterval >= interval)		{			m_TempInterval = 0;			timer_func();		}	}	return GetLastError();}


I think you're while(m_bIsRunning) should be an if(m_bIsRunning), since the while loop at this point is infinite (unless timer_func() breaks out of it, not sure how you're doing it). You also have alot of variables...here's what I'd do

StartTimer(int interval){   timeStart = timeGetTime();   time = 0;   m_interval = interval}Run(){    time += timeGetTime() - timeStart;    timeStart = timeGetTime();    if(time >=  m_interval)        {             do_whatever();             time = 0;        }}


Excuse the poor code and inconsistency but I think it gets the general idea across. Simply call run every frame, it adds to the current amount of time, and if time becomes greater than or equal to your interval, execute the function and reset the timer.
cptrnet
cptrnet
My class is a thread, Run() is executed when the thread runs. There are two things happening in the run function, m_bIsRunning gets set to false when the thread exits. I dont know if thats the right thing to do. m_Time is the time since the thread started, m_TimeInterval is the temporary time to see if its over the interval. Maybe Im making too much of this, I'll try to simplify like your post perfectly_dark. Thanks.
If you insist on saying "DUH", try to make a post that makes a little more sense
fearyourself
fearyourself
A better solution is to have the thread go to sleep and wake up to check what's going on...
For example,


//Time set to 30 at the beginning
Run()
{
while(m_bIsRunning)
{
Sleep(time);
//Create object
createobject();
do
{
Sleep(1); //wake up in a second
}while(Object Still Around);
}
}

It's better than you're solution CPU wise...

cptrnet
cptrnet
The thing with that is, that it sleeps for 30 seconds, and it doesnt tell me how much time is left. I need to display on the screen how much time is left for the powerup, if it sleeps for that long it will never update how much time has elapsed.
If you insist on saying "DUH", try to make a post that makes a little more sense
APCJim
APCJim
Take this with a grain of salt as I'm just starting to look at threads.

I've had to do some timing stuff before, before I knew what threads were, and one major trick to these is as follows:

Don't set your execution interval from finish to start, set it from start to start.

For example, if you want to run a loop every 10 second have something kick it off every 10 seconds external to the actual loop. If you use a timer to start a loop, execute the loop and then restart the timer when you exit the loop then you will add the execution time to the full cycle time every time the loop executes. This time shift will become more serious the longer your program executes.

If you do the above, from start to start, you also have to watch your CPU loading and, within the loop itself, have a check on the time since last execution to make sure the previous execution cycle is complete. If you see that your CPU usage spikes to or above 50% on a cyclic pattern then you need to distribute your loops more evenly. At about 50% CPU loading just adding one more loop could shut you down. If your CPU usage is pretty even at 50% you're done. Adding even a little more could result in the loop getting called before the prior execution of the loop is finished.

I Hope this helped.

Topic Locked

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

Sign in to reply to this topic.