Skip to content
This repository has been archived by the owner on Jun 17, 2020. It is now read-only.

Commit

Permalink
Readability improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
harto committed Jan 23, 2013
1 parent 695585d commit d3a185e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
24 changes: 13 additions & 11 deletions common/src/cljx/enoki/graphics.cljx
Original file line number Diff line number Diff line change
@@ -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`."))
8 changes: 8 additions & 0 deletions swing/src/enoki/graphics/java2d.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
8 changes: 8 additions & 0 deletions web/src/cljs/enoki/graphics/canvas.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit d3a185e

Please sign in to comment.