Skip to main content

GameLoop

Struct GameLoop 

Source
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

Source

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

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());
Source

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§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.