Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Help understanding "TileMap" Class in tutorial  (Read 1496 times)

0 Members and 1 Guest are viewing this topic.

Banana Snooters

  • Newbie
  • *
  • Posts: 1
    • View Profile
Help understanding "TileMap" Class in tutorial
« on: June 28, 2014, 01:05:19 am »
Hello, I'm starting to learn SFML 2.0 and have been working through the tutorials. I'm trying to understand the tilemap example: http://www.sfml-dev.org/tutorials/2.0/graphics-vertex-array.php
I've implemented it into my project and it works perfectly, but there's a part that I don't understand...

// populate the vertex array, with one quad per tile
        for (unsigned int i = 0; i < width; ++i)
            for (unsigned int j = 0; j < height; ++j)
            {
                // get the current tile number
                int tileNumber = tiles[i + j * width];

                // find its position in the tileset texture
                int tu = tileNumber % (m_tileset.getSize().x / tileSize.x);
                int tv = tileNumber / (m_tileset.getSize().x / tileSize.x);

                // get a pointer to the current tile's quad
                sf::Vertex* quad = &m_vertices[(i + j * width) * 4];

so where it says "//find its position in tileset texture", can someone explain what exactly is going on here?
my project has 20x20 pixel tiles, and the texture is 640x400. So 640 pixels/20 pixels =32 pixels. but how does tileNumber / or % by 32 find the position of the tile in the texture??

I  understand how tu and tv are used later when assigning texCoords to the verticies ( they seem to act as multipliers of the tilesize.x), but how do the definitions of tu and tv result in integers corresponding to the location of tile in the texture??

Thanks for any help you can provide me!
« Last Edit: June 28, 2014, 01:15:21 am by Banana Snooters »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Help understanding "TileMap" Class in tutorial
« Reply #1 on: June 28, 2014, 03:19:16 pm »
The tile number is a single dimension but the texture that holds the tiles can store tiles in two dimensions. That is, that the tile numbers wrap on to other other rows.

Here's a simple example:

Tile numbers: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11.

Tiles in texture:
0, 1, 2, 3,
4, 5, 6, 7,
8, 9, 10, 11.

tu works out the column.
tv works out the row.

For tile number 9, the tu would be column 1 and tv would be row 2 (both start at zero).
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything