Skip to content

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

@nashimus

Description

@nashimus

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions