Active FPS counter

Hey, I currently use an FPS counter that works pretty good, yet I want a more active fps counter. You see a basic fps counter seems to only update you with the fps every second, but i want it to tell you per frame or something, like how fraps is an active counter etc, or kevGlass 2d physics demo has active fps counter. So how do i get a cool fps counter?

i think the most easy way is to time how long a frame lasted, and then translate it into fps. For example, if you have a frame which lasted 10ms, thats would be 100 frames per second. :wink:

note that you must use some kind of arithmetic middle calculation (sorry don’t know how math term is called in english) to show on the screen, or you would show every frame different number and that would be unreadable. I think best solution is to update FPS number like 5 times a second but based on arithmetic middle of last ~20 values.

EDIT: oh yeah I agree with ravenger: this_frame_fps = 1000 \ (game_update_time + render_time + sleep_time)

I think you mean “average” … just for the next time you’re looking for the word :slight_smile:

Sorry for OT.

Thank you. I could say “average of last 20 values” but I wanted to use mathematical term that is more precise :slight_smile: Here we have a mathematical term for that, literal translation would be “arithmetic middle”. Let’s say X is set of numbers you chose, then you would say “arithmetic middle of X”.

only thing I can think of is “mean” which is pretty much synonymous with “average” … I cant think of any other clever words. ah well, at least we know what you meant :wink:

edit: or… “median”! or… it doesn’t matter. ignore me. :stuck_out_tongue:

median is different from the mean which is different from the mode.

Mean was the correct word :slight_smile:

I feel like an idiot, the counter is off, i use Fraps to verify, before it seemed accuate, it was past 1k fps, and frap said 999, but now fraps still says 999, and my program says like 700-800 fps. So somethings wrong. For now i am updating it once a second, but i think my currentFramefps is wrong:

if (lastFpsTime >= oneNanoSecond) {
				fpsWrite = fps;  // this integer will be drawn with the "Fps: " string
				lastFpsTime = 0; // resets delta modifyer
				fps = 0;         // fps count reset
			}

//later... the end of  game loop
if (fpsFrameCount>=19) { 
				
				for (int i = 0; i <= 19; i++) {
					totalFps += fpsLog[i];
				}
				fps = (int) (totalFps/20);
				totalFps = 0;
				fpsFrameCount=0; 
			}
			
			currentFrameFps = (afterTime-startTime)/1000;
			
			fpsLog[fpsFrameCount] = currentFrameFps;
			
			//System.out.println("Fps: " + fps);
			fpsFrameCount++;

i was logging 20 numbers, and finding the mean, i print it on the counter the average every second. Well its wrong! Help!

I don’t understand what exactly are you trying in your code, but here’s how I wrote it:

fps variable is the one you print on the screen


while (running) {        
    long start_time = System.nanoTime();
            
    gameUpdate();
    gameRender(); 
    paintScreen();
    sleep();
            
    used_time = (System.nanoTime() - start_time) / 1000000;
    if (used_time > 0)
        fps_store[j] = (int)(1000/used_time);
    else 
        fps_store[j] = 999;
     j++;
     fps = 0;
     for (int fps1: fps_store) {
         fps += fps1;
    }
    fps /= fps_store.length;
    if (j > fps_store.length-1) j=0;
}

I have never, ever seen a for-loop like this, and i cant get it to work:

for (int fps1: fps_store) {
fps += fps1;
}

nvm, i had to make the fpsl; a type long, because my logs are using long data type. i think its working, it says 999 max, so i think ill just say what the fps is instead.

but what if i want the fps to say something over 999? I need accuracy to see if i loose loop performance.

EDIT: its also giving me strange values like 500 straight sometimes, and sometimes it says 1k exact, when it ‘shouldnt’ also it onlu updates per second.
under lower fps, the counter is quite accurate with the fraps one, but when u go above 999 its destroyed and says 1k and its messed up, i donno! sometimes it says lower fps like 600 when its really 999 on fraps.

Ok, i think it works great now, i did it like this:

currentFrameFps = (System.nanoTime()-startTime);
			
			if (currentFrameFps > 0) {
				fpsLog[fpsFrameCount] = (int) (1000000000/currentFrameFps); 
			}
			else {
				fpsLog[fpsFrameCount] = 0;
			}
			fpsFrameCount++;
			fps = 0;
			for (long fps1: fpsLog) {
				fps += fps1;
		    }
			fps /= fpsLog.length;
			if (fpsFrameCount >= fpsLog.length) { fpsFrameCount=0; }

it doesnt limit at 999, because i dont cap fps with sleep in my game. It seems accurate, along with fraps, deadly accurate it is now. I divided it up differently, and if u see any possible bugs let me know!

not actually a bug, but if you don’t cap it to 999 then that else isn’t neccessary anymore, no loop will end in less then one ns and even if it does it shouldn’t store zero as a value:


    else {
        fpsLog[fpsFrameCount] = 0;
    }

fixed :slight_smile: