Fix panic in winit examples when pixels.render() returns Error (#70)

This commit is contained in:
Jay Oster 2020-04-13 20:38:51 -07:00 committed by GitHub
parent d88b4520f8
commit 91db963490
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 5 deletions

View file

@ -1,7 +1,7 @@
#![deny(clippy::all)] #![deny(clippy::all)]
#![forbid(unsafe_code)] #![forbid(unsafe_code)]
use log::debug; use log::{debug, error};
use pixels::{Error, Pixels, SurfaceTexture}; use pixels::{Error, Pixels, SurfaceTexture};
use winit::dpi::{LogicalPosition, LogicalSize, PhysicalSize}; use winit::dpi::{LogicalPosition, LogicalSize, PhysicalSize};
use winit::event::{Event, VirtualKeyCode}; use winit::event::{Event, VirtualKeyCode};
@ -30,7 +30,14 @@ fn main() -> Result<(), Error> {
// The one and only event that winit_input_helper doesn't have for us... // The one and only event that winit_input_helper doesn't have for us...
if let Event::RedrawRequested(_) = event { if let Event::RedrawRequested(_) = event {
life.draw(pixels.get_frame()); life.draw(pixels.get_frame());
pixels.render().unwrap(); if pixels
.render()
.map_err(|e| error!("pixels.render() failed: {}", e))
.is_err()
{
*control_flow = ControlFlow::Exit;
return;
}
} }
// For everything else, for let winit_input_helper collect events to build its state. // For everything else, for let winit_input_helper collect events to build its state.

View file

@ -5,7 +5,7 @@ use std::env;
use std::time::Instant; use std::time::Instant;
use gilrs::{Button, Gilrs}; use gilrs::{Button, Gilrs};
use log::debug; use log::{debug, error};
use pixels::{Error, Pixels, SurfaceTexture}; use pixels::{Error, Pixels, SurfaceTexture};
use simple_invaders::{Controls, Direction, World, SCREEN_HEIGHT, SCREEN_WIDTH}; use simple_invaders::{Controls, Direction, World, SCREEN_HEIGHT, SCREEN_WIDTH};
use winit::dpi::{LogicalPosition, LogicalSize, PhysicalSize}; use winit::dpi::{LogicalPosition, LogicalSize, PhysicalSize};
@ -37,7 +37,14 @@ fn main() -> Result<(), Error> {
// The one and only event that winit_input_helper doesn't have for us... // The one and only event that winit_input_helper doesn't have for us...
if let Event::RedrawRequested(_) = event { if let Event::RedrawRequested(_) = event {
invaders.draw(pixels.get_frame()); invaders.draw(pixels.get_frame());
pixels.render().unwrap(); if pixels
.render()
.map_err(|e| error!("pixels.render() failed: {}", e))
.is_err()
{
*control_flow = ControlFlow::Exit;
return;
}
} }
// Pump the gilrs event loop and find an active gamepad // Pump the gilrs event loop and find an active gamepad

View file

@ -1,6 +1,7 @@
#![deny(clippy::all)] #![deny(clippy::all)]
#![forbid(unsafe_code)] #![forbid(unsafe_code)]
use log::error;
use pixels::{wgpu::Surface, Error, Pixels, SurfaceTexture}; use pixels::{wgpu::Surface, Error, Pixels, SurfaceTexture};
use winit::dpi::LogicalSize; use winit::dpi::LogicalSize;
use winit::event::{Event, VirtualKeyCode}; use winit::event::{Event, VirtualKeyCode};
@ -46,7 +47,14 @@ fn main() -> Result<(), Error> {
// Draw the current frame // Draw the current frame
if let Event::RedrawRequested(_) = event { if let Event::RedrawRequested(_) = event {
world.draw(pixels.get_frame()); world.draw(pixels.get_frame());
pixels.render().unwrap(); if pixels
.render()
.map_err(|e| error!("pixels.render() failed: {}", e))
.is_err()
{
*control_flow = ControlFlow::Exit;
return;
}
} }
// Handle input events // Handle input events