我正在使用带有四边形的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;
};
我已经尝试了其他一些方法来展示它们,但这一切要么不能正确执行,要么会使程序放慢速度。有人能指出我正确的方向吗?
此致
答案 0 :(得分:0)
使用if语句检查TU和TV整数,以及您的磁贴编号,因此请检查那些是否与您要检查对象的磁贴相等。
if(tu&amp; tv == 1){std :: cout&lt;&lt; &#34; 1&#34; &LT;&LT;的std :: ENDL;}