:D Hello :D
I am currently developing a game engine. I'm using maps designed with Tiled and I placed several objects on a test map to develop the feature of loading objects as sprites/entities and I'm not able to get the correct texture rectangle for certain GID
(with correct, I mean the chunk of image that I place as an object on the map), even thought that I'm looking for such GID
s on the same place that I look for pattern tiles. anyway, I have two screenshots to illustrate.
Where I am right now: bad http://ompldr.org/vaGg4eA/Captura%20de%20pantalla%20de%202013-02-15%2017:05:51.png
What I want to achieve: good http://ompldr.org/vaGg5MA/Captura%20de%20pantalla%20de%202013-02-15%2017:20:08.png
There is the relevant source code:
def loadmap(self, mapfilepath=None):
""" Load the map of the corresponding scene.
Not need to implement this method.
All map files must be read in
tmx format, Tiled Map Editor Software
http://www.mapeditor.org/"""
if mapfilepath:
self.__tmxmapfile = common.settings.joinpaths(
common.settings.getrootfolder(),
"maps", mapfilepath)
self.tmxmapdata = tmxloader.load_tmx(self.__tmxmapfile)
heightlist = []
widthlist = []
tilesets = []
# These list of vertexarrays carry only visible vertices,
# By layer.
self.__vertexarraytodraw = []
for i in xrange(0, len(self.tmxmapdata.tilelayers)):
self.__vertexarraytodraw.append(
sfml.VertexArray(sfml.PrimitiveType.QUADS))
# Add a list of empty lists to place sprites
# Each empty list represents a layer.
self.sprites = []
for i in xrange(0, len(self.tmxmapdata.tilelayers)):
self.sprites.append([])
objgid = []
for gid in self.tmxmapdata.getObjects():
# Registers the actual GID.
rgid = self.tmxmapdata.registerGID(gid.gid)
if rgid not in objgid and rgid != 0:
# The returned GID is the GID used internally by pytmx
objgid.append(rgid)
logging.info("GIDs de objetos: {0}".format(objgid))
logging.info("Cargando las baldosas del escenario...")
# Load all the tiles from the set of tiles
# Based on code written by bitcraft.
self.tmxmapdata.images = [0] * self.tmxmapdata.maxgid
for firstgid, tile in sorted((tile.firstgid, tile) for tile in \
self.tmxmapdata.tilesets):
filename = os.path.basename(tile.source)
tilesets.append(
media.loadimg("maps/tilesets/{0}".format(filename)))
w, h = tilesets[-1].size
widthlist.append(w)
heightlist.append(h)
totalheight = sum(heightlist[1:], 0)
real_gid = tile.firstgid - 1
logging.debug("tile.firstgid: {0}".format(
tile.firstgid))
tilewidth = tile.tilewidth + tile.spacing
tileheight = tile.tileheight + tile.spacing
# some tileset images may be slightly larger than the tile area
# ie: may include a banner, copyright, ect.
# this compensates for that
width = ((int((w - tile.margin * 2) + tile.spacing) / tilewidth) \
* tilewidth) - tile.spacing
height = ((int((h - tile.margin * 2) + tile.spacing) / tileheight) \
* tileheight) - tile.spacing
# using product avoids the overhead of nested loops
p = product(xrange(tile.margin, height+tile.margin, tileheight),
xrange(tile.margin, width+tile.margin, tilewidth))
for (y, x) in p:
real_gid += 1
gids = self.tmxmapdata.mapGID(real_gid)
quad = None
# This GID is used somewhere in the scene?
if gids and gids[0][0] in objgid:
# This GID belongs to an object that will be drawn
# Within the scenario.
texpos = sfml.Vector2(float(x), float(y + totalheight))
texsize = sfml.Vector2(tilewidth, tileheight)
quad = sfml.Rectangle(texpos, texsize)
# sfml.Rectangle(
# (x, y + totalheight),
# (tilewidth,
# totalheight + tileheight))
elif gids:
# This GID belongs to a tile.
v1 = sfml.Vertex((0, 0), None, sfml.Vector2(
float(x), float(y + totalheight)))
v2 = sfml.Vertex((0, 0), None, sfml.Vector2(
v1.tex_coords.x + tilewidth,
v1.tex_coords.y))
v3 = sfml.Vertex((0, 0), None, sfml.Vector2(
v1.tex_coords.x + tilewidth,
v1.tex_coords.y + tileheight))
v4 = sfml.Vertex((0, 0), None, sfml.Vector2(
v1.tex_coords.x,
v1.tex_coords.y + tileheight))
quad = (v1, v2, v3, v4,)
elif gids == []:
# This GID is not used for anything.
continue
# Stores the object, no matters if a tuple with vertexs
# or a sfml's rectangle
for gid, flag in gids:
logging.debug("real_gid: {0}, gid:"
" {1}, flag: {2}".format(real_gid,
gid, flag))
self.tmxmapdata.images[gid] = quad
Thanks! :)