Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: sf::FloatRect & performance  (Read 2766 times)

0 Members and 1 Guest are viewing this topic.

player931402

  • Jr. Member
  • **
  • Posts: 51
    • View Profile
sf::FloatRect & performance
« on: November 23, 2012, 03:24:28 pm »
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.

gyscos

  • Jr. Member
  • **
  • Posts: 69
    • View Profile
Re: sf::FloatRect & performance
« Reply #1 on: November 23, 2012, 03:43:29 pm »
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);

player931402

  • Jr. Member
  • **
  • Posts: 51
    • View Profile
Re: sf::FloatRect & performance
« Reply #2 on: November 23, 2012, 04:14:16 pm »
ty

gyscos

  • Jr. Member
  • **
  • Posts: 69
    • View Profile
Re: sf::FloatRect & performance
« Reply #3 on: November 23, 2012, 04:47:21 pm »
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)
  }
}
 

 

anything