pub struct GameLoop { /* private fields */ }Expand description
Represents the core loop for the duration of the game.
§Example
// run at 20 ticks per second
let game_loop = GameLoop::new(20, 5).unwrap();
// begin core game loop
loop {
// handle window events
for action in game_loop.actions() {
match action {
FrameAction::Tick => /* simulate 1 game tick */
FrameAction::Render { interpolation } => /* render the game state interpolated
between previous and next tick */
}
}
}Implementations§
Source§impl GameLoop
impl GameLoop
Sourcepub fn new(tps: usize, max_frameskip: usize) -> Result<Self, GameLoopError>
pub fn new(tps: usize, max_frameskip: usize) -> Result<Self, GameLoopError>
Create a new game loop.
§Arguments
tps: game ticks per secondmax_frameskip: maximum number of consecutive ticks before a render is mandatory. As deWiTTERS explains:
When running on slow hardware, the framerate can drop until the game update loop will reach MAX_FRAMESKIP. In practice this means that when our render FPS drops below 5 (= FRAMES_PER_SECOND / MAX_FRAMESKIP), the actual game will slow down.
§Example
// 20 ticks per second, 5 max frame skip
let game_loop = GameLoop::new(20, 5);
assert!(game_loop.is_ok());
// tps and max_frameskip must be >= 1
assert!(GameLoop::new(0, 1).is_err());
assert!(GameLoop::new(1, 0).is_err());Sourcepub fn actions(&self) -> impl Iterator<Item = FrameAction> + '_
pub fn actions(&self) -> impl Iterator<Item = FrameAction> + '_
The heart of the game loop, this returns an iterator of FrameActions. These indicate
when your game should tick and render to maintain the fixed tick rate while rendering
as fast as possible.
Call this once per game loop iteration.
§Example
let game_loop = /* initialize game loop */
loop {
// handle window events
for action in game_loop.actions() {
match action {
FrameAction::Tick => /* simulate 1 game tick */
FrameAction::Render { interpolation } => /* render the game state interpolated
between previous and next tick */
}
}
}Auto Trait Implementations§
impl !Freeze for GameLoop
impl !RefUnwindSafe for GameLoop
impl Send for GameLoop
impl !Sync for GameLoop
impl Unpin for GameLoop
impl UnsafeUnpin for GameLoop
impl UnwindSafe for GameLoop
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more