[][src]Struct gameloop::GameLoop

pub struct GameLoop { /* fields omitted */ }

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

impl GameLoop[src]

pub fn new(tps: usize, max_frameskip: usize) -> Result<Self, GameLoopError>[src]

Create a new game loop.

Arguments

  • tps: game ticks per second
  • max_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());

pub fn actions(&self) -> impl Iterator<Item = FrameAction> + '_[src]

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 !RefUnwindSafe for GameLoop

impl Send for GameLoop

impl !Sync for GameLoop

impl Unpin for GameLoop

impl UnwindSafe for GameLoop

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.