()
| 2020 | |
| 2021 | |
| 2022 | public void handleDraw() { |
| 2023 | if (g == null) return; |
| 2024 | if (!looping && !redraw) return; |
| 2025 | |
| 2026 | if (insideDraw) { |
| 2027 | System.err.println("handleDraw() called before finishing"); |
| 2028 | System.exit(1); |
| 2029 | } |
| 2030 | |
| 2031 | insideDraw = true; |
| 2032 | g.beginDraw(); |
| 2033 | if (recorder != null) { |
| 2034 | recorder.beginDraw(); |
| 2035 | } |
| 2036 | |
| 2037 | // apply window ratio if set |
| 2038 | if (windowRatio) { |
| 2039 | float aspectH = width / (float) rwidth; |
| 2040 | float aspectV = height / (float) rheight; |
| 2041 | ratioScale = min(aspectH, aspectV); |
| 2042 | ratioTop = (height - ratioScale* rheight) / 2; |
| 2043 | ratioLeft = (width - ratioScale* rwidth) / 2; |
| 2044 | translate(ratioLeft, ratioTop); |
| 2045 | scale(ratioScale); |
| 2046 | } |
| 2047 | |
| 2048 | long now = System.nanoTime(); |
| 2049 | |
| 2050 | if (frameCount == 0) { |
| 2051 | setup(); |
| 2052 | |
| 2053 | } else { // frameCount > 0, meaning an actual draw() |
| 2054 | // update the current frameRate |
| 2055 | |
| 2056 | // Calculate frameRate through average frame times, not average fps, e.g.: |
| 2057 | // |
| 2058 | // Alternating 2 ms and 20 ms frames (JavaFX or JOGL sometimes does this) |
| 2059 | // is around 90.91 fps (two frames in 22 ms, one frame 11 ms). |
| 2060 | // |
| 2061 | // However, averaging fps gives us: (500 fps + 50 fps) / 2 = 275 fps. |
| 2062 | // This is because we had 500 fps for 2 ms and 50 fps for 20 ms, but we |
| 2063 | // counted them with equal weight. |
| 2064 | // |
| 2065 | // If we average frame times instead, we get the right result: |
| 2066 | // (2 ms + 20 ms) / 2 = 11 ms per frame, which is 1000/11 = 90.91 fps. |
| 2067 | // |
| 2068 | // The counter below uses exponential moving average. To do the |
| 2069 | // calculation, we first convert the accumulated frame rate to average |
| 2070 | // frame time, then calculate the exponential moving average, and then |
| 2071 | // convert the average frame time back to frame rate. |
| 2072 | { |
| 2073 | // Get the frame time of the last frame |
| 2074 | double frameTimeSecs = (now - frameRateLastNanos) / 1e9; |
| 2075 | // Convert average frames per second to average frame time |
| 2076 | double avgFrameTimeSecs = 1.0 / frameRate; |
| 2077 | // Calculate exponential moving average of frame time |
| 2078 | final double alpha = 0.05; |
| 2079 | avgFrameTimeSecs = (1.0 - alpha) * avgFrameTimeSecs + alpha * frameTimeSecs; |
no test coverage detected