Reputation: 5167
Im trying to get the time elapse percentage, what I do is first check a time interval, them I want to get the percentage of the elapsed time in that time interval, if that makes sense
Here is my code:
if ( ofGetElapsedTimeMillis() > lastTimeCheck + timePeriod ) {
lastTimeCheck = ofGetElapsedTimeMillis();
cout << "its time!" << endl;
}
float p = (float)ofGetElapsedTimeMillis() / (float)(lastTimeCheck + timePeriod);
cout << "time percentage: " << p << endl;
timePeriod = 3000, so every 3 seconds I update the lastTimeCheck variable and the following line of code gets the percentage in that time interval, I get results from 0.5 to 0.9, but I need p to be from 0 to 1
Thanks in advance - rS
Upvotes: 1
Views: 733
Reputation: 34418
Do you mean the time between lastTimeCheck and lastTimeCheck + timePeriod? That would be
float p = (float)(ofGetElapsedTimeMililis() - lastTimeCheck) / (float)(timePeriod);
You can probably lose one of the float casts too, but I think it's safer and no less readable to leave them in. If you need to guarantee that p is less than or equal to one, though, you should either save and re-use the ofGetTimeElapsedMillis value from the previous call or you should explicitly check p afterwards.
int millisNow = ofGetElapsedTimeMillis();
int millisSinceLastCheck = millisNow - lastTimeCheck;
if (millisSinceLastCheck > timePeriod) {
lastTimeCheck = millisNow;
millisSinceLastCheck = 0;
cout << "it's time!" << endl;
}
float p = (float)(millisSinceLastcheck) / (float)(timePeriod);
cout << "time fraction: " << p << endl;
Upvotes: 2