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

Author Topic: Is the resize event trigger gauranteed when RenderWindow is initialized?  (Read 983 times)

0 Members and 1 Guest are viewing this topic.

dogunbound

  • Newbie
  • *
  • Posts: 7
    • View Profile
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?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10815
    • View Profile
    • development blog
    • Email
Re: Is the resize event trigger gauranteed when RenderWindow is initialized?
« Reply #1 on: October 11, 2022, 07:49:31 am »
SFML doesn't make this guarantee, so I wouldn't rely on it.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything