|
60 | 60 | /* Video state pointer; must precede __EMSCRIPTEN__ key helpers that reference gfx_vs. */ |
61 | 61 | static GfxVideoState *gfx_vs = NULL; |
62 | 62 | #if defined(__EMSCRIPTEN__) && defined(GFX_VIDEO) |
| 63 | +/* Canvas WASM: TI/TI$ from high-res clock (tight GOTO loops have no SLEEP). */ |
| 64 | +static double wasm_gfx_ti_epoch_ms; |
63 | 65 | /* Canvas WASM: trek.bas-style lines pack many ':' statements; yield inside long PRINT too. */ |
64 | 66 | static unsigned wasm_gfx_put_budget; |
65 | 67 | /* Yield every few execute_statement calls — LET/GOTO/IF on one line skip gfx_put_byte. */ |
@@ -3180,16 +3182,6 @@ static void do_sleep_ticks(double ticks) |
3180 | 3182 | ms = 1; |
3181 | 3183 | } |
3182 | 3184 | emscripten_sleep(ms); |
3183 | | -#if defined(GFX_VIDEO) |
3184 | | - /* Canvas WASM has no per-frame loop like Raylib; advance TI/TI$ from real sleep. */ |
3185 | | - if (gfx_vs) { |
3186 | | - uint32_t add = (uint32_t)((ms * 60 + 500) / 1000); |
3187 | | - if (add < 1u) { |
3188 | | - add = 1u; |
3189 | | - } |
3190 | | - gfx_video_advance_ticks60(gfx_vs, add); |
3191 | | - } |
3192 | | -#endif |
3193 | 3185 | } |
3194 | 3186 | #elif defined(_WIN32) |
3195 | 3187 | /* Windows: sleep using Sleep() in milliseconds derived from 60Hz ticks. */ |
@@ -5548,7 +5540,21 @@ static struct value eval_factor(char **p) |
5548 | 5540 | int is_str = (len > 0 && namebuf[len - 1] == '$'); |
5549 | 5541 | #ifdef GFX_VIDEO |
5550 | 5542 | if (gfx_vs) { |
5551 | | - uint32_t t = gfx_vs->ticks60; |
| 5543 | + uint32_t t; |
| 5544 | +#if defined(__EMSCRIPTEN__) |
| 5545 | + /* Canvas WASM: no per-frame tick increment; use monotonic ms → 60 Hz jiffies. */ |
| 5546 | + { |
| 5547 | + double now = emscripten_get_now(); |
| 5548 | + double elapsed_ms = now - wasm_gfx_ti_epoch_ms; |
| 5549 | + if (elapsed_ms < 0.0) { |
| 5550 | + elapsed_ms = 0.0; |
| 5551 | + } |
| 5552 | + t = (uint32_t)((elapsed_ms * 60.0 / 1000.0) + 0.5); |
| 5553 | + t %= GFX_TICKS60_WRAP; |
| 5554 | + } |
| 5555 | +#else |
| 5556 | + t = gfx_vs->ticks60; |
| 5557 | +#endif |
5552 | 5558 | *p = q; |
5553 | 5559 | if (!is_str) { |
5554 | 5560 | return make_num((double)t); |
@@ -9352,6 +9358,7 @@ EMSCRIPTEN_KEEPALIVE int wasm_gfx_screen_screencode_at(int col, int row) |
9352 | 9358 |
|
9353 | 9359 | static void wasm_gfx_set_video(void) |
9354 | 9360 | { |
| 9361 | + wasm_gfx_ti_epoch_ms = emscripten_get_now(); |
9355 | 9362 | gfx_sprite_shutdown(); |
9356 | 9363 | gfx_video_init(&wasm_gfx_state); |
9357 | 9364 | wasm_gfx_state.charset_lowercase = (uint8_t)(petscii_get_lowercase() ? 1 : 0); |
|
0 commit comments