 
 ;; tile/pixels
 
+(defn floor-int [x]
+  (long (Math/floor x)))
+
 (defn tile-to-pixels [[nx ny] [tile-width tile-height] tile-zoom]
   [(int (* (Math/pow 2.0 tile-zoom) nx tile-width))
    (int (* (Math/pow 2.0 tile-zoom) ny tile-height))])

      (.addMouseMotionListener mouse-adapter))
    position-atom))

(defn handle-wheel [component z-atom]
  (.addMouseWheelListener component
    (proxy [MouseAdapter] []

                           RenderingHints/VALUE_ANTIALIAS_ON
                           RenderingHints/VALUE_ANTIALIAS_OFF)))))

(defn paint-tiles [^Graphics2D g available-tiles screen-state [tile-width tile-height]]
  (let [pixel-rect (Rectangle. (- (screen-state :x)) (- (screen-state :y))
                               (screen-state :width) (screen-state :height))]
    ;(println pixel-rect)
    (doseq [[nx ny] (tiles-in-pixel-rectangle pixel-rect [tile-width tile-height])]
      ;(println nx ny)
      (when-let [image (get-in available-tiles [(screen-state :zoom) {:nx nx :ny ny :nz (screen-state :z) :nt 0 :nc 0}])]
        (let [[x y] (tile-to-pixels [nx ny] [tile-width tile-height] 0)]
          (.drawImage g image x y nil))))))

(defn paint-screen [graphics screen-state available-tiles]
  (let [original-transform (.getTransform graphics)]
    (doto graphics
      (.setClip 0 0 (:width @screen-state) (:height @screen-state))
      (.translate (:x @screen-state)
                  (:y @screen-state))
      ;(.rotate @angle)
      enable-anti-aliasing
      (paint-tiles @available-tiles @screen-state [512 512])
      (.setColor Color/YELLOW)
      (.fillOval -30 -30
                 60 60)
