Search code examples
tmx

Converting hd TMX Tilemap to sd, should I do this manually?


I have a tilemap with 960*640 pixels using 64*64 tiles for hd retina display. Now, I need a tilemap for sd display.(I guess that it should be 480*320 pixels using 32*32 tiles)

So, is there easy way to go?

Finding tools like 'Unretiner' in Mac app store.

'hd img -> sd img' by Unretiner
'hd tmx -> sd tmx' by ??

thank u.


Solution

  • Here is a Python script I used to generate sd .tmx from hd .tmx. You can use it by following shell command.

    >python change_tmx_numbers.py <directory_which_contains_hd_tmx_files> <output_directory>
    

    The script:

    #!/usr/bin/python
    import os
    import shutil
    import sys
    import xml.etree.cElementTree as ET
    import re
    
    
    
    def npath(path):
      if path[-1] == '/':
        return path[:-1]
      return path;
    
    def subdir(*paths):
      return '/'.join(paths)
    
    def changeText(text):
      print text
      pointm = '\{(?P<x>-?\d+), (?P<y>-?\d+)\}'
      framem = '\{\{(?P<x>-?\d+), (?P<y>-?\d+)\}, \{(?P<width>\d+), (?P<height>\d+)\}\}'
      m = re.match(pointm, text)
      if m != None:
        rt = '{%d, %d}' % tuple([int(f) / 2 for f in m.groups()])
        print 'change %s -> %s' % (text, rt)
        return rt
      else:
        m = re.match(framem, text)
        if m != None:
          rt = '{{%d, %d}, {%d, %d}}' % tuple([int(f) / 2 for f in m.groups()])
          print 'change %s -> %s' % (text, rt)
          return rt
        else:
          print 'text: \"' + text + '\" is not matched'
          return text
    
    def resizeElement(element):
      elems = list(element)
      if(len(elems) == 0 and element.text != None):
        element.text = changeText(element.text)
      else:
        for el in elems:
          resizeElement(el)
    
    def resizeAttToHalf(attrib, keys):
      for key in keys:
        if key in attrib:
          attrib[key] = str(int(attrib[key]) / 2)
      return attrib
    
    def resizeMap(map):
      map.attrib = resizeAttToHalf(map.attrib, ['tilewidth', 'tileheight'])
    
    
    def resizeImage(im):
      im.attrib = resizeAttToHalf(im.attrib, ['width', 'height'])
      im.attrib['source'] = im.attrib['source'].replace('-hd', '')
    
    def resizeTileset(ts):
      ts.attrib = resizeAttToHalf(ts.attrib, ['tilewidth', 'tileheight'])
      for image in ts.findall('image'): 
        resizeImage(image)
    
    def resizeProperty(prop):
      if 'name' in prop.attrib and prop.attrib['name'] == 'points':
        points = prop.attrib['value'].split(' ')
        nps = [','.join([str(int(p.split(',')[0])/2), str(int(p.split(',')[1])/2)]) for p in points]
        prop.attrib['value'] = ' '.join(nps)
    
    def resizePolygon(prop):
      points = prop.attrib['points'].split(' ')
      nps = [','.join([str(int(p.split(',')[0])/2), str(int(p.split(',')[1])/2)]) for p in points]
      prop.attrib['points'] = ' '.join(nps)
    
    def resizeObject(obj):
      obj.attrib = resizeAttToHalf(obj.attrib, ['x', 'y', 'width', 'height'])
      for props in obj.findall('properties'):
        for prop in props.findall('property'):
          resizeProperty(prop)
      for poly in obj.findall('polygon'):
        resizePolygon(poly)
    
    def resizeplist(plist, dest):
      doc = ET.parse(plist)
      map = doc.getroot()
      resizeMap(map)
      for tileset in map.findall('tileset'):
        resizeTileset(tileset)
    
      for objectg in map.findall('objectgroup'):
        for obj in objectg.findall('object'):
          resizeObject(obj)
    
      destf = open(dest, 'w')
      prologue = '<?xml version="1.0" encoding="UTF-8"?>\n<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">\n'
      destf.write(prologue)
      doc.write(destf)
      destf.close()
    
    
    def walkdir(path, dest):
      files =  [f for f in os.listdir(path) if not os.path.isdir(subdir(path, f))]
      dirs = [d for d in os.listdir(path) if  os.path.isdir(subdir(path, d))]
      for f in files:
        if (f.endswith('-hd.tmx')):
          resizeplist(subdir(path, f), subdir(path, f.replace('-hd', '')))
    #  for d in dirs:
    #    walkdir(subdir(path, d), subdir(dest, d))
    
    
    
    if __name__ == "__main__":
      path = npath(sys.argv[1])
      dest = npath(sys.argv[2])
      walkdir(path, dest)