Trait lux::game::Game [] [src]

pub trait Game {
    fn update(&mut self, dt: f32, window: &mut Window, events: &mut EventIterator) -> LuxResult<()>;
    fn render(&mut self, lag: f32, window: &mut Window, frame: &mut Frame) -> LuxResult<()>;

    fn clear_color(&self) -> Option<[f32; 4]> { ... }
    fn show_fps(&self, _window: &Window) -> bool { ... }
    fn should_close(&self, window: &Window) -> bool { ... }
    fn prepare_window(&mut self, _window: &mut Window) -> LuxResult<()> { ... }
    fn on_close(&mut self, _window: &mut Window) -> LuxResult<()> { ... }
    fn updates_per_s(&self) -> f64 { ... }
    fn s_per_update(&self) -> f64 { ... }
    fn run_until_end(self) -> LuxResult<()> where Self: Sized { ... }
}

A trait that represents basic game functionality.

The game is intended to be run with a GameRunner, but that is optional.

Required Methods

fn update(&mut self, dt: f32, window: &mut Window, events: &mut EventIterator) -> LuxResult<()>

The update portion of the game loop.

The dt (delta-time) is in seconds and will always be the result of calling self.s_per_update().

Any events that are not consumed during this update cycle will remain available on the next update cycle.

This function may be called multiple times in a row without render() being called depending on how the game loop operates and the value of self.s_per_update().

Returns a LuxResult that you can use to indicate if the update was successful or not. Non-Successful updates will terminate the game.

fn render(&mut self, lag: f32, window: &mut Window, frame: &mut Frame) -> LuxResult<()>

The render portion of the game loop.

Lag is the amount of time (in seconds) that have accumulated between updates. You should use this to ease object to their locations in order to keep smooth animations.

Returns a LuxResult that you can use to indicate if the render was successful or not. Non-Successful updates will terminate the game.

Provided Methods

fn clear_color(&self) -> Option<[f32; 4]>

The color that is used to clear the screen before each frame.

None means that the screen will not be cleared.

Defaults to Some(rgba(1.0, 1.0, 1.0, 1.0)) (solid white).

fn show_fps(&self, _window: &Window) -> bool

If running in a GameRunner, this function can be overridden to display a running FPS counter that shows how time is being spent in the game.

Defaults to false (don't show fps).

fn should_close(&self, window: &Window) -> bool

For custom game-closing logic this function can be overridden to conditionally return true.

Defaults to using the value from !window.was_open().

fn prepare_window(&mut self, _window: &mut Window) -> LuxResult<()>

Called once when the GameRunner is set up, this function can be overridden to set properties on the Window.

Defaults to doing nothing.

fn on_close(&mut self, _window: &mut Window) -> LuxResult<()>

Called once before terminating the window.

Defaults to doing nothing.

fn updates_per_s(&self) -> f64

Returs the amount of updates you want to have run in one wall-clock second.

Defaults to 60.0.

fn s_per_update(&self) -> f64

Returns the amount of (fractional) seconds that you want an individual update to take.

Defaults to 1.0 / self.updates_per_s().

Prefer changing the returning value of updates_per_s rather than changing this function.

fn run_until_end(self) -> LuxResult<()> where Self: Sized

Starts this game and runs it until the game is over.

Defaults to wrapping this game in a GameRunner and calling run_until_end on that.

Implementors