Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Contributor
hamlatzis
Posts: 27
Registered: ‎08-06-2012
My Device: PlayBook
My Carrier: Cosmote
Accepted Solution

BB10 simulator: Question regarding clock function

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?

Please use plain text.
Trusted Contributor
taylor-tomt
Posts: 231
Registered: ‎11-19-2010
My Device: Z10, Playbook

Re: BB10 simulator: Question regarding clock function

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.

Please use plain text.
BlackBerry Development Advisor
smcveigh
Posts: 668
Registered: ‎11-29-2011
My Device: developer
My Carrier: other

Re: BB10 simulator: Question regarding clock function

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

Please use plain text.
Contributor
hamlatzis
Posts: 27
Registered: ‎08-06-2012
My Device: PlayBook
My Carrier: Cosmote

Re: BB10 simulator: Question regarding clock function

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

Please use plain text.