Crates.io | ftvf |
lib.rs | ftvf |
version | 0.6.0 |
source | src |
created_at | 2019-09-25 19:09:58.135042 |
updated_at | 2023-09-21 16:36:59.96718 |
description | Temporal logic for writing a Fixed Tickrate, Variable Framerate game in Rust. |
homepage | |
repository | https://github.com/SolraBizna/ftvf |
max_upload_size | |
id | 167619 |
size | 44,118 |
ftvf
is a crate for carrying out game logic the One True Way: Fixed
Tickrate, Variable Framerate. By having your game logic in strictly
fungible ticks, rather than having it vary based on framerate, you gain
many advantages:
Bonus: If you know your refresh rate, ftvf
can help you render frames at
exactly that rate, jitter-free.
To get started, add ftvf
to your dependencies in Cargo.toml
:
ftvf = "0.6"
then initialize yourself a Metronome
:
let mut metronome = Metronome::new(
RealtimeNowSource::new(),
// want 30 ticks per 1 second
Rate::per_second(30, 1),
// accept being up to 5 ticks behind
5,
);
And then your game loop looks like this:
while !world.should_quit() {
world.handle_input();
for reading in metronome.sample(Mode::UnlimitedFrames) {
match reading {
Reading::Tick => world.perform_tick(),
Reading::Frame{phase} => world.render(phase),
Reading::TimeWentBackwards
=> eprintln!("Warning: time flowed backwards!"),
Reading::TicksLost
=> eprintln!("Warning: we're too slow, lost some ticks!"),
// Mode::UnlimitedFrames never returns Idle, but other modes can, and
// this is one way to handle it.
Reading::Idle{duration} => std::thread::sleep(duration),
}
}
}
Your logic ticks operate in discrete, fixed time intervals. Then, when it
comes time to render, you render a frame which represents time some portion
of the way between two ticks, represented by its phase
. Your rendering
process should render an interpolated state between the previous tick and
the current tick, based on the value of phase
. Simple example:
self.render_at(self.previous_position
+ (self.current_position - self.previous_position) * phase);
ftvf
no longer depends on std
. You can use the no_std
feature flag
to make the std
dependency go away, at the cost of not being able to
use the built-in RealtimeNowSource
.Mode::MaxOneFramePerTick
has been renamed to Mode::OneFramePerTick
.metronome.sample()
now returns an iterator directly, instead of making
you repeatedly call metronome.status()
in a disciplined way.Rate
structure, instead of as
tuples.Status
has been renamed to Reading
.Reading::Idle
now directly gives you the wait time as a Duration
,
instead of making you go indirectly through the metronome
.Mode::TargetFramesPerSecond
added.NowSource::sleep
removed.NowSource
no longer implies Copy
.NowSource
implementation for all
Deref<Target=RefCell<NowSource>>
types, including &RefCell<NowSource>
and Box<RefCell<NowSource>>
. This makes fake NowSources
a little more
ergonomic.FakeNowSource
, available with or without no_std
, which
you can use in any situation where real time is not a factor, such as
unit tests or rendering replays to disk.ftvf
is distributed under the zlib license. The complete text is as
follows:
Copyright (c) 2019, 2023 Solra Bizna
This software is provided "as-is", without any express or implied warranty. In no event will the author be held liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:
- The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgement in the product documentation would be appreciated but is not required.
- Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
- This notice may not be removed or altered from any source distribution.