Skip to content

Commit

Permalink
Fixed documentation and guides to match changes
Browse files Browse the repository at this point in the history
  • Loading branch information
rdelfin committed Apr 18, 2020
1 parent 2f7fbe4 commit 788b990
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 40 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)?)
}
}
```
Expand Down
13 changes: 7 additions & 6 deletions docs/guides/GenerativeArt.md
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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 {
Expand Down
13 changes: 8 additions & 5 deletions docs/guides/GenerativeArt.md.skt.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#![allow(unused_variables)]
#![allow(unused_mut)]
use anyhow;
use ggez::*;
use ggez::graphics::*;
use rand::*;
Expand All @@ -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(())
}}
```
Expand All @@ -43,6 +44,7 @@ pub fn main() -> GameResult {{
#![allow(unused_variables)]
#![allow(unused_mut)]
use anyhow;
use ggez::*;
use ggez::graphics::*;
use rand::*;
Expand Down Expand Up @@ -108,6 +110,7 @@ use ggez::graphics::*;
#![allow(unused_variables)]
#![allow(unused_mut)]
use anyhow;
use ggez::*;
use ggez::graphics::*;
use ggez::event::*;
Expand All @@ -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(())
}}
}}
{}
```
```
16 changes: 8 additions & 8 deletions docs/guides/HelloGgez.md
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
}
Expand Down Expand Up @@ -129,15 +129,15 @@ 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
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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -228,15 +228,15 @@ 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(())
}
```

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(())
}
Expand Down
22 changes: 14 additions & 8 deletions docs/guides/HelloGgez.md.skt.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#![allow(unused_variables)]
#![allow(unused_mut)]
use anyhow;
use ggez::*;
use ggez::graphics::*;
use ggez::event::*;
Expand All @@ -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(())
}}
```
Expand All @@ -32,6 +33,7 @@ pub fn main() -> GameResult {{
#![allow(unused_variables)]
#![allow(unused_mut)]
use anyhow;
use ggez::*;
use ggez::graphics::*;
Expand All @@ -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(())
}}
```
Expand All @@ -60,6 +62,8 @@ pub fn main() -> GameResult {{
#![allow(unused_variables)]
#![allow(unused_mut)]
use anyhow;
use ggez::*;
use ggez::graphics::*;
use ggez::event::*;
Expand All @@ -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();
Expand All @@ -97,6 +101,7 @@ pub fn main() -> GameResult {{
#![allow(unused_variables)]
#![allow(unused_mut)]
use anyhow;
use ggez::*;
use ggez::graphics::*;
Expand All @@ -113,6 +118,7 @@ fn main() {{
#![allow(unused_variables)]
#![allow(unused_mut)]
use anyhow;
use ggez::*;
use ggez::graphics::*;
Expand All @@ -121,4 +127,4 @@ struct State {{
}}
{}
```
```
5 changes: 3 additions & 2 deletions src/input/keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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(
Expand Down
7 changes: 4 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)?)
//! }
//! }
//!
Expand Down
5 changes: 3 additions & 2 deletions src/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 788b990

Please sign in to comment.