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

Author Topic: window creation lag before it centers  (Read 1634 times)

0 Members and 1 Guest are viewing this topic.

metulburr

  • Newbie
  • *
  • Posts: 49
    • View Profile
    • Email
window creation lag before it centers
« on: November 25, 2013, 04:48:04 am »
I figured this code to center the window on a single monitor. It does work in general. However it appears to lag for a second or two off to the top-left corner before the window moves/jumps to the center. I would consider this normal behavior, for the exception that Python/Pygame's SDL center window does not do this. It immediately creates it's window without lag in the center.

So i am assuming it is me doing something wrong in coding it. I just do not know what?

The code stripped down to min:
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <iostream>

class Control{
    public:
        sf::RenderWindow window;
        unsigned int monitor[2] = {sf::VideoMode::getDesktopMode().width, sf::VideoMode::getDesktopMode().height},
            screen[2] = {800,600};
        std::string title = "SFML test";
       
        Control(){
            window.create(sf::VideoMode(screen[0], screen[1]), title);
            center_window();
        }
       
        void center_window(){
            window.setPosition(
                sf::Vector2i(
                    (monitor[0] / 2) - (screen[0] / 2),
                    (monitor[1] / 2) - (screen[1] / 2)
                )
            );
        }
       
        void events(sf::Event e){
            while(window.pollEvent(e)){
                if (e.type == sf::Event::Closed){
                    window.close();
                }
            }
        }
       
        void render(){
            window.clear();
            window.display();
        }
       
        void run(){
            while (window.isOpen()){
                sf::Event event;
                events(event);
                //update();
                render();
            }
        }
};

int main(){
    Control app;
    app.run();
}
 

EDIT:
actually it is not like 2 seconds but more like a half a second. It is just enough to notice the difference between SDL centering.
« Last Edit: November 25, 2013, 04:54:02 am by metulburr »
OS Ubuntu 13.04, Arch Linux, Gentoo, Windows 7/8
SFML 2.1
Ati Radeon HD 6770

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: window creation lag before it centers
« Reply #1 on: November 25, 2013, 07:52:27 am »
SDL provides the ability to center the window upon creation. SFML doesn't, you have to do it after the window has been created. That's the difference ;)
Laurent Gomila - SFML developer

wintertime

  • Sr. Member
  • ****
  • Posts: 255
    • View Profile
Re: window creation lag before it centers
« Reply #2 on: November 25, 2013, 11:54:21 am »
If there was a hidden window Style you could set and change, he could emulate the centering without it being shown and other people might find more uses for that. I would think that would be a more general solution if some day an addition gets made.

metulburr

  • Newbie
  • *
  • Posts: 49
    • View Profile
    • Email
Re: window creation lag before it centers
« Reply #3 on: November 25, 2013, 02:17:57 pm »
Quote
SDL provides the ability to center the window upon creation. SFML doesn't, you have to do it after the window has been created. That's the difference ;)
ok thanks. It's not really a big deal to anyone other than the programmer anyways.
OS Ubuntu 13.04, Arch Linux, Gentoo, Windows 7/8
SFML 2.1
Ati Radeon HD 6770

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: window creation lag before it centers
« Reply #4 on: November 25, 2013, 02:40:59 pm »
Quote
If there was a hidden window Style you could set and change, he could emulate the centering without it being shown and other people might find more uses for that
That, or providing the ability to call setters before creating the window (so that when it is created it already has its final position or whatever you want). One or the other should be added soon.
Laurent Gomila - SFML developer