/
ofApp.cpp
119 lines (87 loc) · 3.52 KB
/
ofApp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include "ofApp.h"
//--------------------------------------------------------------
void ofApp::setup() {
ofBackgroundHex(0xc5c9b2);
bTimerReached = false;
startTime = ofGetElapsedTimeMillis(); // get the start time
endTime = (int)ofRandom(1000, 5000); // in milliseconds
}
//--------------------------------------------------------------
void ofApp::update() {
}
//--------------------------------------------------------------
void ofApp::draw(){
float barWidth = 500;
// update the timer this frame
float timer = ofGetElapsedTimeMillis() - startTime;
if(timer >= endTime && !bTimerReached) {
bTimerReached = true;
ofMessage msg("Timer Reached");
ofSendMessage(msg);
}
// the background to the progress bar
ofSetColor(100);
ofDrawRectangle((ofGetWidth()-barWidth)/2, ofGetHeight()/2, barWidth, 30);
// get the percentage of the timer
float pct = ofMap(timer, 0.0, endTime, 0.0, 1.0, true);
ofSetHexColor(0xf02589);
ofDrawRectangle((ofGetWidth()-barWidth)/2, ofGetHeight()/2, barWidth*pct, 30);
// draw the percentage
ofSetColor(20);
ofDrawBitmapString(ofToString(pct*100, 0)+"%", ((ofGetWidth()-barWidth)/2)+barWidth+10, (ofGetHeight()/2)+20);
// the timer was reached :)
if(bTimerReached) {
ofSetColor(20);
ofDrawBitmapString("Timer Reached!", (ofGetWidth()-100)/2, (ofGetHeight()/2)+70);
}
// some information about the timer
string info = "FPS: "+ofToString(ofGetFrameRate(),0)+"\n";
info += "Start Time: "+ofToString(startTime, 1)+"\n";
info += "End Time: "+ofToString(endTime/1000.0, 1)+" seconds\n";
info += "Timer: "+ofToString(timer/1000.0, 1)+" seconds\n";
info += "Percentage: "+ofToString(pct*100, 1)+"%\n";
info += "\nPress ' ' to get a new random end time\n";
ofSetColor(0);
ofDrawBitmapString(info, 20, 20);
}
//--------------------------------------------------------------
void ofApp::keyPressed(int key) {
if(key == ' ') {
bTimerReached = false; // reset the timer
startTime = ofGetElapsedTimeMillis(); // get the start time
endTime = (int)ofRandom(1000, 5000); // in milliseconds
}
}
//--------------------------------------------------------------
void ofApp::mousePressed(int x, int y, int button) {
startTime = ofGetElapsedTimeMillis();
endTime = (int)ofRandom(100, 5000); // milliseconds
}
//--------------------------------------------------------------
void ofApp::gotMessage(ofMessage msg){
cout << "Got the message: " + msg.message << endl;
}
//--------------------------------------------------------------
void ofApp::keyReleased(int key){
}
//--------------------------------------------------------------
void ofApp::mouseMoved(int x, int y){
}
//--------------------------------------------------------------
void ofApp::mouseDragged(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::dragEvent(ofDragInfo dragInfo){
}