Skip to content

Commit b26500e

Browse files
authored
Fix crash when window gets minimized (#332). Fixes #275
* Make sure aspect ratio is always valid * Do not render while window has an invalid size (e.g. window is minimized)
1 parent 0dd21a0 commit b26500e

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

src/main.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,11 @@ fn main2() {
352352
game.tick(delta);
353353
game.server.tick(&mut game.renderer, delta);
354354

355+
// Check if window is valid, it might be minimized
356+
if physical_width == 0 || physical_height == 0 {
357+
return;
358+
}
359+
355360
game.renderer.update_camera(physical_width, physical_height);
356361
game.server.world.compute_render_list(&mut game.renderer);
357362
game.chunk_builder.tick(&mut game.server.world, &mut game.renderer, version);
@@ -364,7 +369,6 @@ fn main2() {
364369
ui_container.tick(&mut game.renderer, delta, width as f64, height as f64);
365370
game.renderer.tick(&mut game.server.world, delta, width, height, physical_width, physical_height);
366371

367-
368372
if fps_cap > 0 && !vsync {
369373
let frame_time = now.elapsed();
370374
let sleep_interval = Duration::from_millis(1000 / fps_cap as u64);

src/render/mod.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,10 +259,13 @@ impl Renderer {
259259
self.height = height;
260260
gl::viewport(0, 0, width as i32, height as i32);
261261

262+
let fovy = cgmath::Rad::from(cgmath::Deg(90.0_f32));
263+
let aspect = (width as f32 / height as f32).max(1.0);
264+
262265
self.perspective_matrix = cgmath::Matrix4::from(
263266
cgmath::PerspectiveFov {
264-
fovy: cgmath::Rad::from(cgmath::Deg(90f32)),
265-
aspect: (width as f32 / height as f32),
267+
fovy,
268+
aspect,
266269
near: 0.1f32,
267270
far: 500.0f32,
268271
}

0 commit comments

Comments
 (0)