# HG changeset patch # User Robert McIntyre # Date 1340229036 18000 # Node ID 130ba9f49db592f13c63f651201d0eb8e55079c2 # Parent f992a0a0480d3b3339d0e7f9b2ba5ec0fcf3f35b added function to slice up an image into an appropriate representation for the GB. diff -r f992a0a0480d -r 130ba9f49db5 clojure/com/aurellem/run/image.clj --- a/clojure/com/aurellem/run/image.clj Wed Jun 20 13:59:21 2012 -0500 +++ b/clojure/com/aurellem/run/image.clj Wed Jun 20 16:50:36 2012 -0500 @@ -308,7 +308,7 @@ (+ y (* 8 (int (/ tile 20)))))))))) (defn tile->palette [tile] - (sort (set tile))) + (vec (sort (set tile)))) (require 'clojure.set) @@ -333,6 +333,68 @@ unique-palettes (absorb-contract (set palettes))] unique-palettes)) +(defn tile-pallete + "find the first appropirate palette for the tile in the + provided list of palettes." + [tile palettes] + (let [tile-colors (set tile)] + (swank.util/find-first + #(clojure.set/subset? tile-colors (set %)) + palettes))) + + +(defn image->gb-image + "Returns the image in a format amenable to the gameboy's + internal representation. The format is: + {:width -- width of the image + :height -- height of the image + :palettes -- vector of all the palettes the image + needs, in proper order + :tiles -- vector of all the tiles the image needs, + in proper order. A tile is 64 palette + indices. + :data -- vector of pairs of the format: + [tile-index, palette-index] + in row-oriented order}" + [^BufferedImage image] + (let [image-palettes (palettes image) + palette-index (zipmap + image-palettes + (range (count image-palettes))) + tiles (gb-tiles image) + unique-tiles (vec (distinct tiles)) + tile-index (zipmap unique-tiles + (range (count unique-tiles)))] + {:width (.getWidth image) + :height (.getHeight image) + :palettes image-palettes + :tiles + (vec + (for [tile unique-tiles] + (let [colors + (vec (tile-pallete tile image-palettes)) + color-index + (zipmap colors (range (count colors)))] + (mapv color-index tile)))) + :data + (vec + (for [tile tiles] + (let [tile-colors (set (tile->palette tile))] + [(tile-index tile) + (palette-index + (tile-pallete tile image-palettes))])))})) + + + + + + + + + + + ) + (defn wait-until-v-blank "Modified version of frame-metronome. waits untill LY == 144, indicating start of v-blank period."