SFML community forums

Help => Graphics => Topic started by: smguyk on March 29, 2014, 10:05:24 am

Title: Multiple sf::Views
Post by: smguyk 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
Title: Re: Multiple sf::Views
Post by: Nexus 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().
Title: Re: Multiple sf::Views
Post by: smguyk 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?
Title: Re: Multiple sf::Views
Post by: Nexus 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().
Title: Re: Multiple sf::Views
Post by: smguyk on March 30, 2014, 12:19:58 pm
Ah, thanks so much!

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