8000 Improve sub-context passing ergonomics by nobbele · Pull Request #1037 · ggez/ggez · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Improve sub-context passing ergonomics #1037

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
May 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,9 @@ impl EventHandler for MyGame {
}

fn draw(&mut self, ctx: &mut Context) -> GameResult<()> {
let mut canvas = graphics::Canvas::from_frame(&ctx.gfx, Color::WHITE);
let mut canvas = graphics::Canvas::from_frame(ctx, Color::WHITE);
// Draw code here...
canvas.finish(&mut ctx.gfx)
canvas.finish(ctx)
}
}
```
Expand Down
21 changes: 17 additions & 4 deletions docs/FAQ.md
8000
Original file line number Diff line numberDiff line change
Expand Up @@ -25,6 +25,7 @@
* **[Miscellaneous](#misc)**
* [How do I load my `conf.toml` file?](#misc_conf)
* [I get a console window when I launch my executable on Windows](#misc_win_console)
* [What is this `Has<GraphicsContext>` parameter?](#has_parameter)

---

Expand Down Expand Up @@ -120,7 +121,7 @@ graphics::set_screen_coordinates(&mut context, Rect::new(0.0, 0.0, 1.0, 1.0)).un
```

and scaling your `Image`s with `graphics::DrawParam`.

Please note that updating your coordinate system like this may also
be necessary [when drawing onto canvases of custom sizes](https://github.com/ggez/ggez/blob/aed56921fbca8ac8192b492f0a46d92e4a0a95bb/src/graphics/canvas.rs#L44-L48).

Expand Down Expand Up @@ -196,7 +197,7 @@ When created, a window starts out with a coordinate system perfectly
corresponding to its physical size in pixels. That's why, initially,
translating mouse coordinates to logical coordinates is not necessary
at all. Both systems are just the same.

But once physical and logical coordinates get out of sync problems
start to arise. If you want more info on how to navigate this issue
take a look at the [`input_test`](../examples/input_test.rs) and [`graphics_settings`](../examples/graphics_settings.rs) examples.
Expand Down Expand Up @@ -228,11 +229,11 @@ provides types for other math libraries to convert to and from with.
What you are supposed to do is to add a math library of your choice to
your game such as glam or nalgebra, usually with a "mint" feature. For
example. You can add

```rust
glam = { version = "0.15.2", features = ["mint"] }
```

in your Cargo.toml, then when you try to pass
something to, say `DrawParam::new().dest(my_point)`, you will
be able to pass a glam type like
Expand Down Expand Up @@ -404,3 +405,15 @@ If you wish, you can also disable it only in release mode:
```

[`egui`]: https://github.com/emilk/egui#integrations

<a name="has_parameter">

## What is this `Has` parameter?

You may notice a lot of methods taking a `gfx: &impl Has<GraphicsContext>` parameter or `ctxs: &impl HasTwo<GraphicsContext, Filesystem>`, what is up with that?

Since version 0.8, we support split borrowing the sub-contexts. **This is not useful for most users so you may just pass `ctx` to any parameter taking `Has`**. If you want to perform a split-borrow:

You may pass `&ctx.gfx` for `gfx: &impl Has<GraphicsContext>`

Or you may pass it like `&(&ctx.gfx, &ctx.fs)` for `ctxs: &impl HasTwo<GraphicsContext, Filesystem>`
36 changes: 18 additions & 18 deletions docs/guides/GenerativeArt.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ Geometry, it's all coming back now.
Here is the code for a [circle](https://docs.rs/ggez/0.7.0/ggez/graphics/struct.Mesh.html#method.new_circle):
```rust,skt-expression,no_run
graphics::Mesh::new_circle(
&ctx.gfx,
ctx,
graphics::DrawMode::fill(),
mint::Point2{x: 200.0, y: 300.0},
100.0,
Expand All @@ -86,8 +86,8 @@ graphics::Mesh::new_circle(
Now now now, hold on a second... I said 2 pieces of information!
Why did I just write like a million?!

Well, `ctx.gfx` is needed to tell `ggez` where you are drawing to.
`ctx` is what is passed to `update` and `draw` and holds our engine's state. `ctx.gfx` is then the graphics sub-context.
Well, `ctx` is needed to tell `ggez` where you are drawing to.
`ctx` is what is passed to `update` and `draw` and holds our engine's state.

[`graphics::DrawMode::fill()`](https://docs.rs/ggez/0.7.0/ggez/graphics/enum.DrawMode.html) is choosing between
outlining the circle or filling it in.
Expand All @@ -109,19 +109,19 @@ To draw it, we first need to create our `Canvas`, filling it with black. Then we
drawing the circle onto it. Lastly, we submit this draw call.
```rust,skt-draw,no_run
fn draw(&mut self, ctx: &mut Context) -> GameResult {
let mut canvas = graphics::Canvas::from_frame(&ctx.gfx, graphics::Color::BLACK);
let mut canvas = graphics::Canvas::from_frame(ctx, graphics::Color::BLACK);

let circle = graphics::Mesh::new_circle(
&ctx.gfx,
ctx,
graphics::DrawMode::fill(),
mint::Point2{x: 200.0, y: 300.0},
100.0,
0.1,
graphics::Color::WHITE,
)?;

canvas.draw(&circle, graphics::DrawParam::default());
canvas.finish(&mut ctx.gfx)?;
canvas.finish(ctx)?;
Ok(())
}
```
Expand All @@ -135,7 +135,7 @@ Rectangles are represented by 3 pieces of information: origin, width, and height
Here is the code for a [rectangle](https://docs.rs/ggez/0.7.0/ggez/graphics/struct.Mesh.html#method.new_rectangle):
```rust,skt-expression,no_run
graphics::Mesh::new_rectangle(
&ctx.gfx,
ctx,
graphics::DrawMode::fill(),
graphics::Rect::new(500.0, 250.0, 200.0, 100.0),
graphics::Color::WHITE,
Expand All @@ -156,17 +156,17 @@ And that's how a rectangle is created!
Let's try it out with some quick code:
```rust,skt-draw,no_run
fn draw(&mut self, ctx: &mut Context) -> GameResult {
let mut canvas = graphics::Canvas::from_frame(&ctx.gfx, graphics::Color::BLACK);
let mut canvas = graphics::Canvas::from_frame(ctx, graphics::Color::BLACK);

let rect = graphics::Mesh::new_rectangle(
&ctx.gfx,
ctx,
graphics::DrawMode::fill(),
graphics::Rect::new(500.0, 250.0, 200.0, 100.0),
graphics::Color::WHITE,
)?;

canvas.draw(&rect, graphics::DrawParam::default());
canvas.finish(&mut ctx.gfx)?;
canvas.finish(ctx)?;
Ok(())
}
```
Expand Down Expand Up @@ -234,21 +234,21 @@ 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 {
let mut canvas = graphics::Canvas::from_frame(&ctx.gfx, graphics::Color::BLACK);
let mut canvas = graphics::Canvas::from_frame(ctx, graphics::Color::BLACK);
for shape in &self.shapes {
// Make the shape...
let mesh = match shape {
&Shape::Rectangle(rect) => {
graphics::Mesh::new_rectangle(
&ctx.gfx,
ctx,
graphics::DrawMode::fill(),
rect,
graphics::Color::WHITE
)?
}
&Shape::Circle(origin, radius) => {
graphics::Mesh::new_circle(
&ctx.gfx,
ctx,
graphics::DrawMode::fill(),
origin,
radius,
Expand All @@ -261,7 +261,7 @@ fn draw(&mut self, ctx: &mut Context) -> GameResult {
// ...and then draw it.
canvas.draw(&mesh, graphics::DrawParam::default());
}
canvas.finish(&mut ctx.gfx)?;
canvas.finish(ctx)?;
Ok(())
}
```
Expand Down
6 changes: 3 additions & 3 deletions examples/01_super_simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ struct MainState {
impl MainState {
fn new(ctx: &mut Context) -> GameResult<MainState> {
let circle = graphics::Mesh::new_circle(
&ctx.gfx,
ctx,
graphics::DrawMode::fill(),
vec2(0., 0.),
100.0,
Expand All @@ -36,13 +36,13 @@ impl event::EventHandler<ggez::GameError> for MainState {

fn draw(&mut self, ctx: &mut Context) -> GameResult {
let mut canvas = graphics::Canvas::from_frame(
&ctx.gfx,
ctx,
graphics::CanvasLoadOp::Clear([0.1, 0.2, 0.3, 1.0].into()),
);

canvas.draw(&self.circle, Vec2::new(self.pos_x, 380.0));

canvas.finish(&mut ctx.gfx)?;
canvas.finish(ctx)?;

Ok(())
}
Expand Down
6 changes: 3 additions & 3 deletions examples/02_hello_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ impl MainState {
fn new(ctx: &mut Context) -> GameResult<MainState> {
ctx.gfx.add_font(
"LiberationMono",
graphics::FontData::from_path(&ctx.fs, "/LiberationMono-Regular.ttf")?,
graphics::FontData::from_path(ctx, "/LiberationMono-Regular.ttf")?,
);

let s = MainState { frames: 0 };
Expand All @@ -32,7 +32,7 @@ impl event::EventHandler<ggez::GameError> for MainState {

fn draw(&mut self, ctx: &mut Context) -> GameResult {
let mut canvas = graphics::Canvas::from_frame(
&ctx.gfx,
ctx,
graphics::CanvasLoadOp::Clear([0.1, 0.2, 0.3, 1.0].into()),
);

Expand All @@ -46,7 +46,7 @@ impl event::EventHandler<ggez::GameError> for MainState {
dest_point,
);

canvas.finish(&mut ctx.gfx)?;
canvas.finish(ctx)?;

self.frames += 1;
if (self.frames % 100) == 0 {
Expand Down
16 changes: 8 additions & 8 deletions examples/03_drawing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ struct MainState {
impl MainState {
/// Load images and create meshes.
fn new(ctx: &mut Context) -> GameResult<MainState> {
let image1 = graphics::Image::from_path(&ctx.fs, &ctx.gfx, "/dragon1.png", true)?;
let image2 = graphics::Image::from_path(&ctx.fs, &ctx.gfx, "/shot.png", true)?;
let image1 = graphics::Image::from_path(ctx, "/dragon1.png", true)?;
let image2 = graphics::Image::from_path(ctx, "/shot.png", true)?;

let mb = &mut graphics::MeshBuilder::new();
mb.rectangle(
Expand All @@ -29,14 +29,14 @@ impl MainState {
graphics::Color::new(1.0, 0.0, 0.0, 1.0),
)?;

let rock = graphics::Image::from_path(&ctx.fs, &ctx.gfx, "/rock.png", true)?;
let rock = graphics::Image::from_path(ctx, "/rock.png", true)?;

let meshes = vec![
(None, build_mesh(ctx)?),
(Some(rock), build_textured_triangle(ctx)?),
];

let rect = graphics::Mesh::from_data(&ctx.gfx, mb.build());
let rect = graphics::Mesh::from_data(ctx, mb.build());

let s = MainState {
image1,
Expand Down Expand Up @@ -82,7 +82,7 @@ fn build_mesh(ctx: &mut Context) -> GameResult<graphics::Mesh> {
Color::new(1.0, 0.0, 1.0, 1.0),
)?;

Ok(graphics::Mesh::from_data(&ctx.gfx, mb.build()))
Ok(graphics::Mesh::from_data(ctx, mb.build()))
}

fn build_textured_triangle(ctx: &mut Context) -> GameResult<graphics::Mesh> {
Expand All @@ -107,7 +107,7 @@ fn build_textured_triangle(ctx: &mut Context) -> GameResult<graphics::Mesh> {
let triangle_indices = vec![0, 1, 2];

Ok(graphics::Mesh::from_data(
&ctx.gfx,
ctx,
graphics::MeshData {
vertices: &triangle_verts,
indices: &triangle_indices,
Expand All @@ -128,7 +128,7 @@ impl event::EventHandler<ggez::GameError> for MainState {

fn draw(&mut self, ctx: &mut Context) -> GameResult {
let mut canvas = graphics::Canvas::from_frame(
&ctx.gfx,
ctx,
graphics::CanvasLoadOp::Clear([0.1, 0.2, 0.3, 1.0].into()),
);

Expand Down Expand Up @@ -186,7 +186,7 @@ impl event::EventHandler<ggez::GameError> for MainState {
}

// Finished drawing, show it all on the screen!
canvas.finish(&mut ctx.gfx)?;
canvas.finish(ctx)?;

Ok(())
}
Expand Down
4 changes: 2 additions & 2 deletions examples/04_snake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ impl event::EventHandler<ggez::GameError> for GameState {
fn draw(&mut self, ctx: &mut Context) -> GameResult {
// First we create a canvas that renders to the frame, and clear it to a (sort of) green color
let mut canvas = graphics::Canvas::from_frame(
&ctx.gfx,
ctx,
graphics::CanvasLoadOp::Clear([0.0, 1.0, 0.0, 1.0].into()),
);

Expand All @@ -428,7 +428,7 @@ impl event::EventHandler<ggez::GameError> for GameState {
// Finally, we "flush" the draw commands.
// Since we rendered to the frame, we don't need to tell ggez to present anything else,
// as ggez will automatically present the frame image unless told otherwise.
canvas.finish(&mut ctx.gfx)?;
canvas.finish(ctx)?;

// We yield the current thread until the next update
ggez::timer::yield_now();
Expand Down
25 changes: 12 additions & 13 deletions examples/05_astroblasto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,12 +244,12 @@ struct Assets {

impl Assets {
fn new(ctx: &mut Context) -> GameResult<Assets> {
let player_image = graphics::Image::from_path(&ctx.fs, &ctx.gfx, "/player.png", true)?;
let shot_image = graphics::Image::from_path(&ctx.fs, &ctx.gfx, "/shot.png", true)?;
let rock_image = graphics::Image::from_path(&ctx.fs, &ctx.gfx, "/rock.png", true)?;
let player_image = graphics::Image::from_path(ctx, "/player.png", true)?;
let shot_image = graphics::Image::from_path(ctx, "/shot.png", true)?;
let rock_image = graphics::Image::from_path(ctx, "/rock.png", true)?;

let shot_sound = audio::Source::new(&ctx.fs, &ctx.audio, "/pew.ogg")?;
let hit_sound = audio::Source::new(&ctx.fs, &ctx.audio, "/boom.ogg")?;
let shot_sound = audio::Source::new(ctx, "/pew.ogg")?;
let hit_sound = audio::Source::new(ctx, "/boom.ogg")?;

Ok(Assets {
player_image,
Expand Down Expand Up @@ -335,7 +335,7 @@ impl MainState {

let (width, height) = ctx.gfx.drawable_size();
let screen =
graphics::ScreenImage::new(&ctx.gfx, graphics::ImageFormat::Rgba8UnormSrgb, 1., 1., 1);
graphics::ScreenImage::new(ctx, graphics::ImageFormat::Rgba8UnormSrgb, 1., 1., 1);

let s = MainState {
screen,
Expand Down Expand Up @@ -367,7 +367,7 @@ impl MainState {

self.shots.push(shot);

self.assets.shot_sound.play(&ctx.audio)?;
self.assets.shot_sound.play(ctx)?;

Ok(())
}
Expand All @@ -390,7 +390,7 @@ impl MainState {
rock.life = 0.0;
self.score += 1;

self.assets.hit_sound.play(&ctx.audio)?;
self.assets.hit_sound.play(ctx)?;
}
}
}
Expand Down Expand Up @@ -501,8 +501,7 @@ impl EventHandler for MainState {
fn draw(&mut self, ctx: &mut Context) -> GameResult {
// Our drawing is quite simple.
// Just clear the screen...
let mut canvas =
graphics::Canvas::from_screen_image(&ctx.gfx, &mut self.screen, Color::BLACK);
let mut canvas = graphics::Canvas::from_screen_image(ctx, &mut self.screen, Color::BLACK);

// Loop over all objects drawing them...
{
Expand Down Expand Up @@ -538,8 +537,8 @@ impl EventHandler for MainState {
graphics::DrawParam::from(score_dest).color(Color::WHITE),
);

canvas.finish(&mut ctx.gfx)?;
ctx.gfx.present(&self.screen.image(&ctx.gfx))?;
canvas.finish(ctx)?;
ctx.gfx.present(&self.screen.image(ctx))?;

// And yield the timeslice
// This tells the OS that we're done using the CPU but it should
Expand Down Expand Up @@ -574,7 +573,7 @@ impl EventHa 5DC8 ndler for MainState {
self.input.fire = true;
}
KeyCode::P => {
self.screen.image(&ctx.gfx).encode(
self.screen.image(ctx).encode(
ctx,
graphics::ImageEncodingFormat::Png,
"/screenshot.png",
Expand Down
Loading
0