Skip to content
This repository has been archived by the owner on Jan 2, 2025. It is now read-only.

Commit

Permalink
Replace external Time crate with std::time
Browse files Browse the repository at this point in the history
`std::time::Duration` is stable as of Rust 1.3. Using it simplifies our
dependencies and should make interoperability with other code easier.

There is a couple of downsides:

First, the API of the Duration in std is smaller than the one in the
time crate so we need to e.g. do our own conversion to total seconds and
milliseconds elapsed. I'm hoping this will get better in the future or
will be something the time crate will adopt while using
std::time::Duration for the underlying data.

Second, this is a breaking change because it no longer works with the
older (<=1.2) compilers.
  • Loading branch information
tomassedovic committed Sep 28, 2015
1 parent 8e6de5b commit fbcac42
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ path = "src/lib.rs"
[dependencies]
bitflags = "0.1"
libc = "0.1"
time = "0.1"
lazy_static = "0.1"
rustc-serialize = { optional = true, version = "0.3" }
serde = { optional = true, version = "0.6" }
Expand Down
18 changes: 13 additions & 5 deletions examples/samples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use rand::ThreadRng;
use std::char::from_u32;
use std::fs::read_dir;
use std::cmp::{min, max};
use std::time::Duration;

const SAMPLE_SCREEN_WIDTH : i32 = 46;
const SAMPLE_SCREEN_HEIGHT : i32 = 20;
Expand Down Expand Up @@ -284,6 +285,10 @@ impl LineSample {
}
}

fn seconds_from_duration(duration: Duration) -> f32 {
duration.as_secs() as f32 + (duration.subsec_nanos() as f32 / 1_000_000_000.0)
}

impl Render for LineSample {
fn initialize(&mut self, console: &mut Offscreen) {
system::set_fps(30);
Expand All @@ -294,7 +299,7 @@ impl Render for LineSample {
console: &mut Offscreen,
_root: &Root,
event: Option<(EventFlags, Event)>) {
let elapsed_seconds: f32 = (system::get_elapsed_time().num_milliseconds() as f32) / 1000.0;
let elapsed_seconds = seconds_from_duration(system::get_elapsed_time());
let flag_byte = self.bk_flag as i32 & 0xff;
if flag_byte == BackgroundFlag::Alph as i32 {
self.set_alpha(elapsed_seconds, BackgroundFlag::Alph);
Expand Down Expand Up @@ -1302,13 +1307,15 @@ impl Render for ImageSample {
console.set_default_background(colors::BLACK);
console.clear();

let elapsed_seconds: f32 = (system::get_elapsed_time().num_milliseconds() as f32) / 1000.0;
let elapsed_seconds = seconds_from_duration(system::get_elapsed_time());
let x = (SAMPLE_SCREEN_WIDTH/2) as f32 + (elapsed_seconds as f32).cos() * 10.0;
let y = (SAMPLE_SCREEN_HEIGHT/2) as f32;
let scale_x = 0.2 + 1.8 * (1.0 + (elapsed_seconds / 2.0).cos()) / 2.0;
let scale_y = scale_x;
let angle = elapsed_seconds;
let elapsed = system::get_elapsed_time().num_milliseconds() / 2000;
let duration = system::get_elapsed_time();
let elapsed_milliseconds = duration.as_secs() as u32 * 1000 + duration.subsec_nanos() / 1_000_000;
let elapsed = elapsed_milliseconds / 2000;

if elapsed % 2 != 0 {
// split the color channels of circle.png
Expand Down Expand Up @@ -1679,10 +1686,11 @@ fn print_help_message(root: &mut Root) {
system::get_fps()));

let time = system::get_elapsed_time();
let millis = time.as_secs() as u32 * 1000 + time.subsec_nanos() / 1_000_000;
root.print_ex(79, 47, BackgroundFlag::None, TextAlignment::Right,
format!("elapsed {:8}ms {:4.2}s",
time.num_milliseconds(),
time.num_milliseconds() as f32/ 1000.0));
millis,
millis as f32/ 1000.0));

root.print(2, 47, format!("{}{} : select a sample",
chars::ARROW_N, chars::ARROW_S));
Expand Down
10 changes: 4 additions & 6 deletions src/system.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
extern crate time;

use std::str;
use std::ptr;
use std::time::Duration;

use std::ffi::{CStr, CString};
use std::path::Path;
use bindings::ffi;

use self::time::Duration;

pub fn set_fps(fps: i32) {
assert!(fps >= 0);
unsafe {
Expand All @@ -31,16 +28,17 @@ pub fn get_last_frame_length() -> f32 {
}

pub fn sleep(time: Duration) {
let duration_ms = (time.as_secs() * 1000) as u32 + (time.subsec_nanos() / 1_000_000);
unsafe {
ffi::TCOD_sys_sleep_milli(time.num_milliseconds() as u32);
ffi::TCOD_sys_sleep_milli(duration_ms);
}
}

pub fn get_elapsed_time() -> Duration {
let ms: u32 = unsafe {
ffi::TCOD_sys_elapsed_milli()
};
Duration::milliseconds(ms as i64)
Duration::from_millis(ms as u64)
}

pub fn save_screenshot<P>(path: P) where P: AsRef<Path> {
Expand Down

0 comments on commit fbcac42

Please sign in to comment.