Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Your output is the number of seconds your program has run for, not framerate. You should be dividing your frame count (which you aren't collecting) by the total time run.</p> <p>To get the frame count, simply add a new variable outside of your game loop, and increment it each time through...</p> <pre><code>public static void main(String[] args) { long frames = 0; joglplat m = new joglplat(); while(true){ frames++; // other code here System.out.println("framerate: " + ((System.nanoTime() / 1000000 - startTime) / frames ) ); } } </code></pre> <p>Note, however, that this will give you the average framerate throughout the entire execution of your program. Two other options you have are to get the instantaneous framerate, and the average framerate over the past N frames.</p> <p>All styles in one (untested/uncompiled, so might have some errors, but should get you started in the right direction):</p> <pre><code>public static void main(String[] args) { long startTime = System.nanoTime(); long lastFrameTime = startTime; long frames = 0; int framesToAverage = 10; long[] frameTimes = new long[framesToAverage]; joglplat m = new joglplat(); while(true){ // logic here long currentFrameDuration = System.nanoTime() - lastFrame; lastFrameTime = System.nanoTime(); long instantFramerate = currentFrameDuration / 1000000; int currentFrameIndex = frames % frameTimes.length; frameTimes[currentFrameIndex] = currentFrameDuration; frames++; long averageFramerate = ( ( lastFrameTime - startTime) / frames ) / 1000000; long instantFramerate = currentFrameDuration / 1000000; if( frames &gt; frameTimes.length ) { // if it isn't, we don't have enough data yet int firstFrameIndex = currentFrameIndex + 1; if( firstFrameIndex &gt; frameTimes.length ) { firstFrameIndex = 0; } long averageFrameratePerN = ( ( frameTimes[currentFrameIndex] - frameTimes[firstFrameindex] ) / frameTimes.length ) / 1000000; } // yield/sleep here } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload