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

Author Topic: Questions on sf::View  (Read 1359 times)

0 Members and 1 Guest are viewing this topic.

smguyk

  • Jr. Member
  • **
  • Posts: 79
    • View Profile
Questions on sf::View
« on: March 25, 2014, 10:14:46 am »
For my side scroller, I need a camera that always centers on my player's position.

I have 3 questions regarding the use of sf::View:

In my player's update function, I create a view, set its center to the player's position, and set it as the window's view.

  sf::View v(sf::FloatRect(0, 0, 320, 240));
  v.setCenter(player_.getPosition());
  window_->setView(v);

1.) I tried using the view as an sf::View class member but it didn't work, so now I'm creating the view every tick as seen in the code above. Is that how it is supposed to be done, or should I be wary of how resource intensive it may be, aka what is the correct way to do it?

2.) Now that I have the view that is smaller than the entire map, not all information is displayed anymore. For example I had a UI (displaying hit points and stuff) and an FPS display in my window's corners which I now can't see anymore unless my player, ie the view, is near it. Do I have to use multiple views or something to display everything?

3.) Due to using a view it's not necessary anymore to render the entire map, but only what is seen in the view. Is that done automatically or do I need to somehow program myself what is to be rendered?

I would appreciate any help on this

Thank you!

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Questions on sf::View
« Reply #1 on: March 25, 2014, 10:26:09 am »
1) It doesn't matter. Do it the way that best fits your design. If a member variable doesn't work, you'd better find out why.

2) Yes, use a separate view for the UI. You could take the window's default view.

3) Nothing is culled automatically, you have to check yourself what you want to draw. sf::FloatRect::intersects() might be helpful here.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11008
    • View Profile
    • development blog
    • Email
Re: Questions on sf::View
« Reply #2 on: March 25, 2014, 10:26:28 am »
1. Use it as a class member. If you fail to make something a class member, you might want to get your C++ book and catch up on how to do so.

2. Use multiple views. For example one for the world, which moves with the player and a static one, that doesn't move at all.

3. It will not be rendered, but you need to handle the processing yourself, i.e. you'll still send all the vertices to the GPU, although only a portion is needed. Depending what exactly is going on, you could also optimize your logic.
Keep in mind however that if the world is relatively small, the optimization might not really be worth the invested time. For large worlds the performance will suffer, thus it's recommended.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

smguyk

  • Jr. Member
  • **
  • Posts: 79
    • View Profile
Re: Questions on sf::View
« Reply #3 on: March 25, 2014, 06:50:31 pm »
Thanks guys, you helped me a lot!

I got everything to work, now I just need to specify what part of the map should be rendered. I think I'll  be able to do that.

 

anything