Trait pixel_game_lib::PixelGame
source · pub trait PixelGame: Sizedwhere
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§
sourcefn update(
&mut self,
input: &Input,
mouse_pos: Option<Vec2<usize>>,
dt: f32
) -> bool
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 ifSome
, ifNone
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
Provided Methods§
sourcefn run(self, window_config: WindowConfig) -> Result<()>
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.