Nazariglez / notan

Cross-platform multimedia layer
https://nazariglez.github.io/notan-web/
Apache License 2.0
802 stars 55 forks source link

Scale issue with decimal scale factors on X11 #313

Open Nazariglez opened 7 months ago

Nazariglez commented 7 months ago

Code to reproduce:

use notan::{draw::*, prelude::*};

fn main() -> Result<(), String> {
    notan::init_with(setup)
        .add_config(DrawConfig)
        .add_config(WindowConfig {
            width: 512,
            height: 512,
            ..Default::default()
        })
        .draw(draw)
        .build()
}

#[derive(AppState)]
struct State {
    screen: (Box<[u8]>, Texture),
}

fn setup(gfx: &mut Graphics) -> State {
    let (width, height) = gfx.size();
    let (width_u, height_u) = (width as usize, height as usize);

    let mut pixel_buf = vec![0; 4 * width_u * height_u].into_boxed_slice();

    // fill the pixel data with the color 0x424242
    for chunk in pixel_buf.chunks_exact_mut(4) {
        let [r, g, b, a] = chunk else { unreachable!() };
        *r = 42;
        *g = 42;
        *b = 42;
        *a = 255;
    }

    // place a pixel in each corner of the pixel data
    for (px, py) in [
        (0, 0),
        (width_u - 1, 0),
        (0, height_u - 1),
        (width_u - 1, height_u - 1),
    ] {
        for off in 0..4 {
            pixel_buf[px * 4 + py * (width_u * 4) + off] = 255;
        }
    }

    let tex = gfx
        .create_texture()
        .from_bytes(&pixel_buf, width, height)
        .build()
        .unwrap();

    State {
        screen: (pixel_buf, tex),
    }
}

fn draw(gfx: &mut Graphics, state: &mut State) {
    let mut draw = gfx.create_draw();
    draw.image(&state.screen.1);
    gfx.render(&draw);
}

Run with WINIT_X11_SCALE_FACTOR=1.0 cargo run. Related info: https://docs.rs/winit/latest/winit/dpi/index.html#how-is-the-scale-factor-calculated