-
I have this code, in my draw function:
view->setCenter(_player->getPosition());
window.setView(*view);
_map->draw(window,view->getViewport());
and in _map the draw is
void TileMap::draw(sf::RenderWindow &window, const sf::FloatRect &_camerarect)
{
unsigned int tilesdraw(0);
for ( int i = 0; i< mapW ; i++ )
for ( int j = 0; j<mapH; j++ )
if ( _camerarect.intersects(tiles[i][j]->getGlobalBounds()) )
{
window.draw(*tiles[i][j]);
tiledisegned++;
}
std::cout<<"Tiles drawed: "<<tilesdraw<<std::endl;
}
I made that for draw only the tiles that are in the window, but its not work ( at least not draw anything ), any ideas whats I wrong?
sfml2, visual studio c++ 2010.
-
A sf::View has 2 rect :
- That part of the game map that it sees
- The part of the window where to draw it
Viewport is the second one, holding the portion of the window (with values in [0, 1]) where to draw the content of the view.
The View doesn't provide an easy way to get the viewed region ; you can get it with :
const Vector2f & center = view.getCenter();
const Vector2f & size = view.getSize();
FloatRect rect(center - size/2, size);
-
ty
-
Still, there might be better ways to achieve what you are doing ; for example, if your tiles have the same size, you could select a range of x and y values to draw, instead of running the test for each tile.
Something like :
const Vector2f & center = view.getCenter();
const Vector2f & size = view.getSize();
Vector2f topLeft = center - size / 2;
Vector2f botRight = center + size / 2;
int start_x = floor(topLeft.x / cellSize);
int end_x = ceil(botRight.x / cellSize);
int start_y = floor(topLeft.y / cellSize);
int end_y = ceil(botRight.y / cellSize);
for (int x = start_x; x < end_x; ++x) {
for (int y = start_y; y < end_y, ++y) {
// Draw tile (x, y)
}
}