I wrote this small bit of code to familiarize myself with C++ again:
int main() {
// Crashes if font fails to load
const sf::Font font = load_font().value();
sf::RenderWindow window(sf::VideoMode(800, 600), "Game Of Life");
window.setVerticalSyncEnabled(false);
const sf::Vector2u window_size = window.getSize();
// UI
sf::Text title = center_text_origin(sf::Text("Game of Life", font, FONT_SIZE));
title.setStyle(sf::Text::Bold);
const auto set_title_position = [](sf::Text &title, sf::Vector2u window_size) {
float amt_push_downwards = title.getLocalBounds().height / 2 + 10;
sf::Vector2f pos = sf::Vector2f(
window_size.x / 2.0,
amt_push_downwards
);
title.setPosition(pos);
};
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
switch (event.type) {
case sf::Event::Closed:
window.close();
break;
case sf::Event::Resized:
{
const sf::FloatRect new_window_area(0.f, 0.f, event.size.width, event.size.height);
const sf::Vector2u new_window_size(rect_size(new_window_area));
window.setView(sf::View(new_window_area));
set_title_position(title, new_window_size);
}
break;
default:
break;
}
}
window.clear();
window.draw(title);
window.display();
}
return 0;
}
I noticed that my title text does not need to have an initialized position set, as the resize event gets triggered when RenderWindow is initialized. Is this guaranteed? Is this a quirk that may or may not be prevalent in future versions of SFML?