#game #engine #time #fps #simulation

game_time

A library for handling time in games. Game_time provides methods to track frame time in games. It allows decoupling game time from wall time as well as tracking and setting frame rate.

2 unstable releases

Uses old Rust 2015

0.2.0 Jun 27, 2017
0.1.0 Jun 24, 2017

#1030 in Game dev

Download history 15/week @ 2023-10-30 14/week @ 2023-11-06 13/week @ 2023-11-13 17/week @ 2023-11-20 19/week @ 2023-11-27 11/week @ 2023-12-04 9/week @ 2023-12-11 13/week @ 2023-12-18 13/week @ 2023-12-25 6/week @ 2024-01-01 15/week @ 2024-01-08 14/week @ 2024-01-15 12/week @ 2024-01-22 12/week @ 2024-01-29 10/week @ 2024-02-05 25/week @ 2024-02-12

60 downloads per month

MIT license

45KB
749 lines

game_time 0.2.0

Build Status game_time on docs.rs game_time on crates.io codecov

Time handling for games and other real-time simulations.

Provides types and helpers for dealing with time in games. game_time allows for easy decoupling of internal "game" time and external "wall" time, as well as tracking frame rate.

Additionally, different kinds of time steps may be used, while also setting a multiplier for speeding up/slowing down game time progression.

Usage

Put this in your Cargo.toml:

[dependencies]
game_time = "0.2.0"

Overview

game_time consists of 4 main types.

  • GameClock provides the basic time keeping for a simulation, tracking frames and returning a GameTime at the beginning of each.
  • GameTime provides the time information for each frame.
  • FrameCount objects can be optionally used to track frame rate and other runtime statistics, utilizing swappable methods for computing the average fps.
  • FrameRunner combines a GameClock and FrameCount and makes it easy to run the simulation at a given frame rate.

For each frame, a TimeStep is passed to GameClock in order to advance the frame. This allows the frame rate to be changed at any time, and allows different kinds of time steps (fixed, variable and a constant step are supported by default) to be used based on what is most useful. Together, these objects combine to form a convenient but flexible framework for time progression.

Examples

Run a simulation with 50ms frames, without fps counting:

use game_time::GameClock;
use game_time::step;
use game_time::FloatDuration;

let mut clock = GameClock::new();
let end_time = FloatDuration::seconds(10.0);
let mut sim_time = clock.last_frame_time().clone();
let step = step::ConstantStep::new(FloatDuration::milliseconds(50.0));

while sim_time.total_game_time() < end_time {
    sim_time = clock.tick(&step);
    println!("Frame #{} at time={:?}", sim_time.frame_number(), sim_time.total_game_time());
}

Run a simulation at 30fps, sleeping if necessary to maintain the framerate:

use game_time::{GameClock, FrameCounter, FrameCount, FloatDuration};
use game_time::framerate::RunningAverageSampler;
use game_time::step;

let mut clock = GameClock::new();
let mut counter = FrameCounter::new(30.0, RunningAverageSampler::with_max_samples(60));
let end_time = FloatDuration::seconds(10.0);
let mut sim_time = clock.last_frame_time().clone();

while sim_time.total_game_time() < end_time {
    sim_time = clock.tick(&step::FixedStep::new(&counter));
    counter.tick(&sim_time);
    println!("Frame #{} at time={:?}", sim_time.frame_number(), sim_time.total_game_time());
}

Dependencies

~1.7–2.6MB
~42K SLoC