SFML community forums

General => Feature requests => Topic started by: zmertens on July 29, 2015, 07:01:56 am

Title: Simple method for Centering Window Position
Post by: zmertens on July 29, 2015, 07:01:56 am
I think it might be beneficial for the SFML library to have a quick method for setting the window in the user's screen center. I'm sure this has been thought of before by SFML devs, however, I don't see any discussion in the forums that I could find. Here's a quick and simple look at how I think it might be implemented in the Window class.

For instance, at line 220 in SFML/src/SFML/Window.cpp:
////////////////////////////////////////////////////////////
void Window::setPosition(const Vector2i& position)
{
    if (m_impl)
        m_impl->setPosition(position);
}

could be called using something like:

window.setPosition(sf::Vector2i(sf::Window::CenterHorizontal(), sf::Window::CenterVertical()));

Where sf::Window::CenterHorizontal/Vertical are static public methods that calculate the equivalent call:

window.setPosition(sf::Vector2i(sf::VideoMode::getDesktopMode().width * 0.5 - window.getSize().x * 0.5, sf::VideoMode::getDesktopMode().height * 0.5 - window.getSize().y * 0.5));
 

I'm sure there'd be better ways to implement this functionality within SFML's API but this was a quick way to illustrate what I was referring by setting the window to the user's screen center. I realize it's not very much code to replace but I think it does reduce some clutter in the call. Part of the inspiration was from SDL's library with regards to:
SDL_CreateWindow(titleString.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, windowWidth, windowHeight, SDL_WINDOW_OPENGL);
Title: Re: Simple method for Centering Window Position
Post by: Laurent on July 29, 2015, 07:42:13 am
Adding a new function to the public API just to save one simple line of code on user side... you'll have to find more convincing arguments ;)

By the way, your static functions wouldn't work, they have to know the size of the window so they must be non-static.
Title: Re: Simple method for Centering Window Position
Post by: Hapax on July 29, 2015, 02:21:58 pm
the user's screen center
Which screen?  ;)
Title: Re: Simple method for Centering Window Position
Post by: zmertens on July 29, 2015, 05:37:22 pm
Yeah I thought about it some more in the morning and it seems pretty convoluted. I was also think adding something like a bit flag along with "sf::Style::Resizeable | sf::Style::CenterWindow" and what not but I don't that would be very elegant either.

Which screen?  ;)

 Oh yeah ... :o
Title: Re: Simple method for Centering Window Position
Post by: Tank on August 07, 2015, 05:13:12 pm
PySFML has some nice additions to sf::Rectangle that I really enjoyed: http://www.python-sfml.org/api/graphics.html#rectangle (right, bottom, center, size, position)