Skip to content

Commit 95ed6a1

Browse files
author
diekmann
committedMar 19, 2021
inverting the control, so the browser controls the main loop.
this gives the browser a chance to render the frames.
1 parent e855dce commit 95ed6a1

File tree

5 files changed

+36
-8
lines changed

5 files changed

+36
-8
lines changed
 

Diff for: ‎doom/README.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,12 @@ But using git tag `llvmorg-11.1.0`.
110110

111111
Dat feel! After so much theory and no way to test. Finally seeing the first screen of doom rendered. Awesome!
112112

113-
![Doom rendering the first screen to an HTML5 canvas](imgs/doom_first_screen_renders_to_canvas.png)
113+
![Doom rendering the first screen to an HTML5 canvas](imgs/doom_first_screen_renders_to_canvas.png)
114+
115+
---
116+
117+
If I don't make `I_FinishUpdate` `panic!()`, then doom runs in its infinite game loop.
118+
Unfortunately, this runs at 100% CPU, firefox complains that a website is misbehaving, and nothing is rendered, since the browser has no chance of drawing the animation.
119+
120+
Probably, I want to change doom such that doom itself is not looping, but I can call the loop via `window.requestAnimationFrame()`.
121+
This somehow inverses control and gives the browser a chance to render the frames.

Diff for: ‎doom/index.html

+6
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@
5959
WebAssembly.instantiateStreaming(fetch('target/wasm32-unknown-unknown/debug/doom.wasm'), importObject)
6060
.then(obj => {
6161
obj.instance.exports.main();
62+
63+
function step(timestamp) {
64+
obj.instance.exports.doom_loop_step();
65+
window.requestAnimationFrame(step);
66+
}
67+
window.requestAnimationFrame(step);
6268
});
6369
</script>
6470
</body>

Diff for: ‎doom/linuxdoom-1.10/d_main.c

+13-5
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ void D_Display (void)
351351
//
352352
extern boolean demorecording;
353353

354-
void D_DoomLoop (void)
354+
void D_DoomLoop_prepare (void)
355355
{
356356
if (demorecording)
357357
G_BeginRecording ();
@@ -365,9 +365,10 @@ void D_DoomLoop (void)
365365
}
366366

367367
I_InitGraphics ();
368+
}
368369

369-
while (1)
370-
{
370+
371+
void D_DoomLoop_loop (void) {
371372
// frame syncronous IO operations
372373
I_StartFrame ();
373374

@@ -403,7 +404,13 @@ void D_DoomLoop (void)
403404
// Update sound output.
404405
I_SubmitSound();
405406
#endif
406-
}
407+
}
408+
409+
void D_DoomLoop (void) { // TODO: remove!!
410+
D_DoomLoop_prepare();
411+
while(1){
412+
D_DoomLoop_loop();
413+
}
407414
}
408415

409416

@@ -1170,5 +1177,6 @@ void D_DoomMain (void)
11701177

11711178
}
11721179

1173-
D_DoomLoop (); // never returns
1180+
//D_DoomLoop (); // never returns // inversion of control, browser controls the loop.
1181+
D_DoomLoop_prepare();
11741182
}

Diff for: ‎doom/src/main.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ pub type c_long_double = ::std::os::raw::c_double; //?
88
// C libraries
99
extern "C" {
1010
// d_main.c
11-
fn D_DoomMain() -> !;
11+
fn D_DoomMain();
12+
fn D_DoomLoop_loop();
1213

1314
// m_argv.c
1415
static mut myargc: c_int;
@@ -90,3 +91,8 @@ fn main() {
9091
D_DoomMain();
9192
};
9293
}
94+
95+
#[no_mangle]
96+
pub extern "C" fn doom_loop_step() {
97+
unsafe { D_DoomLoop_loop() };
98+
}

Diff for: ‎doom/src/video.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ extern "C" fn I_FinishUpdate() {
352352
unsafe {
353353
js_draw_screen(canvas.as_ptr());
354354
}
355-
panic!("I_FinishUpdate unimplemented");
355+
crate::log!("I_FinishUpdate");
356356
}
357357

358358
#[no_mangle]

0 commit comments

Comments
 (0)
Please sign in to comment.