diff --git a/common/src/cljx/enoki/graphics.cljx b/common/src/cljx/enoki/graphics.cljx index 24884ba..077de28 100644 --- a/common/src/cljx/enoki/graphics.cljx +++ b/common/src/cljx/enoki/graphics.cljx @@ -1,23 +1,25 @@ -;; Graphics operations +;; Graphics operations. (ns enoki.graphics) (defprotocol Context - ;; (bg! [this colour] - ;; "Set background colour") - ;; (fg! [this colour] - ;; "Set foreground colour") + (clear! [this] - "Fill the display with the current background-colour. Returns `this'.") + "Fill the display with the current background colour and return `this`.") + (draw-text! [this s x y] - "Draw string `s' at location [x, y]")) + "Draw string `s' at location `(x, y)` and return `this`.")) (defprotocol Display + (init-display! [this] - "Do any required initialisation, e.g. make frame visible") + "Do any required initialisation, e.g. make frame visible.") + (display-width [this] - "Return the display width in pixels") + "Return the display width in pixels.") + (display-height [this] - "Return the display height in pixels") + "Return the display height in pixels.") + (render [this ctx] - "Draws to the display using a function (fn [g])")) + "Draws to the display using a function `(fn [g])`, where `g` is a `Context`.")) diff --git a/swing/src/enoki/graphics/java2d.clj b/swing/src/enoki/graphics/java2d.clj index bcd7fcc..125893c 100644 --- a/swing/src/enoki/graphics/java2d.clj +++ b/swing/src/enoki/graphics/java2d.clj @@ -12,26 +12,34 @@ (:import [java.awt Canvas Color Dimension])) (defrecord Graphics2DContext [display g] + Context + (clear! [this] (.clearRect g 0 0 (display-width display) (display-height display)) this) + (draw-text! [this s x y] (.drawString g (str s) x y) this)) (defrecord CanvasDisplay [frame canvas] + Display + (init-display! [_] (seesaw/invoke-now (seesaw/pack! frame) (seesaw/show! frame)) (.createBufferStrategy canvas 2) (printf "width=%d, height=%d%n" (.getWidth canvas) (.getHeight canvas))) + (display-width [_] (.getWidth canvas)) + (display-height [_] (.getHeight canvas)) + (render [this f] (let [bs (.getBufferStrategy canvas) g (.getDrawGraphics bs)] diff --git a/web/src/cljs/enoki/graphics/canvas.cljs b/web/src/cljs/enoki/graphics/canvas.cljs index 0787d10..8e396a1 100644 --- a/web/src/cljs/enoki/graphics/canvas.cljs +++ b/web/src/cljs/enoki/graphics/canvas.cljs @@ -4,23 +4,31 @@ (:use [enoki.graphics :only [Context Display display-width display-height]])) (defrecord CanvasContext [display ctx] + Context + (clear! [this] (.clearRect ctx 0 0 (display-width display) (display-height display)) this) + (draw-text! [this s x y] (.fillText ctx s x y) this)) (defrecord CanvasDisplay [canvas] + Display + (init-display! [_] ;; Nothing to do here ) + (display-width [_] (.-width canvas)) + (display-height [_] (.-height canvas)) + (render [this f] (->> (.getContext canvas "2d") (->CanvasContext this)