def blocks(GD): blk = Image.open("assets/block25.png") blks = Image.new("RGBA", (32, 32 * 30)) for i in range(30): rblk = blk.rotate(3 * i, Image.BILINEAR) blks.paste(rblk, (0,32 * i)) im = gdprep.palettize(blks, 16) parade(GD, im) GD.microcode(splitscreen) GD.getbrush(mkbg()) GD.paint(0,0) GD.sync_pic() hh = open("../../dna.h", "w") ir = gdprep.ImageRAM(hh) # ir.addsprites("sphere", (16,16), im, gdprep.PALETTE16A, (8,8)) locs = loadspr(ir, im, (16, 16)) sprpal = gdprep.getpal(im) GD.wrstr(gd.PALETTE16A, sprpal) ball = gdprep.palettize(Image.open("assets/lighting00.png"), 256) ir.nxtpage = 63 ir.addsprites("sphere", (16,16), ball, gdprep.PALETTE256A, (8,8)) GD.wrstr(gd.RAM_SPRPAL, gdprep.getpal(ball)) GD.wrstr(gd.RAM_SPRIMG, ir.used()) runsim(GD, locs, 0x0003, blk6x8, 4.0, 120) cp = GD.charpal() GD.fade(cp, 32, 0)
def blocks(GD): blk = Image.open("assets/block25.png") blks = Image.new("RGBA", (32, 32 * 30)) for i in range(30): rblk = blk.rotate(3 * i, Image.BILINEAR) blks.paste(rblk, (0, 32 * i)) im = gdprep.palettize(blks, 16) parade(GD, im) GD.microcode(splitscreen) GD.getbrush(mkbg()) GD.paint(0, 0) GD.sync_pic() hh = open("../../dna.h", "w") ir = gdprep.ImageRAM(hh) # ir.addsprites("sphere", (16,16), im, gdprep.PALETTE16A, (8,8)) locs = loadspr(ir, im, (16, 16)) sprpal = gdprep.getpal(im) GD.wrstr(gd.PALETTE16A, sprpal) ball = gdprep.palettize(Image.open("assets/lighting00.png"), 256) ir.nxtpage = 63 ir.addsprites("sphere", (16, 16), ball, gdprep.PALETTE256A, (8, 8)) GD.wrstr(gd.RAM_SPRPAL, gdprep.getpal(ball)) GD.wrstr(gd.RAM_SPRIMG, ir.used()) runsim(GD, locs, 0x0003, blk6x8, 4.0, 120) cp = GD.charpal() GD.fade(cp, 32, 0)
def test_imageRAM(self): hh = StringIO.StringIO() singles = [Image.open("rock0r-pal16.png"), gdprep.palettize(Image.open("rock0r.png"), 16), gdprep.palettize(Image.open("rock0r-pal16.png"), 16)] for r in singles: print r.mode ir = gdprep.ImageRAM(hh) ir.addsprites("rock0", (16, 16), r, gdprep.PALETTE16A, center = (8,8)) self.assert_(len(hh.getvalue()) > 0) self.assertEqual(len(gdprep.getpal(r)), 16) ir = gdprep.ImageRAM(hh) (walk,) = gdprep.palettize([Image.open("walk.png")], 16) ir.addsprites("walk", (32, 32), walk, gdprep.PALETTE16A, center = (8,32)) print len(ir.used()) print hh.getvalue()
def test_sprites(self): ir = gdprep.ImageRAM(StringIO.StringIO()) (rock0, rock1) = gdprep.palettize( [Image.open("rock0r.png"), Image.open("rock1r.png")], 16) ir.addsprites("rock0", (16, 16), rock0, gdprep.PALETTE16A, center=(8, 8)) ir.addsprites("rock1", (32, 32), rock1, gdprep.PALETTE16A, center=(16, 16)) gd.wr16(gameduino.RAM_PAL, gameduino.RGB(0, 255, 0)) gd.wrstr(gameduino.RAM_SPRIMG, ir.used()) gd.wrstr(gameduino.PALETTE16A, gdprep.getpal(rock0)) for i in range(128): gd.sprite(i, 200 + 20 * (i & 7), 20 * (i / 8), i / 2, gdprep.PALETTE16A[i & 1], 0) (pic, chr, pal) = gdprep.encode(Image.open("platformer.png")) gd.wrstr(gameduino.RAM_CHR, chr) gd.wrstr(gameduino.RAM_PAL, pal) for y in range(32): gd.wrstr(gameduino.RAM_PIC + 64 * y, pic[16 * y:16 * y + 16])
def loadcaption(GD, msg, color=(255, 255, 255)): # caption area is 256x32 # Sprite slots 128 up, and pages 56-63 available # 32 4-color pages im = Image.new("RGBA", (256, 32)) dr = ImageDraw.Draw(im) for sz in sorted(fonts, reverse=True): font = fonts[sz] (w, h) = font.getsize(msg) if (w <= 256) and (h <= 32): break # assert 0, str(sz) x = (256 - w) / 2 y = (32 - h) / 2 for xd in range(-3, 4): for yd in range(-2, 3): dr.text((x + xd, y + yd), msg, font=font, fill=(0, 0, 0)) dr.text((x, y), msg, font=font, fill=color) im = gdprep.palettize(im, 4) ir = gdprep.ImageRAM(None) loadspr(ir, im, (16, 16), 4) GD.wrstr(gd.RAM_SPRIMG + 1024 * 14, ir.used()) sprpal = gdprep.getpal(im) GD.wrstr(gd.PALETTE4A, sprpal)
def loadcaption(GD, msg, color = (255,255,255)): # caption area is 256x32 # Sprite slots 128 up, and pages 56-63 available # 32 4-color pages im = Image.new("RGBA", (256, 32)) dr = ImageDraw.Draw(im) for sz in sorted(fonts, reverse = True): font = fonts[sz] (w,h) = font.getsize(msg) if (w <= 256) and (h <= 32): break # assert 0, str(sz) x = (256 - w) / 2 y = (32 - h) / 2 for xd in range(-3,4): for yd in range(-2,3): dr.text((x+xd,y+yd), msg, font=font, fill=(0,0,0)) dr.text((x,y), msg, font=font, fill=color) im = gdprep.palettize(im, 4) ir = gdprep.ImageRAM(None) loadspr(ir, im, (16,16), 4) GD.wrstr(gd.RAM_SPRIMG + 1024 * 14, ir.used()) sprpal = gdprep.getpal(im) GD.wrstr(gd.PALETTE4A, sprpal)
def test_471fcf9e(self): im = Image.open("471fcf9e.png") imp = gdprep.palettize(im, 16) print gdprep.getpal(imp)
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)
#!/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())
## 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))
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))
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()
def dna(GD): if 0: ramp = Image.open("assets/ramp.png") (dpic, dchr, dpal) = gdprep.encode(ramp) w = ramp.size[0] / 8 h = ramp.size[1] / 8 for y in range(h): for x in range(0, 50, w): GD.m[64 * y + x:64 * y + x + w] = dpic[w * y:w * y + w] GD.sync_pic() GD.wrstr(gd.RAM_CHR, dchr.tostring()) GD.wrstr(gd.RAM_PAL, dpal.tostring()) else: GD.getbrush(Image.open("originals/atparty.png")) GD.paint(0, 0) GD.sync_pic() cp = GD.charpal() GD.fade(cp, 32, 8) im = Image.new("RGBA", (64 * 16, 16)) for i in range(64): im.paste(Image.open("assets/lighting%02d.png" % i), (16 * i, 0)) im = gdprep.palettize(im, 16) im.save("out.png") hh = open("../../dna.h", "w") ir = gdprep.ImageRAM(hh) # ir.addsprites("sphere", (16,16), im, gdprep.PALETTE16A, (8,8)) locs = loadspr(ir, im, (16, 16)) print len(ir.used()) GD.wrstr(gd.RAM_SPRIMG, ir.used()) sprpal = gdprep.getpal(im) GD.wrstr(gd.PALETTE16A, sprpal) GD.wrstr(gd.PALETTE16B, array.array('H', [swap_rb(c) for c in sprpal])) GD.wrstr(gd.RAM_SPRPAL + 0, array.array('H', [swap_rg(sprpal[i & 15]) for i in range(256)])) GD.wrstr(gd.RAM_SPRPAL + 512, array.array('H', [swap_rg(sprpal[i >> 4]) for i in range(256)])) GD.wrstr(gd.RAM_SPRPAL + 1024, array.array('H', [swap_bg(sprpal[i & 15]) for i in range(256)])) GD.wrstr(gd.RAM_SPRPAL + 1536, array.array('H', [swap_bg(sprpal[i >> 4]) for i in range(256)])) """ for i in range(256): // palette 0 decodes low nibble, hence (i & 15) GD.wr16(RAM_SPRPAL + (i << 1), SWAP_RG(rdpal(i & 15))); // palette 1 decodes nigh nibble, hence (i >> 4) GD.wr16(RAM_SPRPAL + 512 + (i << 1), SWAP_RG(rdpal(i >> 4))); """ def draw_sphere(slot, x, y, frame, c): palix = locs[frame][1] & 1 pals = { 0: gdprep.PALETTE16A, 1: gdprep.PALETTE16B, 2: (0, 1), 3: (2, 3) }[c] GD.sprite(i, x, y, locs[frame][0], pals[palix]) GD.hide() if 1: GD.wrstr(gd.PALETTE16A + 15 * 2, "aa") for i in range(64): draw_sphere(i, 64 + 17 * (i & 0xf), 116 + 17 * (i / 16), i, 0) GD.sync_spr() GD.wait() GD.pause() GD.wrstr(gd.PALETTE16A, sprpal) GD.pause() for ii in range(10): cols = [random.randrange(4) for i in range(64)] for i in range(64): draw_sphere(i, 64 + 17 * (i & 0xf), 116 + 17 * (i / 16), i, cols[i]) GD.sync_spr() GD.wait(20) GD.pause() for i in range(256): x = i % 16 y = i / 16 draw_sphere(i, 64 + 18 * x, 6 + 18 * y, i & 63, i >> 6) GD.sync_spr() GD.pause() mcloud = [Ball() for i in range(256)] for ii in range(480): for i, b in enumerate(mcloud): draw_sphere(i, b.x, b.y, i & 63, i >> 6) if ii < 320: [b.move() for b in mcloud] else: [b.fall() for b in mcloud] GD.sync_spr() GD.wait(1) GD.hide() GD.sync_spr() GD.pause() phi = 1.0 for ii in range(60 * 20): rotation(phi, *norm(math.sin(ii / 77.), 1, 1)) phi += 0.027 prj = [project(*p) for p in cloud] prj = sorted(prj, key=lambda p: -p[2]) for i, p in enumerate(prj): x, y, z, c = p frame = max(0, min(63, int(32 + z / 500))) draw_sphere(i, x, y, frame, c) if ii == 0 and (i & 1): GD.sync_spr() GD.wait() GD.sync_spr() GD.wait() GD.pause()
#!/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())
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))
def dna(GD): if 0: ramp = Image.open("assets/ramp.png") (dpic, dchr, dpal) = gdprep.encode(ramp) w = ramp.size[0] / 8 h = ramp.size[1] / 8 for y in range(h): for x in range(0, 50, w): GD.m[64 * y + x:64 * y + x + w] = dpic[w*y:w*y+w] GD.sync_pic() GD.wrstr(gd.RAM_CHR, dchr.tostring()) GD.wrstr(gd.RAM_PAL, dpal.tostring()) else: GD.getbrush(Image.open("originals/atparty.png")) GD.paint(0,0) GD.sync_pic() cp = GD.charpal() GD.fade(cp, 32, 8) im = Image.new("RGBA", (64 * 16, 16)) for i in range(64): im.paste(Image.open("assets/lighting%02d.png" % i), (16*i,0)) im = gdprep.palettize(im, 16) im.save("out.png") hh = open("../../dna.h", "w") ir = gdprep.ImageRAM(hh) # ir.addsprites("sphere", (16,16), im, gdprep.PALETTE16A, (8,8)) locs = loadspr(ir, im, (16, 16)) print len(ir.used()) GD.wrstr(gd.RAM_SPRIMG, ir.used()) sprpal = gdprep.getpal(im) GD.wrstr(gd.PALETTE16A, sprpal) GD.wrstr(gd.PALETTE16B, array.array('H', [swap_rb(c) for c in sprpal])) GD.wrstr(gd.RAM_SPRPAL + 0, array.array('H', [swap_rg(sprpal[i & 15]) for i in range(256)])) GD.wrstr(gd.RAM_SPRPAL + 512, array.array('H', [swap_rg(sprpal[i >> 4]) for i in range(256)])) GD.wrstr(gd.RAM_SPRPAL + 1024, array.array('H', [swap_bg(sprpal[i & 15]) for i in range(256)])) GD.wrstr(gd.RAM_SPRPAL + 1536, array.array('H', [swap_bg(sprpal[i >> 4]) for i in range(256)])) """ for i in range(256): // palette 0 decodes low nibble, hence (i & 15) GD.wr16(RAM_SPRPAL + (i << 1), SWAP_RG(rdpal(i & 15))); // palette 1 decodes nigh nibble, hence (i >> 4) GD.wr16(RAM_SPRPAL + 512 + (i << 1), SWAP_RG(rdpal(i >> 4))); """ def draw_sphere(slot, x, y, frame, c): palix = locs[frame][1] & 1 pals = { 0: gdprep.PALETTE16A, 1: gdprep.PALETTE16B, 2: (0, 1), 3: (2, 3)}[c] GD.sprite(i, x, y, locs[frame][0], pals[palix]) GD.hide() if 1: GD.wrstr(gd.PALETTE16A + 15*2, "aa") for i in range(64): draw_sphere(i, 64 + 17 * (i & 0xf), 116 + 17 * (i / 16), i, 0) GD.sync_spr() GD.wait() GD.pause() GD.wrstr(gd.PALETTE16A, sprpal) GD.pause() for ii in range(10): cols = [random.randrange(4) for i in range(64)] for i in range(64): draw_sphere(i, 64 + 17 * (i & 0xf), 116 + 17 * (i / 16), i, cols[i]) GD.sync_spr() GD.wait(20) GD.pause() for i in range(256): x = i % 16 y = i / 16 draw_sphere(i, 64 + 18 * x, 6 + 18 * y, i & 63, i >> 6) GD.sync_spr() GD.pause() mcloud = [Ball() for i in range(256)] for ii in range(480): for i,b in enumerate(mcloud): draw_sphere(i, b.x, b.y, i & 63, i >> 6) if ii < 320: [b.move() for b in mcloud] else: [b.fall() for b in mcloud] GD.sync_spr() GD.wait(1) GD.hide() GD.sync_spr() GD.pause() phi = 1.0 for ii in range(60 * 20): rotation(phi, *norm(math.sin(ii / 77.), 1, 1)) phi += 0.027 prj = [project(*p) for p in cloud] prj = sorted(prj, key=lambda p:-p[2]) for i,p in enumerate(prj): x,y,z,c = p frame = max(0, min(63, int(32 + z / 500))) draw_sphere(i, x, y, frame, c) if ii == 0 and (i & 1): GD.sync_spr() GD.wait() GD.sync_spr() GD.wait() GD.pause()
#!/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())