我对此代码有疑问:
extern crate piston_window;
extern crate find_folder;
extern crate gfx_graphics;
extern crate gfx;
extern crate gfx_device_gl;
use piston_window::*;
use gfx_device_gl::{Resources, Output, CommandBuffer};
use gfx_graphics::GfxGraphics;
struct Object {
x: f64,
y: f64,
sprite: Option<Texture<Resources>>,
}
impl Object {
fn new() -> Object {
Object {
x: 0.0,
y: 0.0,
sprite: None,
}
}
fn mov(&mut self, x: f64, y: f64) {
self.x = x;
self.y = y;
}
fn render(&self,
g: &mut GfxGraphics<Resources, CommandBuffer<Resources>, Output>,
view: math::Matrix2d) {
let square = rectangle::square(0.0, 0.0, 100.0);
let red = [1.0, 0.0, 0.0, 1.0];
match self.sprite {
None => {
rectangle(red,
square,
view.trans(self.x, self.y).trans(-50.0, -50.0),
g);
}
Some(ref sprite) => {
image(sprite, view.trans(self.x, self.y).trans(-50.0, -50.0), g);
}
}
}
fn setSprite(&mut self, sprite: Texture<Resources>) {
self.sprite = Some(sprite);
}
}
struct Game {
position: f64,
one: bool,
two: bool,
three: bool,
four: bool,
five: bool,
six: bool,
seven: bool,
eight: bool,
nine: bool,
player: Object,
}
impl Game {
fn new() -> Game {
Game {
rotation: 0.0,
player: Object::new(),
one: false,
two: false,
three: false,
four: false,
five: false,
six: false,
seven: false,
eight: false,
nine: false,
}
}
fn on_load(&mut self, w: &PistonWindow) {
let assets = find_folder::Search::ParentsThenKids(3, 3) //Cerca la cartella assets
.for_folder("assets").unwrap();
let tris = assets.join("x.png");
let tris = Texture::from_path(&mut *w.factory, &tris, Flip::None, &TextureSettings::new())
.unwrap();
self.player.setSprite(tris);
}
fn on_update(&mut self, upd: UpdateArgs) {
self.position += 3.0 * upd.dt;
if self.one {
self.player.mov(100.0, 100.0);
}
if self.two {
self.player.mov(100.0, 200.0);
}
if self.three {
self.player.mov(100.0, 300.0);
}
if self.four {
self.player.mov(200.0, 100.0);
}
if self.five {
self.player.mov(300.0, 100.0);
}
if self.six {
self.player.mov(200.0, 200.0);
}
if self.seven {
self.player.mov(200.0, 300.0);
}
if self.eight {
self.player.mov(300.0, 200.0);
}
if self.nine {
self.player.mov(300.0, 300.0);
}
}
fn on_draw(&mut self, ren: RenderArgs, e: PistonWindow) {
e.draw_2d(|c, g| {
clear([0.0, 0.0, 0.0], g);
let center = c.transf((ren.width / 2) as f64, (ren.eight / 2) as f64);
self.player.render(g, center);
});
}
fn on_input(&mut self, inp: Input) {
match inp {
Input::Press(but) => {
match but {
Button::Keyboard(Key::D1) => {
self.one = true;
}
Button::Keyboard(Key::D2) => {
self.two = true;
}
Button::Keyboard(Key::D3) => {
self.three = true;
}
Button::Keyboard(Key::D4) => {
self.four = true;
}
Button::Keyboard(Key::D5) => {
self.five = true;
}
Button::Keyboard(Key::D6) => {
self.six = true;
}
Button::Keyboard(Key::D7) => {
self.seven = true;
}
Button::Keyboard(Key::D8) => {
self.eight = true;
}
Button::Keyboard(Key::D9) => {
self.nine = true;
}
_ => {}
}
}
}
}
}
fn main() {
let title = "Tris"; //Titolo della finestra
let mut window: PistonWindow = WindowSettings::new(title, [800, 600]) //Crea la finestra
.exit_on_esc(true)
.build()
.unwrap_or_else(|e| { panic!("Failed to build PistonWindow: {}", e) });
let mut game = Game::new();
game.on_load(&window);
let assets = find_folder::Search::ParentsThenKids(3, 3) //Cerca la cartella assets
.for_folder("assets").unwrap();
let tris = assets.join("tris.png");
let tris = Texture::from_path(&mut window.factory,
&tris,
Flip::None,
&TextureSettings::new())
.unwrap();
let x = assets.join("x.png");
let x = Texture::from_path(&mut window.factory, &x, Flip::None, &TextureSettings::new())
.unwrap();
let o = assets.join("o.png");
let o = Texture::from_path(&mut window.factory, &o, Flip::None, &TextureSettings::new())
.unwrap();
while let Some(e) = window.next() {
// Inizia il ciclo e disegna la roba sotto
window.draw_2d(&e, |c, g| {
clear([0.0, 0.0, 0.0, 0.0], g);
image(&tris, c.transform, g);
});
match e.event {
Some(Event::Update(upd)) => {
game.on_update(upd);
}
Some(Event::Render(ren)) => {
game.on_draw(ren, e);
}
Some(Event::Input(inp)) => {
game.on_input(inp);
}
_ => {}
}
}
}
当我尝试编译它时,它说错误:
the trait bound `gfx_device_gl::Resources: gfx_core::Resources` is not satisfied [E0277] src/main.rs:14 sprite: Option<Texture<Resources>>,
第二个错误:
error: the trait bound `gfx_device_gl::Resources: gfx_core::Resources` is not satisfied [E0277] src/main.rs:39 fn setSprite(&mut self, sprite: Texture<Resources>) { src/main.rs:40 self.sprite = Some(sprite); src/main.rs:41 }
我该如何解决?我在网上看了一眼,却一无所获。
答案 0 :(得分:0)
我在非常相似的代码和设置上得到了相同的编译错误
gfx_device_gl = "=0.9.0"
Cargo.toml中的导致它停止发生在我身上。以前,我使用0.11.0
。
更改此内容后,我得到其他(我相信单独/独立)编译错误,但这可能会解决您的困境。