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

Author Topic: Simple method for Centering Window Position  (Read 7598 times)

0 Members and 1 Guest are viewing this topic.

zmertens

  • Jr. Member
  • **
  • Posts: 80
    • View Profile
Simple method for Centering Window Position
« 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);
The truth will set you free but first it will piss you off.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Simple method for Centering Window Position
« Reply #1 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.
Laurent Gomila - SFML developer

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Simple method for Centering Window Position
« Reply #2 on: July 29, 2015, 02:21:58 pm »
the user's screen center
Which screen?  ;)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

zmertens

  • Jr. Member
  • **
  • Posts: 80
    • View Profile
Re: Simple method for Centering Window Position
« Reply #3 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
« Last Edit: July 29, 2015, 05:39:53 pm by zwookie »
The truth will set you free but first it will piss you off.

Tank

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1486
    • View Profile
    • Blog
    • Email
Re: Simple method for Centering Window Position
« Reply #4 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)

 

anything