Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

mismatch between objects' GID and his correct texture image #9

Closed
shackra opened this Issue · 6 comments

2 participants

@shackra

Hello!

on my engine, I am loading objects as sprites. I see a mismatch between the GID and the image rectangle that is suppose to use, not sure if this is a bug or I don't know how to use this module to load tmx objects as sprites.

Here, a test game:
Captura de pantalla de 2013-02-15 17:05:51

The map on Tiled:
Captura de pantalla de 2013-02-15 17:20:08

Here is the code that I use to A) load all the tiles of the map and B) to load every object as a sprite:

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
@bitcraft
Owner
@shackra
@bitcraft
Owner
@bitcraft
Owner
@shackra

Ok, I changed some parts of my code and the game test works as expected!

Captura de pantalla de 2013-03-06 12:50:18

There stills more work for me that needs to be done:
1

thank you very much! :D

@shackra shackra closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.