~facundo/enjuewemela/trunk

« back to all changes in this revision

Viewing changes to enjuewemela/cocos/tools/gentileset.py

  • Committer: facundo at com
  • Date: 2010-11-20 01:42:31 UTC
  • mfrom: (62.1.3 lint-issues)
  • Revision ID: facundo@taniquetil.com.ar-20101120014231-b2tkyc3mwr84ggcc
Project reorder and lint issues

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
'''Cocos tileset generator
2
 
 
3
 
Usage: gentileset.py  <image_name> <tile_width> <tile_height> [<output.xml>]
4
 
 
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.
8
 
 
9
 
More info on the schema of the generated xml can be found on cocos.tiles
10
 
documentation.
11
 
 
12
 
'''
13
 
 
14
 
from xml.dom.minidom import getDOMImplementation
15
 
 
16
 
def build_tileset_xml(image_name, image, tw, th):
17
 
    h_tiles = image.width / tw
18
 
    v_tiles = image.height / th
19
 
    
20
 
    dom = getDOMImplementation()
21
 
 
22
 
    doc = dom.createDocument(None, "resource", None)
23
 
    top_element = doc.documentElement
24
 
    
25
 
    atlas_element = doc.createElement('imageatlas')
26
 
    atlas_element.setAttribute('size', '%dx%d' % (tw, th))
27
 
    atlas_element.setAttribute('file', image_name)
28
 
    
29
 
    tileset_element = doc.createElement('tileset')
30
 
    
31
 
    for y in range(v_tiles):
32
 
        for x in range(h_tiles):
33
 
            id = "t%d" % (y*h_tiles + x)
34
 
            
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)
39
 
            
40
 
            tile_elm = doc.createElement('tile')
41
 
            tile_elm.setAttribute('id', str(id))
42
 
            
43
 
            image_ref_elm = doc.createElement('image')
44
 
            image_ref_elm.setAttribute('ref', 'i-%s' % id)
45
 
            
46
 
            tile_elm.appendChild(image_ref_elm)
47
 
            tileset_element.appendChild(tile_elm)
48
 
    
49
 
    top_element.appendChild(atlas_element)
50
 
    top_element.appendChild(tileset_element)
51
 
    
52
 
    return doc
53
 
    
54
 
 
55
 
if __name__ == "__main__":
56
 
    import sys
57
 
    import pyglet
58
 
    
59
 
    def exit(msg=None):
60
 
        if msg: print msg
61
 
        print "Usage: %s <image_name> <tile_width> <tile_height> [<output.xml>]" % sys.argv[0]
62
 
        sys.exit(1)
63
 
    
64
 
    if len(sys.argv) < 4:
65
 
        exit()
66
 
    
67
 
    image_name = sys.argv[1]
68
 
    try:
69
 
        tile_w, tile_h = int(sys.argv[2]), int(sys.argv[3])
70
 
    except ValueError:
71
 
        exit("<tile_width> and <tile_height> should be integers.")
72
 
    
73
 
    try:
74
 
        image = pyglet.image.load(image_name)
75
 
    except IOError:
76
 
        exit("Invalid image file '%s'" % image_name)
77
 
        
78
 
    try:
79
 
        output = file(sys.argv[4], 'w')
80
 
    except IndexError:
81
 
        output = None
82
 
        
83
 
    doc = build_tileset_xml(image_name, image, tile_w, tile_h)
84
 
    print >>output, doc.toprettyxml()
85
 
    output.close()   
86
 
    
 
 
b'\\ No newline at end of file'