Created
December 3, 2021 05:05
-
-
Save jvillste/66a85da6c2d4fd5836ad62ac12c1e4c1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns flow-gl.swing.render-loop-test | |
(:import [java.awt Canvas Color Font] | |
java.awt.geom.Rectangle2D$Double | |
javax.swing.JFrame)) | |
(defn start [] | |
(let [last-paint-time-atom (atom (System/currentTimeMillis)) | |
frame-rates-atom (atom []) | |
j-frame (JFrame.) | |
canvas (Canvas.)] | |
(.add (.getContentPane j-frame) | |
canvas) | |
(doto j-frame | |
(.setSize 200 100) | |
(.setVisible true)) | |
(.setIgnoreRepaint canvas true) | |
(.createBufferStrategy canvas 2) | |
(let [buffer-strategy (.getBufferStrategy canvas) | |
font (Font. "Dialog" Font/PLAIN 30) | |
frame-rate-sample-count 100] | |
(while true | |
(let [time-now (System/currentTimeMillis) | |
graphics (.getDrawGraphics buffer-strategy)] | |
(try | |
(swap! frame-rates-atom (fn [frame-rates] | |
(take frame-rate-sample-count | |
(conj frame-rates | |
(/ 1000 | |
(max 1 | |
(- time-now | |
@last-paint-time-atom))))))) | |
(reset! last-paint-time-atom time-now) | |
(doto graphics | |
(.setColor (Color. (float 1) | |
(float 1) | |
(float 1) | |
(float 1))) | |
(.fill (Rectangle2D$Double. (double 0) | |
(double 0) | |
(double 200) | |
(double 100))) | |
(.setColor (Color. (float 0) | |
(float 0) | |
(float 0) | |
(float 1))) | |
(.setFont font) | |
(.drawString (str (int (/ (reduce + @frame-rates-atom) | |
frame-rate-sample-count))) | |
0 | |
50)) | |
(Thread/sleep 10) | |
(finally | |
(.dispose graphics) | |
(.show buffer-strategy)))))))) | |
(comment | |
(start) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment