Skip to content

Commit 0814bc0

Browse files
author
diekmann
committedMar 18, 2021
gettimeofday
looks like Doom wants to draw something!!!
1 parent df30abf commit 0814bc0

File tree

4 files changed

+42
-12
lines changed

4 files changed

+42
-12
lines changed
 

‎doom/index.html

+10-1
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,18 @@
1919
console.log(string);
2020
}
2121

22+
function GetTimeOfDay(ptr) {
23+
var timeval = new Uint32Array(memory.buffer, ptr, 2);
24+
// TODO: maybe wait for animation frame?
25+
let t = new Date();
26+
timeval[0] = t.getSeconds();
27+
timeval[1] = t.getMilliseconds() * 1000 // as usec YOLO;
28+
}
29+
2230
var importObject = {
2331
js: {
24-
console_log: consoleLogString
32+
console_log: consoleLogString,
33+
js_timeofday: GetTimeOfDay,
2534
},
2635
env: {
2736
memory: memory

‎doom/src/main.rs

+31-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::ffi::CString;
22
use std::ffi::{CStr};
3-
use std::os::raw::{c_char, c_int};
3+
use std::os::raw::{c_char, c_int, c_long};
44

55
#[allow(non_camel_case_types)]
66
pub type c_wchar = ::std::os::raw::c_long;
@@ -145,31 +145,56 @@ extern "C" fn I_ShutdownGraphics() {
145145
}
146146

147147

148+
// struct timeval { time_t tv_sec; suseconds_t tv_usec; };
149+
#[repr(C)]
150+
struct Timeval {
151+
tv_sec: c_long, // TODO is this i32 or i64??
152+
tv_usec: c_long,
153+
}
154+
155+
156+
#[link(wasm_import_module = "js")]
157+
extern "C" {
158+
fn js_timeofday(ptr: *mut Timeval);
159+
}
160+
161+
#[no_mangle]
162+
extern "C" fn gettimeofday(tv: *mut Timeval, _tz: i32) -> c_int {
163+
// timezone is obsolete and should not be needef for doom.
164+
let tv = match unsafe { tv.as_mut() } {
165+
None => return 0 /*do nothing*/ ,
166+
Some(tv) => tv,
167+
};
168+
169+
unsafe { js_timeofday(tv) };
170+
log!("gettimeofday slightly unimplemented (TODO: required for Doom's ticks)");
171+
0 // success
172+
}
148173

149174

150175
#[no_mangle]
151176
extern "C" fn I_InitGraphics() {
152-
panic!("I_InitGraphics unimplemented");
177+
log!("I_InitGraphics (TODO)");
153178
}
154179

155180
#[no_mangle]
156181
extern "C" fn I_StartFrame() {
157-
panic!("I_StartFrame unimplemented");
182+
log!("I_StartFrame (TODO)");
158183
}
159184

160185
#[no_mangle]
161186
extern "C" fn I_StartTic() {
162-
panic!("I_StartTic unimplemented");
187+
log!("I_StartTic unimplemented!!!!!!!!!!!!!!!!!!");
163188
}
164189

165190
#[no_mangle]
166191
extern "C" fn I_UpdateNoBlit() {
167-
panic!("I_UpdateNoBlit unimplemented");
192+
log!("I_UpdateNoBlit unimplemented!!");
168193
}
169194

170195
#[no_mangle]
171196
extern "C" fn I_SetPalette(_: i32) {
172-
panic!("I_SetPalette unimplemented");
197+
log!("I_SetPalette unimplemented");
173198
}
174199

175200
#[no_mangle]

‎doom/src/printf.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::os::raw::c_int;
22

3+
#[repr(C)]
34
struct IOVec {
45
iov_base: *const u8, // void*
56
iov_len: usize, // size_t

‎doom/src/unimplemented_libc.rs

-5
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,6 @@ extern "C" fn strerror(_: i32) -> i32 {
2525
panic!("strerror unimplemented");
2626
}
2727

28-
#[no_mangle]
29-
extern "C" fn gettimeofday(_: i32, _: i32) -> i32 {
30-
panic!("gettimeofday unimplemented");
31-
}
32-
3328
#[no_mangle]
3429
extern "C" fn exit(_: i32) {
3530
panic!("exit unimplemented");

0 commit comments

Comments
 (0)
Please sign in to comment.