Hi,
I'm stucked for about 1 week trying to make an algorithm which will allow me to render an hexagonal tilemap.
I would like to know how I can implement this system through SFML in an optimzed way.
For now I have a class which extends Drawable and that's all...
Here is what I've done :
void load(int hexSide, const int* tiles , int width , int height){
vertices.setPrimitiveType(sf::TrianglesFan); //vertices est mon sf::VertexArray
vertices.resize(width * height * 6);
for (unsigned int i = 0; i < width; ++i)
{
for (unsigned int j = 0; j < height; ++j)
{
auto tile = tiles[i + j * width];
//auto tu = tile % (tileset.getSize().x / tileSize.x);
//auto tv = tile / (tileset.getSize().x / tileSize.x);
auto hex = &vertices[(i + j * width) * 6];
//Here I don't know what to put ..
hex[0].position = sf::Vector2f(i*j*width, i*j*height);
hex[1].position = sf::Vector2f(...);
hex[2].position = sf::Vector2f(...);
hex[3].position = sf::Vector2f(...);
hex[4].position = sf::Vector2f(...);
hex[5].position = sf::Vector2f(...);
}
}
}
PS: I woud like to have pointy hexagon.
Any help would be appreciated.
Thanks in advance.
Yeah.. I tried again and again.. But I'm not able to figure out :/
My code :
vertices.setPrimitiveType(sf::TrianglesFan);
vertices.resize(map_width * map_height * 8);
int relative_offset = 2;
int hex_size = 64;
int offset = relative_offset * hex_size;
float height = hex_size * 2;
float width = std::sqrt(3) * hex_size;
for (unsigned int i = 0; i < map_width; ++i)
{
for (unsigned int j = 0; j < map_height; ++j)
{
// get the current tile number
int tileNumber = tiles[i + j * map_width];
// find its position in the tileset texture
//int tx = tileNumber % (m_tileset.getSize().x / (int)width);
//int ty = tileNumber / (m_tileset.getSize().x / width);
// get a pointer to the current tile's hex
sf::Vertex* hex = &vertices[(i + j * map_width) * 8];
// define its centre
hex[0].position = sf::Vector2f(offset + i * width, offset + j * height);
//define its corners
for (int k = 1; k <= 6; k++)
{
float angle = 2 * PI / 6 * (k + 0.5);
hex[k].position = sf::Vector2f(
offset + i * width + hex_size * cos(angle),
offset + j * height + hex_size * sin(angle));
}
hex[7].position = sf::Vector2f(hex[1].position.x, hex[1].position.y);
printf("%f\n", hex[0].position.x);
hex[0].color = sf::Color::Red;
for (int k = 1; k <= 4; k++)
{
float angle = 2 * PI / 6 * (k + 0.5);
hex[k].color = sf::Color::Cyan;
}
}
}
I don't know why it doesn't work, it seems that the second hex starts from the origin of the previous.
This is what I get :
(http://image.prntscr.com/image/5a543c5283f44fa2b090a3093bcd1386.png)
Any idea?
Thanks you