11-15-2012 04:07 AM
I'm not sure but I have the notion that under the simulator for BB10 the clock() function returns a value in host's resolution timer and not the correct one
And since
#define CLOCKS_PER_SEC 1000000
when converting the clock to seconds it will have wrong results at least under windows. Because under Windows it's
#define CLOCKS_PER_SEC 1000
I want to keep constant 60 FPS and I have a code like:
const int FRAMES_PER_SECOND = 60;
const int SKIP_TICKS = 1000 / FRAMES_PER_SECOND;
int GetTickCount(void)
{
return MathUtils::dti( (double)clock() / CLOCKS_PER_SEC );
}
and game's loop is:
int t_prev = GetTickCount();
mbGameRunning = true;
while ( mbGameRunning )
{
int t = GetTickCount();
if ( (t - t_prev) > SKIP_TICKS )
tick();
draw();
eventloop();
t_prev = t;
}
Am I missing something?
Solved! Go to Solution.
11-15-2012 09:40 AM
The clock() function only increments while your program is being handled by the OS. When the OS switches to another task, your program's 'clock' stops running.
You have to use:
clock_gettime(), which is the system clock that always runs.
11-15-2012 10:31 PM
also, please don't code your loop this way if you can avoid it.
it will spend most of its time running-ready most likely, unless your event-handling function happens to block for a suitable amount of time. this will chew through battery unnecessarily, as it's more or less the same as writing a while(1) {} loop.
a better solution would be to use a periodic timer set to expire at a rate of 60Hz and block periodically waiting for that event.
the bps event handler allows you to specify a timeout, so that's the perfect place to block waiting for input, etc. but also be guaranteed to wake up at least once every 1/60th of a second.
Cheers,
Sean
11-16-2012 05:50 AM
As I'm creating a game I want to be able to control the FPS and not rely on the system when it will call me. From my experience from other platforms this kind of loop is the best way and you could also take into account the time difference between rendering frames to make motion smoother.
I've decided to use the clock_gettime