Skip to content

Commit

Permalink
remove redundant window size from surface data struct; DRY resize logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Davidster committed Aug 6, 2023
1 parent 7593e7c commit a32bd6e
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 25 deletions.
14 changes: 8 additions & 6 deletions ikari/src/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down Expand Up @@ -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!(
Expand All @@ -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) {
Expand Down Expand Up @@ -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),
)
Expand Down
47 changes: 34 additions & 13 deletions ikari/src/gameloop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand All @@ -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<()>,
Expand Down Expand Up @@ -155,11 +168,13 @@ pub fn run(
Err(err) => match err.downcast_ref::<wgpu::SurfaceError>() {
// 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,
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 0 additions & 4 deletions ikari/src/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,8 +338,6 @@ pub struct SurfaceData {
pub surface: wgpu::Surface,
// TODO: does this need to be a mutex still?
pub surface_config: Mutex<wgpu::SurfaceConfiguration>,
// TODO: remove this? we can get the size from the surface_config 🤔
pub window_size: Mutex<winit::dpi::PhysicalSize<u32>>,
}

impl BaseRenderer {
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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();
Expand Down
8 changes: 6 additions & 2 deletions ikari/src/ui_overlay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -859,9 +859,13 @@ impl IkariUiOverlay {
}
}

pub fn resize(&mut self, window_size: winit::dpi::PhysicalSize<u32>, 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,
);
}
Expand Down

0 comments on commit a32bd6e

Please sign in to comment.