emoon / rust_minifb

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

invisible window #315

Open kyoobey opened 1 year ago

kyoobey commented 1 year ago

yesterday i was trying to fix https://github.com/wasmerio/wasmer/pull/3591 which depends on minifb, after implementing the missing async traits wasmer compiles and everything seems working on the terminal but there's no window

today i'm having a similar issue with rhai, here's what i've written so far:

`main.rs` ```rs // logging #[macro_use] extern crate log; use fbtest::{ init_logger, timestamp, create_window, update_window, generate_frame, pixel::{Pixel, DARK_GREY, WHITE} }; use rhai::{ Engine, EvalAltResult, Locked, AST }; use std::time::Duration; const WIDTH: usize = 600; const HEIGHT: usize = 600; fn main () { init_logger(); let fps = Duration::from_millis(1000/60); let mut window = create_window(WIDTH, HEIGHT, fps, "fbtest1") .unwrap_or_else(|| unimplemented!()); let rhai = Engine::new(); let script = "40 + 2"; debug!("rhai output: {}", rhai.eval::(script).unwrap()); let ast = rhai.compile(format!("\ x -= (t % 1.0)*2.0-1.0;\ (x*x + y*y) < (sin(t*1.2)*0.5+0.5)*0.25\ ")).unwrap(); let start_time = timestamp(); while window.is_open() { let buffer = generate_frame::<{WIDTH}, {HEIGHT}>(start_time, pixel, &rhai, &ast); update_window(&buffer, WIDTH, HEIGHT, &mut window); break; } loop {} } fn pixel (mut x: f64, y: f64, t: f64, rhai: &Engine, ast2: &AST) -> Pixel { let mut out = DARK_GREY; // commenting the following shows the window !? let ast1 = rhai.compile(format!("\ let x = {x};\ let y = {y};\ let t = {t};\ ")).unwrap(); // let check = rhai.eval_ast::(&ast1.merge(ast2)).unwrap(); // debug!("rhai debug: {check}"); // if check { // out = WHITE; // } x -= (t % 1.0)*2.0-1.0; if (x*x + y*y) < ((t*1.2).sin()*0.5+0.5)*0.25 { out = WHITE; } out } ```
`lib.rs` ```rs // unstable #![allow(incomplete_features)] #![feature(generic_const_exprs)] #![feature(result_option_inspect)] // logging #[macro_use] extern crate log; use rhai::{Engine, AST}; use simple_logger::SimpleLogger; // imports // use minifb::{ Window, WindowOptions }; use minifb::WindowOptions; use std::time::{ Duration, Instant }; pub fn init_logger () { SimpleLogger::new() .with_level(log::LevelFilter::Trace) .init() .expect("Couldn't initialize logger"); } pub fn timestamp () -> Instant { Instant::now() } pub fn create_window ( width: usize, height: usize, fps: Duration, name: &str ) -> Option { Window::new(name, width, height,WindowOptions::default()) .map_err(|error| error!("window creation failed! `{error}`")) .map(|mut x| { x.limit_update_rate(Some(fps)); x }) .ok() } pub fn update_window ( buffer: &[u32], width: usize, height: usize, window: &mut Window ) { window.update_with_buffer(buffer, width, height) .unwrap_or_else(|error| warn!("{}", error)); } pub fn generate_frame ( start_time: Instant, pixel: fn(f64, f64, f64, &Engine, &AST) -> pixel::Pixel, rhai: &Engine, ast: &AST ) -> [u32; WIDTH * HEIGHT] { let mut buffer = [0u32; WIDTH * HEIGHT]; for i in 0..WIDTH { for j in 0..HEIGHT { let (mut x, mut y, t); x = i as f64 / WIDTH as f64; y = j as f64 / HEIGHT as f64; (x, y) = (x * 2.0 - 1.0, y * 2.0 - 1.0); t = (Instant::now() - start_time).as_secs_f64(); buffer[i + j * WIDTH] = u32::from(pixel(x, y, t, rhai, ast)); } } buffer } pub mod pixel { pub struct Pixel { pub r: u8, pub g: u8, pub b: u8, pub a: u8, } impl Pixel { pub const fn new (r: u8, g: u8, b: u8, a: u8) -> Self { Self { r, g, b, a } } } impl From for u32 { fn from (pixel: Pixel) -> u32 { ((pixel.r as u32) << 16) + ((pixel.g as u32) << 8 ) + (pixel.b as u32) } } pub const WHITE: Pixel = Pixel::new(255, 255, 255, 0); pub const DARK_GREY: Pixel = Pixel::new(16, 16, 16, 0); pub const BLACK: Pixel = Pixel::new(0, 0, 0, 0); } ```

even using the macros from log crate results in the same issue, window is not visible

emoon commented 1 year ago

Just to make sure. Are you trying to do this for Web or are you trying to run it on macOS, Linux, Windows or similar?

kyoobey commented 1 year ago

linux wayland

emoon commented 1 year ago

Sigh, So many issues with Wayland :( but this is a new one. I will in the near future have a look at all the Wayland issues. I haven't implemented the Wayland support myself, but I think i will need to "take ownership" and fix the issues. I will put this on the list when I do.

If you have the possibility to test on another target and see if it behaves the same that would be much appreciated.