NAN fps with ofGetFrameRate() - 0.06

A bug with how fps is calculated can produce NAN which means ofGetFrameRate will always return NAN. So far I have only seen this on Win32 machines but the code is incorrect as it has a division by zero.

The fix is in ofAppGlutWindow.cpp:
The code bellow should be changed from:

  
  	// -------------- fps calculation:  
	timeNow = ofGetElapsedTimef();  
	if( (timeNow-timeThen) > 0.05f || nFramesForFPS == 0 ) {  
 		fps = (double)nFramesForFPS / (timeNow-timeThen);  
      	timeThen = timeNow;  
		nFramesForFPS = 0;  
  
		//hack for windows - was getting NAN - maybe unitialized vars???  
		if( nFrameCount < 5) frameRate = fps;  
		else frameRate = 0.9f * frameRate + 0.1f * fps;  
  	}  
  	nFramesForFPS++;  
  	// --------------  

to:

  
  	// -------------- fps calculation:  
	timeNow = ofGetElapsedTimef();  
	if( ( timeNow - timeThen ) > 0 ) {  
 		fps = 1.0 / (timeNow-timeThen);  
		frameRate *= 0.9f;  
        frameRate += 0.1f*fps;  
  	}  
  	timeThen = timeNow;  

The || in

  
	if( (timeNow-timeThen) > 0.05f || nFramesForFPS == 0 ) {  

Was meaning that we were doing a division by 0 on the next line.

This fix should be in 0.061
Theo

Okay so this problem still exists but in a different way.

Currently with the patch this if statement occassionally fails

  
   if( ( timeNow - timeThen ) > 0 ) {  
  
   }  

The result is that fps is set to = 1.0 / (timeNow - timethen), which produces a NAN. This is pretty bad as once fps is set to NAN, frameRate will become NAN and not recover.

So somehow it passes the if check as a number greater than 0, but it produces wonky values when dividing 1.0.

My though was that this was somehow related to either variable precision or the fact that the time difference was being calculated on the fly twice.

Changing the relevant variables from float to double and calculating the time difference once and using that variable for both the check and the fps calculation fixes the issue.

At the top of ofAppGlutWindow.cpp
Change:

  
float			timeNow, timeThen, fps;  

to:

  
double			timeNow, timeThen, fps;  

New fps code:

  
  	// -------------- fps calculation:  
	timeNow = ofGetElapsedTimef();  
    double diff = timeNow-timeThen;  
	if( diff  > 0.0f ) {  
 		fps = 1.0 / diff;  
		frameRate *= 0.9f;  
        frameRate += 0.1f*fps;  
  	}  
  	timeThen = timeNow;  

cool, I just fixed and commited ofxiPhone

(P.S. we should look at extracting this code out and making it common)