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

invisible window #315

Open
kyoobey opened this issue Mar 17, 2023 · 3 comments
Open

invisible window #315

kyoobey opened this issue Mar 17, 2023 · 3 comments
Labels

Comments

@kyoobey
Copy link

kyoobey commented Mar 17, 2023

yesterday i was trying to fix wasmerio/wasmer#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

// 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::<i64>(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::<bool>(&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

// 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> {
	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 <const WIDTH: usize, const HEIGHT: usize> (
	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<Pixel> 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
Copy link
Owner

emoon commented Mar 17, 2023

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
Copy link
Author

kyoobey commented Mar 17, 2023

linux wayland

@emoon
Copy link
Owner

emoon commented Mar 18, 2023

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.

@emoon emoon added the wayland label Mar 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants