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

Author Topic: [Solved] Vertex Array 2.0  (Read 2601 times)

0 Members and 1 Guest are viewing this topic.

SentUnto_1

  • Newbie
  • *
  • Posts: 6
    • View Profile
[Solved] Vertex Array 2.0
« on: February 24, 2021, 09:38:43 pm »
I'm back. I've made an amount of progress with my game, and the help that I previously recieved was awesome. I now have a new issue though. I have followed the structure of the vertex array tutorail to the 'T', and also tried to follow the advice given in my previous post. The map that I am drawing from (the [] itself) isn't 100% truly rendered as it was described in the code. Some tile aren't spawning where they are supposed to be, and tiles are replacing other tiles in random areas.

Most of this is directly from the tutorial and has been adapted to suit my classes.

Nothing looks amiss when compared to the tutorial so I'm stumped.


bool TileMap::load(const std::string& tileset, sf::Vector2u tileSize, const int* tiles, unsigned int width, unsigned int height)
{
    // load the tileset texture
    if (!m_tileset.loadFromFile(tileset))
        return false;

    // resize the vertex array to fit the level size
    m_vertices.setPrimitiveType(sf::Quads);
    m_vertices.resize(width * height * 5);

    // 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];

            // define its 4 corners
            quad[0].position = sf::Vector2f(i * tileSize.x, j * tileSize.y);
            quad[1].position = sf::Vector2f((i + 1) * tileSize.x, j * tileSize.y);
            quad[2].position = sf::Vector2f((i + 1) * tileSize.x, (j + 1) * tileSize.y);
            quad[3].position = sf::Vector2f(i * tileSize.x, (j + 1) * tileSize.y);
           
            // define its 4 texture coordinates
            quad[0].texCoords = sf::Vector2f(tu * tileSize.x, tv * tileSize.y); //grass
            quad[1].texCoords = sf::Vector2f((tu + 1) * tileSize.x, tv * tileSize.y); //long grass
            quad[2].texCoords = sf::Vector2f((tu + 1) * tileSize.x, (tv + 1) * tileSize.y); //dirt path
            quad[3].texCoords = sf::Vector2f(tu * tileSize.x, (tv + 1) * tileSize.y); //cobble

            quad[4].texCoords = sf::Vector2f(tu * tileSize.x, tv * tileSize.y); //shrub
            quad[5].texCoords = sf::Vector2f((tu + 1) * tileSize.x, tv * tileSize.y); //rock
            quad[6].texCoords = sf::Vector2f((tu + 1) * tileSize.x, (tv + 1) * tileSize.y); //house top left
            quad[7].texCoords = sf::Vector2f(tu * tileSize.x, (tv + 1) * tileSize.y); //house top right      
           
            quad[8].texCoords = sf::Vector2f(tu * tileSize.x, tv * tileSize.y); //house bottom left
            quad[9].texCoords = sf::Vector2f((tu + 1) * tileSize.x, tv * tileSize.y); //house bottom right
            quad[10].texCoords = sf::Vector2f((tu + 1) * tileSize.x, (tv + 1) * tileSize.y);//tree top left
            quad[11].texCoords = sf::Vector2f(tu * tileSize.x, (tv + 1) * tileSize.y);//tree top right

            quad[12].texCoords = sf::Vector2f(tu * tileSize.x, tv * tileSize.y); //tree mid left
            quad[13].texCoords = sf::Vector2f((tu + 1) * tileSize.x, tv * tileSize.y); //tree mid right
            quad[14].texCoords = sf::Vector2f((tu + 1) * tileSize.x, (tv + 1) * tileSize.y); //tree bottom left
            quad[15].texCoords = sf::Vector2f(tu * tileSize.x, (tv + 1) * tileSize.y); //tree bottom right

            quad[16].texCoords = sf::Vector2f(tu * tileSize.x, tv * tileSize.y); //fire top left
            quad[17].texCoords = sf::Vector2f((tu + 1) * tileSize.x, tv * tileSize.y); //fire top right
            quad[18].texCoords = sf::Vector2f((tu + 1) * tileSize.x, (tv + 1) * tileSize.y); //fire bottom left
            quad[19].texCoords = sf::Vector2f(tu * tileSize.x, (tv + 1) * tileSize.y); //fire bottom right



            quad[20].texCoords = sf::Vector2f(tu * tileSize.x, tv * tileSize.y); //path corner grass top left
            quad[21].texCoords = sf::Vector2f((tu + 1) * tileSize.x, tv * tileSize.y); //path corner grass top right
            quad[22].texCoords = sf::Vector2f((tu + 1) * tileSize.x, (tv + 1) * tileSize.y);//path corner grass bottom left
            quad[23].texCoords = sf::Vector2f(tu * tileSize.x, (tv + 1) * tileSize.y);//path corner grass bottom right

            quad[24].texCoords = sf::Vector2f(tu * tileSize.x, tv * tileSize.y); //dirt path turn right
            quad[25].texCoords = sf::Vector2f((tu + 1) * tileSize.x, tv * tileSize.y); //dirt path both dirs
            quad[26].texCoords = sf::Vector2f((tu + 1) * tileSize.x, (tv + 1) * tileSize.y); //dirt path turn left
            quad[27].texCoords = sf::Vector2f(tu * tileSize.x, (tv + 1) * tileSize.y); //dirt path left verge

            quad[28].texCoords = sf::Vector2f(tu * tileSize.x, tv * tileSize.y); //dirt path no verge
            quad[29].texCoords = sf::Vector2f((tu + 1) * tileSize.x, tv * tileSize.y); //dirt path right verge
            quad[30].texCoords = sf::Vector2f((tu + 1) * tileSize.x, (tv + 1) * tileSize.y); //dirt path down turn right
            quad[31].texCoords = sf::Vector2f(tu * tileSize.x, (tv + 1) * tileSize.y); //dirt path walk down both dirs

            quad[32].texCoords = sf::Vector2f(tu * tileSize.x, tv * tileSize.y); //dirt path walk down turn left
            quad[33].texCoords = sf::Vector2f((tu + 1) * tileSize.x, tv * tileSize.y);//map boundary tile
            quad[34].texCoords = sf::Vector2f((tu + 1) * tileSize.x, (tv + 1) * tileSize.y);
            quad[35].texCoords = sf::Vector2f(tu * tileSize.x, (tv + 1) * tileSize.y);

        }

    return true;
}

void TileMap::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
    // apply the transform
    states.transform *= getTransform();

    // apply the tileset texture
    states.texture = &m_tileset;

    // draw the vertex array
    target.draw(m_vertices, states);
}
void TileMap::render(sf::RenderTarget& target)
{
    const int startArea[] =
    {
        33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,
        33,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,27,28,29,4,4,4,4,4,4,4,4,4,4,4,4,33,
        33,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,27,28,29,0,0,0,0,0,0,0,0,0,0,0,4,33,
        33,4,0,0,0,0,0,0,0,0,0,5,0,0,0,0,27,28,29,0,0,0,0,16,17,0,0,0,0,0,4,33,
        33,4,0,0,0,0,0,0,0,0,0,0,4,4,4,0,27,28,29,0,0,0,0,18,19,0,0,5,0,0,4,33,
        33,4,0,0,0,0,0,0,0,0,0,0,4,5,4,0,27,28,29,0,0,0,0,0,0,0,0,0,0,0,4,33,
        33,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,27,28,29,5,0,0,0,0,10,11,0,0,0,0,4,33,
        33,4,0,0,0,0,0,0,0,10,11,0,0,0,0,0,27,28,29,0,0,0,0,0,12,13,0,0,0,0,4,33,
        33,4,0,0,0,0,0,0,0,12,13,0,0,0,0,0,27,28,29,0,0,0,0,0,14,15,0,0,0,0,4,33,
        33,4,0,0,0,0,0,0,0,14,15,0,0,0,0,0,27,28,21,25,25,25,25,25,25,25,25,25,25,25,25,33,
        33,4,0,0,0,0,0,0,0,0,0,0,0,0,6,7,27,28,28,28,28,28,28,28,28,28,28,28,28,28,28,33,
        33,4,0,0,0,0,0,0,0,0,0,1,1,0,8,9,30,31,31,31,31,31,31,31,31,31,31,31,31,31,31,33,
        33,4,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,10,11,0,0,0,0,0,0,4,33,
        33,4,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,12,13,0,0,0,0,0,0,4,33,
        33,4,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,14,15,0,0,0,0,0,0,4,33,
        33,4,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,5,0,0,0,0,0,0,0,0,0,4,33,
        33,4,0,0,0,0,0,0,0,0,0,0,0,0,5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,33,
        33,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,33,
        33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,
    };

    // create the tilemap from the level definition
    TileMap tileMap;
       
    if (!tileMap.load("Textures/newtexture.png", sf::Vector2u(64, 64), startArea, 32, 19))
    {
        throw "ERROR::TILEMAP::COULD NOT LOAD TILEMAP";
    }
     
    target.draw(tileMap);
   
}


 

From the first image you can see that the amount of shrubs do not match to the tile numbers in the array and that the boundary tile does not spawn on the left side but does on the top.

The second image is my atlas.

The third image is what happened on the bottom right corner of the screen.

EDIT:
For all of the texCoords lines I am getting a compiler warning -> C4244   'argument': conversion from 'unsigned int' to 'T', possible loss of data. Could this have something to do with generating the map badly if some values are being lost??
« Last Edit: February 25, 2021, 10:41:34 am by SentUnto_1 »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Vertex Array 2.0
« Reply #1 on: February 25, 2021, 08:47:12 am »
I didn't read everything, but after a quick look at your code, I have the feeling that you don't really understand what you're doing.

The way you build tiles doesn't make any sense at all and is full of errors. Your code should even crash, as you write past the end of the vertex array memory.
Laurent Gomila - SFML developer

SentUnto_1

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Vertex Array 2.0
« Reply #2 on: February 25, 2021, 09:10:02 am »
Thank you for replying Laurent. I was trying my best to follow the vertex array tutorial that you designed and in my previously raised issue I was advised to expand on the texCoords if I wanted to use more than the 4 tiles that you used in the tutorial. Other than following both of these I am sure I haven't added much else. Perhaps I took the previous advice too literally? Either way could you please help me? So far I have reached the conclusion that I have a memory and storage problem, but I'm not sure how to fix it..

SentUnto_1

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Vertex Array 2.0
« Reply #3 on: February 25, 2021, 10:41:17 am »
I clearly misinterpreted the previous advice! I have fixed the memory issue and the texCoords issue. Turns out I didn't need to create a new quad[n+1].texCoord for each new tile from the tileset, I was also missing 2 tiles at the end of the first and last row in the array.