 (ns sormilla.gui
-  (:import [java.awt Color Graphics2D]
+  (:require [clojure.java.io :as io])
+  (:import [java.awt Canvas Color Graphics2D Font]
            [java.awt.geom Ellipse2D$Double]
            [javax.swing JFrame JComponent]))
 
 (set! *warn-on-reflection* true)
+
+(def cutive (with-open [in (io/input-stream (io/resource "CutiveMono-Regular.ttf"))]
+              (Font/createFont Font/TRUETYPE_FONT in)))
 
 (defn ->safe [throttle f]
   (fn [& args]

(defn make-frame [source render & {:keys [interval safe safe-throttle max-size top exit-on-close] :or {interval 50 safe-throttle 500 max-size true}}]
  (let [run (atom true)
        frame (JFrame.)
        source (if safe (->safe safe-throttle source) source)
        render (if safe (->safe safe-throttle render) render)]
    (if max-size
      (.setExtendedState frame JFrame/MAXIMIZED_BOTH)
      (.setSize frame 500 500))
    (when top
      (.setAlwaysOnTop frame true))
    (.setLocationByPlatform frame true)
    (when exit-on-close
      (.setDefaultCloseOperation frame JFrame/EXIT_ON_CLOSE))
    (.setVisible frame true)
    (.createBufferStrategy frame 2)
    (let [strategy (.getBufferStrategy frame)]
      (future
        (try
          (while (deref run)
            (when-let [data (source)]
              (loop [contents-lost? true]
                (loop [restored? true]
                  (let [g (.getDrawGraphics strategy)]
                    (render g (.getWidth frame) (.getHeight frame) data)
                    (.dispose g))
                  (when (.contentsRestored strategy) (recur true)))
                (.show strategy)
                (when (.contentsLost strategy) (recur true))))
            (Thread/sleep interval))
          (catch Throwable e
            (println "Oh shit!" e)
            (throw e))))

  (defn r [^Graphics2D g ^long w ^long h data]
    (.setColor g Color/DARK_GRAY)
    (.fillRect g 0 0 w h)
    (.setColor g Color/RED)
    (.drawLine g 0 0 w h)
    (.drawLine g 0 h w 0))
  
  (defn s []
    "foo")
  
  (def f (make-frame #'s #'r))
  (close-frame! f))
