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

Author Topic: Understanding Views  (Read 1367 times)

0 Members and 2 Guests are viewing this topic.

BrittleWind

  • Newbie
  • *
  • Posts: 2
    • View Profile
Understanding Views
« on: June 11, 2018, 02:57:47 am »
I was going over the documentation for views and watching various video tutorials on the topic and was hoping for some clarification.

My current understanding of views is:

1. When we create a view, we need to specify the center of the view, as well as its dimensions.

2. When we want to change how much of the screen the view takes, we change its viewport.

3. When we want to draw something to the view, we need to set the view on the window, and then draw it.


My question is: How is the view actually determining what goes inside it? I originally, thought that the first step was just creating an "imaginary" rectangle that we later filled in during step 3.

If the contents of the view are being captured in the first step when we specify its center & dimensions, then how does it know what to capture if I haven't drawn anything yet?

I realize that this is a basic question, but I haven't been able to find any resources online to help clear my confusion.

Thank you for your time.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Understanding Views
« Reply #1 on: June 11, 2018, 08:22:44 am »
I assume you're not actually interested in the implementation of sf::View, but seem to have a deeper question. What are you really trying to figure out?

A view is nothing more than an OpenGL projection matrix, meaning it's basically a camera looking into your scene.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

BrittleWind

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Understanding Views
« Reply #2 on: June 12, 2018, 07:27:11 am »
Sorry for my misleading question. My question is just how I tell the view what it will contain. On the tutorial, the section on this shows that you only have to set the center & position of the view.

However, when I tried doing something similar, I wasn't able to understand my result.
#include <SFML/Graphics.hpp>

using namespace sf; //I know this is bad :(

int main() {
        RenderWindow w(VideoMode(800, 600), "View Test");
        Font f;
        View v;

        if (!f.loadFromFile("Roboto-bold.ttf"))
                return -1;

        Text t;
        t.setString("Hello!");
        t.setFont(f);
        t.setFillColor(Color::White);
        t.setCharacterSize(100);

        Vector2f pos = t.getPosition();
        FloatRect size = t.getGlobalBounds();

        v.setCenter(Vector2f(pos.x / 2.f, pos.y / 2.f));
        v.setSize(Vector2f(size.width, size.height));

        while (w.isOpen()) {
               
                Event e;

                while (w.pollEvent(e)) {
                        switch (e.type) {
                        case Event::Closed:
                                w.close();
                        }
                }
                w.setView(v);
                w.clear();

                w.draw(t);

                w.display();

        }
}
 

With the following output:


My goal here was to create a view that contains the text, but I am assuming that I "captured" the incorrect part based on the output.

I understand that the view appears blown-up because by default the view takes the entire screen.

To summarize my question: When do we actually control what the view contains?
Hypothetically, if we have multiple things we want to display (like a background, healthbar, score, menu, etc..) at the same time, is it considered "good" to assign a view to each element, or is there a better way to do this?

I realize this is a simple question, but I've looked up a lot of different resources online and am still very confused, so thank you for your time.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Understanding Views
« Reply #3 on: June 12, 2018, 08:08:12 am »
Quote
v.setCenter(Vector2f(pos.x / 2.f, pos.y / 2.f));
v.setSize(Vector2f(size.width, size.height));
This is wrong. Assuming you want to center the view on the text, you should rather write:
v.setCenter(Vector2f(size.left + sze.width / 2, size.top + size.height / 2));
v.setSize(Vector2f(size.width, size.height));
Or simply:
v.reset(size);

Quote
To summarize my question: When do we actually control what the view contains?
Any entity that is (partially or completely) inside the view's area, as defined with setSize/setCenter or reset, will appear on screen, if the view is active at the time the entity is drawn.

A view is simply a transformation matrix, which maps scene coordinates to window coordinates (it really is just a few additions and multiplications). When an entity is draw, its coordinates are simply transformed by the view; the result is that anything that is outside the window area after that transformation will obviously not be visible.
The view does not "capture" anything, nothing complicated happens behind the scene, only the coordinate transformation.
Laurent Gomila - SFML developer