SFML community forums

Help => General => Topic started by: senhor on October 23, 2013, 06:11:20 pm

Title: How Can I limit what to draw
Post by: senhor on October 23, 2013, 06:11:20 pm
I am working on a RTS game and game gets laggy with big maps so I need to limit which parts of map to draw.
I want to draw only visible part of map so I coded like that:

 
void Node::draw( sf::RenderWindow& window )
{
    sf::Vector2f  node_position_in_coords;
    sf::View      view = window.getView();
    sf::Rect<float> rt;

    rt.left = view.getCenter().x - view.getSize().x/2.f;
    rt.top  = view.getCenter().y - view.getSize().y/2.f;
    rt.width  = view.getSize().x;
    rt.height = view.getSize().y;

    node_position_in_coords = window.mapPixelToCoords( sf::Vector2i( _x, _y) );

    if( rt.contains( node_position_in_coords ) )
        window.draw( _s_tile );
}
 

But it only draws a specific part of map.
Title: Re: How Can I limit what to draw
Post by: G. on October 23, 2013, 10:59:00 pm
Hard to say with such limited information.

Are you sure _x and _y are in pixels and not already in coords?
Also, if your map is a regular grid (tilemap), instead of looping on and testing every tile, loop on x from the tile corresponding to the left side of the view to the tile corresponding to the right side of the view and loop on y from the top tile in the view to the bottom tile in the view. (divide the view coordinate by your tile width to get these tile indices)
Title: Re: How Can I limit what to draw
Post by: Rhimlock on October 25, 2013, 10:14:56 am
It looks like you are drawing every tile as a single sprite.

You should take a look at VertexArrays, to speed up the drawing.

http://www.sfml-dev.org/tutorials/2.1/graphics-vertex-array.php

(Especially the part about tilemap)
Title: Re: How Can I limit what to draw
Post by: senhor on October 25, 2013, 03:41:16 pm
thanks for your advice Ghosa but that problem still isn't solved  :(
Title: Re: How Can I limit what to draw
Post by: Ixrec on October 25, 2013, 09:15:26 pm
Like G said, with tilemaps it should be very simple to look at/draw only the tiles within a certain distance of your current position, which should be more than enough.  If there are any other sources of lag you'll have to give us complete/minimal code for us to help find them.
Title: Re: How Can I limit what to draw
Post by: senhor on October 26, 2013, 10:04:21 pm
I think lag is all about drawing the map for now. I will change my code with vertex arrays as soon as possible and will tell you the result. Thanks for your helps.