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

Author Topic: Some help with viewports?  (Read 3520 times)

0 Members and 1 Guest are viewing this topic.

thePyro_13

  • Full Member
  • ***
  • Posts: 156
    • View Profile
Some help with viewports?
« on: November 15, 2011, 10:07:17 am »
I've started playing with views in my current project and have noticed this problem. If I alter the views viewport at all(including (1,1,1,1)) then my window only seems to render the clear() colour. The sprites render fine if I only remove this line.

REDACTED: I can't get Viewports to work anywhere. :(

But I figured I'd post here first. Are their any known bugs related to viewport that could be causing this? Or can anyone provide some pointers on which functions could be effecting the viewport like this.

EDIT:

Ok, so now I've got this:
Code: [Select]
#include "SFML\Graphics.hpp"

int main()
{
sf::RenderWindow window;
window.Create(sf::VideoMode(800,600), "sf::Viewport test");

sf::View view = window.GetView();
view.SetViewport(sf::FloatRect(1, 1, 1, 1));
window.SetView(view); //if I comment out this line I get the expected black window with white circle in the middle

sf::Shape shape = sf::Shape::Circle(sf::Vector2f(window.GetWidth() /2, window.GetHeight() /2), 50, sf::Color::White);

while(window.IsOpened())
{
sf::Event e;
while(window.PollEvent(e))
{
if(e.Type = sf::Event::Closed)
window.Close();
}

window.Clear();
window.Draw(shape);
window.Display();
}

return EXIT_SUCCESS;
}

If I comment out the window.SetView(view); line, then everything works as expected. I'm also able to comment out the SetViewport() line and everything works as-well. I'm using a shape here, but the same problem appears with sprites.

I can't think of anything else I can remove, is this not how view-ports are meant to be used?

sec_goat

  • Newbie
  • *
  • Posts: 17
    • View Profile
Some help with viewports?
« Reply #1 on: November 17, 2011, 09:43:45 pm »
I am by no means an expert. but try initializing your view and viewport a bit differently.

I declare my view in my class header file, then reset it and set the view port later.

Code: [Select]

static sf::View currentView;

currentView.Reset(sf::FloatRect(0, 0, 768, 768));

currentView.SetViewport(sf::FloatRect(0.f, 0.f, 0.75f, 1.f));

//I then move my viewport by changing it's center
currentView.SetCenter(100,100);

//then update the renderwidnows view
window.SetView(currentView);


this appears to work just fine for me.

EDIT:

it appears if I just change your viewport init then it works fine, not sure exactly why

Code: [Select]

#include "SFML\Graphics.hpp"

int main()
{
   sf::RenderWindow window;
   window.Create(sf::VideoMode(800,600), "sf::Viewport test");

   sf::View view = window.GetView();
   view.SetViewport(sf::FloatRect(0, 0, 1, 1));
   window.SetView(view); //if I comment out this line I get the expected black window with white circle in the middle

   sf::Shape shape = sf::Shape::Circle(sf::Vector2f(window.GetWidth() /2, window.GetHeight() /2), 50, sf::Color::White);

   while(window.IsOpened())
   {
      sf::Event e;
      while(window.PollEvent(e))
      {
         if(e.Type = sf::Event::Closed)
            window.Close();
      }

      window.Clear();
      //window.SetView(view);
      window.Draw(shape);
      window.Display();
   }

   return EXIT_SUCCESS;
}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Some help with viewports?
« Reply #2 on: November 17, 2011, 10:22:07 pm »
A rect with left == right and top == bottom is an empty rect. To cover the whole window the correct viewport is [0, 0, 1, 1].
Laurent Gomila - SFML developer

thePyro_13

  • Full Member
  • ***
  • Posts: 156
    • View Profile
Some help with viewports?
« Reply #3 on: November 18, 2011, 02:04:12 am »
Ah thanks both of you, It's really been really hot in Australia this week. I can't believe I couldn't connect the dots myself. :p

Now I feel silly. :P