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

Author Topic: Trouble with views  (Read 3428 times)

0 Members and 1 Guest are viewing this topic.

SuperV1234

  • SFML Team
  • Full Member
  • *****
  • Posts: 190
    • View Profile
Trouble with views
« on: April 24, 2011, 10:13:09 am »
I'm using SFML.NET 2.0, and trying to create a "camera" for my roguelike game.

I want my 1024x768 RenderWindow to look like this:

There is a rectangle (295, 40, 704, 704) where the game camera should be, the rest of the window should be for the UI.

I want to be able to pan, rotate and zoom, while still keeping the camera in that rectangle.

---

I initialize my View like this:
Code: [Select]
View = new View(new FloatRect(295, 40, 704, 704));

Then in the Draw method:
Code: [Select]
public void Draw()
        {
            SFMLManager.RenderWindow.SetView(View);
            Sprite.Position = DrawPosition;            SFMLManager.RenderWindow.Draw(Sprite);
SFMLManager.RenderWindow.SetView(SFMLManager.RenderWindow.DefaultView);
        }




The result is this!


As you can see the View starts from the top-left of the window, and not at (295;40). Also, it overlaps the whole window and if I pan it around it just acts like a drag'n'drop control - it has no boundaries that make it looks like a "game camera" if you know what I mean.

How can I make the View look like a camera, that is always contained in a 704x704 rectangle (starting at 295,40) coordinates?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Trouble with views
« Reply #1 on: April 24, 2011, 11:04:40 am »
The view has two parameters: what it sees, and where it displays it. What you're playing with is what the view sees, so no matter what you do it is still displayed in the whole window.

What you need to change is the Viewport attribute of the view.
Laurent Gomila - SFML developer

SuperV1234

  • SFML Team
  • Full Member
  • *****
  • Posts: 190
    • View Profile
Trouble with views
« Reply #2 on: April 24, 2011, 11:08:09 am »
Quote from: "Laurent"
The view has two parameters: what it sees, and where it displays it. What you're playing with is what the view sees, so no matter what you do it is still displayed in the whole window.

What you need to change is the Viewport attribute of the view.


Thanks for the clarification. So I should put (0,0,1024,768) as the View's first parameter?

I'm also confused about the Viewport: since its values are relative to the window, how can I easily adjust it to be in the (295,40,704,704) area?

---

Using maths I got this:
Code: [Select]
View.Viewport = new FloatRect(0.288f,
                 0.052f,
                 0.688f,
                 0.917f);


There has to be a more elegant solution, right?

Anyway, it now displays in the "rectangle" as intended, but only when:
Code: [Select]
View = new View(new FloatRect(0, 0, 1024, 1024));


Instead, when I had:
Code: [Select]
View = new View(new FloatRect(0, 0, 1024, 768));
changing the View.Viewport gave me a distorted image.

I'm even more confused now ---
1) How can I set the Viewport without having to do all the divisions to get values such as (0.917f) and so on?
2) When I create a new View, what should the FloatRect sizes be? Should they be the same as the "camera area"? Or the same as the RenderWindow?
3) Why does changing the Viewport stretches the result? Shouldn't the Viewport only tell the View where the "camera area" is?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Trouble with views
« Reply #3 on: April 24, 2011, 06:33:06 pm »
Quote
1) How can I set the Viewport without having to do all the divisions to get values such as (0.917f) and so on?

Write them in your code instead of calculating them yourself ;)

Quote
2) When I create a new View, what should the FloatRect sizes be? Should they be the same as the "camera area"? Or the same as the RenderWindow?

The FloatRect that you pass to the constructor is what the camera sees, in world coordinates (so, nothing to do with the window/viewport).

Quote
3) Why does changing the Viewport stretches the result? Shouldn't the Viewport only tell the View where the "camera area" is?

You still display the same things but in a smaller area, that's why things are stretched. If you want to keep a 1:1 ratio you must use the same proportions for the source rectangle and the viewport.
Laurent Gomila - SFML developer

SuperV1234

  • SFML Team
  • Full Member
  • *****
  • Posts: 190
    • View Profile
Trouble with views
« Reply #4 on: April 24, 2011, 10:47:32 pm »
Thanks for the clarification :)

I have one last question - how can I avoid drawing sprites not 'seen' by the camera to increase performance?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Trouble with views
« Reply #5 on: April 24, 2011, 10:53:16 pm »
You can check if the current view (sf::RenderWindow::GetView()) contains any of the object's coordinates. Use sf::View's methods GetCenter() and GetSize() to get the visible rectangle, and the rest is computed by a few inequalities.

By the way, full quotes aren't always necessary ;)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

SuperV1234

  • SFML Team
  • Full Member
  • *****
  • Posts: 190
    • View Profile
Trouble with views
« Reply #6 on: April 24, 2011, 11:06:59 pm »
Is the visible rectangle supposed to be...? :
GetCenter().X - (GetSize().X / 2)
GetCenter().Y - (GetSize().Y / 2)
GetCenter().X + (GetSize().X / 2)
GetCenter().Y + (GetSize().Y / 2)

And to check if the view contains a Sprite, should I convert the coordinates with RenderWindow.ConvertCoords then check if they are inside that rectangle?

---

Nevermind, solved. :)