                                             :bg bg
                                             :x  (+ x i)
                                             :y  y
-                                            :opts {}})
+                                            :opts mask-opts})
                                  string)]
      (rat/put-chars! screen characters))))
 
-(defn -main
+(defn single-thread-main
   "Show a terminal and echo input."
-  [& args]
+  [& _]
   #_(loop [i 30]
     (log/info "Starting in " i)
     (Thread/sleep 1000)
     (when (pos? i)
       (recur (dec i))))
-  (let [terminal (make-terminal 80 24)
+  (let [terminal (make-terminal "glterminal.main" 80 24)
         last-key (atom nil)]
     (future (go-loop []
               (reset! last-key (async/<! (rat/get-key-chan terminal)))

       :font-texture-height height
       :font-texture-image texture-image})))

(defn- texture-id
  ([buffered-image]
  (let [width (.getWidth buffered-image)
        height (.getHeight buffered-image)
        texture-buffer (BufferUtils/createByteBuffer (* width height 4))
        data (-> buffered-image
               (.getRaster)
               (.getDataBuffer)
               (as-> db (cast DataBufferByte db))
               (.getData))]
    (.put texture-buffer data 0 (alength data))
    (.flip texture-buffer)
    (texture-id width height texture-buffer)))
  ([width height]
   (texture-id width height (BufferUtils/createByteBuffer (* width height 4))))
  ([^long width ^long height ^ByteBuffer texture-buffer]
   (let [texture-id (GL11/glGenTextures)]
      ;;(.order texture-buffer (ByteOrder/nativeOrder))
      (GL11/glBindTexture GL11/GL_TEXTURE_2D texture-id)
      (GL11/glPixelStorei GL11/GL_UNPACK_ALIGNMENT 1)
      (GL11/glTexParameteri GL11/GL_TEXTURE_2D GL11/GL_TEXTURE_MIN_FILTER GL11/GL_NEAREST)
      (GL11/glTexParameteri GL11/GL_TEXTURE_2D GL11/GL_TEXTURE_MAG_FILTER GL11/GL_NEAREST)
      (GL11/glTexImage2D GL11/GL_TEXTURE_2D 0 GL11/GL_RGBA width height 0 GL11/GL_RGBA GL11/GL_UNSIGNED_BYTE texture-buffer)
      texture-id)))

(defn- get-fields [#^Class static-class]
  (. static-class getFields))

;; Normally this would be a record, but until http://dev.clojure.org/jira/browse/CLJ-1224 is fixed
;; it is not performant to memoize records because hashCode values are not cached and are recalculated
;; each time.

(defrecord GLCharacter [character fg-color bg-color style fx-character fx-fg-color fg-bg-color])
(defn make-terminal-character
  ([character fg-color bg-color style]
   (make-terminal-character character fg-color bg-color style nil nil nil))
  ([character fg-color bg-color style fx-character fx-fg-color fg-bg-color]
   (GLCharacter. character fg-color bg-color style fx-character fx-fg-color fg-bg-color)))
  #_{:character character
   :fg-color  fg-color
   :bg-color  bg-color
   :style     style}

(defrecord OpenGlTerminal [^int columns
                           ^int rows
                           ^int texture-columns
                           ^int texture-rows
                           normal-font
                           antialias
                           character-map-cleared
                           character-map
                           cursor-xy
