Permalink
Browse files

Handle `dt` in wasm

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 61c5025da235683dfaf39046381e65a193ffc87a
Showing with 10 additions and 8 deletions.
  1. +8 −5 dose-response.js
  2. +2 −3 src/main.rs
@@ -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) {
@@ -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);

});
@@ -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![];

0 comments on commit 61c5025

Please sign in to comment.