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

Author Topic: How to detect OnScreen Sprites when using views?  (Read 3962 times)

0 Members and 2 Guests are viewing this topic.

burningprodigy

  • Newbie
  • *
  • Posts: 4
    • View Profile
How to detect OnScreen Sprites when using views?
« on: July 10, 2012, 06:46:07 am »
I'm using a custom sf::View for my RenderWindow in my tile-based RPG. Now I use sf::View's Move() method to scroll around the map. Now the problem is, how do I detect the sprites that are currently on the screen? I tried this:

/** pro::Tile is derived from sf::Sprite **/
bool LevelRenderer::OnScreen(pro::Tile& tile, sf::RenderWindow& app)
{
    return (tile.GetPosition().x > 0 && tile.GetPosition().x < app.GetWidth()
            && tile.GetPosition().y > 0 && tile.GetPosition().y < app.GetHeight());
}

However, although it initially works fine, after I Move() the view, the sprite's positions aren't updated and new sprites that appear on the screen are not drawn. What should I do?

Xenir

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: How to detect OnScreen Sprites when using views?
« Reply #1 on: July 10, 2012, 10:52:32 am »
It works bad, because you are checking sprite's position depending on starting view, when left upper corner is (0, 0). You should check position depending on the current view, not starting. I think you should use sf::RenderWindow::convertCoords(...) to convert sprite's position to local, depending on current view and then check if the sprite is currently on the screen. I'm not sure, because I didn't use sf::View yet, but I think here's your problem. Read the tutorial about sf::View to make sure or search the documentation.

Edit: Oops... It was small fault in bbcode... Sorry for that
« Last Edit: July 11, 2012, 07:12:04 pm by Xenir »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: How to detect OnScreen Sprites when using views?
« Reply #2 on: July 11, 2012, 04:42:30 pm »
However, although it initially works fine, after I Move() the view, the sprite's positions aren't updated ...
Ehrm that's the sense of a sf::View...  ???

When you move the view around the view coordinates won't fit the window coordinates but the sprite position doesn't not change. A transformation on a view affects all objects drawn with that view but doesn't change anything internal on any object.

I strongly advice you to use SFML 2.0rc since the 1.6 version is updated and contains many bugs...

And my tutorial on sf::View, that Xenir already linked (with a bad link :-[ ), is I think a good source to understand the view, although you won't be able to use everything described there with SFML 1.6.

As for your problem; it would be easy to implement with convertCoords() unfortunatly that doesn't exist in SFML 1.6 and I'm not quite sure if sf::Sprite::TransformToGlobal() will do the trick...
You basically have to take in account the moved view when checking against the sprites position.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: How to detect OnScreen Sprites when using views?
« Reply #3 on: July 11, 2012, 04:47:12 pm »
Quote
it would be easy to implement with convertCoords() unfortunatly that doesn't exist in SFML 1.6
It does ;)
Laurent Gomila - SFML developer

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: How to detect OnScreen Sprites when using views?
« Reply #4 on: July 11, 2012, 04:54:59 pm »
Quote
it would be easy to implement with convertCoords() unfortunatly that doesn't exist in SFML 1.6
It does ;)

Oh I see, it's not part of the sf::RenderTarget (as in SFML 2.0) where I was looking for it, but it's part of the RenderWindow itself.

So to get the rectangle that gets actually displayed, you just have to call the function four times, for the coordinates (0/0), (0/window height), (window width, 0) amd (window width, window height).
With that rect you can now use the function intersect() to find out if the sprite in some way touches the currently shown part of the view.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: How to detect OnScreen Sprites when using views?
« Reply #5 on: July 11, 2012, 05:00:14 pm »
Quote
So to get the rectangle that gets actually displayed, you just have to call the function four times, for the coordinates (0/0), (0/window height), (window width, 0) amd (window width, window height).
sf::View has functions to retrieve its rectangle in a more direct way ;)
Laurent Gomila - SFML developer