-
Notifications
You must be signed in to change notification settings - Fork 0
/
tmxHandler.py
205 lines (175 loc) · 5.86 KB
/
tmxHandler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
from xml.dom import minidom, Node
import base64
import gzip
import StringIO
import os.path
import pygame
#-------------------------------------------------------------------------------
def decode_base64(in_str):
return base64.decodestring(in_str)
def encode_base64(in_str):
return base64.encodestring(in_str)
def decompress_gzip(in_str):
compressed_stream = StringIO.StringIO(in_str)
gzipper = gzip.GzipFile(fileobj=compressed_stream)
return gzipper.read()
def compress_gzip(in_str):
compressed_stream = StringIO.StringIO()
gzipper = gzip.GzipFile(fileobj=compressed_stream, mode="wb")
gzipper.write(in_str)
gzipper.close()
return compressed_stream.getvalue()
def getCode(val):
code = chr(int(val & 255))
code = code + chr(int((val>>8) & 255))
code = code + chr(int((val>>16) & 255))
code = code + chr(int((val>>24) & 255))
return code
def decode(encoded_content):
s = encoded_content
if encoded_content:
s = decode_base64(s)
s = decompress_gzip(s)
#print "Content after decode and decompress : %s" % (s)
decoded_content = []
for idx in xrange(0, len(s), 4):
val = ord(str(s[idx])) | (ord(str(s[idx + 1])) << 8) | \
(ord(str(s[idx + 2])) << 16) | (ord(str(s[idx + 3])) << 24)
decoded_content.append(val)
return decoded_content
def encode(content_list):
"Encodes an array of ints corresponding to tiles id"
coded_content = ""
for n in content_list:
coded_content = coded_content + getCode(n)
coded_content = compress_gzip(coded_content)
coded_content = encode_base64(coded_content)
return coded_content
class Tileset(object):
def __init__(self, name, tileWidth, tileHeight, firstgid, source):
self.name = name
self.tileWidth = int(tileWidth)
self.tileHeight = int(tileHeight)
self.firstgid = int(firstgid)
self.imageSource = source
self.load()
def load(self):
self.image = pygame.image.load(self.imageSource).convert_alpha()
self.frames = {}
self.width = self.image.get_width()
self.height = self.image.get_height()
self.nbLines = self.height / self.tileHeight
self.nbCols = self.width / self.tileWidth
nb = self.firstgid
for y in range(self.nbLines):
for x in range(self.nbCols):
rect = (x*self.tileWidth, y*self.tileHeight, self.tileWidth, self.tileHeight)
img = self.image.subsurface(rect)
self.frames[nb] = img
nb = nb + 1
class TileLayer(object):
def __init__(self, name, width, height, data = []):
self.name = name
self.width = width
self.height = height
self.data = data
#self.tiles = [] # [x][y] -> gid
self.clearTiles()
self.makeTiles()
def clearTiles(self):
self.tiles = []
for x in range(self.width):
col = []
for y in range(self.height):
col.append(0)
self.tiles.append(col)
def makeTiles(self):
X = 0
Y = 0
for gid in self.data:
#if gid != 0:
self.tiles[X][Y]=gid
X += 1
if X == self.width:
X = 0
Y += 1
#-------------------------------------------------------------------------------
class TmxMapData(object):
def __init__(self):
self.myMap = None
def load(self, filename):
self.myMap = self.parseMap(filename)
self.width = int(self.myMap.getAttribute("width"))
self.height = int(self.myMap.getAttribute("height"))
self.tileWidth = int(self.myMap.getAttribute("tilewidth"))
self.tileHeight = int(self.myMap.getAttribute("tileheight"))
self.tilesets = self.getTilesets(self.myMap)
self.frames = {} # gid : img frame
self.offsets = {} # gid : Y tile offset
self.maxTileWidth = 0
self.maxTileHeight = 0
for t in self.tilesets:
if t.tileWidth > self.maxTileWidth : self.maxTileWidth = t.tileWidth
if t.tileHeight > self.maxTileHeight : self.maxTileHeight = t.tileHeight
self.frames.update(t.frames)
offset = t.tileHeight - self.tileHeight
for nb in t.frames:
self.offsets[nb] = offset
#print "gid %s, offset %s" % (nb, offset)
#del self.tilesets # all images are now in self.frames
self.layerImageWidth = self.width * self.tileWidth
self.layerImageHeight = self.height * self.tileHeight
def getChildNodes(self, node, name):
children = []
for child in node.childNodes:
if (child.nodeType == Node.ELEMENT_NODE and child.nodeName == name):
children.append(child)
return children
def parseMap(self, filename):# return map node
dom = minidom.parseString(open(filename, "rb").read())
for node in self.getChildNodes(dom, "map"):
return node
return None
def getTilesets(self, myMap):
tilesets = []
for node in self.getChildNodes(self.myMap, "tileset"):
name = node.getAttribute("name")
width = node.getAttribute("tilewidth")
height = node.getAttribute("tileheight")
firstgid = node.getAttribute("firstgid")
for child in self.getChildNodes(node, "image"):
source = child.getAttribute("source")
# poor hack to fix the image path found in tmx files :
source = source.replace("../", "")
#print "name : %s, W : %s, H : %s, gid : %s" % (name, width, height, firstgid)
tileset = Tileset(name, width, height, firstgid, source)
tilesets.append(tileset)
return tilesets
def getLayerNames(self):
data = []
layers = self.getChildNodes(self.myMap, "layer")
for layer in layers:
name = layer.getAttribute("name")
data.append(name)
return data
def getLayerData(self, layername):
layers = self.getChildNodes(self.myMap, "layer")
for layer in layers:
name = layer.getAttribute("name")
if name == layername:
layerData = self.getChildNodes(layer, "data")
return layerData[0].childNodes[0]._get_data()
return None
def getLayerDataDic(self, layername):
data = {}
layers = self.getChildNodes(self.myMap, "layer")
for layer in layers:
name = layer.getAttribute("name")
if name == layername:
layerData = self.getChildNodes(layer, "data")
#data.append(layerData[0].childNodes[0]._get_data())
data[layername] = layerData[0].childNodes[0]._get_data()
return data