Hi, i have sf::Drawable class:
class DrawTileMap : public sf::Drawable
{
public:
void load (const Texture tileset, int ( &TileMap )[500][500]);
void update (int ( &TileMap )[500][500]);
private:
virtual void draw (sf::RenderTarget& target, sf::RenderStates states) const;
VertexArray m_vertices;
Texture m_tileset;
Vector2u tileSize = {64, 64};
};
//==================================================================================================
void DrawTileMap::load(const Texture tileset, int ( &TileMap )[500][500])
{
m_tileset = tileset;
m_vertices.setPrimitiveType(sf::Quads);
m_vertices.resize(500 * 500 * 4);
}
//==================================================================================================
void DrawTileMap::update(int ( &TileMap )[500][500])
{
int tu = 0;
int tv = 0;
for(unsigned int x = 0; x < 500; ++x)
for(unsigned int y = 0; y < 500; ++y)
{
switch(TileMap[ x ] [ y ] )
{
case 1: tu = 0;tv = 2;break;
case 2: tu = 1;tv = 2;break;
case 3: tu = 2;tv = 2;break;
case 4: tu = 3;tv = 2;break;
case 5: tu = 4;tv = 2;break;
case 6: tu = 0;tv = 3;break;
case 7: tu = 1;tv = 3;break;
case 8: tu = 2;tv = 3;break;
case 9: tu = 3;tv = 3;break;
case 10: tu = 4;tv = 3;break;
default:tu = 0;tv = 0;break;
}
sf::Vertex* tile64 = &m_vertices[(x + y * 500) * 4];
tile64[0].position = sf::Vector2f(x * tileSize.x, y * tileSize.y);
tile64[1].position = sf::Vector2f((x + 1) * tileSize.x, y * tileSize.y);
tile64[2].position = sf::Vector2f((x + 1) * tileSize.x, (y + 1) * tileSize.y);
tile64[3].position = sf::Vector2f(x * tileSize.x, (y + 1) * tileSize.y);
tile64[0].texCoords = sf::Vector2f(tu * tileSize.x, tv * tileSize.y);
tile64[1].texCoords = sf::Vector2f((tu + 1) * tileSize.x, tv * tileSize.y);
tile64[2].texCoords = sf::Vector2f((tu + 1) * tileSize.x, (tv + 1) * tileSize.y);
tile64[3].texCoords = sf::Vector2f(tu * tileSize.x, (tv + 1) * tileSize.y);
}
}
//==================================================================================================
void DrawTileMap::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
states.texture = &m_tileset;
target.draw(m_vertices, states);
}
//==================================================================================================
my game map size is 500x500
this class draws a game map in full size (500x500)
how to draw only a visible area of the map, for example 100x100