update-Event with interval

As I can see ofBaseApp has some standard methods like setup(), update() etc…
I often use the update(). But I think there is one problem. If you want to count something or you have some animations or other stuff which depends on the time the update() method without a interval between the last and the new call doesn’t work well. The reason for this is that the update() call depends on the FPS. But I think there are a lot of stuff which have to depend on the time.

I know that you can compute the interval with:

interval = ofGetElapsedTimeMillis() - prevMillis;  
prevMillis = ofGetElapsedTimeMillis();   

But this has to be done in every own update() method.

In my opinion it would be nice if this calculations will be done in the core and ofBaseApp gets the new method update(double interval) which delivers the time between the last and the new update call.

What do you think?


glut has a timer callback you can use for this, only don’t set a fixed framerate as that will put your process to sleep in between calls to update and will trash all the timer thing (you can already use vertical sync)

it will only work with functions but you can call any method in your object from there:


where 1000 is 1 second between calls, function is the function that you want to be called and needs to have the following signature:

void function(void * data)  

and data is anything you want to be passed to your function, for example the instance of the object you want to call from inside.

haven’t thought of that but yes, it can be interesting to have some kind of timer event, for 007 perhaps :slight_smile:

1 Like

Thanks for your answer arturo.
I know the glutTimerFunc, but as you mentioned it only works with functions. And in my opinion, if you have a lot of different objects its really bad to use callback functions to call all the object methods.

What I have done is to extend the ofAppGlutWindow::idle_cb(void), because it computes exactly what I want (the diffMillis).

void ofAppGlutWindow::idle_cb(void) {  
  static ofEventArgs voidEventArgs;  
  static ofUpdateEventArgs updateEventArgs;  // NEW  
  diffMillis = ofGetElapsedTimeMillis() - prevMillis;  // NEW  
  if (nFrameCount != 0 && bFrameRateSet == true){  
    // diffMillis = ofGetElapsedTimeMillis() - prevMillis; //OLD  
    if (diffMillis > millisForFrame){  
      ; // we do nothing, we are already slower than target frame  
    } else {  
      int waitMillis = millisForFrame - diffMillis;  
      #ifdef TARGET_WIN32  
        usleep(waitMillis * 1000);  
  prevMillis = ofGetElapsedTimeMillis();  
  updateEventArgs.interval = diffMillis * 0.001; // NEW  
  if(ofAppPtr) ofAppPtr->update();  
  #ifdef OF_USING_POCO  
  ofNotifyEvent( ofEvents.update, voidEventArgs);  
  ofNotifyEvent( ofEvents.updateInterval, updateEventArgs); // NEW  

I know it that its bad to extend the core myself, but I see no other easier way.