Timer class

hi:

has anybody used a C++ timer class in OF? i’m looking for one, preferably cross-platform (OSX/Windows)

tia,

timer - you mean to do a callback at a certain time regularly?

if so, you might want to take a look at glutTimerFunc(), which can be setup to call your function at a certain interval.

  • zach

thanks, zach! that’s exactly i was looking for

Sorry, could you post an example about the way you implemented the timer or glutTimerFunc()?
thanks,

ivo

I’d love an example on this as well. I need to have my function repeat and be able to change the designated time of the function. So far I have :

  
  
glutTimerFunc(1000, callbackFunction, 1);  
  
void callbackFunction(int status)  
{  
...  
  
    if(status == 1)  //repeat  
        callbackFunction(1);  
}  

You need to do call the function in itself or the function won’t repeat. Any ideas on how I could change the initial time though? Do I need to stop this function first, then start a new one?

I can’t seem to get something as simple as this to run in OF

testApp.h

  
void callbackFunction(int status) ;  

testApp.h

  
void testApp::setup(){  
  
	glutTimerFunc(1000, callbackFunction,1);  
}  
  
void testApp::callbackFunction(int status)  
{  
  
  
    if(status == 1)  //repeat  
        callbackFunction(1);  
}  

The errors are:

  
error: argument of type `void (testApp::)(void*)' does not match `void (*)(int)'  

hey progen

the callback is meant to be a simple c function, not a class method, it you transform this:

  
void testApp::callbackFunction(int status)   

into:

  
void callbackFunction(int status)   

it should work without problem.

I am still getting the same error. Should I declare the callbackFunction in testApp.h like this:

  
class testApp : public ofSimpleApp{  
  
	public:  
        void callbackFunction(int status);  
  
}  

no, you should take the function outside the class:

  
  
void callbackFunction(int status);   
  
class testApp : public ofSimpleApp{  
  
  
}  

and then in testApp.cpp:

  
void callbackFunction(int status){  
    if(status == 1)  //repeat  
        glutTimerFunc(1000, callbackFunction,1);   
...  
}  

edit: you should call the glutTimerfuncion inside callbackFunction, not the callback function

btw, when using the timer function be careful with using a fixed frame rate: ofSetFrameRate(…)

that will put your application to sleep in between calls to update and the timer function won’t be called till the app wakes up again.

so if you don’t mind very much about the timer accuracy or your time lapse is really large, like 1sec or something like that, it doesn’t matter very much but if you are concerned about the accuracy of the timer calls don’t use ofSetFrameRate.

great! Thanks for the help arturo…

Could i in essence to it like this?

  
  
void testApp::setup(){  
	  
	glutTimerFunc(100, chafunc,1);  
}  
  
void chafunc(int status){  
    if(status == 1)  
    printf("hello");  
    glutTimerFunc(100, chafunc,1);  
  
}  
  

And how would I stop the timer?

you just need to not call the glutTimerFunc again:

  
  
bool resetTimer;  
void testApp::setup(){  
   resetTimer=true;  
   glutTimerFunc(100, chafunc,1);  
}  
  
void chafunc(int status){  
    if(resetTimer) glutTimerFunc(100, chafunc,1);  
    if(status == 1)  
    printf("hello");  
  
}   
  
void testApp::mousePressed(...){  
    resetTimer=false;  
}  
  

will stop the timer on mousePresed

Hi, there

I have a problem with the glutTimerFunc implementation.
I use the following code:

testApp.cpp

  
  
#include "testApp.h"  
void testApp::timerFunction(int value)  
{  
    cout << "HERE WE ARE" << endl;  
      
    // Redraw the scene  
    //glutPostRedisplay();  
      
    // Reset timer  
    glutTimerFunc(1000, timerFunction, 1);  
}  
  
//--------------------------------------------------------------  
void testApp::setup(){  
    glutTimerFunc(1000, timerFunction, 1);  
}  
  

testApp.h

  
  
class testApp : public ofBaseApp{  
  
	public:  
		void setup();  
		void update();  
		void draw();  
                void timerFunction(int value);  
};  
  

And I am taking the error at glutTimerFunc line (second argument (timerFunction)):
Reference to non-static member function must be called

Any idea why?
Thanks in advance
Aris

I don’t understand how to use GLUT functionality in openFrameworks in general. I have been trying to use anyone’s examples above leveraging glutTimerFunc() and I just get told that glutTimerFunc() is an undeclared identifier.

Do I have to include something in the header of my ofApp.cpp/mm file?

Thanks

@cloud_canvas did you ever figure out how to include GLUT header files in ofApp? I have the same question.

we don’t use glut anymore, windows are now created using glfw. if you need a timer there’s a lot of time functions in OF itself that can help, perhaps open a different thread

1 Like