Sign up ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

: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 GIDs on the same place that I look for pattern tiles. anyway, I have two screenshots to illustrate.

Where I am right now: bad

What I want to achieve: good

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"""
    if mapfilepath:
        self.__tmxmapfile = common.settings.joinpaths(
                                        "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)):

        # 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)):

        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)"GIDs de objetos: {0}".format(objgid))"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 \
            filename = os.path.basename(tile.source)

            w, h = tilesets[-1].size
            totalheight = sum(heightlist[1:], 0)
            real_gid = tile.firstgid - 1
            logging.debug("tile.firstgid: {0}".format(

            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,
                    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.y + tileheight))
                    quad = (v1, v2, v3, v4,)

                elif gids == []:
                    # This GID is not used for anything.

                # 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! :)

share|improve this question

1 Answer 1

up vote 1 down vote accepted

and as I should have guessed, this is related to a bug on pytmx, more details here:

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.