Skip to content

Commit e9d2b12

Browse files
cursoragentomiq
andcommitted
Canvas WASM TI/TI$: use emscripten_get_now for tight loops
Follow-up: advancing ticks60 only in do_sleep_ticks still froze TI in GOTO-only loops. Read TI/TI$ from elapsed monotonic ms since run start; keep gfx_video_advance_ticks60 for basic-gfx frame loop only. Co-authored-by: Chris Garrett <chris@chrisg.com>
1 parent 0774365 commit e9d2b12

2 files changed

Lines changed: 19 additions & 12 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
### Unreleased
44

5-
- **Canvas WASM `TI` / `TI$`**: **`GfxVideoState.ticks60`** was only advanced in the **Raylib** main loop, so **canvas** builds left **`TI`** / **`TI$`** frozen. **`emscripten_sleep`** in **`do_sleep_ticks`** now advances **`ticks60`** by elapsed milliseconds (60 Hz), shared via **`gfx_video_advance_ticks60`**. **Terminal WASM** (`basic.js`, no **`GFX_VIDEO`**) still uses wall-clock **`time()`** for **`TI`** / **`TI$`** (seconds + `HHMMSS`), not C64 jiffies.
5+
- **Canvas WASM `TI` / `TI$`**: **`GfxVideoState.ticks60`** was only advanced in the **Raylib** main loop, so **canvas** **`TI`** / **`TI$`** stayed frozen (especially in tight **`GOTO`** loops). **Canvas** now derives 60 Hz jiffies from **`emscripten_get_now()`** since **`basic_load_and_run_gfx`** ( **`gfx_video_advance_ticks60`** still drives **basic-gfx** each frame). **Terminal WASM** (`basic.js`, no **`GFX_VIDEO`**) still uses **`time()`** for **`TI`** / **`TI$`** (seconds + wall-clock `HHMMSS`), not C64 jiffies.
66

77
- **WordPress**: New plugin **`wordpress/rgc-basic-tutorial-block`** — Gutenberg block **RGC-BASIC embed** with automatic script/style enqueue, **`copy-web-assets.sh`** to sync **`web/tutorial-embed.js`**, **`vfs-helpers.js`**, and modular WASM into **`assets/`**; optional **Settings → RGC-BASIC Tutorial** base URL.
88

basic.c

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@
6060
/* Video state pointer; must precede __EMSCRIPTEN__ key helpers that reference gfx_vs. */
6161
static GfxVideoState *gfx_vs = NULL;
6262
#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;
6365
/* Canvas WASM: trek.bas-style lines pack many ':' statements; yield inside long PRINT too. */
6466
static unsigned wasm_gfx_put_budget;
6567
/* 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)
31803182
ms = 1;
31813183
}
31823184
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
31933185
}
31943186
#elif defined(_WIN32)
31953187
/* Windows: sleep using Sleep() in milliseconds derived from 60Hz ticks. */
@@ -5548,7 +5540,21 @@ static struct value eval_factor(char **p)
55485540
int is_str = (len > 0 && namebuf[len - 1] == '$');
55495541
#ifdef GFX_VIDEO
55505542
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
55525558
*p = q;
55535559
if (!is_str) {
55545560
return make_num((double)t);
@@ -9352,6 +9358,7 @@ EMSCRIPTEN_KEEPALIVE int wasm_gfx_screen_screencode_at(int col, int row)
93529358

93539359
static void wasm_gfx_set_video(void)
93549360
{
9361+
wasm_gfx_ti_epoch_ms = emscripten_get_now();
93559362
gfx_sprite_shutdown();
93569363
gfx_video_init(&wasm_gfx_state);
93579364
wasm_gfx_state.charset_lowercase = (uint8_t)(petscii_get_lowercase() ? 1 : 0);

0 commit comments

Comments
 (0)