jeremyletang / rust-sfml

SFML bindings for Rust
Other
631 stars 88 forks source link

RenderWindow set_position does not work #284

Closed dogunbound closed 2 years ago

dogunbound commented 2 years ago

Min reproducible code:

use sfml::{graphics::RenderWindow, system::Vector2i, window::Style};

fn main() {
    // Create a new window
    let mut window = RenderWindow::new(
        (1280, 720),
        "Test",
        Style::DEFAULT,
        &Default::default(),
    );
    let new_window_position = Vector2i::new(23, 31);
    window.set_position(new_window_position);
    assert_eq!(new_window_position, window.position());
}

assert_eq! panics.

dogunbound commented 2 years ago

After some more testing, I think it's the window.position() function that is faulty

This properly moves the window.

use sfml::{graphics::RenderWindow, system::Vector2i, window::Style};
use std::thread::sleep;
use std::time::Duration;

fn main() {
    // Create a new window
    let mut window = RenderWindow::new((1280, 720), "Test", Style::DEFAULT, &Default::default());
    let (min, max, step) = (0, 1000, 50);
    let mut current_step = step;
    let mut current_position = Vector2i::new(min, min);

    while window.is_open() {
        current_step = if current_position.x <= min {
            step
        } else if current_position.x >= max {
            -step
        } else {
            current_step
        };
        current_position.x += current_step;
        current_position.y += current_step;
        window.set_position(current_position);

        sleep(Duration::from_millis(250));
    }
}
crumblingstatue commented 2 years ago

It seems highly likely that the position change is simply not instantaneous. You need to wait for the window to change to the new position before you can query the new position.

    window.set_position(new_window_position);
    while new_window_position != window.position() {
        eprintln!("Waiting");
    }
    assert_eq!(new_window_position, window.position());

This prints a single "Waiting" for me. It's so fast, it has happened right after printing "waiting". It's just not instantaneous.