Skip to main content

How to center a SFGUI window (sfg::Window)

Sidebar

Simply get the SFGUI window size using GetAllocation, the sfml window size using getSize, then do this arithmetic:

auto window = sfg::Window::Create();
auto win_rect = window->GetAllocation();
sf::Vector2f size(win_rect.width, win_rect.height);
window->SetPosition(((sf::Vector2f)rwindow->getSize() - size) / 2.0f);
rubenwardy's profile picture, the letter R

Hi, I'm Andrew Ward. I'm a software developer, an open source maintainer, and a graduate from the University of Bristol. I’m a core developer for Luanti, an open source voxel game engine.

Comments

Leave comment

Shown publicly next to your comment. Leave blank to show as "Anonymous".
Optional, to notify you if rubenwardy replies. Not shown publicly.
Max 1800 characters. You may use plain text, HTML, or Markdown.