emoon / rust_minifb

Cross platfrom window and framebuffer crate for Rust
MIT License
1.04k stars 99 forks source link

Mouse coordinates are off in fullscreen, MacOS #319

Open ljdoig opened 1 year ago

ljdoig commented 1 year ago

It seems the mouse coordinates are off when you go into fullscreen mode on macOS (Ventura 13.1). It gets it right for any resized window and for any scale_mode, except when I go fullscreen the mouse is not where it thinks. This doesn't seem to affect Windows users.

Fullscreen:

fullscreen

Non fullscreen (regardless of scale mode, aspect ratio or size)

small2 small

code:

use minifb::*;

fn main() {
    let mut window = Window::new(
        "Fullscreen",
        800,
        500,
        WindowOptions {
            resize: true,
            scale_mode: ScaleMode::Center,
            ..WindowOptions::default()
        },
    )
    .unwrap();
    while window.is_open() && !window.is_key_down(Key::Escape) {
        assert_eq!(
            window.get_mouse_pos(MouseMode::Discard),
            window.get_unscaled_mouse_pos(MouseMode::Discard)
        );
        let (width, height) = window.get_size();
        let mut pixels = vec![0x00000000; width * height];
        // Paint the row and col of pixels that the mouse is hovering over
        window
            .get_mouse_pos(MouseMode::Discard)
            .map(|(mouse_x, mouse_y)| {
                for mouse_x in 0..width {
                    pixels[mouse_x as usize + mouse_y as usize * width] = 0x00FFFFFF;
                }
                for mouse_y in 0..height {
                    pixels[mouse_x as usize + mouse_y as usize * width] = 0x00FFFFFF;
                }
            });
        window.update_with_buffer(&pixels, width, height).unwrap();
    }
}

On a possibly unrelated note, should this work after resizing?

assert_eq!(
            window.get_mouse_pos(MouseMode::Discard),
            window.get_unscaled_mouse_pos(MouseMode::Discard)
        );

Thanks for your time

emoon commented 1 year ago

Thanks for the report. My guess is that minifb needs to detect when the application enters fullscreen as I think it currently always includes the size of the titlebar currently.

ljdoig commented 1 year ago

This seems correct as I get the same behaviour with :

        WindowOptions {
            title: false,
            ..WindowOptions::default()
        }