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.