Hello,
I am having trouble playing around with tile sets and vertex array.
I followed the tutorial:
http://www.sfml-dev.org/tutorials/2.0/graphics-vertex-array.php (http://www.sfml-dev.org/tutorials/2.0/graphics-vertex-array.php)
And tried using the tile map example in my code but I can only see 3 tiles being drawn.
I believe that my problem comes from this part of the code:
// 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);
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);
In my code I only create the map randomly like so:
void GameWindow::designMap(int width, int height)
{
int levelMap[width * height];
for (int i = 0; i < width * height ; i++)
levelMap[i] = rand() % 3 + 1;
if (!map.load("ressources/tileset4.png", sf::Vector2u(blockWidth, blockHeight), levelMap, width, height))
throw GlobExcep("Cannot load tile image!");
}
with map being an attribute of type TileMap in my class.
But when I draw the map I can only see 3 different tiles being drawn. Here is the tile set I used ( with tiles being 32*32 like in the example):
(http://img713.imageshack.us/img713/103/snu.png)
and here is the result I get from drawing in map of 20 * 20.
(http://img526.imageshack.us/img526/8421/xnq8.png)
I first thought that it was the format of the tileset I was using that was causing this issue so I changed it to a tileset like so:
(http://img208.imageshack.us/img208/7353/xt3.png)
but I would still get the same output and never get to see tile 1 drawn even if I create a map of 100 000 * 100 000
So I believe that I'm misunderstanding something about how to split tileset into quads.
So if somebody could help me understanding what I am doing wrong or what do I need to do to adapt this code:
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);
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);
}
to any other tileset format that would be great!
window.draw(vertex_ptr, vertex_count, primitive_type);
?
window.draw(vertex_ptr, vertex_count, primitive_type);
?
Not too sure how that would help me to draw only one vertice (I suppose that is the correct terminology ?)
Unless the vertex_count actually correspond to the vertice that you want to draw from the vertex_ptr?
window.draw(&vertex_array[i * 4], 4, sf::Triangles);
This code draw 4 vertices (one tile) starting at tile number i (vertex number i * 4).
But there's absolutely no point using a vertex array if you draw its contents quad by quad.