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

Author Topic: View  (Read 4901 times)

0 Members and 1 Guest are viewing this topic.

Xeon06

  • Jr. Member
  • **
  • Posts: 58
    • View Profile
View
« on: February 20, 2010, 01:30:11 am »
Hey folks,

So I have been starting to work on my game's camera and I have a few questions about the working of views. More specifically, what is Viewport? It's a rectangle of some sort but I can't figure what it does, it only always seems start at 0, 0 and be of size 1, 1. Some example code:

Code: [Select]

RenderWindow app = new RenderWindow(new VideoMode(800, 600), "Test");
Console.WriteLine(app.DefaultView.Viewport);
//Outputs [FloatRect] Left(0) Top(0) Right(1) Bottom(1)

View camera = new View(new Vector2(0, 0), new Vector2(app.Width, app.Height));
Console.WriteLine(camera.Viewport);
//Outputs [FloatRect] Left(0) Top(0) Right(1) Bottom(1)


Perhaps I don't understand this both in cases I would expect it to look like:
Code: [Select]

[FloatRect] Left(0) Top(0) Right (800) Bottom(600)


I found that RenderWindow.GetViewport seems to be what I am looking for, as passing it my view returns the expected Rectangle. However, why does this one return an IntRect and the other ones FloatRect's?

Thanks.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
View
« Reply #1 on: February 20, 2010, 10:26:02 am »
Yes, viewports require some explanations, it's not an intuitive concept ;)

The viewport is the area inside the render target where the contents of the view will be displayed. It allows for split screen, minimap, etc.

I chose to define its coordinates as factors in range [0, 1] so that the size of the render target doesn't matter, the viewport will remain proportional to it. It's like saying "I want the view to be displayed in the first half of the screen" instead of "in the rectangle [0, 0, 400, 600]". And this solution doesn't break the default behaviour of the viewport always covering the entire render target.

The RenderTarget.GetViewport function applies the viewport to a given render target, and computes the corresponding rectangle in pixels. The coordinates of the result are integers because it represents pixels, not units of the 2D world.
Laurent Gomila - SFML developer

Xeon06

  • Jr. Member
  • **
  • Posts: 58
    • View Profile
View
« Reply #2 on: February 20, 2010, 03:59:01 pm »
Ah ok that makes much more sense now, thanks.

Just a little suggestion on the side but it would be cool to have the FloatRect.Intersects method have an overload for an IntRect and vice versa, or a way to convert from one to another. I tried implementing that in extension methods but since I was using it from a property it all messed up and the values were always 0.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
View
« Reply #3 on: February 20, 2010, 06:19:00 pm »
IntRect and FloatRect usually belong to two different worlds (pixels for Int and world units for Float), I don't think that it is necessary to be able to mix them.
Laurent Gomila - SFML developer

Xeon06

  • Jr. Member
  • **
  • Posts: 58
    • View Profile
View
« Reply #4 on: February 20, 2010, 06:32:42 pm »
My goal was to check whether a game world object fit the view, in order to know if I should draw it or not. I have a property on my game objects, Boundaries, that is a FloatRect because it's a world position. But maybe SFML performs automatic optimization (i.e. doesn't draw stuff outside of visible views)? If so my problem is solved.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
View
« Reply #5 on: February 20, 2010, 07:59:53 pm »
No, SFML doesn't perform any optimization, but I don't see any IntRect in your problem.
Laurent Gomila - SFML developer

Xeon06

  • Jr. Member
  • **
  • Posts: 58
    • View Profile
View
« Reply #6 on: February 21, 2010, 04:47:33 am »
I must compare my entitie's FloatRect with RenderWindow.GetviewPort's IntRect.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
View
« Reply #7 on: February 21, 2010, 10:33:22 am »
The viewport has nothing to do with what is visible or not. This is defined by the source area of the view (Center + Size + Rotation).

This is like taking a picture and sticking it on a wall: what is visible on the picture is defined by the Center/Size/Rotation of the view, and the viewport defines where you stick the picture on the wall; it doesn't change what is visible, only where it will be visible.
Laurent Gomila - SFML developer

Xeon06

  • Jr. Member
  • **
  • Posts: 58
    • View Profile
View
« Reply #8 on: February 21, 2010, 03:41:17 pm »
Oh, ok then, thanks!

 

anything