Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PistonWindow.set_size() does not always apply size #285

Open
nashimus opened this issue Nov 14, 2020 · 5 comments
Open

PistonWindow.set_size() does not always apply size #285

nashimus opened this issue Nov 14, 2020 · 5 comments

Comments

@nashimus
Copy link

Example to reproduce

Compiler

rustc --version
rustc 1.47.0 (18bf6b4f0 2020-10-07)

Cargo.toml

[dependencies]
piston_window = "0.113.0"

main.rs

extern crate piston_window;

use piston_window::*;

fn main() {
    let mut window: PistonWindow = WindowSettings::new("Test Window", [480.0, 480.0])
        .exit_on_esc(true)
        .build()
        .unwrap();

    while let Some(e) = window.next() {
        if let Some(_) = e.render_args() {
            render(&mut window, &e);
        }

        if let Some(r) = e.resize_args() {
            resize(r, &mut window);
        }
    }
}

fn render(window: &mut PistonWindow, e: &Event) {
    let win_size = window.size();
    window.draw_2d(e, |c, g, _device| {
        clear([1.0; 4], g);
        rectangle(
            [1.0, 1.0, 0.0, 1.0],
            [
                0.0,
                0.0,
                win_size.width,
                win_size.height,
            ],
            c.transform,
            g,
        );
    });
}

fn resize(r: ResizeArgs, window: &mut PistonWindow) {
    println!("window resized to {} {}", r.window_size[0], r.window_size[1]);

    if r.window_size[0] != r.window_size[1] {
        // try to maintain aspect ratio 1:1
        let xy = (r.window_size[0] + r.window_size[1]) / 2.0;
        let size = Size { width: xy, height: xy };
        window.set_size(size);
        println!("square set to {:?}", size);
        println!("actual window size = {:?}", window.size());
    }
}

Example of output when it does not apply:

window resized to 480 480
window resized to 480 478.7692307692308
square set to Size { width: 479.38461538461536, height: 479.38461538461536 }
actual window size = Size { width: 480.0, height: 478.7692307692308 }
window resized to 490.46153846153845 470.7692307692308
square set to Size { width: 480.61538461538464, height: 480.61538461538464 }
actual window size = Size { width: 490.46153846153845, height: 470.7692307692308 }
window resized to 518.7692307692307 446.7692307692308
square set to Size { width: 482.7692307692307, height: 482.7692307692307 }
actual window size = Size { width: 518.7692307692307, height: 446.7692307692308 }

Example of output when it does apply:

window resized to 617.2307692307693 361.84615384615387
square set to Size { width: 489.53846153846155, height: 489.53846153846155 }
actual window size = Size { width: 617.2307692307693, height: 361.84615384615387 }
window resized to 489.84615384615387 489.84615384615387

@bvssvni
Copy link
Member

bvssvni commented Nov 15, 2020

Have you checked other window backends?

Try PistonWindow<Sdl2Window> and add pistoncore-sdl2_window.

This will tell us whether the bug is in Piston or in the backend.

@nashimus
Copy link
Author

I'll give that a shot.

@nashimus
Copy link
Author

nashimus commented Nov 15, 2020

window.set_size() does work when using PistonWindow<Sdl2Window>.

@bvssvni
Copy link
Member

bvssvni commented Nov 15, 2020

Do you believe there can be a bug here? It seems pretty straight forward to me: https://github.com/PistonDevelopers/glutin_window/blob/master/src/lib.rs#L519

@bvssvni
Copy link
Member

bvssvni commented Nov 15, 2020

If the bug isn't in glutin_window, then it is probably in winit: https://crates.io/crates/winit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants