Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Remove the time crate
The vast majority of our `time` usage can now be done with
`std::time`.

The one bit that can't is the datetime in the replay file names.

This doesn't compile until that's resolved.
  • Loading branch information
tomassedovic committed Dec 13, 2017
1 parent 6a16a6a commit 386ef64
Show file tree
Hide file tree
Showing 17 changed files with 93 additions and 72 deletions.
13 changes: 0 additions & 13 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Expand Up @@ -12,7 +12,6 @@ path = "src/main.rs"

[dependencies]
bitflags = "1.0"
time = "0.1.38"
rand = "0.3.18"
clap = { version = "2.20.1", optional = true }
serde = "1.0"
Expand Down
14 changes: 7 additions & 7 deletions src/animation.rs
@@ -1,7 +1,7 @@
use color::Color;
use point::{Point, SquareArea};

use time::Duration;
use std::time::Duration;
use timer::Timer;

pub trait AreaOfEffect {
Expand Down Expand Up @@ -34,15 +34,15 @@ impl SquareExplosion {
assert!(initial_radius <= max_radius);
// Count the initial wave plus the rest that makes the difference
let wave_count = max_radius - initial_radius + 1;
let wave_duration = Duration::milliseconds(100);
let wave_duration = Duration::from_millis(100);
SquareExplosion {
center,
max_radius,
initial_radius,
current_radius: initial_radius,
color,
wave_count,
timer: Timer::new(wave_duration * wave_count),
timer: Timer::new(wave_duration.checked_mul(wave_count as u32).unwrap()),
}
}
}
Expand Down Expand Up @@ -98,7 +98,7 @@ impl CardinalExplosion {
assert!(initial_radius <= max_radius);
// Count the initial wave plus the rest that makes the difference
let wave_count = max_radius - initial_radius + 1;
let wave_duration = Duration::milliseconds(100);
let wave_duration = Duration::from_millis(100);
CardinalExplosion {
center,
max_radius,
Expand All @@ -107,7 +107,7 @@ impl CardinalExplosion {
kill_color,
shatter_color,
wave_count,
timer: Timer::new(wave_duration * wave_count),
timer: Timer::new(wave_duration.checked_mul(wave_count as u32).unwrap()),
}
}
}
Expand Down Expand Up @@ -222,7 +222,7 @@ impl DiagonalExplosion {
assert!(initial_radius <= max_radius);
// Count the initial wave plus the rest that makes the difference
let wave_count = max_radius - initial_radius + 1;
let wave_duration = Duration::milliseconds(100);
let wave_duration = Duration::from_millis(100);
DiagonalExplosion {
center,
max_radius,
Expand All @@ -231,7 +231,7 @@ impl DiagonalExplosion {
kill_color,
shatter_color,
wave_count,
timer: Timer::new(wave_duration * wave_count),
timer: Timer::new(wave_duration.checked_mul(wave_count as u32).unwrap()),
}
}
}
Expand Down
16 changes: 9 additions & 7 deletions src/engine/glium.rs
Expand Up @@ -10,7 +10,9 @@ use glium::glutin::VirtualKeyCode as BackendKey;
use image;
use keys::{Key, KeyCode};
use point::Point;
use time::{Duration, PreciseTime};
use std::time::{Duration, Instant};
use util;


// NOTE: This is designed specifically to deduplicated characters on
// the same position (using Vec::dedup). So the only thing considered
Expand Down Expand Up @@ -228,25 +230,25 @@ pub fn main_loop<T>(
let mut keys = vec![];
// We're not using alpha at all for now, but it's passed everywhere.
let alpha = 1.0;
let mut previous_frame_time = PreciseTime::now();
let mut fps_clock = Duration::milliseconds(0);
let mut previous_frame_time = Instant::now();
let mut fps_clock = Duration::from_millis(0);
let mut frame_counter = 0;
let mut fps = 1;
let mut running = true;


while running {
let now = PreciseTime::now();
let dt = previous_frame_time.to(now);
let now = Instant::now();
let dt = previous_frame_time.duration_since(now);
previous_frame_time = now;

// Calculate FPS
fps_clock = fps_clock + dt;
frame_counter += 1;
if fps_clock.num_milliseconds() > 1000 {
if util::num_milliseconds(fps_clock) > 1000 {
fps = frame_counter;
frame_counter = 1;
fps_clock = Duration::milliseconds(0);
fps_clock = Duration::from_millis(0);
}

drawcalls.clear();
Expand Down
2 changes: 1 addition & 1 deletion src/engine/mod.rs
Expand Up @@ -5,7 +5,7 @@ use keys::Key;
use point::Point;
use std::borrow::Cow;

use time::Duration;
use std::time::Duration;


#[cfg(feature = "opengl")]
Expand Down
21 changes: 11 additions & 10 deletions src/game.rs
Expand Up @@ -18,11 +18,12 @@ use render;
use state::{self, Command, Side, State};
use stats::{FrameStats, Stats};
use std::collections::{HashMap, VecDeque};
use std::i64;
use std::u64;
use std::io::Write;
use std::iter::FromIterator;
use time::Duration;
use std::time::Duration;
use timer::{Stopwatch, Timer};
use util;
use world::World;


Expand Down Expand Up @@ -101,9 +102,9 @@ pub fn update(

let paused_one_step = state.paused && state.keys.matches_code(KeyCode::Right);
let timed_step = if state.replay && !state.paused &&
(state.replay_step.num_milliseconds() >= 50 || state.replay_full_speed)
(util::num_milliseconds(state.replay_step) >= 50 || state.replay_full_speed)
{
state.replay_step = Duration::zero();
state.replay_step = Duration::new(0, 0);
true
} else {
false
Expand Down Expand Up @@ -227,9 +228,9 @@ pub fn update(
};
state.screen_fading = Some(animation::ScreenFade::new(
fade_color,
Duration::milliseconds(fade_duration),
Duration::milliseconds(200),
Duration::milliseconds(300),
Duration::from_millis(fade_duration),
Duration::from_millis(200),
Duration::from_millis(300),
fade_percentage,
));
}
Expand All @@ -252,7 +253,7 @@ pub fn update(
// NOTE: re-centre the display if the player reached the end of the screen
if state.pos_timer.finished() {
let display_pos = state.player.pos - screen_left_top_corner;
let dur = Duration::milliseconds(400);
let dur = Duration::from_millis(400);
let exploration_radius = formula::exploration_radius(state.player.mind);
// TODO: move the screen roughly the same distance along X and Y
if display_pos.x < exploration_radius ||
Expand Down Expand Up @@ -930,13 +931,13 @@ fn show_exit_stats(stats: &Stats) {
stats
.longest_update_durations()
.iter()
.map(|dur| dur.num_microseconds().unwrap_or(i64::MAX))
.map(|dur| util::num_microseconds(*dur).unwrap_or(u64::MAX))
.map(|us| us as f32 / 1000.0)
.collect::<Vec<_>>(),
stats
.longest_drawcall_durations()
.iter()
.map(|dur| dur.num_microseconds().unwrap_or(i64::MAX))
.map(|dur| util::num_microseconds(*dur).unwrap_or(u64::MAX))
.map(|us| us as f32 / 1000.0)
.collect::<Vec<_>>()
);
Expand Down
2 changes: 1 addition & 1 deletion src/graphics.rs
Expand Up @@ -3,7 +3,7 @@
use color::Color;
use engine::Draw;
use point::Point;
use time::Duration;
use std::time::Duration;


pub trait Render {
Expand Down
2 changes: 1 addition & 1 deletion src/item.rs
Expand Up @@ -5,7 +5,7 @@ use self::Kind::*;
use color::{self, Color};
use graphics::Render;
use player::Modifier;
use time::Duration;
use std::time::Duration;


#[derive(Clone, Copy, PartialEq, Debug, Eq, Hash)]
Expand Down
2 changes: 1 addition & 1 deletion src/level.rs
Expand Up @@ -6,7 +6,7 @@ use point;

use rand::{self, Rng};
use std::collections::HashMap;
use time::Duration;
use std::time::Duration;


/// Position within a level. Ensured to be always within bounds.
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Expand Up @@ -4,7 +4,6 @@
#[macro_use]
extern crate bitflags;
extern crate rand;
extern crate time;
#[macro_use]
extern crate serde_derive;
extern crate serde_json;
Expand Down Expand Up @@ -56,6 +55,7 @@ mod render;
mod state;
mod stats;
mod timer;
mod util;
mod world;


Expand Down
2 changes: 1 addition & 1 deletion src/monster.rs
Expand Up @@ -9,7 +9,7 @@ use player::Modifier;
use point::Point;

use rand::{Rand, Rng};
use time::Duration;
use std::time::Duration;
use world::World;


Expand Down
2 changes: 1 addition & 1 deletion src/player.rs
Expand Up @@ -6,7 +6,7 @@ use monster::{Monster, CompanionBonus};
use point::Point;
use ranged_int::Ranged;
use std::fmt::{Display, Error, Formatter};
use time::Duration;
use std::time::Duration;


#[derive(Copy, Clone, PartialEq, Debug)]
Expand Down
15 changes: 8 additions & 7 deletions src/render.rs
Expand Up @@ -14,7 +14,8 @@ use state::{Side, State};
use std::borrow::Cow;
use std::collections::HashMap;

use time::Duration;
use std::time::Duration;
use util;
use world::Chunk;


Expand Down Expand Up @@ -55,7 +56,7 @@ pub fn render_game(state: &State, dt: Duration, fps: i32, drawcalls: &mut Vec<Dr
let display_area = Rectangle::center(state.screen_position_in_world, state.map_size / 2);
let screen_coords_from_world = |pos| pos - screen_left_top_corner;

let total_time_ms = state.clock.num_milliseconds();
let total_time_ms = util::num_milliseconds(state.clock) as i64;
let world_size = state.world_size;

let player_will_is_max = state.player.will.is_max();
Expand Down Expand Up @@ -443,21 +444,21 @@ fn render_panel(
lines.push(
format!(
"upd: {}, dc: {}",
frame_stat.update.num_milliseconds(),
frame_stat.drawcalls.num_milliseconds()
util::num_milliseconds(frame_stat.update),
util::num_milliseconds(frame_stat.drawcalls)
).into(),
);
}
lines.push(
format!(
"longest upd: {}",
state.stats.longest_update().num_milliseconds()
util::num_milliseconds(state.stats.longest_update())
).into(),
);
lines.push(
format!(
"longest dc: {}",
state.stats.longest_drawcalls().num_milliseconds()
util::num_milliseconds(state.stats.longest_drawcalls())
).into(),
);
}
Expand Down Expand Up @@ -501,7 +502,7 @@ fn render_panel(
x: x + 1,
y: bottom - 1,
},
format!("dt: {}ms", dt.num_milliseconds()).into(),
format!("dt: {}ms", util::num_milliseconds(dt)).into(),
fg,
));
drawcalls.push(Draw::Text(
Expand Down
11 changes: 5 additions & 6 deletions src/state.rs
Expand Up @@ -13,8 +13,7 @@ use std::collections::VecDeque;
use std::fs::{self, File};
use std::io::{self, BufRead, BufReader, Write};
use std::path::{Path, PathBuf};
use time;
use time::Duration;
use std::time::{self, Duration};
use timer::Timer;
use world::World;

Expand Down Expand Up @@ -52,7 +51,7 @@ pub enum Command {


pub fn generate_replay_path() -> PathBuf {
let cur_time = time::now();
let cur_time = time::Instant::now();
// Timestamp in format: 2016-11-20T20-04-39.123. We can't use the
// colons in the timestamp -- Windows don't allow them in a path.
let timestamp = format!(
Expand Down Expand Up @@ -177,10 +176,10 @@ impl State {
replay,
replay_full_speed,
exit_after,
clock: Duration::zero(),
replay_step: Duration::zero(),
clock: Duration::new(0, 0),
replay_step: Duration::new(0, 0),
stats: Stats::new(6000), // about a minute and a half at 60 FPS
pos_timer: Timer::new(Duration::milliseconds(0)),
pos_timer: Timer::new(Duration::from_millis(0)),
old_screen_pos: (0, 0).into(),
new_screen_pos: (0, 0).into(),
paused: false,
Expand Down

0 comments on commit 386ef64

Please sign in to comment.