Skip to content

Commit

Permalink
Handle dt in wasm
Browse files Browse the repository at this point in the history
We now gather the delta time from javascript and pass it to the game
update function as we should.
  • Loading branch information
tomassedovic committed Dec 20, 2017
1 parent 4eff11c commit 61c5025
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
13 changes: 8 additions & 5 deletions dose-response.js
Expand Up @@ -170,10 +170,13 @@ fetch('target/wasm32-unknown-unknown/release/dose-response.wasm')
console.log("The game is initialised.");
console.log("Gamestate pointer:", gamestate_ptr);

var previous_frame_timestamp = 0;

function update(timestamp) {
//window.requestAnimationFrame(update);
//console.log(timestamp);
window.setTimeout(update, 100);
window.requestAnimationFrame(update);
let dt = timestamp - previous_frame_timestamp;
previous_frame_timestamp = timestamp;

for(let key of pressed_keys) {
var key_code = -1;
if(key.key in keymap) {
Expand All @@ -189,8 +192,8 @@ fetch('target/wasm32-unknown-unknown/release/dose-response.wasm')
}
pressed_keys = [];

results.instance.exports.update(gamestate_ptr);
results.instance.exports.update(gamestate_ptr, dt);
}
update();
update(previous_frame_timestamp);

});
5 changes: 2 additions & 3 deletions src/main.rs
Expand Up @@ -380,12 +380,11 @@ extern {


#[no_mangle]
pub fn update(state_ptr: *mut State) {
pub fn update(state_ptr: *mut State, dt_ms: u32) {
#[allow(unsafe_code)]
let mut state: Box<State> = unsafe { Box::from_raw(state_ptr) };


let dt = std::time::Duration::from_millis(100);
let dt = std::time::Duration::from_millis(dt_ms as u64);
let display_size = point::Point::new(0, 0);
let fps = 60;
let keys: Vec<keys::Key> = vec![];
Expand Down

0 comments on commit 61c5025

Please sign in to comment.