Advertisement

timer problem

Started by February 23, 2006 09:18 PM
3 comments, last by Mawww 18 years, 9 months ago
Ok I finally started really debugging this strange problem: aprox once a second, my fps drops from 300 to 10 for a few frames. I debugged a lot, (un)commenting quite a lot of code in order to find that my timer seems to return an incorrect time. So in fact, my fps is constant, its just my timer wich screw up. here's the timer code :

mwTimerLinux::mwTimerLinux ()
{
	gettimeofday (&m_firstTime, 0);
	m_lastTime = m_firstTime;
}

real mwTimerLinux::getInterval ()
{
	static timeval currentTime;
	gettimeofday (&currentTime, 0);

	real interval = (real)(currentTime.tv_sec - m_lastTime.tv_sec) 
		+ .000001f*(currentTime.tv_usec - m_lastTime.tv_usec);

	m_lastTime = currentTime;

	return interval;
}
any idea of wy this code does not works well ?
Tchou kanaky ! tchou !
The code itself looks fine. Why don't you show me the function where you implement it?
We''re sorry, but you don''t have the clearance to read this post. Please exit your browser at this time. (Code 23)
Advertisement
void mwSystem::execute (mwCam* camera, mwObj* world){    MW_FUNC_BEGIN;    bool run = true;    real interval;    setCamera (camera);    setWorld (world);    g_renderer->activateShaderProgram (NULL);    g_screen->linkToRenderer();    m_timer->getInterval();    g_log->write ("entering the main loop now");	while (run)	{		interval = m_timer->getInterval ()*m_timeMul;					// call all registred callbacks		for (std::list<mwTask*>::const_iterator iter = m_taskList.begin();			 iter != m_taskList.end(); ++iter)		{			if (!(*iter)->update (interval)) 				run = false;		}	        // This could also be treated as a task, but i'm not coding an operating        // system, i'm coding a game engine...		executeCycle (interval);	}    MW_FUNC_END_SUCCESS;}void mwSystem::executeCycle (real interval){	m_world->update(interval);	g_renderer->clear();	m_camera->activate();	g_renderer->updateLighting();	m_world->draw(MW_PERSPECTIVE);	g_renderer->begin2DMode();	m_world->draw(MW_ORTHO);	g_renderer->end2DMode();	g_screen->swapBuffers();    g_renderer->reportErrors ();}


here's my main game loop. I tried all sorts of commenting in executeCycle and execute, and what I found is that my timer seems to return invalid interval some times. if I bypass my timer (returning 0.001 at each call for exemple) the game runs smooth, so I guess my framerate is in fact pretty constant. by the way if someone knows a fraps like tool for linux so I can have my real fps in overlay (not the one my timer gives me) that could be great.

thanks in advance
Tchou kanaky ! tchou !
It's probably a math error from unexpected small numbers. Try requiring a minimum value for interval before updating your state.

Incidentally, what is m_timeMul?
We''re sorry, but you don''t have the clearance to read this post. Please exit your browser at this time. (Code 23)
ok I will have a look at that, m_timeMul is the time multiplier, so I can do slow motions or fast motions or anything like that. I'll verify if m_timeMul does not change... thanks
Tchou kanaky ! tchou !

This topic is closed to new replies.

Advertisement