ofTimer example
ofTimer has been added recently into utils, but I didn't find any exemple. I could suggest a timer example as a pull request, would it be useful ? Is there a typical way to use ofTimer as non blocking timer ? use ofTimer in a ofThread + isTimerAwake() method ? or an event ?
ping @openframeworks/documentation
I'd like to :+1: this request.
Based on the ofTimer document, I made the following. Actually the code was to display a string with a Korean TTF font. Anyway, I hope one can catch the idea of how to use ofTimer if there is still someone who wants to see an example.
Here, Timer is used to increase the counter variable every second. Then, draw() function displays the counter.
H file
#pragma once
#include "ofMain.h"
#include "ofxTrueTypeFontUC.h"
class Scheduler : public ofThread {
public:
unsigned long counter = 0;
Scheduler() {
timer.setPeriodicEvent(1000000000); // 1 second
startThread();
}
private:
ofTimer timer;
void threadedFunction() {
while (isThreadRunning()) {
timer.waitNext();
counter ++;
}
}
};
class ofApp : public ofBaseApp{
public:
void setup();
void update();
void draw();
void keyPressed(int key);
void keyReleased(int key);
void mouseMoved(int x, int y );
void mouseDragged(int x, int y, int button);
void mousePressed(int x, int y, int button);
void mouseReleased(int x, int y, int button);
void mouseEntered(int x, int y);
void mouseExited(int x, int y);
void windowResized(int w, int h);
void dragEvent(ofDragInfo dragInfo);
void gotMessage(ofMessage msg);
bool bFirst;
string typeStr;
ofTrueTypeFont franklinBook14;
ofTrueTypeFont verdana14;
ofTrueTypeFont verdana30;
ofTrueTypeFont franklinBook14A;
ofTrueTypeFont verdana14A;
ofxTrueTypeFontUC tfontuc;
Scheduler mysch;
};
C++ file
#include "ofApp.h"
//--------------------------------------------------------------
void ofApp::setup(){
ofBackground(54, 54, 54, 255);
tfontuc.loadFont("SDMiSaeng.ttf", 64, true, true);
ofTrueTypeFont::setGlobalDpi(72);
verdana14.load("verdana.ttf", 14, true, true);
verdana14.setLineHeight(18.0f);
verdana14.setLetterSpacing(1.037);
bFirst = true;
typeStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ\nabcdefghijklmnopqrstuvwxyz\n0123456789,:&!?";
}
//--------------------------------------------------------------
void ofApp::update(){
}
//--------------------------------------------------------------
void ofApp::draw(){
ofSetColor(225);
verdana14.drawString("Font Example - use keyboard to type", 30, 35);
tfontuc.drawString ("안녕하세요.", 150, 240);
string num = string("타이머: ") + to_string (mysch.counter) + " 초";
tfontuc.drawString (num, 150, 400);
}
//--------------------------------------------------------------
void ofApp::keyPressed(int key){
if(key == OF_KEY_DEL || key == OF_KEY_BACKSPACE){
typeStr = typeStr.substr(0, typeStr.length()-1);
}
else if(key == OF_KEY_RETURN ){
typeStr += "\n";
}else{
if( bFirst ){
typeStr.clear();
bFirst = false;
}
ofAppendUTF8(typeStr,key);
}
}
//--------------------------------------------------------------
void ofApp::keyReleased(int key){
}
//--------------------------------------------------------------
void ofApp::mouseMoved(int x, int y ){
}
//--------------------------------------------------------------
void ofApp::mouseDragged(int x, int y, int button){
}
//--------------------------------------------------------------
void ofApp::mousePressed(int x, int y, int button){
}
//--------------------------------------------------------------
void ofApp::mouseReleased(int x, int y, int button){
}
//--------------------------------------------------------------
void ofApp::mouseEntered(int x, int y){
}
//--------------------------------------------------------------
void ofApp::mouseExited(int x, int y){
}
//--------------------------------------------------------------
void ofApp::windowResized(int w, int h){
}
//--------------------------------------------------------------
void ofApp::gotMessage(ofMessage msg){
}
//--------------------------------------------------------------
void ofApp::dragEvent(ofDragInfo dragInfo){
}
Would we need to kill the thread effectively in the class Scheduler for it not to raise errors on app quit?
Thanks!
Sorry to revive this, but I always get a bug on the class ofThread when quitting the application after I pasted this piece of code.
Hi @luisArandas,
I usually call ofThread::waitForThread(true) in the destructor to get rid of these errors.
https://github.com/openframeworks/openFrameworks/blob/ce1bbc751595881398934e82029d74fe099899c5/libs/openFrameworks/utils/ofThread.h#L136-L170