diff --git a/README.md b/README.md index 0569cf43f..158010e4d 100644 --- a/README.md +++ b/README.md @@ -101,14 +101,15 @@ to common problems. ### Basic Project Template ```rust,no_run -use ggez::{graphics, Context, ContextBuilder, GameResult}; +use anyhow; +use ggez::{graphics, Context, ContextBuilder}; use ggez::event::{self, EventHandler}; fn main() { // Make a Context. let (mut ctx, mut event_loop) = ContextBuilder::new("my_game", "Cool Game Author") - .build() - .expect("aieee, could not create ggez context!"); + .build() + .expect("aieee, could not create ggez context!"); // Create an instance of your event handler. // Usually, you should provide it with the Context object to @@ -136,15 +137,15 @@ impl MyGame { } impl EventHandler for MyGame { - fn update(&mut self, _ctx: &mut Context) -> GameResult<()> { + fn update(&mut self, _ctx: &mut Context) -> anyhow::Result<()> { // Update code here... Ok(()) } - fn draw(&mut self, ctx: &mut Context) -> GameResult<()> { + fn draw(&mut self, ctx: &mut Context) -> anyhow::Result<()> { graphics::clear(ctx, graphics::WHITE); // Draw code here... - graphics::present(ctx) + Ok(graphics::present(ctx)?) } } ``` diff --git a/docs/guides/GenerativeArt.md b/docs/guides/GenerativeArt.md index 7bdc19f24..e9616f7e3 100644 --- a/docs/guides/GenerativeArt.md +++ b/docs/guides/GenerativeArt.md @@ -14,16 +14,17 @@ Modify `Cargo.toml` to include `ggez` in the dependencies. Just like in `Hello ggez!`, we're going to use a loop and a struct. Let's start with this code in `src/main.rs`: ```rust,no_run +use anyhow; use ggez::*; struct State { } impl ggez::event::EventHandler for State { - fn update(&mut self, _ctx: &mut Context) -> GameResult { + fn update(&mut self, _ctx: &mut Context) -> anyhow::Result<()> { Ok(()) } - fn draw(&mut self, ctx: &mut Context) -> GameResult { + fn draw(&mut self, ctx: &mut Context) -> anyhow::Result<()> { graphics::present(ctx)?; Ok(()) } @@ -71,7 +72,7 @@ Geometry, it's all coming back now. Here is the code for a [circle](https://docs.rs/ggez/0.4.0/ggez/graphics/struct.Mesh.html#method.new_circle): ```rust,skt-draw,no_run -fn draw(&mut self, ctx: &mut Context) -> GameResult { +fn draw(&mut self, ctx: &mut Context) -> anyhow::Result<()> { let circle = graphics::Mesh::new_circle( ctx, graphics::DrawMode::fill(), @@ -108,7 +109,7 @@ And that's how a circle is drawn! Let's try it out with some quick code: ```rust,skt-draw,no_run -fn draw(&mut self, ctx: &mut Context) -> GameResult { +fn draw(&mut self, ctx: &mut Context) -> anyhow::Result<()> { let circle = graphics::Mesh::new_circle( ctx, graphics::DrawMode::fill(), @@ -152,7 +153,7 @@ And that's how a rectangle is drawn! Let's try it out with some quick code: ```rust,skt-draw,no_run -fn draw(&mut self, ctx: &mut Context) -> GameResult { +fn draw(&mut self, ctx: &mut Context) -> anyhow::Result<()> { let rect = graphics::Mesh::new_rectangle( ctx, graphics::DrawMode::fill(), @@ -227,7 +228,7 @@ We're using `Vec.push` to add 2 new values to our `Vec`. But we still don't see anything... You need to modify `draw` to illustrate your new `State`. ```rust,skt-draw,no_run -fn draw(&mut self, ctx: &mut Context) -> GameResult { +fn draw(&mut self, ctx: &mut Context) -> anyhow::Result<()> { for shape in &self.shapes { // Make the shape... let mesh = match shape { diff --git a/docs/guides/GenerativeArt.md.skt.md b/docs/guides/GenerativeArt.md.skt.md index c3dcd395d..ec5dc79a3 100644 --- a/docs/guides/GenerativeArt.md.skt.md +++ b/docs/guides/GenerativeArt.md.skt.md @@ -4,6 +4,7 @@ #![allow(unused_variables)] #![allow(unused_mut)] +use anyhow; use ggez::*; use ggez::graphics::*; use rand::*; @@ -25,14 +26,14 @@ impl State {{ }} impl event::EventHandler for State {{ - fn update(&mut self, _ctx: &mut Context) -> GameResult {{ + fn update(&mut self, _ctx: &mut Context) -> anyhow::Result<()> {{ Ok(()) }} {} }} -pub fn main() -> GameResult {{ +pub fn main() -> anyhow::Result<()> {{ Ok(()) }} ``` @@ -43,6 +44,7 @@ pub fn main() -> GameResult {{ #![allow(unused_variables)] #![allow(unused_mut)] +use anyhow; use ggez::*; use ggez::graphics::*; use rand::*; @@ -108,6 +110,7 @@ use ggez::graphics::*; #![allow(unused_variables)] #![allow(unused_mut)] +use anyhow; use ggez::*; use ggez::graphics::*; use ggez::event::*; @@ -122,15 +125,15 @@ struct State {{ }} impl event::EventHandler for State {{ - fn update(&mut self, _ctx: &mut Context) -> GameResult {{ + fn update(&mut self, _ctx: &mut Context) -> anyhow::Result<()> {{ Ok(()) }} - fn draw(&mut self, _ctx: &mut Context) -> GameResult {{ + fn draw(&mut self, _ctx: &mut Context) -> anyhow::Result<()> {{ Ok(()) }} }} {} -``` \ No newline at end of file +``` diff --git a/docs/guides/HelloGgez.md b/docs/guides/HelloGgez.md index 92b8cca50..c0219115b 100644 --- a/docs/guides/HelloGgez.md +++ b/docs/guides/HelloGgez.md @@ -84,10 +84,10 @@ Let's add `EventHandler` to our `src/main.rs` file: struct State {} impl ggez::event::EventHandler for State { - fn update(&mut self, ctx: &mut Context) -> GameResult<()> { + fn update(&mut self, ctx: &mut Context) -> anyhow::Result<()> { Ok(()) } - fn draw(&mut self, ctx: &mut Context) -> GameResult<()> { + fn draw(&mut self, ctx: &mut Context) -> anyhow::Result<()> { Ok(()) } } @@ -129,7 +129,7 @@ warning: unused variable: `state` warning: unused variable: `ctx` --> src\main.rs:9:24 | -9 | fn update(&mut self, ctx: &mut Context) -> GameResult<()> { +9 | fn update(&mut self, ctx: &mut Context) -> anyhow::Result<()> { | ^^^ | = note: to avoid this warning, consider using `_ctx` instead @@ -137,7 +137,7 @@ warning: unused variable: `ctx` warning: unused variable: `ctx` --> src\main.rs:13:22 | -13 | fn draw(&mut self, ctx: &mut Context) -> GameResult<()> { +13 | fn draw(&mut self, ctx: &mut Context) -> anyhow::Result<()> { | ^^^ | = note: to avoid this warning, consider using `_ctx` instead @@ -184,7 +184,7 @@ You should get 2 warnings: warning: unused variable: `ctx` --> src\main.rs:9:24 | -9 | fn update(&mut self, ctx: &mut Context) -> GameResult<()> { +9 | fn update(&mut self, ctx: &mut Context) -> anyhow::Result<()> { | ^^^ | = note: #[warn(unused_variables)] on by default @@ -193,7 +193,7 @@ warning: unused variable: `ctx` warning: unused variable: `ctx` --> src\main.rs:13:22 | -13 | fn draw(&mut self, ctx: &mut Context) -> GameResult<()> { +13 | fn draw(&mut self, ctx: &mut Context) -> anyhow::Result<()> { | ^^^ | = note: to avoid this warning, consider using `_ctx` instead @@ -228,7 +228,7 @@ let state = &mut State { dt: std::time::Duration::new(0, 0) }; So now that we have state to update, let's update it in our `update` callback! We'll use [`timer::delta`](https://docs.rs/ggez/0.4.0/ggez/timer/fn.delta.html) to get the delta time. ```rust,skt-update,no_run -fn update(&mut self, ctx: &mut Context) -> GameResult { +fn update(&mut self, ctx: &mut Context) -> anyhow::Result<()> { self.dt = timer::delta(ctx); Ok(()) } @@ -236,7 +236,7 @@ fn update(&mut self, ctx: &mut Context) -> GameResult { To see the changes in `State`, you need to modify the `draw` callback. ```rust,skt-draw,no_run -fn draw(&mut self, ctx: &mut Context) -> GameResult { +fn draw(&mut self, ctx: &mut Context) -> anyhow::Result<()> { println!("Hello ggez! dt = {}ns", self.dt.subsec_nanos()); Ok(()) } diff --git a/docs/guides/HelloGgez.md.skt.md b/docs/guides/HelloGgez.md.skt.md index e8df6e6f4..3ceed9cb2 100644 --- a/docs/guides/HelloGgez.md.skt.md +++ b/docs/guides/HelloGgez.md.skt.md @@ -4,6 +4,7 @@ #![allow(unused_variables)] #![allow(unused_mut)] +use anyhow; use ggez::*; use ggez::graphics::*; use ggez::event::*; @@ -14,14 +15,14 @@ struct State {{ }} impl EventHandler for State {{ - fn update(&mut self, _ctx: &mut Context) -> GameResult {{ + fn update(&mut self, _ctx: &mut Context) -> anyhow::Result<()> {{ Ok(()) }} {} }} -pub fn main() -> GameResult {{ +pub fn main() -> anyhow::Result<()> {{ Ok(()) }} ``` @@ -32,6 +33,7 @@ pub fn main() -> GameResult {{ #![allow(unused_variables)] #![allow(unused_mut)] +use anyhow; use ggez::*; use ggez::graphics::*; @@ -42,14 +44,14 @@ struct State {{ }} impl event::EventHandler for State {{ - fn draw(&mut self, _ctx: &mut Context) -> GameResult {{ + fn draw(&mut self, _ctx: &mut Context) -> anyhow::Result<()> {{ Ok(()) }} {} }} -pub fn main() -> GameResult {{ +pub fn main() -> anyhow::Result<()> {{ Ok(()) }} ``` @@ -60,6 +62,8 @@ pub fn main() -> GameResult {{ #![allow(unused_variables)] #![allow(unused_mut)] + +use anyhow; use ggez::*; use ggez::graphics::*; use ggez::event::*; @@ -71,16 +75,16 @@ struct State {{ }} impl EventHandler for State {{ - fn update(&mut self, _c: &mut Context) -> GameResult {{ + fn update(&mut self, _c: &mut Context) -> anyhow::Result<()> {{ Ok(()) }} - fn draw(&mut self, _c: &mut Context) -> GameResult {{ + fn draw(&mut self, _c: &mut Context) -> anyhow::Result<()> {{ Ok(()) }} }} -pub fn main() -> GameResult {{ +pub fn main() -> anyhow::Result<()> {{ let (ref mut ctx, ref mut event_loop) = ContextBuilder::new("foo", "bar") .build() .unwrap(); @@ -97,6 +101,7 @@ pub fn main() -> GameResult {{ #![allow(unused_variables)] #![allow(unused_mut)] +use anyhow; use ggez::*; use ggez::graphics::*; @@ -113,6 +118,7 @@ fn main() {{ #![allow(unused_variables)] #![allow(unused_mut)] +use anyhow; use ggez::*; use ggez::graphics::*; @@ -121,4 +127,4 @@ struct State {{ }} {} -``` \ No newline at end of file +``` diff --git a/src/input/keyboard.rs b/src/input/keyboard.rs index 736580766..0de892da4 100644 --- a/src/input/keyboard.rs +++ b/src/input/keyboard.rs @@ -3,6 +3,7 @@ //! Example: //! //! ```rust, compile +//! use anyhow; //! use ggez::event::{self, EventHandler, KeyCode, KeyMods}; //! use ggez::{graphics, nalgebra as na, timer}; //! use ggez::input::keyboard; @@ -13,7 +14,7 @@ //! } //! //! impl EventHandler for MainState { -//! fn update(&mut self, ctx: &mut Context) -> GameResult { +//! fn update(&mut self, ctx: &mut Context) -> anyhow::Result<()> { //! // Increase or decrease `position_x` by 0.5, or by 5.0 if Shift is held. //! if keyboard::is_key_pressed(ctx, KeyCode::Right) { //! if keyboard::is_mod_active(ctx, KeyMods::SHIFT) { @@ -29,7 +30,7 @@ //! Ok(()) //! } //! -//! fn draw(&mut self, ctx: &mut Context) -> GameResult { +//! fn draw(&mut self, ctx: &mut Context) -> anyhow::Result<()> { //! graphics::clear(ctx, [0.1, 0.2, 0.3, 1.0].into()); //! // Create a circle at `position_x` and draw //! let circle = graphics::Mesh::new_circle( diff --git a/src/lib.rs b/src/lib.rs index d55ae788f..e44f17a45 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -28,6 +28,7 @@ //! ## Basic Project Template //! //! ```rust,no_run +//! use anyhow; //! use ggez::{Context, ContextBuilder, GameResult}; //! use ggez::event::{self, EventHandler}; //! use ggez::graphics; @@ -63,17 +64,17 @@ //! } //! //! impl EventHandler for MyGame { -//! fn update(&mut self, _ctx: &mut Context) -> GameResult<()> { +//! fn update(&mut self, _ctx: &mut Context) -> anyhow::Result<()> { //! // Update code here... //! # Ok(()) //! } //! -//! fn draw(&mut self, ctx: &mut Context) -> GameResult<()> { +//! fn draw(&mut self, ctx: &mut Context) -> anyhow::Result<()> { //! graphics::clear(ctx, graphics::WHITE); //! //! // Draw code here... //! -//! graphics::present(ctx) +//! Ok(graphics::present(ctx)?) //! } //! } //! diff --git a/src/timer.rs b/src/timer.rs index 4b189daf4..a068edec9 100644 --- a/src/timer.rs +++ b/src/timer.rs @@ -220,17 +220,18 @@ pub fn time_since_start(ctx: &Context) -> time::Duration { /// in your `update()` callback: /// /// ```rust +/// # use anyhow; /// # use ggez::*; /// # fn update_game_physics() -> GameResult { Ok(()) } /// # struct State; /// # impl ggez::event::EventHandler for State { -/// fn update(&mut self, ctx: &mut Context) -> GameResult { +/// fn update(&mut self, ctx: &mut Context) -> anyhow::Result<()> { /// while(timer::check_update_time(ctx, 60)) { /// update_game_physics()?; /// } /// Ok(()) /// } -/// # fn draw(&mut self, _ctx: &mut Context) -> GameResult { Ok(()) } +/// # fn draw(&mut self, _ctx: &mut Context) -> anyhow::Result<()> { Ok(()) } /// # } /// ``` pub fn check_update_time(ctx: &mut Context, target_fps: u32) -> bool {