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

Author Topic: Understanding the View class  (Read 2056 times)

0 Members and 1 Guest are viewing this topic.

BMB

  • Newbie
  • *
  • Posts: 21
    • View Profile
Understanding the View class
« on: December 11, 2011, 11:28:06 pm »
Can someone explain in simple English how this works? I am having a lot of trouble understanding it.

mateandmetal

  • Full Member
  • ***
  • Posts: 171
  • The bird is the word
    • View Profile
    • my blog
Understanding the View class
« Reply #1 on: December 11, 2011, 11:49:59 pm »
sf::View Camera;
sf::FloatRect CameraFR;
sf::Vector2f CameraPos;

// Camera settings
CameraFR.Left = 0;
CameraFR.Top = 0;
CameraFR.Width = (float) (your window width)
CameraFR.Height = (float) (your window Height);

// Set the viewport
Camera.Reset(CamaraFR);
Camera.SetViewport(sf::FloatRect(0, 0, 1, 1));

// Set the camera center
Camera.SetCenter (CameraPos);

// Set the camera
yourWindow.SetView(Camera);

then draw your scene
move the camera with CameraPos :)
- Mate (beverage) addict
- Heavy metal addict _lml
- SFML 2 addict
- My first (and free) game: BichingISH!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Understanding the View class
« Reply #2 on: December 12, 2011, 07:39:05 am »
There's an online doc and a tutorial (in simple english ;)). Have you read them?
Laurent Gomila - SFML developer

BMB

  • Newbie
  • *
  • Posts: 21
    • View Profile
Understanding the View class
« Reply #3 on: December 12, 2011, 10:00:51 am »
Quote from: "Laurent"
There's an online doc and a tutorial (in simple English ;)). Have you read them?

Yes, I did. After reading them, I still didn't understand it. I think I am having trouble with the concepts, for instance: Do I draw to the window coordinates or the view's? Are the window coordinates changed? Or do I keep drawing as usual and the window will just display it according to the view? And when I rotate/zoom/whatever am I changing the view's source or target? Or is it the same thing?

For instance, can someone please explain how the following scenario would translate into window/views code?
A game map of 2000*2000 pixels. The window is 800*600, displaying only a part of the map (say the middle). In the top right corner of it, there is a mini map of 100*100, displaying the entire map, rotated at 45 degrees (so the center of it will actually be at 100 from the top and right, not at 50)

Here is how it seems to me it might go, though I am still unclear on the exact code:
[list=1]
  • create a view of zoom 1, center at 1000,1000 and size 800*600, and set it as the window's view
  • draw the whole map to the window as usual
  • create a view of zoom 1, center at 1000,1000, size 2000*2000, rotation 45 and viewport .75,0,1,.33 and set it as the window's view
  • [/list:o]
    Assuming this is right, some points I am unclear on:
  • Should the viewport of the minimap view be 200*200 or 100*100? It is 100*100, but takes up a 200*200 NSEW rectangle on the screen
  • Do I need to redraw something after assigning the second view, or doew it draw itself right away?
  • Does it take it's info from the whole window, or from the previously assigned view?
  • What is the difference between step 3 above, or setting it to half the size and a zoom of 0.5, for example?
  • If I now draw another tile to the window (say a 20*20 tile at 0,0) where will it appear? In the main map, in the minimap full size, or in the minimap scaled?

These are some of the things I am unclear about that I can think of off the top of my head. If anyone could clarify them for me, I would appreciate it.

P.S. I know this is probably a terrible way to handle this scenario, as it involves "drawing" the whole map at full resolution to some memory surface every time. It would be much more efficient to draw only the visible area, and draw a reduced resolution one in the minimap, but it seems to me to cover most of the points I don't understand, so for the purpose of this question, please explain how to do it with views, not a better way.

BMB

  • Newbie
  • *
  • Posts: 21
    • View Profile
Understanding the View class
« Reply #4 on: December 12, 2011, 03:50:12 pm »
After much trial and error, here are my conclusions about the View class:
  • Views don't "take" their info from anywhere. They only display what is drawn on them after they are set as the active view.
  • When drawing to a window, it is first passed through the active view. If it is within it's source rectangle, it will be translated to the viewport and drawn there.
  • The view will always be displayed within the boundaries of it's viewport, filling it as much as possible after it is rotated.
  • Calling Clear() on the window, clears anything displayed in it, irrelevant to which view it was drawn to.
  • Even when rotating it, the view always displays within it's viewport.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Understanding the View class
« Reply #5 on: December 12, 2011, 04:18:16 pm »
Your conclusions are good, I think you understood everything about views.

(I didn't have the time to give you a detailed answer today, sorry that you had to try by yourself)
Laurent Gomila - SFML developer

 

anything