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

Author Topic: (Solved) VertexArray Tilemap not drawing correctly  (Read 6338 times)

0 Members and 1 Guest are viewing this topic.

mateandmetal

  • Full Member
  • ***
  • Posts: 171
  • The bird is the word
    • View Profile
    • my blog
(Solved) VertexArray Tilemap not drawing correctly
« on: February 17, 2012, 09:19:40 pm »
Hi There!

Somewhere in the forum, Laurent wrote this example:

Code: [Select]
Here is what a typical tilemap would look like with the new API:


const int size = 32;

// load the texture containing all the tiles
sf::Texture tileset;
tileset.LoadFromFile("tileset.png");

// create the map
sf::VertexArray map(sf::Quads, width * height * 4);

for (int x = 0; x < width; ++x)
    for (int y = 0; y < height; ++y)
    {
        int current = (x + y * width) * 4;

        // define the position of the 4 points of the current tile
        map[current + 0].Position = sf::Vector2f((x + 0) * size, (y + 0) * size);
        map[current + 1].Position = sf::Vector2f((x + 0) * size, (y + 1) * size);
        map[current + 2].Position = sf::Vector2f((x + 1) * size, (y + 1) * size);
        map[current + 3].Position = sf::Vector2f((x + 1) * size, (y + 0) * size);

        // define the texture coordinates of the 4 points of the current tile
        int tx = /* X index of the tile in the tileset */;
        int ty = /* Y index of the tile in the tileset */;
        map[current + 0].TexCoords = sf::Vector2i((tx + 0) * size, (ty + 0) * size);
        map[current + 1].TexCoords = sf::Vector2i((tx + 0) * size, (ty + 1) * size);
        map[current + 2].TexCoords = sf::Vector2i((tx + 1) * size, (ty + 1) * size);
        map[current + 3].TexCoords = sf::Vector2i((tx + 1) * size, (ty + 0) * size);
    }

// draw the map
window.Draw(map, &tileset);



I´m trying to use something like this on my own Tilemap class. I´m loading the "desert" example map from the Tiled Map Editor. Here is my drawing function:


Code: [Select]
void Tilemap::DibujarMapa (sf::RenderWindow &ventana) {


    // Creo un array de vertices para dibujar el mapa mas rapido
    sf::VertexArray mapa (sf::Quads, AnchoMapa * AltoMapa * 4);


    int NumCols = TileSheet.GetWidth() / AnchoTile;



    for (int x = 0; x < AnchoMapa; x++) {
        for (int y = 0; y < AltoMapa; y++) {


            int VerticeActual = (x + y * AnchoMapa) * 4;

            // posicion de los cuatro vertices
            mapa[VerticeActual + 0].Position = sf::Vector2f((x + 0) * AnchoTile, (y + 0) * AltoTile);
            mapa[VerticeActual + 1].Position = sf::Vector2f((x + 0) * AnchoTile, (y + 1) * AltoTile);
            mapa[VerticeActual + 2].Position = sf::Vector2f((x + 1) * AnchoTile, (y + 1) * AltoTile);
            mapa[VerticeActual + 3].Position = sf::Vector2f((x + 1) * AnchoTile, (y + 0) * AltoTile);


            int TileActual = ArrayDeTiles [ x + y * AnchoMapa ];

            // posicion de los vertices de la textura
            int tx = (TileActual % NumCols) * AnchoTile;
            int ty = (TileActual / NumCols) * AltoTile;

            mapa[VerticeActual + 0].TexCoords = sf::Vector2f((tx + 0) * AnchoTile, (ty + 0) * AltoTile);
            mapa[VerticeActual + 1].TexCoords = sf::Vector2f((tx + 0) * AnchoTile, (ty + 1) * AltoTile);
            mapa[VerticeActual + 2].TexCoords = sf::Vector2f((tx + 1) * AnchoTile, (ty + 1) * AltoTile);
            mapa[VerticeActual + 3].TexCoords = sf::Vector2f((tx + 1) * AnchoTile, (ty + 0) * AltoTile);

        } // for

    } // otro for


    // Dibujar los vertices con la textura indicada
    ventana.Draw(mapa, &TileSheet);
}



AnchoMapa = map width in Tiles
AltoMapa = map height in Tiles
AnchoTile = Tile width
AltoTile = tile height
Tilesheet is my sf::Texture (the tileset image)
ArrayDeTiles is a int* array containing the tile data loaded from XML file


I think tx, ty is the texture coordinate for the tile I need to draw.. So I´m using the same calculation for my spritesheet drawing method. I know I´m doing something wrong in this calculation, but I dont get it   :?

here is a screenshot:
http://postimage.org/image/qfnifjuap/


What is the correct method for calculating tx, ty?

Using SFML 2.0 21/1, Code::Blocks
(sorry for my bad eng)
- Mate (beverage) addict
- Heavy metal addict _lml
- SFML 2 addict
- My first (and free) game: BichingISH!

Elgan

  • Jr. Member
  • **
  • Posts: 77
    • AOL Instant Messenger - Flat+1,+17+st+Cl
    • View Profile
(Solved) VertexArray Tilemap not drawing correctly
« Reply #1 on: February 17, 2012, 09:24:00 pm »
would you like my Tiled map loader? edited from one I found on the forums (forgot who sorry), but I added support for full layers, objects, tilemaps etc.

it uses tinyxml, loads, displays them, I havent looked over it again yet, as I will eventually be changing it from tinyxml, and other map info.


at the moment It draws just one background, then tiles over, for speed, so you may want to easily change that.

BlueMagic

  • Newbie
  • *
  • Posts: 49
    • View Profile
(Solved) VertexArray Tilemap not drawing correctly
« Reply #2 on: February 17, 2012, 10:45:05 pm »
I believe the problem is this:
Code: [Select]
         
            int tx = (TileActual % NumCols) * AnchoTile;
            int ty = (TileActual / NumCols) * AltoTile;

            mapa[VerticeActual + 0].TexCoords = sf::Vector2f((tx + 0) * AnchoTile, (ty + 0) * AltoTile);
            mapa[VerticeActual + 1].TexCoords = sf::Vector2f((tx + 0) * AnchoTile, (ty + 1) * AltoTile);
            mapa[VerticeActual + 2].TexCoords = sf::Vector2f((tx + 1) * AnchoTile, (ty + 1) * AltoTile);
            mapa[VerticeActual + 3].TexCoords = sf::Vector2f((tx + 1) * AnchoTile, (ty + 0) * AltoTile);


tx and ty should be the integer index of the tile in the tile set, so that when you calculate the texcoord you multiply for the width. But what you are doing is multplying twice (first in the declaration of tx and then in the calculation of the texcoord). So I would change that to :

Code: [Select]
         
            int tx = (TileActual % NumCols);
            int ty = (TileActual / NumCols) ;

            mapa[VerticeActual + 0].TexCoords = sf::Vector2f((tx + 0) * AnchoTile, (ty + 0) * AltoTile);
            mapa[VerticeActual + 1].TexCoords = sf::Vector2f((tx + 0) * AnchoTile, (ty + 1) * AltoTile);
            mapa[VerticeActual + 2].TexCoords = sf::Vector2f((tx + 1) * AnchoTile, (ty + 1) * AltoTile);
            mapa[VerticeActual + 3].TexCoords = sf::Vector2f((tx + 1) * AnchoTile, (ty + 0) * AltoTile);

mateandmetal

  • Full Member
  • ***
  • Posts: 171
  • The bird is the word
    • View Profile
    • my blog
(Solved) VertexArray Tilemap not drawing correctly
« Reply #3 on: February 18, 2012, 03:00:58 am »
Thanks BlueMagic... much better now !!

http://postimage.org/image/o6caut44r/

 now I dont know why the weird lines.....  :?  :oops:
- Mate (beverage) addict
- Heavy metal addict _lml
- SFML 2 addict
- My first (and free) game: BichingISH!

BlueMagic

  • Newbie
  • *
  • Posts: 49
    • View Profile
(Solved) VertexArray Tilemap not drawing correctly
« Reply #4 on: February 18, 2012, 04:05:59 pm »
It looks weird, but if I were to say I'd say it's a problem of the tile set itself.

mateandmetal

  • Full Member
  • ***
  • Posts: 171
  • The bird is the word
    • View Profile
    • my blog
(Solved) VertexArray Tilemap not drawing correctly
« Reply #5 on: February 18, 2012, 08:44:14 pm »
Solved! .. The tileset has black lines between each tile..

Code: [Select]
mapa[VerticeActual + 0].TexCoords = sf::Vector2f(((tx + 0) * SAnchoTile) + 1, ((ty + 0) * SAltoTile) + 1 );
            mapa[VerticeActual + 1].TexCoords = sf::Vector2f(((tx + 0) * SAnchoTile) + 1, ((ty + 1) * SAltoTile) - 1);
            mapa[VerticeActual + 2].TexCoords = sf::Vector2f(((tx + 1) * SAnchoTile) - 1, ((ty + 1) * SAltoTile) - 1);
            mapa[VerticeActual + 3].TexCoords = sf::Vector2f(((tx + 1) * SAnchoTile) - 1, ((ty + 0) * SAltoTile) + 1);


 8)
- Mate (beverage) addict
- Heavy metal addict _lml
- SFML 2 addict
- My first (and free) game: BichingISH!

 

anything