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

Posts: 27
Registered: ‎08-06-2012
My Device: PlayBook
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 )





     t_prev = t;


Am I missing something?

Trusted Contributor
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.

BlackBerry Development Advisor
Posts: 668
Registered: ‎11-29-2011
My Device: developer

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.




Posts: 27
Registered: ‎08-06-2012
My Device: PlayBook

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