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

Author Topic: Multiple sf::Views  (Read 1943 times)

0 Members and 1 Guest are viewing this topic.

smguyk

  • Jr. Member
  • **
  • Posts: 79
    • View Profile
Multiple sf::Views
« on: March 29, 2014, 10:05:24 am »
When using multiple sf::Views, do they really have to be reset every frame?

When I don't call reset(), it won't work, but it feels a bit weird having to do that...

In my game I have a view that displays the UI and stuff (defaultView_), and another view that follows the player and shows a small part of the complete world around the player (camera_). I use this code in my draw function:

  window_->clear();

  // Player camera
  camera_.reset(sf::FloatRect(0, 0, 640, 480));
  camera_.setCenter(calculateNewCameraPosition());
  window_->setView(camera_);

  window_->draw(tileMap_);
  window_->draw(player_);

  // View for the UI
  defaultView_.reset(sf::FloatRect(0, 0, 1024, 768));
  window_->setView(defaultView_);

  window_->draw(playerInfoText_);
  window_->draw(fpsText_);

  window_->display();

Is that the correct way to do it? I'm kind of starting a new project and I want to do everything right from the beginning!

Thanks
« Last Edit: March 29, 2014, 10:07:16 am by smguyk »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Multiple sf::Views
« Reply #1 on: March 29, 2014, 11:07:00 am »
If you only move the view, you don't have to call reset() again and again. It's enough to call setCenter().

And the default view needn't be changed at all. You don't even need a variable for it, as there is sf::RenderWindow::getDefaultView().
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

smguyk

  • Jr. Member
  • **
  • Posts: 79
    • View Profile
Re: Multiple sf::Views
« Reply #2 on: March 30, 2014, 10:08:43 am »
Thanks.

I changed my code to this:

  window_->clear();

  camera_.reset(sf::FloatRect(0, 0, 640, 480));
  camera_.setCenter(calculateNewCameraPosition());
  window_->setView(camera_);

  window_->draw(tileMap_);
  window_->draw(player_);

  window_->setView(window_->getDefaultView());

  window_->draw(playerInfoText_);
  window_->draw(fpsText_);

  window_->display();

It looks like this which is perfect (I'm standing in the bottom left corner): -
but when I leave out camera_.reset(sf::FloatRect(0, 0, 640, 480)); the view looks like this: -

Any idea what's wrong?
« Last Edit: March 30, 2014, 12:19:23 pm by smguyk »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Multiple sf::Views
« Reply #3 on: March 30, 2014, 11:29:01 am »
reset(), as implied by the name, sets the whole view (size and center). You can call it once in the beginning, and unless the size changes later, it will be enough to move the view by calling setCenter().
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

smguyk

  • Jr. Member
  • **
  • Posts: 79
    • View Profile
Re: Multiple sf::Views
« Reply #4 on: March 30, 2014, 12:19:58 pm »
Ah, thanks so much!

I forgot to call reset() at least once before!

 

anything