SFML community forums

Help => General => Topic started by: metulburr on November 25, 2013, 04:48:04 am

Title: window creation lag before it centers
Post by: metulburr 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.
Title: Re: window creation lag before it centers
Post by: Laurent 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 ;)
Title: Re: window creation lag before it centers
Post by: wintertime 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.
Title: Re: window creation lag before it centers
Post by: metulburr 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.
Title: Re: window creation lag before it centers
Post by: Laurent 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.