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

Window content not resizing on Windows #295

Closed
alvaro-cuesta opened this issue May 4, 2016 · 6 comments
Closed

Window content not resizing on Windows #295

alvaro-cuesta opened this issue May 4, 2016 · 6 comments

Comments

@alvaro-cuesta
Copy link

alvaro-cuesta commented May 4, 2016

I'm having problems handling resizing windows on Windows using piston_window. Resizing the window keeps everything I draw at the bottom-left corner of the window with the exact same intial size.

I tried both with the default (Glutin according to the docs) and SDL2 window and the problem persists, which I guess means the problem is in gfx_graphics.

Here's a simple test case:

extern crate piston;
extern crate piston_window;
extern crate sdl2_window;

use piston_window::{PistonWindow, WindowSettings, clear, rectangle};
use sdl2_window::Sdl2Window;
use piston::event_loop::Events;

fn main() {
    let mut window: PistonWindow<Sdl2Window> =
        WindowSettings::new("Resize bug", [640, 480])
        .exit_on_esc(true)
        .build()
        .unwrap();

    let mut events = window.events();
    while let Some(e) = events.next(&mut window) {
        window.draw_2d(&e, |c, g| {
            clear([0.0, 0.0, 0.0, 1.0], g);

            let size = c.get_view_size();
            let (w, h) = (size[0] as f64, size[1] as f64);

            rectangle(
                [0.1, 0.6, 0.3, 1.0],
                [0.0, 0.0, w/2.0, h/2.0],
                c.transform, g
            );

            rectangle(
                [0.1, 0.6, 0.3, 1.0],
                [w/2.0, h/2.0, w/2.0, h/2.0],
                c.transform, g
            );
        });
    }
}

Dependencies:

piston = "0.20.0"
piston_window = "0.44.0"
pistoncore-sdl2_window = { git = "https://github.com/PistonDevelopers/sdl2_window" }

As you can see I'm using c.get_view_size() and the reported size changes as I resize (which I guess means the framebuffer is being resized) but the actual drawing in the window stays as 640x480 at (0, 0).

@bvssvni
Copy link
Member

bvssvni commented May 4, 2016

Use window.draw_size().

@alvaro-cuesta
Copy link
Author

@bvssvni the problem is not the draw size (both window.draw_size() and c.get_view_size() change as I resize the window). The problem persists anyways.

@bvssvni
Copy link
Member

bvssvni commented May 4, 2016

Here is the fixed code:

extern crate piston_window;
extern crate sdl2_window;

use piston_window::*;
use sdl2_window::Sdl2Window;

fn main() {
    let mut window: PistonWindow<Sdl2Window> =
        WindowSettings::new("Resize bug", [640, 480])
        .exit_on_esc(true)
        .build()
        .unwrap();

    while let Some(e) = window.next() {
        let size = window.size();
        window.draw_2d(&e, |c, g| {
            clear([0.0, 0.0, 0.0, 1.0], g);

            let (w, h) = (size.width as f64, size.height as f64);

            rectangle(
                [0.1, 0.6, 0.3, 1.0],
                [0.0, 0.0, w/2.0, h/2.0],
                c.transform, g
            );

            rectangle(
                [0.1, 0.6, 0.3, 1.0],
                [w/2.0, h/2.0, w/2.0, h/2.0],
                c.transform, g
            );
        });
    }
}
  • piston is reexported in piston_window, so you do not need both
  • call window.next() since it does proper cleanup and resizes the frame targets

@alvaro-cuesta
Copy link
Author

The window.next() instead of .events() fixed it, thanks!

@kvark
Copy link
Member

kvark commented May 4, 2016

Thanks @bvssvni !

@Xaeroxe
Copy link

Xaeroxe commented Dec 18, 2016

@bvssvni Thanks! That's great that this works. Although it seems the tutorials and examples could use some updating as some of them use events.next() currently.

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

4 participants