diff options
| author | LLLL Colonq <llll@colonq> | 2025-02-17 17:37:07 -0500 |
|---|---|---|
| committer | LLLL Colonq <llll@colonq> | 2025-02-17 17:37:07 -0500 |
| commit | 3c56138527b9b6baa6a6b23733cd54b4d281170f (patch) | |
| tree | ffd68c93f5455f47ef31b37a2395b5bf1d3336a2 | |
| parent | f5d5478c0ca8e0a512c17206e78e077e7aac1e58 (diff) | |
NORESIZE option
| -rw-r--r-- | src/context.rs | 46 | ||||
| -rw-r--r-- | src/lib.rs | 23 |
2 files changed, 39 insertions, 30 deletions
diff --git a/src/context.rs b/src/context.rs index 502bbdb..2171053 100644 --- a/src/context.rs +++ b/src/context.rs @@ -17,6 +17,7 @@ extern { pub struct Context { pub render_width: f32, pub render_height: f32, + pub resize: bool, pub glfw: std::cell::RefCell<glfw::Glfw>, pub window: std::cell::RefCell<glfw::PWindow>, pub gl: glow::Context, @@ -29,6 +30,7 @@ pub struct Context { pub struct Context { pub render_width: f32, pub render_height: f32, + pub resize: bool, pub window: winit::window::Window, pub gl: glow::Context, pub emptyvao: glow::VertexArray, @@ -50,12 +52,13 @@ impl Context { } #[cfg(not(target_arch = "wasm32"))] - pub fn new(glfw: std::cell::RefCell<glfw::Glfw>, window: std::cell::RefCell<glfw::PWindow>, gl: glow::Context, render_width: f32, render_height: f32) -> Self { + pub fn new(glfw: std::cell::RefCell<glfw::Glfw>, window: std::cell::RefCell<glfw::PWindow>, gl: glow::Context, render_width: f32, render_height: f32, resize: bool) -> Self { let emptyvao = unsafe { gl.create_vertex_array().expect("failed to initialize vao") }; let ret = Self { render_width, render_height, + resize, glfw, window, gl, emptyvao, @@ -66,7 +69,7 @@ impl Context { } #[cfg(target_arch = "wasm32")] - pub fn new(window: winit::window::Window, gl: glow::Context, render_width: f32, render_height: f32) -> Self { + pub fn new(window: winit::window::Window, gl: glow::Context, render_width: f32, render_height: f32, resize: bool) -> Self { let emptyvao = unsafe { gl.create_vertex_array().expect("failed to initialize vao") }; @@ -76,6 +79,7 @@ impl Context { let ret = Self { render_width, render_height, + resize, window, gl, emptyvao, @@ -107,24 +111,26 @@ impl Context { #[cfg(target_arch = "wasm32")] pub fn maximize_canvas(&self) { - web_sys::window() - .and_then(|win| win.document()) - .and_then(|doc| { - let inner_size = { - let browser_window = doc.default_view() - .or_else(web_sys::window) - .unwrap(); - winit::dpi::PhysicalSize::new( - browser_window.inner_width().unwrap().as_f64().unwrap(), - browser_window.inner_height().unwrap().as_f64().unwrap(), - ) - }; - self.window.canvas().unwrap().set_width(inner_size.width as _); - self.window.canvas().unwrap().set_height(inner_size.height as _); - let _ = self.window.request_inner_size(inner_size); - Some(()) - }) - .expect("failed to resize canvas"); + if self.resize { + web_sys::window() + .and_then(|win| win.document()) + .and_then(|doc| { + let inner_size = { + let browser_window = doc.default_view() + .or_else(web_sys::window) + .unwrap(); + winit::dpi::PhysicalSize::new( + browser_window.inner_width().unwrap().as_f64().unwrap(), + browser_window.inner_height().unwrap().as_f64().unwrap(), + ) + }; + self.window.canvas().unwrap().set_width(inner_size.width as _); + self.window.canvas().unwrap().set_height(inner_size.height as _); + let _ = self.window.request_inner_size(inner_size); + Some(()) + }) + .expect("failed to resize canvas"); + } } #[cfg(target_arch = "wasm32")] @@ -27,14 +27,12 @@ use wasm_bindgen::JsCast; #[cfg(not(target_arch = "wasm32"))] use glfw::Context; -#[cfg(not(target_arch = "wasm32"))] use bitflags::bitflags; - -#[cfg(not(target_arch = "wasm32"))] bitflags! { pub struct Options: u32 { - const OVERLAY = 0b00000001; - const HIDDEN = 0b00000010; + const OVERLAY = 0b00000001; + const HIDDEN = 0b00000010; + const NORESIZE = 0b00000100; } } @@ -67,6 +65,7 @@ where log::info!("hello computer, starting up..."); + let resize = !options.contains(Options::NORESIZE); let (rglfw, rwindow, gl, events) = { use glfw::fail_on_errors; let mut glfw = glfw::init(glfw::fail_on_errors!()).expect("failed to initialize GLFW"); @@ -114,7 +113,10 @@ where let glfw = std::cell::RefCell::new(rglfw); let window = std::cell::RefCell::new(rwindow); - let ctx = Box::leak(Box::new(context::Context::new(glfw, window, gl, w as f32, h as f32))); + let ctx = Box::leak(Box::new(context::Context::new( + glfw, window, gl, + w as f32, h as f32, resize, + ))); let game = Box::leak(Box::new(gnew(ctx).await)); let st = Box::leak(Box::new(state::State::new(&ctx))); @@ -181,7 +183,7 @@ where } #[cfg(target_arch = "wasm32")] -pub async fn run<'a, F, G, Fut>(w: u32, h: u32, gnew: F) +pub async fn run<'a, F, G, Fut>(w: u32, h: u32, options: Options, gnew: F) where Fut: std::future::Future<Output = G>, G: state::Game + 'static, @@ -196,9 +198,10 @@ where let event_loop = winit::event_loop::EventLoop::new() .expect("failed to initialize event loop"); + let resize = !options.contains(Options::NORESIZE); let (window, gl) = { let window = winit::window::WindowBuilder::new() - .with_maximized(true) + .with_maximized(resize) .with_decorations(false) .build(&event_loop) .expect("failed to initialize window"); @@ -217,7 +220,7 @@ where (window, gl) }; - let ctx = Box::leak(Box::new(context::Context::new(window, gl, w as f32, h as f32))); + let ctx = Box::leak(Box::new(context::Context::new(window, gl, w as f32, h as f32, resize))); ctx.maximize_canvas(); let game = Box::leak(Box::new(gnew(ctx).await)); let st = Box::leak(Box::new(state::State::new(&ctx))); @@ -379,5 +382,5 @@ impl state::Game for TestGame { #[cfg(target_arch = "wasm32")] #[wasm_bindgen] pub async fn main_js_test() { - run(240, 160, TestGame::new).await; + run(240, 160, Options::empty(), TestGame::new).await; } |
