Theme

alex/beta-minecraft-rust

Minecraft Beta 1.7.3 Rewrite

BRANCH main
main
SNAPSHOT Draw terrain GPU-driven from packed per-section buffers with GPU occlusion culling, add a render distance slider (2-128) with a quad/disk shape, cut chunk bookkeeping at high render distances, and fix a light underflow at y=0
DOWNLOAD
Download file

root / crates / client / src / timer.rs

use std::time::Instant;

const TICKS_PER_SECOND: f64 = 20.0;
const MAX_TICKS_PER_FRAME: u32 = 10;

/// Port of `Timer`: converts wall time into 20 Hz game ticks plus a partial tick for interpolation.
pub struct Timer {
    last: Instant,
    partial: f64,
}

impl Timer {
    pub fn new() -> Self {
        Self {
            last: Instant::now(),
            partial: 0.0,
        }
    }

    /// Returns how many ticks to run for a frame shown at `now`.
    pub fn advance(&mut self, now: Instant) -> u32 {
        let elapsed = now.duration_since(self.last).as_secs_f64().min(1.0);
        self.last = now;
        self.partial += elapsed * TICKS_PER_SECOND;
        let ticks = self.partial as u32;
        self.partial -= f64::from(ticks);
        ticks.min(MAX_TICKS_PER_FRAME)
    }

    pub fn partial_ticks(&self) -> f32 {
        self.partial as f32
    }

    pub fn freeze(&mut self) {
        self.last = Instant::now();
    }
}