Okay, so I am trying to adapt the TileMap example class found here:
http://www.sfml-dev.org/tutorials/2.2/graphics-vertex-array.phpfor use in my own project.
I created a quick tileset png file:
And quickly rewrote the class in a header and cpp file:
class Planetary_surface: public sf::Drawable, public sf::Transformable
{ public:
bool load(const std::string& tileset, sf::Vector2u tileSize, const int* tiles, unsigned int width, unsigned int height);
private:
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
sf::VertexArray m_vertices;
sf::Texture m_tileset;
};
and cpp
bool Planetary_surface::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
// why is it 4 though...
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);
}
return true;
}
}
void Planetary_surface::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);
}
which seems true to the original code
But when I run it, I get this as an output:
Which has the first column drawn properly, but not the rest.
When I copy the example class as it was defined in the original document (just one big class declaration) into the main cpp and use it, it renders everything properly as expected, but when I use the class that I defined in the header/cpp file pair, I keep getting the issue with only the first column being drawn. I cant figure out how the heck that is happening.