I have been experimenting with Piston lately. I am trying to create a window, load some .png
images and print them.
I tried with this code:
extern crate piston;
extern crate graphics;
extern crate piston_window;
use piston_window::*;
pub struct GameView<G: Graphics> {
pub grass: G::Texture,
}
pub fn game_piston(game: game::Game) {
let opengl = OpenGL::V3_2;
// Create a Piston window.
let size = (WORLD_SIZE as u32) * LAND_SIZE_PX;
let window_settings: WindowSettings = WindowSettings::new("tbs-piston", [size, size])
// .fullscreen(true)
.opengl(opengl)
.exit_on_esc(true);
let mut window: PistonWindow = PistonWindow::build_from_window_settings(&window_settings)
.unwrap_or_else(|e| panic!("Failed to build PistonWindow: {}", e))
.lazy(true);
// Create a new game and run it.
let mut game_controller = GameController {
game,
select_mode: None,
};
let game_view = GameView {
select: Texture::from_path(
&mut window.factory.clone(),
Path::new("resources/grass.png"),
Flip::None,
&TextureSettings::new()
).expect("Expected"),
};
while let Some(e) = window.next() {
window.draw_2d(&e, |c, g| {
game_view.draw(&game_controller, &c, g);
});
}
}
but I get this, totally misterious to me, error from the compiler:
error[E0481]: lifetime of function argument does not outlive the function call
--> src/interface_piston/mod.rs:297:22
|
297 | window.draw_2d(&e, |c, g| {
| ______________________^
298 | | game_view.draw(&game_controller, &c, g);
299 | | });
| |___^
|
note: the function argument is only valid for the anonymous lifetime #3 defined on the body at 297:22
--> src/interface_piston/mod.rs:297:22
|
297 | window.draw_2d(&e, |c, g| {
| ______________________^
298 | | game_view.draw(&game_controller, &c, g);
299 | | });
| |___^
error[E0486]: type of expression contains references that are not valid during the expression: `[closure@src/interface_piston/mod.rs:297:22: 299:4 game_view:&interface_piston::GameView<gfx_graphics::back_end::GfxGraphics<'_, gfx_device_gl::Resources, gfx_device_gl::command::CommandBuffer>>, game_controller:&interface_piston::GameController]`
--> src/interface_piston/mod.rs:297:22
|
297 | window.draw_2d(&e, |c, g| {
| ______________________^
298 | | game_view.draw(&game_controller, &c, g);
299 | | });
| |___^
|
note: type is only valid for the anonymous lifetime #3 defined on the body at 297:22
--> src/interface_piston/mod.rs:297:22
|
297 | window.draw_2d(&e, |c, g| {
| ______________________^
298 | | game_view.draw(&game_controller, &c, g);
299 | | });
| |___^
error[E0486]: type of expression contains references that are not valid during the expression: `interface_piston::GameView<gfx_graphics::back_end::GfxGraphics<'_, gfx_device_gl::Resources, gfx_device_gl::command::CommandBuffer>>`
--> src/interface_piston/mod.rs:235:18
|
235 | let game_view = GameView {
| __________________^
236 | | // font: Font::new(ctx, "/DejaVuSerif.ttf", 12).expect("Expected"),
237 | | select: Texture::from_path(
238 | | &mut window.factory.clone(),
... |
278 | | ).expect("Expected"),
279 | | };
| |____^
|
note: type is only valid for the anonymous lifetime #3 defined on the body at 297:22
--> src/interface_piston/mod.rs:297:22
|
297 | window.draw_2d(&e, |c, g| {
| ______________________^
298 | | game_view.draw(&game_controller, &c, g);
299 | | });
| |___^
error[E0488]: lifetime of variable does not enclose its declaration
--> src/interface_piston/mod.rs:235:6
|
235 | let game_view = GameView {
| ^^^^^^^^^
|
note: the variable is only valid for the anonymous lifetime #3 defined on the body at 297:22
--> src/interface_piston/mod.rs:297:22
|
297 | window.draw_2d(&e, |c, g| {
| ______________________^
298 | | game_view.draw(&game_controller, &c, g);
299 | | });
| |___^
error[E0495]: cannot infer an appropriate lifetime for capture of `game_view` by closure due to conflicting requirements
--> src/interface_piston/mod.rs:297:22
|
297 | window.draw_2d(&e, |c, g| {
| ______________________^
298 | | game_view.draw(&game_controller, &c, g);
299 | | });
| |___^
|
note: first, the lifetime cannot outlive the anonymous lifetime #3 defined on the body at 297:22...
--> src/interface_piston/mod.rs:297:22
|
297 | window.draw_2d(&e, |c, g| {
| ______________________^
298 | | game_view.draw(&game_controller, &c, g);
299 | | });
| |___^
note: ...so that the reference type `&interface_piston::GameView<gfx_graphics::back_end::GfxGraphics<'_, gfx_device_gl::Resources, gfx_device_gl::command::CommandBuffer>>` does not outlive the data it points at
--> src/interface_piston/mod.rs:297:3
|
297 | / window.draw_2d(&e, |c, g| {
298 | | game_view.draw(&game_controller, &c, g);
299 | | });
| |____^
note: but, the lifetime must be valid for the expression at 297:22...
--> src/interface_piston/mod.rs:297:22
|
297 | window.draw_2d(&e, |c, g| {
| ______________________^
298 | | game_view.draw(&game_controller, &c, g);
299 | | });
| |___^
note: ...so type `[closure@src/interface_piston/mod.rs:297:22: 299:4 game_view:&interface_piston::GameView<gfx_graphics::back_end::GfxGraphics<'_, gfx_device_gl::Resources, gfx_device_gl::command::CommandBuffer>>, game_controller:&interface_piston::GameController]` of expression is valid during the expression
--> src/interface_piston/mod.rs:297:22
|
297 | window.draw_2d(&e, |c, g| {
| ______________________^
298 | | game_view.draw(&game_controller, &c, g);
299 | | });
| |___^
What I am trying to do should be totally trivial, and it should not require extra dependencies like opengl_graphics
. What am I doing wrong?