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

Author Topic: How Can I limit what to draw  (Read 1716 times)

0 Members and 1 Guest are viewing this topic.

senhor

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
How Can I limit what to draw
« 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.

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: How Can I limit what to draw
« Reply #1 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)

Rhimlock

  • Jr. Member
  • **
  • Posts: 73
    • View Profile
Re: How Can I limit what to draw
« Reply #2 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)

senhor

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: How Can I limit what to draw
« Reply #3 on: October 25, 2013, 03:41:16 pm »
thanks for your advice Ghosa but that problem still isn't solved  :(

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: How Can I limit what to draw
« Reply #4 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.

senhor

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: How Can I limit what to draw
« Reply #5 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.