import Image
import ImageDraw
import gameduino
import gameduino.prep as gdprep
import array
from gameduino.compress import Codec

hh = open("chopper.h", "w")
ir = gdprep.ImageRAM(hh)

map = Image.open("map.png").convert("L")
mh = map.size[1]
def ceiling(x):
    for y in range(0, mh):
        if map.getpixel((x,y)) != 255:
            break
    h = (y * 256 + map.getpixel((x,y))) / 16
    return h
def floor(x):
    for y in range(0, mh):
        if map.getpixel((x,mh-1-y)) != 255:
            break
    h = (y * 256 + map.getpixel((x,mh-1-y))) / 16
    return h

gdprep.dump(hh, "roof", array.array('B', [ceiling(x) for x in range(2048)]))
gdprep.dump(hh, "foot", array.array('B', [floor(x) for x in range(2048)]))

names = [ "chopper1.png", "chopper_explode.png", "chopper_dead.png" ]
(chopper,explode,dead) = gdprep.palettize([Image.open(nm).convert("RGBA") for nm in names], 16)
gdprep.dump(hh, "chopper_palette", gdprep.getpal(chopper))
ir.addsprites("chopper", (32,32), chopper, gdprep.PALETTE16A, center = (16,16))
ir.addsprites("explode", (32,32), explode, gdprep.PALETTE16A, center = (16,16))
ir.addsprites("dead", (32,32), dead, gdprep.PALETTE16A, center = (16,16))

blocks = gdprep.palettize(Image.open("pinnacle.png"), 16)
gdprep.dump(hh, "blocks_palette", gdprep.getpal(blocks))
ir.addsprites("blocks", (16,16), blocks, gdprep.PALETTE16B)

cc = Codec(b_off = 9, b_len = 3)
uncompressed = ir.used().tostring()
wcd = cc.toarray(uncompressed)
print "compressed to", len(wcd)
gdprep.dump(hh, "chopper_compressed", wcd)

(dpic,dchr,dpal) = gdprep.encode(Image.open("bg.png"))
print len(dchr) / 16, "chars"

gdprep.dump(hh, "desert_pic", dpic)
gdprep.dump(hh, "desert_chr_compressed", cc.toarray(dchr.tostring()))
gdprep.dump(hh, "desert_pal_compressed", cc.toarray(dpal.tostring()))
