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:
(https://image.prntscr.com/image/ZspTQTy6Sl_Ahl7bG9tE_g.png)
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.
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);
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.