Ejemplo n.º 1
0
import sys
from PIL import Image
from gameduino import prep

image_path = ""
h_name = ""

# retrieve the command line arguments
if (len(sys.argv) > 2):
    image_path = sys.argv[1]
    sprite_name = sys.argv[2]
else:
    print "Not all commands entered"
    sys.exit(0)

# create the header file
hdr = open(sprite_name + ".h", "w")
ir = prep.ImageRAM(hdr)

im = Image.open(image_path)

# show the image to be changed
im.show()

sprite = prep.palettize(im, 4)

# dump to a h file
prep.dump(hdr, sprite_name + "_palette", sprite)
ir.addsprites(sprite_name, (16, 16), sprite, prep.PALETTE4A)
Ejemplo n.º 2
0
import Image
import gameduino.prep as gdprep

out=open("sprites.h", "w")
ir = gdprep.ImageRAM(out)
im= Image.open("sprites/jaromil.png")
jw = gdprep.palettize(im, 4)
ir.addsprites("jaromil", (16, 32), jw, gdprep.PALETTE4A, center = (8,16))
gdprep.dump(out, "sprites", ir.used())
# f***s up order of colors somehow :(
#gdprep.dump(out, "jaromil_palette", gdprep.getpal(im))
Ejemplo n.º 3
0
## check file type of sprite sheet
check_sprite = args.sheet.split('.')
if (len(check_sprite) != 2 or check_sprite[1] != 'png'):
    raise TypeError('Supply a .png file! You supplied %s.' % args.sheet)

header = open(args.output, "w")

img_ram = gdprep.ImageRAM(header)

## select the correct palette
if (args.palette_size == 4):
    palette = gdprep.PALETTE4A

elif (args.palette_size == 16):
    palette = gdprep.PALETTE16A

else:
    palette = gdprep.PALETTE256A

img_pal = gdprep.palettize(Image.open(args.sheet), args.palette_size)
img_ram.addsprites(args.varname, (16, 16),
                   img_pal,
                   palette,
                   center=args.center)

name_img = args.varname + '_img'
name_pal = args.varname + '_pal'
gdprep.dump(header, name_img, img_ram.used())
gdprep.dump(header, name_pal, gdprep.getpal(img_pal))
Ejemplo n.º 4
0
#!/usr/bin/python

import Image
import gameduino.prep as gdprep

(dpic, dchr, dpal) = gdprep.encode(Image.open("window.png"))
hdr = open("gfx.h", "w")
gdprep.dump(hdr, "windowChars", dchr)
gdprep.dump(hdr, "windowPal", dpal)

(dpic, dchr, dpal) = gdprep.encode(Image.open("button.png"))
gdprep.dump(hdr, "buttonChars", dchr)
gdprep.dump(hdr, "buttonPal", dpal)

(dpic, dchr, dpal) = gdprep.encode(Image.open("appicon.png"))
gdprep.dump(hdr, "appIconPic", dpic)
gdprep.dump(hdr, "appIconChars", dchr)
gdprep.dump(hdr, "appIconPal", dpal)

ir = gdprep.ImageRAM(hdr)
arrow = gdprep.palettize(Image.open("mouse-arrow.png"), 4)
gdprep.dump(hdr, "mouseArrowPal", gdprep.getpal(arrow))
ir.addsprites("mouseArrow", (16, 16), arrow, gdprep.PALETTE4A, center=(8, 8))
gdprep.dump(hdr, "mouseArrowImg", ir.used())
Ejemplo n.º 5
0
                    help='Name of output header file.')
parser.add_argument('--variable-name',
                    default='background',
                    dest='varname',
                    help='Name of the arrays that will be generated')
## update name and replace in call to Image.open() and the check for sheet, update help message
parser.add_argument(dest='sheet', help='')

args = parser.parse_args()

## check if output is a .h file
check_header = args.output.split('.')
if (len(check_header) != 2 or check_header[1] != 'h'):
    raise TypeError('Supply a .h file! You supplied %s.' % args.output)

## check if sheet is a .png file
check_sheet = args.sheet.split('.')
if (len(check_sheet) != 2 or check_sheet[1] != 'png'):
    raise TypeError('Supply a .png file! You supplied %s.' % args.sheet)

(dpic, dchr, dpal) = gdprep.encode(Image.open(args.sheet))
header = open(args.output, "w")

name_pic = args.varname + '_pic'
name_chr = args.varname + '_chr'
name_pal = args.varname + '_pal'

gdprep.dump(header, name_pic, dpic)
gdprep.dump(header, name_chr, dchr)
gdprep.dump(header, name_pal, dpal)
Ejemplo n.º 6
0
if (args.center[1] < 0 or args.center[1] > 16):
    raise ValueError('y coordinate is not within 0 to 16.')

## check file type of sprite sheet
check_sprite = args.sheet.split('.')
if (len(check_sprite) != 2 or check_sprite[1] != 'png'):
    raise TypeError('Supply a .png file! You supplied %s.' % args.sheet)

header = open(args.output, "w")

img_ram = gdprep.ImageRAM(header)

## select the correct palette
if (args.palette_size == 4):
    palette = gdprep.PALETTE4A

elif (args.palette_size == 16):
    palette = gdprep.PALETTE16A

else:
    palette = gdprep.PALETTE256A

img_pal = gdprep.palettize(Image.open(args.sheet), args.palette_size)
img_ram.addsprites(args.varname, (16, 16), img_pal, palette, center = args.center)

name_img = args.varname + '_img'
name_pal = args.varname + '_pal'
gdprep.dump(header, name_img, img_ram.used())
gdprep.dump(header, name_pal, gdprep.getpal(img_pal))

Ejemplo n.º 7
0
def showbg(dstdir, im, pic_chr_pal, name, compress):
    (picdata, chrdata, paldata) = pic_chr_pal
    # tilecode: mapping from tilecoords to code
    tilecode = {}
    stride = im.size[0] / 8
    for i in range(im.size[0] / 8):
        for j in range(im.size[1] / 8):
            tilecode["%s,%s" % (i, j)] = picdata[i + (j * stride)]

    # codetiles: mapping from code to all tiles w that code
    codetiles = [[] for i in range(256)]
    for i in range(im.size[0] / 8):
        for j in range(im.size[1] / 8):
            code = picdata[i + (j * stride)]
            codetiles[code].append((i, j))

    fullscreen = (im.size == (512, 512))

    nbytes = len(chrdata) / 16

    hh = StringIO.StringIO()
    if compress:
        cc = Codec(b_off=9, b_len=3)
        if fullscreen:
            picdata = cc.toarray(picdata.tostring())
        chrdata = cc.toarray(chrdata.tostring())
        paldata = cc.toarray(paldata.tostring())

    gdprep.dump(hh, "%s_pic" % name, picdata)
    gdprep.dump(hh, "%s_chr" % name, chrdata)
    gdprep.dump(hh, "%s_pal" % name, paldata)

    tmpl = common
    if fullscreen and compress:
        tmpl += "  GD.uncompress(RAM_PIC, ${name}_pic);\n"
    else:
        tmpl += """  for (byte y = 0; y < %d; y++)
    GD.copy(RAM_PIC + y * 64, ${name}_pic + y * %d, %d);
""" % (im.size[1] / 8, im.size[0] / 8, im.size[0] / 8)
    if compress:
        tmpl += "  GD.uncompress(RAM_CHR, ${name}_chr);\n"
        tmpl += "  GD.uncompress(RAM_PAL, ${name}_pal);\n"
    else:
        tmpl += "  GD.copy(RAM_CHR, ${name}_chr, sizeof(${name}_chr));\n"
        tmpl += "  GD.copy(RAM_PAL, ${name}_pal, sizeof(${name}_pal));\n"

    tmpl += """}

void loop()
{
}"""

    ploader = string.Template(tmpl).substitute({'name': name})

    nameSpace = {
        'name': name,
        'width': im.size[0],
        'height': im.size[1],
        'nchars': nbytes,
        'nbytes': len(picdata) + len(chrdata) + len(paldata),
        'tilecode': json.dumps(tilecode),
        'codetiles': json.dumps(codetiles),
        'pagecode': open("bgcomplete.js").read(),
        'pde': cgi.escape(ploader),
        'h': hh.getvalue(),
    }
    ct = Template(open("bgcomplete.html").read(), searchList=[nameSpace])
    open("%s/index.html" % dstdir, "w").write(str(ct))

    z = zipfile.ZipFile(dstdir + "/" + name + ".zip", "w")
    z.writestr("%s/%s.pde" % (name, name), ploader)
    z.writestr("%s/%s.h" % (name, name), hh.getvalue())
    z.close()
Ejemplo n.º 8
0
def spencode(dstdir, im, name, compress, size, palette):
    def spproblem(problem):
        nameSpace = {'problem': problem}
        ct = Template(open("sperror.html").read(), searchList=[nameSpace])
        open("%s/index.html" % dstdir, "w").write(str(ct))

    sprites = []
    for y in range(0, im.size[1], size[1]):
        for x in range(0, im.size[0], size[0]):
            sprites.append((x, y))
    hh = StringIO.StringIO()
    ir = gdprep.ImageRAM(hh)
    if '256' in palette:
        ncol = 256
    elif '16' in palette:
        ncol = 16
    else:
        ncol = 4
    im = im.convert("RGBA")
    imp = gdprep.palettize(im, ncol)
    imp.convert("RGBA").save("%s/paletted.png" % dstdir)
    try:
        ir.addsprites(name,
                      size,
                      imp,
                      eval("gdprep.%s" % palette),
                      center=(size[0] / 2, size[1] / 2))
    except OverflowError:
        return spproblem(
            "The sprite sheet uses too much memory.  Some suggestions: use the smallest palette possible; make sure you have set transparency in the source image."
        )

    if compress:
        cc = Codec(b_off=9, b_len=3)
        sprimg = cc.toarray(ir.used().tostring())
    else:
        sprimg = ir.used()

    gdprep.dump(hh, name + "_sprimg", sprimg)
    gdprep.dump(hh, name + "_sprpal", gdprep.getpal(imp))

    def gencode():
        palram = {
            "PALETTE256A": "RAM_SPRPAL",
            "PALETTE256B": "RAM_SPRPAL + 512",
            "PALETTE256C": "RAM_SPRPAL + 1024",
            "PALETTE256D": "RAM_SPRPAL + 1536"
        }
        yield common
        pram = palram.get(palette, palette)
        yield "  GD.copy(%s, ${name}_sprpal, sizeof(${name}_sprpal));" % pram
        if compress:
            yield "  GD.uncompress(RAM_SPRIMG, ${name}_sprimg);"
        else:
            yield "  GD.copy(RAM_SPRIMG, ${name}_sprimg, sizeof(${name}_sprimg));"
        yield ""
        yield "  // For show, randomly scatter the frames on the screen"
        yield "  GD.__wstartspr(0);"
        yield "  for (int anim = 0; anim < %s_FRAMES; anim++)" % name.upper()
        yield "    draw_${name}(random(400), random(300), anim, 0);"
        yield "  GD.__end();"
        yield "}"
        yield ""
        yield "void loop()"
        yield "{"
        yield "}"

    ploader = "\n".join(list(gencode())).replace("${name}", name)
    nameSpace = {
        'name':
        name,
        'sprites':
        sprites,
        'width':
        size[0],
        'height':
        size[1],
        'nbytes':
        len(sprimg),
        'pagecode':
        'sprites=' + json.dumps(sprites) + ';' + open("spcomplete.js").read(),
        'pde':
        cgi.escape(ploader),
        'h':
        hh.getvalue(),
    }

    ct = Template(open("spcomplete.html").read(), searchList=[nameSpace])
    open("%s/index.html" % dstdir, "w").write(str(ct))

    z = zipfile.ZipFile(dstdir + "/" + name + ".zip", "w")
    z.writestr("%s/%s.pde" % (name, name), ploader)
    z.writestr("%s/%s.h" % (name, name), hh.getvalue())
    z.close()
Ejemplo n.º 9
0
#!/usr/bin/python

import Image
import gameduino.prep as gdprep
 
(dpic, dchr, dpal) = gdprep.encode(Image.open("window.png"))
hdr = open("gfx.h", "w")
gdprep.dump(hdr, "windowChars", dchr)
gdprep.dump(hdr, "windowPal", dpal)

(dpic, dchr, dpal) = gdprep.encode(Image.open("button.png"))
gdprep.dump(hdr, "buttonChars", dchr)
gdprep.dump(hdr, "buttonPal", dpal)

(dpic, dchr, dpal) = gdprep.encode(Image.open("appicon.png"))
gdprep.dump(hdr, "appIconPic", dpic)
gdprep.dump(hdr, "appIconChars", dchr)
gdprep.dump(hdr, "appIconPal", dpal)

ir = gdprep.ImageRAM(hdr)
arrow = gdprep.palettize(Image.open("mouse-arrow.png"), 4)
gdprep.dump(hdr, "mouseArrowPal", gdprep.getpal(arrow))
ir.addsprites("mouseArrow", (16, 16), arrow, gdprep.PALETTE4A, center = (8, 8))
gdprep.dump(hdr, "mouseArrowImg", ir.used())
Ejemplo n.º 10
0
#!/usr/bin/python

import Image
import gameduino.prep as gdprep
 
(dpic, dchr, dpal) = gdprep.encode(Image.open("lines.png"))
hdr = open("gfx.h", "w")
#gdprep.dump(hdr, "lines_pic", dpic)
gdprep.dump(hdr, "lines_chr", dchr)
gdprep.dump(hdr, "lines_pal", dpal)

ir = gdprep.ImageRAM(hdr)
players = gdprep.palettize(Image.open("players.png"), 4)
gdprep.dump(hdr, "players_pal", gdprep.getpal(players))
ir.addsprites("players", (16, 16), players, gdprep.PALETTE4A, center = (8, 8))
gdprep.dump(hdr, "players_img", ir.used())
Ejemplo n.º 11
0
parser = argparse.ArgumentParser(description='......something relevant.',
                                 epilog='Arguments can be called using --[first letter]. For example --output can be used by typing --o.')
parser.add_argument('--output', default='background.h', help='Name of output header file.')
parser.add_argument('--variable-name', default='background', dest='varname', 
                    help='Name of the arrays that will be generated')
## update name and replace in call to Image.open() and the check for sheet, update help message
parser.add_argument(dest='sheet', help='')

args = parser.parse_args()

## check if output is a .h file
check_header = args.output.split('.')
if (len(check_header) != 2 or check_header[1] != 'h'):
    raise TypeError('Supply a .h file! You supplied %s.' % args.output)

## check if sheet is a .png file
check_sheet = args.sheet.split('.')
if (len(check_sheet) != 2 or check_sheet[1] != 'png'):
    raise TypeError('Supply a .png file! You supplied %s.' %args.sheet)

(dpic, dchr, dpal) = gdprep.encode(Image.open(args.sheet))
header = open(args.output, "w")

name_pic = args.varname + '_pic'
name_chr = args.varname + '_chr'
name_pal = args.varname + '_pal'

gdprep.dump(header, name_pic, dpic)
gdprep.dump(header, name_chr, dchr)
gdprep.dump(header, name_pal, dpal)