Skip to content

Commit

Permalink
Add TimeStamp struct (#23)
Browse files Browse the repository at this point in the history
* Add TimeStamp(...)
  • Loading branch information
DraftedDev committed Jul 24, 2023
1 parent dd83b18 commit b059d6b
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 7 deletions.
4 changes: 2 additions & 2 deletions examples/move.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use qilin::render::color::Color;
use qilin::render::sketch::Sketch;
use qilin::scene::Scene;
use qilin::simplified::vec2;
use qilin::types::{GameConfig, FPS60};
use qilin::types::{GameConfig, TimeStamp, FPS60};
use qilin::Key;
use qilin::ScaleMode;
use qilin::Vector2;
Expand Down Expand Up @@ -85,7 +85,7 @@ fn main() {
update_rate_limit: FPS60, // limit update rate to 30 fps, default is 60 fps
width: 800, // set initial width
height: 600, // set initial height
fixed_time_step: Duration::from_secs_f32(1.0 / 120.0), // how many times fixed_update will be called, (1.0 / 120.0) means 300 times per second.
fixed_time_step: TimeStamp(Duration::from_secs_f32(1.0 / 120.0)), // how many times fixed_update will be called, (1.0 / 120.0) means 120 times per second.
window: WindowOptions {
scale_mode: ScaleMode::AspectRatioStretch, // scale pixels to fit in aspect ratio
resize: true, // make window resizeable
Expand Down
4 changes: 2 additions & 2 deletions examples/prefs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::time::Duration;

use qilin::scene::Scene;

use qilin::types::{GameConfig, FPS30};
use qilin::types::{GameConfig, TimeStamp, FPS30};
use qilin::Key;
use qilin::ScaleMode;
use qilin::WindowOptions;
Expand Down Expand Up @@ -86,7 +86,7 @@ fn main() {
update_rate_limit: FPS30, // limit update rate to 30 fps, default is 60 fps
width: 800, // set initial width
height: 600, // set initial height
fixed_time_step: Duration::from_secs_f32(1.0 / 10.0), // for better docs, see GameConfig or examples/move.
fixed_time_step: TimeStamp(Duration::from_secs_f32(1.0 / 10.0)), // for better docs, see GameConfig or examples/move.
window: WindowOptions {
scale_mode: ScaleMode::AspectRatioStretch, // scale pixels to fit in aspect ratio
resize: true, // make window resizeable
Expand Down
1 change: 1 addition & 0 deletions examples/shapes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use qilin::types::{GameConfig, FPS30};
use qilin::ScaleMode;
use qilin::WindowOptions;


struct ShapeScene;

impl Scene for ShapeScene {
Expand Down
2 changes: 1 addition & 1 deletion src/game/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl Game {

let mut canvas = Canvas::new(*width, *height);

let fixed_time_step = self.config.fixed_time_step;
let fixed_time_step = self.config.fixed_time_step.0;

let mut last_time = Instant::now();
let mut accumulated_time = Duration::from_secs(0);
Expand Down
13 changes: 11 additions & 2 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub struct GameConfig {
/// Height of the window. Default is 600.
pub height: usize,
/// Defines the fixed time step and how many times [crate::scene::Scene::fixed_update] is called in one second. Default is [Duration::from_secs_f32(1.0 / 30.0)] meaning 30 times per second.
pub fixed_time_step: Duration,
pub fixed_time_step: TimeStamp,
/// Window options.
pub window: WindowOptions,
}
Expand All @@ -27,7 +27,7 @@ impl Default for GameConfig {
update_rate_limit: FPS60,
width: 800,
height: 600,
fixed_time_step: Duration::from_secs_f32(1.0 / 60.0),
fixed_time_step: TimeStamp::default(),
window: WindowOptions {
borderless: false,
title: true,
Expand All @@ -42,6 +42,15 @@ impl Default for GameConfig {
}
}

/// An alternative type to [Duration] to avoid [Default::default] issues.
#[derive(Clone, Debug)]
pub struct TimeStamp(pub Duration);

impl Default for TimeStamp {
#[inline]
fn default() -> Self { Self(Duration::from_secs_f32(1.0 / 60.0)) }
}

/// Update Rate as Duration. Use `types::FPS30`, `types::FPS60` or `types::FPS120` for FPS 30, 60 or 120 fps.
pub type UpdateRate = Duration;
pub const FPS30: Duration = Duration::from_nanos(33333333);
Expand Down

0 comments on commit b059d6b

Please sign in to comment.