Skip to content
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

How to port existing code from Game to GameIterator #521

Closed
bvssvni opened this issue Aug 5, 2014 · 1 comment
Closed

How to port existing code from Game to GameIterator #521

bvssvni opened this issue Aug 5, 2014 · 1 comment

Comments

@bvssvni
Copy link
Member

bvssvni commented Aug 5, 2014

The GameIterator is strictly better than the Game trait and it scales much better. I think we should mark Game as deprecated and port all code over to GameIterator.

For example, if you are writing a game consisting of higher level building blocks, then it is very nice to redirect e from the iterator to a single method:

for e in Gameiterator::new(...) {
    // If event is handled, continue to next event.
    if ui.event(&e) { continue; }
    if world.event(&e) { continue; }
}

If you are using the Game trait you need to deal with each event specifically and call the methods. When Piston adds a new type of event, for example gamepad input, the higher level building blocks will not receive those events. All the parents blocks need to update their code. This adds a linear complexity on the number of blocks.

@bvssvni
Copy link
Member Author

bvssvni commented Aug 5, 2014

Porting code is easy because you can implement the methods directly on the structure. Considering the following example:

impl<W: GameWindow> Game<W> for App {
    fn load(&mut self, _window: &mut W) {
        let asset_store = AssetStore::from_folder("../bin/assets");
        let image = asset_store.path("rust-logo.png").unwrap();
        self.image = Some(Texture::from_path(&image).unwrap());
    }

    fn render(&mut self, _window: &mut W, args: &RenderArgs) {
        let ref mut gl = self.gl;
        gl.viewport(0, 0, args.width as i32, args.height as i32);
        let ref c = Context::abs(args.width as f64, args.height as f64);
        c.rgb(1.0, 1.0, 1.0).draw(gl);

        // Draw image.
        c.image(self.image.as_ref().unwrap()).draw(gl);
    }
}

This can be changed to:

impl App {
    fn load<W: GameWindow>(&mut self, _window: &mut W) {
        let asset_store = AssetStore::from_folder("../bin/assets");
        let image = asset_store.path("rust-logo.png").unwrap();
        self.image = Some(Texture::from_path(&image).unwrap());
    }

    fn render<W: GameWindow>(&mut self, _window: &mut W, args: &RenderArgs) {
        let ref mut gl = self.gl;
        gl.viewport(0, 0, args.width as i32, args.height as i32);
        let ref c = Context::abs(args.width as f64, args.height as f64);
        c.rgb(1.0, 1.0, 1.0).draw(gl);

        // Draw image.
        c.image(self.image.as_ref().unwrap()).draw(gl);
    }
}

@bvssvni bvssvni added the Draft label Aug 5, 2014
bvssvni added a commit to bvssvni/piston that referenced this issue Aug 18, 2014
@bvssvni bvssvni added Information and removed Draft labels Aug 18, 2014
@bvssvni bvssvni changed the title Deprecate Game trait How to port existing code from Game to GameIterator Aug 18, 2014
@bvssvni bvssvni closed this as completed Aug 18, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant