Page 1 of 1

How can I sync the game Logic at 30 times per sec? +details

Posted: Mon Mar 05, 2007 9:30 pm
by CuecaX
I saw this "howto" yesterday:
http://irrlicht.sourceforge.net/phpBB2/ ... .php?t=723

but it s not what i want :(
i dont want to multiply anything, i prefer use fixed values :)


> i want with something like a timer locked at 30 or 60 ticks per second (I cant get the irrlicht timer working correctly :( )

> or a allegro´s* like wait vsync() function to sync with the screen´s refresh rate.

> or at least a non irrlicht related timer :P



*I tested the allegro´s vsync() with irrlicht and it works, but I can´t use allegro beacause i want to port my game for PSP :)

thanks in advance :)

Posted: Mon Mar 05, 2007 11:16 pm
by Baiame
But with what you want to do, you can never, ever guarantee a time-consistent simulation. What exactly is it about delta time systems that bothers you?

Posted: Mon Mar 05, 2007 11:44 pm
by omaremad
Try using doubles to store the values you get off the irrlicht timers, other data types didnt work for me :?

Posted: Tue Mar 06, 2007 12:55 pm
by Strong99
30 ticks is 1000 miliseconds / 30 =tick

so you get something like this

Code: Select all

time = Device->getTimer()->getRealTimer()
if (time - lasttime >= tick)
{
  // do something
  lasttime = time;
}else{
   Sleep ( (1000 /30 )(time - lasttime - 1))
} 

but with the sleep some bit better :P

Posted: Tue Mar 06, 2007 3:22 pm
by rogerborg
Indeed, Irrlicht could do with a ITimer::sleep() method. The following code is Windows specific, and is perhaps a little more paranoid about timer granularity than you probably need. Note that it skips rendering if the frame rate lags behind the required frequency, and there's no check for the pathological case where it can't catch up. You'll need to include winbase.h (just include windows.h) and link against winmm.lib for timeGetDevCaps().

Code: Select all

	ITimer * timer = device->getTimer();
	u32 lastFrame = timer->getTime();
	enum { FRAME_TIME = 1000 / 30 };
	u32 nextFrameTime = lastFrame + FRAME_TIME;
	TIMECAPS timeCaps;
	(void)timeGetDevCaps(&timeCaps, sizeof(timeCaps));

	while(device->run())
	{
		u32 now = timer->getTime();

		if(now >= nextFrameTime)
		{
			doGameLogic();	// always do your game logic
			nextFrameTime += FRAME_TIME;

			if(now < nextFrameTime)
				doRendering(); // Only render if we're not lagging
		}
		else
		{
			if(nextFrameTime - now > timeCaps.wPeriodMin)
				Sleep(nextFrameTime - now);
			else
				Sleep(0);
		}
	}

subject

Posted: Tue Mar 06, 2007 8:05 pm
by CuecaX
thanks for help, I will try this sleep() way...

rogerborg: thanks, but I want a portable code :)
Baiame wrote:But with what you want to do, you can never, ever guarantee a time-consistent simulation. What exactly is it about delta time systems that bothers you?
i dont want my game running well using all processing power at 200fps :P
and i think that using fixed values is simple and easier.

Posted: Tue Mar 06, 2007 8:18 pm
by roxaz
if you want portable code so why dont you write some? :?

Posted: Tue Mar 06, 2007 9:09 pm
by rogerborg
Indeed. If you do write portable code (i.e. a cross platform sleep() method) then could you consider adding it to I/CTimer and submitting a patch please.

Posted: Tue Mar 06, 2007 10:27 pm
by hybrid
We already have a cross-platform sleep method in the upcoming next release of Irrlicht :) You have the option to halt the timers as well.

Re: subject

Posted: Wed Mar 07, 2007 3:51 am
by Baiame
CuecaX wrote:i dont want my game running well using all processing power at 200fps :P
and i think that using fixed values is simple and easier.
Oh, sorry, I misunderstood your first post. Yeah, that's a good idea.

Posted: Wed Mar 07, 2007 12:36 pm
by CuecaX
roxaz wrote:if you want portable code so why dont you write some? :?
because i´m entering in a competition and dont have much time... i need to finish the game until the end of the month :(
rogerborg wrote:Indeed. If you do write portable code (i.e. a cross platform sleep() method) then could you consider adding it to I/CTimer and submitting a patch please.
if i did something helpful of course i will release the code ^^
hybrid wrote:We already have a cross-platform sleep method in the upcoming next release of Irrlicht :) You have the option to halt the timers as well.
:) :) :)