Trait pixel_game_lib::PixelGame

source ·
pub trait PixelGame: Sized
where Self: 'static,
{ // Required methods fn update( &mut self, input: &Input, mouse_pos: Option<Vec2<usize>>, dt: f32 ) -> bool; fn render(&mut self, canvas: &mut Canvas<'_>); // Provided method fn run(self, window_config: WindowConfig) -> Result<()> { ... } }
Expand description

Setup a game with a shared state and run it.

This is only a helper for constructing a global game state around the [window] function, which can also be easily used standalone.

Required Methods§

source

fn update( &mut self, input: &Input, mouse_pos: Option<Vec2<usize>>, dt: f32 ) -> bool

Update loop, called every update tick.

§Arguments
  • input - Input helper that can be used to handle different input states.
  • mouse_pos - Mouse position on the buffer if Some, if None mouse is outside of the buffer, not necessarily the window.
  • dt - Delta time, time in seconds since the last update call. Can be used to handle physics.
§Returns
  • true if the window and thus the game should be closed
source

fn render(&mut self, canvas: &mut Canvas<'_>)

Render loop, called every render tick.

§Arguments
  • canvas - Pixel buffer where all the graphics are rendered on.

Provided Methods§

source

fn run(self, window_config: WindowConfig) -> Result<()>

Run the game, spawning the window.

§Arguments
  • window_config - Configuration for the window, can be used to set the buffer size, the window title and other things.
Examples found in repository?
examples/window.rs (line 55)
44
45
46
47
48
49
50
51
52
53
54
55
56
fn main() {
    // Active modifiable state
    let state = GameState { pixels_to_draw: 0 };

    // Window configuration with huge pixels
    let window_config = WindowConfig {
        buffer_size: Extent2::new(64, 64),
        scaling: 8,
        ..Default::default()
    };

    state.run(window_config).expect("Error running game");
}

Object Safety§

This trait is not object safe.

Implementors§