diff --git a/ikari/src/game.rs b/ikari/src/game.rs index f17024c4..ae61168b 100644 --- a/ikari/src/game.rs +++ b/ikari/src/game.rs @@ -4,6 +4,7 @@ use crate::audio::*; use crate::ball::*; use crate::character::*; use crate::game_state::*; +use crate::gameloop::resize_window; use crate::light::*; use crate::math::*; use crate::mesh::*; @@ -1096,9 +1097,10 @@ pub fn increment_render_scale( let delta = 0.1; let change = if increase { delta } else { -delta }; - { + let framebuffer_size = { let mut renderer_data_guard = renderer.data.lock().unwrap(); let surface_config_guard = surface_data.surface_config.lock().unwrap(); + renderer_data_guard.render_scale = (renderer_data_guard.render_scale + change).clamp(0.1, 4.0); log::info!( @@ -1109,11 +1111,11 @@ pub fn increment_render_scale( (surface_config_guard.height as f32 * renderer_data_guard.render_scale.sqrt()).round() as u32, ); - } - renderer.resize_surface(window.inner_size().into(), surface_data); - renderer.resize(window.inner_size().into()); - ui_overlay.resize(window.inner_size(), window.scale_factor()); + (surface_config_guard.width, surface_config_guard.height) + }; + + resize_window(renderer, ui_overlay, surface_data, window, framebuffer_size); } pub fn increment_exposure(renderer_data: &mut RendererData, increase: bool) { @@ -1569,7 +1571,7 @@ pub fn update_game_state( deg_to_rad(90.0), )) .scale( - (1080.0 / surface_data.window_size.lock().unwrap().height as f32) + (1080.0 / surface_data.surface_config.lock().unwrap().height as f32) * 0.06 * Vec3::new(1.0, 1.0, 1.0), ) diff --git a/ikari/src/gameloop.rs b/ikari/src/gameloop.rs index 0479d2d7..d36ff0d7 100644 --- a/ikari/src/gameloop.rs +++ b/ikari/src/gameloop.rs @@ -3,6 +3,7 @@ use crate::game_state::*; use crate::renderer::*; use crate::time::*; use crate::ui_overlay::AudioSoundStats; +use crate::ui_overlay::IkariUiOverlay; #[cfg(target_arch = "wasm32")] use wasm_bindgen::prelude::*; @@ -13,6 +14,18 @@ use winit::{ window::Window, }; +pub fn resize_window( + renderer: &mut Renderer, + ui_overlay: &mut IkariUiOverlay, + surface_data: &SurfaceData, + window: &winit::window::Window, + new_size: (u32, u32), +) { + renderer.resize_surface(new_size, surface_data); + renderer.resize(new_size); + ui_overlay.resize(new_size, window.scale_factor()); +} + pub fn run( window: Window, event_loop: EventLoop<()>, @@ -155,11 +168,13 @@ pub fn run( Err(err) => match err.downcast_ref::() { // Reconfigure the surface if lost Some(wgpu::SurfaceError::Lost) => { - renderer.resize_surface(window.inner_size().into(), &surface_data); - renderer.resize(window.inner_size().into()); - game_state - .ui_overlay - .resize(window.inner_size(), window.scale_factor()); + resize_window( + &mut renderer, + &mut game_state.ui_overlay, + &surface_data, + &window, + window.inner_size().into(), + ); } // The system is out of memory, we should probably quit Some(wgpu::SurfaceError::OutOfMemory) => *control_flow = ControlFlow::Exit, @@ -194,18 +209,24 @@ pub fn run( match &event { WindowEvent::Resized(size) => { if size.width > 0 && size.height > 0 { - renderer.resize_surface((*size).into(), &surface_data); - renderer.resize((*size).into()); - game_state.ui_overlay.resize(*size, window.scale_factor()); + resize_window( + &mut renderer, + &mut game_state.ui_overlay, + &surface_data, + &window, + (*size).into(), + ); } } WindowEvent::ScaleFactorChanged { new_inner_size, .. } => { if new_inner_size.width > 0 && new_inner_size.height > 0 { - renderer.resize_surface((**new_inner_size).into(), &surface_data); - renderer.resize((**new_inner_size).into()); - game_state - .ui_overlay - .resize(**new_inner_size, window.scale_factor()); + resize_window( + &mut renderer, + &mut game_state.ui_overlay, + &surface_data, + &window, + (**new_inner_size).into(), + ); } } WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit, diff --git a/ikari/src/renderer.rs b/ikari/src/renderer.rs index 60043d49..e1aaac35 100644 --- a/ikari/src/renderer.rs +++ b/ikari/src/renderer.rs @@ -338,8 +338,6 @@ pub struct SurfaceData { pub surface: wgpu::Surface, // TODO: does this need to be a mutex still? pub surface_config: Mutex, - // TODO: remove this? we can get the size from the surface_config 🤔 - pub window_size: Mutex>, } impl BaseRenderer { @@ -373,7 +371,6 @@ impl BaseRenderer { let surface_data = SurfaceData { surface, surface_config: Mutex::new(surface_config), - window_size: Mutex::new(window_size), }; Ok((base, surface_data)) @@ -2812,7 +2809,6 @@ impl Renderer { } pub fn resize_surface(&mut self, new_size: (u32, u32), surface_data: &SurfaceData) { - *surface_data.window_size.lock().unwrap() = new_size.into(); let (new_width, new_height) = new_size; let surface_config = { let mut surface_config_guard = surface_data.surface_config.lock().unwrap(); diff --git a/ikari/src/ui_overlay.rs b/ikari/src/ui_overlay.rs index 21ed06a4..96f2d533 100644 --- a/ikari/src/ui_overlay.rs +++ b/ikari/src/ui_overlay.rs @@ -859,9 +859,13 @@ impl IkariUiOverlay { } } - pub fn resize(&mut self, window_size: winit::dpi::PhysicalSize, scale_factor: f64) { + pub fn resize( + &mut self, + (framebuffer_width, framebuffer_height): (u32, u32), + scale_factor: f64, + ) { self.viewport = iced_winit::Viewport::with_physical_size( - iced::Size::new(window_size.width, window_size.height), + iced::Size::new(framebuffer_width, framebuffer_height), scale_factor, ); }