Skip to content

Instantly share code, notes, and snippets.

@wjlroe
Created May 12, 2016 13:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wjlroe/99c435a0e2d63d459922fdfbe2e87548 to your computer and use it in GitHub Desktop.
Save wjlroe/99c435a0e2d63d459922fdfbe2e87548 to your computer and use it in GitHub Desktop.
A Rust type error has me confounded
cargo build
Compiling piston_game v0.1.0 (file:///Users/will/Code/play/games/piston_game)
src/main.rs:17:19: 17:23 error: type mismatch resolving `<gfx_graphics::back_end::GfxGraphics<'_, gfx_device_gl::Resources, gfx_device_gl::command::CommandBuffer> as graphics::graphics::Graphics>::Texture == I`:
expected struct `gfx_texture::Texture`,
found type parameter [E0271]
src/main.rs:17 scene.draw(c.transform, g);
^~~~
src/main.rs:17:19: 17:23 help: run `rustc --explain E0271` to see a detailed explanation
error: aborting due to previous error
extern crate piston_window;
extern crate sprite;
extern crate find_folder;
use std::rc::Rc;
use piston_window::*;
use sprite::*;
fn run<I: ImageSize>(window: &mut PistonWindow, scene: &mut Scene<I>) {
while let Some(e) = window.next() {
window.draw_2d(&e, |c, g| {
clear([1.0; 4], g);
rectangle([1.0, 0.0, 0.0, 1.0], // red
[0.0, 0.0, 100.0, 100.0],
c.transform,
g);
scene.draw(c.transform, g);
});
}
}
fn main() {
let (width, height) = (640, 480);
let opengl = OpenGL::V3_2;
let mut window: PistonWindow = WindowSettings::new("Hello Piston!", (width, height))
.exit_on_esc(true)
.opengl(opengl)
.build()
.unwrap();
let assets = find_folder::Search::ParentsThenKids(3, 3)
.for_folder("assets")
.unwrap();
let mut scene = Scene::new();
let tex = Rc::new(Texture::from_path(&mut window.factory,
assets.join("spritesheet.png"),
Flip::None,
&TextureSettings::new())
.unwrap());
let mut sprite = Sprite::from_texture(tex.clone());
sprite.set_position(width as f64 / 2.0, height as f64 / 2.0);
sprite.set_anchor(0.0, 0.0);
scene.add_child(sprite);
run(&mut window, &mut scene);
}
// Abridged source code for piston's sprite library (the Scene struct is what I'm dealing with)
pub struct Scene<I: ImageSize> {
children: Vec<Sprite<I>>,
children_index: HashMap<Uuid, usize>,
running: HashMap<Uuid,
Vec<(Behavior<Animation>, State<Animation, AnimationState>, bool)>>,
// Set of sprites that should be removed once animations have finished.
dead_sprites: HashSet<Uuid>,
}
impl<I: ImageSize> Scene<I> {
/// Create a new scene
pub fn new() -> Scene<I> {
Scene {
children: Vec::new(),
children_index: HashMap::new(),
running: HashMap::new(),
dead_sprites: HashSet::new(),
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment