簡體   English   中英

如何從VertexArray SFML查找頂點的坐標

[英]How to find the coordinates of a Vertex from a VertexArray SFML

我正在使用帶有Quads的VertexArray來制作TileMap,以進行一些測試。 我正在嘗試找出如何找出游戲對象位於哪個頂點(四邊形)上。

bool 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 * 4);

    // 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)
    {
        // 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);

        //std::cout << tu << " : " << tv << std::endl; <- Used this to out put it all, but it kept on showing the same coordinates and slowed the program down a lot.

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

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

        // 4 coordinates
        quad[0].texCoords = sf::Vector2f(tu * tileSize.x, tv * tileSize.y);
        quad[1].texCoords = sf::Vector2f((tu + 1) * tileSize.x, tv * tileSize.y);
        quad[2].texCoords = sf::Vector2f((tu + 1) * tileSize.x, (tv + 1) * tileSize.y);
        quad[3].texCoords = sf::Vector2f(tu * tileSize.x, (tv + 1) * tileSize.y);
    }

    return true;
}

私人的:

virtual void 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);
}
sf::VertexArray m_vertices;
sf::Texture m_tileset;

};

我嘗試了其他幾種方法來展示它們,但是它們要么不能正確執行,要么會使程序運行緩慢。 有人可以指出我正確的方向嗎?

問候

使用if語句檢查TU和TV整數,那里是您的圖塊編號,因此請檢查這些整數是否等於要檢查對象的圖塊。

if(tu&tv == 1){std :: cout <<“ 1” << std :: endl;}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM