Skip to content

Commit

Permalink
app: Correct invalid window size, reject window resize events if the …
Browse files Browse the repository at this point in the history
…window is minimized
  • Loading branch information
cohaereo committed Feb 2, 2025
1 parent c00d1ad commit 5568e83
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 19 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/)
- When a texture handle is explicitly set to `none`, Alkahest will now properly unset the texture instead of binding the fallback texture
- Bake rustc version as a constant instead of checking it at runtime
- Fixed solid geometry not rendering in simpler maps (Shard, Sunken, etc.)
- Fixed invalid window sizes being handled (eg. win32 resizes the window to 0x0 when minimizing it)

## 0.5.0 - 2024-07-24

Expand Down
50 changes: 31 additions & 19 deletions crates/alkahest/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ impl AlkahestApp {

let window = winit::window::WindowBuilder::new()
.with_title("Alkahest")
.with_min_inner_size(PhysicalSize::new(640, 360))
.with_min_inner_size(PhysicalSize::new(1280, 720))
.with_inner_size(config::with(|c| {
PhysicalSize::new(c.window.width, c.window.height)
}))
Expand All @@ -104,6 +104,14 @@ impl AlkahestApp {
.unwrap();
let window = Arc::new(window);

// Make sure the window size in the config is not below the minimum size
config::with_mut(|c| {
let corrected_size = window.inner_size();
c.window.width = corrected_size.width;
c.window.height = corrected_size.height;
});
config::try_persist().ok();

puffin::set_scopes_on(cfg!(feature = "profiler"));

let gctx = Arc::new(GpuContext::create(&window).unwrap());
Expand Down Expand Up @@ -274,27 +282,31 @@ impl AlkahestApp {
}
}
WindowEvent::Resized(new_dims) => {
if let Some(swap_chain) = gctx.swap_chain.as_ref() {
let _ = gui.renderer.as_mut().map(|renderer| {
let _ = renderer
.resize_buffers(swap_chain, || {
gctx.resize_swapchain(new_dims.width, new_dims.height);
HRESULT(0)
})
.unwrap();
});
}
let minimized = window.is_minimized().unwrap_or(false);
if !minimized && new_dims.width > 0 && new_dims.height > 0 {
if let Some(swap_chain) = gctx.swap_chain.as_ref() {
let _ = gui.renderer.as_mut().map(|renderer| {
let _ = renderer
.resize_buffers(swap_chain, || {
gctx.resize_swapchain(new_dims.width, new_dims.height);
HRESULT(0)
})
.unwrap();
});
}

renderer.resize_buffers(new_dims.width, new_dims.height);
renderer.resize_buffers(new_dims.width, new_dims.height);

resources.get_mut::<Camera>().set_viewport(Viewport {
size: glam::UVec2::new(new_dims.width, new_dims.height),
origin: glam::UVec2::ZERO,
});
resources.get_mut::<Camera>().set_viewport(Viewport {
size: glam::UVec2::new(new_dims.width, new_dims.height),
origin: glam::UVec2::ZERO,
});

config::with_mut(|c| {
(c.window.width, c.window.height) = (new_dims.width, new_dims.height)
});
config::with_mut(|c| {
(c.window.width, c.window.height) =
(new_dims.width, new_dims.height)
});
}
}
WindowEvent::RedrawRequested => {
if *next_config_save < std::time::Instant::now() {
Expand Down

0 comments on commit 5568e83

Please sign in to comment.