1
'''Cocos tileset generator
3
Usage: gentileset.py <image_name> <tile_width> <tile_height> [<output.xml>]
5
This script generates a tileset xml file from a givend image, tile width and
6
height. If an output parameter is provided the generated xml is printed to
7
this file, if it's not the output is written to the standard output.
9
More info on the schema of the generated xml can be found on cocos.tiles
14
from xml.dom.minidom import getDOMImplementation
16
def build_tileset_xml(image_name, image, tw, th):
17
h_tiles = image.width / tw
18
v_tiles = image.height / th
20
dom = getDOMImplementation()
22
doc = dom.createDocument(None, "resource", None)
23
top_element = doc.documentElement
25
atlas_element = doc.createElement('imageatlas')
26
atlas_element.setAttribute('size', '%dx%d' % (tw, th))
27
atlas_element.setAttribute('file', image_name)
29
tileset_element = doc.createElement('tileset')
31
for y in range(v_tiles):
32
for x in range(h_tiles):
33
id = "t%d" % (y*h_tiles + x)
35
image_elm = doc.createElement('image')
36
image_elm.setAttribute('id', 'i-%s' % id)
37
image_elm.setAttribute('offset', '%d,%d' % (x*th, y*tw))
38
atlas_element.appendChild(image_elm)
40
tile_elm = doc.createElement('tile')
41
tile_elm.setAttribute('id', str(id))
43
image_ref_elm = doc.createElement('image')
44
image_ref_elm.setAttribute('ref', 'i-%s' % id)
46
tile_elm.appendChild(image_ref_elm)
47
tileset_element.appendChild(tile_elm)
49
top_element.appendChild(atlas_element)
50
top_element.appendChild(tileset_element)
55
if __name__ == "__main__":
61
print "Usage: %s <image_name> <tile_width> <tile_height> [<output.xml>]" % sys.argv[0]
67
image_name = sys.argv[1]
69
tile_w, tile_h = int(sys.argv[2]), int(sys.argv[3])
71
exit("<tile_width> and <tile_height> should be integers.")
74
image = pyglet.image.load(image_name)
76
exit("Invalid image file '%s'" % image_name)
79
output = file(sys.argv[4], 'w')
83
doc = build_tileset_xml(image_name, image, tile_w, tile_h)
84
print >>output, doc.toprettyxml()
b'\\ No newline at end of file'