diff clojure/com/aurellem/run/image.clj @ 507:24b459a95b46

saving progress.
author Robert McIntyre <rlm@mit.edu>
date Wed, 20 Jun 2012 19:51:50 -0500
parents 130ba9f49db5
children e6c02264dc9c
line wrap: on
line diff
     1.1 --- a/clojure/com/aurellem/run/image.clj	Wed Jun 20 16:50:36 2012 -0500
     1.2 +++ b/clojure/com/aurellem/run/image.clj	Wed Jun 20 19:51:50 2012 -0500
     1.3 @@ -124,7 +124,7 @@
     1.4           (flatten (map gb-rgb->bits palettes)))]))
     1.5  
     1.6  (defn display-one-color
     1.7 -  "Displayes a single color onto the gameboy screen. input rgb in
     1.8 +  "Displayes a single color onto the gameboy screen. Input rgb in
     1.9     gameboy rgb."
    1.10    ([state [r g b]]
    1.11       ;; construct a kernel that displays a single color
    1.12 @@ -276,12 +276,6 @@
    1.13        (.dispose))
    1.14      im))
    1.15  
    1.16 -
    1.17 -(count
    1.18 - (filter
    1.19 -  (fn [[r g b]]
    1.20 -    (= (max r g b) r ))(set (vals gb-color-map))))
    1.21 -
    1.22  (def test-image
    1.23    (ImageIO/read
    1.24     (File. user-home "/proj/vba-clojure/images/test-gb-image.png")))
    1.25 @@ -385,15 +379,6 @@
    1.26              (tile-pallete tile image-palettes))])))}))
    1.27  
    1.28  
    1.29 -    
    1.30 -    
    1.31 -
    1.32 -        
    1.33 -
    1.34 -  
    1.35 -
    1.36 -
    1.37 -  )
    1.38  
    1.39  (defn wait-until-v-blank
    1.40    "Modified version of frame-metronome. waits untill LY == 144,
    1.41 @@ -413,12 +398,73 @@
    1.42      (concat timing-loop continue-if-144)))
    1.43  
    1.44  
    1.45 -(defn display-image-kernel [^BufferedImage image]
    1.46 -  ;; assume image tile data is stored at 0xA000
    1.47 -  ;;   "      "   palette date is at     0xB000
    1.48 +(def bg-character-data 0x9000)
    1.49 +
    1.50 +(defn gb-tile->bytes
    1.51 +  "Tile is a vector of 64 numbers between 0 and 3 that
    1.52 +   represent a single 8x8 color tile in the GB screen.
    1.53 +   It gets bit-packed into to 16 8-bit numbers in the following
    1.54 +   form:
    1.55 +
    1.56 +   0-low  1-low  ... 7-low
    1.57 +   0-high 1-high ... 7-high
    1.58 +   .
    1.59 +   .
    1.60 +   .
    1.61 +   55-low  ........ 63-low
    1.62 +   55-high ........ 63-high"
    1.63 +  [tile]
    1.64 +  (let [row->bits
    1.65 +        (fn [row]
    1.66 +          (mapv
    1.67 +           (fn [row*]
    1.68 +             (Integer/parseInt (apply str row*) 2))
    1.69 +           [(map #(bit-and 0x01 %) row)
    1.70 +            (map #(bit-shift-right (bit-and 0x02 %) 1)
    1.71 +                 row)]))]
    1.72 +    (vec
    1.73 +     (flatten
    1.74 +      (map row->bits
    1.75 +           (partition 8 tile))))))
    1.76 +
    1.77    
    1.78  
    1.79 +(defn display-image-kernel [^BufferedImage image]
    1.80 +  (let [gb-image (image->gb-image image)]
    1.81 +  
    1.82 +    [(clear-music-registers)
    1.83  
    1.84 +     ;; [ ] disable LCD protection circuit.
    1.85 +     
    1.86 +     ;; now we can write to all video RAM anytime with
    1.87 +     ;; impunity.
    1.88 +
    1.89 +
    1.90 +     
    1.91 +     ;; we're only using background palettes; just set the
    1.92 +     ;; minimum required bg palettes for this image,
    1.93 +     ;; starting with palette #0.
    1.94 +
    1.95 +     (set-palettes bg-palette-select bg-palette-data
    1.96 +                   (:palettes gb-image))
    1.97 +
    1.98 +     ;; [ ] switch to bank 0 to set BG character data.
    1.99 +     
   1.100 +     
   1.101 +     ;; [ ] write minimum amount of tiles to BG character
   1.102 +     ;; section
   1.103 +     
   1.104 +
   1.105 +
   1.106 +     ;; [ ] disable the display of OBJ tiles.
   1.107 +     
   1.108 +
   1.109 +     ;; [ ] reactivate the LCD display
   1.110 +     
   1.111 +
   1.112 +     (infinite-loop)
   1.113 +
   1.114 +     
   1.115    )
   1.116  
   1.117