From f1cef733cf60e9742fbbda2784aea8c60b1bdcfd Mon Sep 17 00:00:00 2001 From: Krasilnikov Roman Date: Fri, 13 Sep 2024 20:54:11 +0300 Subject: [PATCH] Unlocked implementation --- index.html | 2 ++ raylib_factory.js | 40 ++++++++++++++++++++++++++++++---------- raylib_worker.js | 6 ++++++ 3 files changed, 38 insertions(+), 10 deletions(-) diff --git a/index.html b/index.html index b096ab5..d47b097 100644 --- a/index.html +++ b/index.html @@ -154,6 +154,7 @@ return `wasm/${example}.wasm` case IMPL.BLOCKING: case IMPL.LOCKING: + case IMPL.UNLOCKED: return `wasm/${example}.native.wasm` } } @@ -251,6 +252,7 @@ [IMPL.GAME_FRAME]: "game frame", [IMPL.BLOCKING]: "blocking", [IMPL.LOCKING]: "locking", + [IMPL.UNLOCKED]: "unlocked", } const defaultImpl = IMPL.GAME_FRAME const raylibImplSelect = document.getElementById("raylib-impl-select"); diff --git a/raylib_factory.js b/raylib_factory.js index 5acac8c..0b5675e 100644 --- a/raylib_factory.js +++ b/raylib_factory.js @@ -95,11 +95,7 @@ export class BlockingRaylibJs extends RaylibJsBase { } WindowShouldClose() { - while (this.unpressedKeyIndex > 0) { - this.currentPressedKeyState.delete( - this.unpressedKeys[--this.unpressedKeyIndex] - ); - } + this.resetKeyState(); let now; this.eventsQueue.pop(this.processEvent); do { @@ -116,6 +112,14 @@ export class BlockingRaylibJs extends RaylibJsBase { super.EndDrawing(); this.platform.render(this.ctx); } + + resetKeyState() { + while (this.unpressedKeyIndex > 0) { + this.currentPressedKeyState.delete( + this.unpressedKeys[--this.unpressedKeyIndex] + ); + } + } } export class LockingRaylibJs extends BlockingRaylibJs { @@ -125,11 +129,7 @@ export class LockingRaylibJs extends BlockingRaylibJs { } WindowShouldClose() { - while (this.unpressedKeyIndex > 0) { - this.currentPressedKeyState.delete( - this.unpressedKeys[--this.unpressedKeyIndex] - ); - } + this.resetKeyState(); Atomics.wait(this.status, 0, 0); Atomics.store(this.status, 0, 0); this.eventsQueue.pop(this.processEvent); @@ -140,10 +140,23 @@ export class LockingRaylibJs extends BlockingRaylibJs { } } +export class UnLockedRaylibJs extends BlockingRaylibJs { + WindowShouldClose() { + this.resetKeyState(); + const now = performance.now(); + // scheduler.yield(); + this.eventsQueue.pop(this.processEvent); + this.dt = (now - this.previous) / 1000.0; + this.previous = now; + return this.windowShouldClose; + } +} + export const IMPL = { GAME_FRAME: 'gameFrame', BLOCKING: 'blocking', LOCKING: 'locking', + UNLOCKED: 'unlocked', }; export const RENDERING_METHOD = { @@ -166,6 +179,7 @@ export const CTX_FACTORIES = { [IMPL.GAME_FRAME]: ({ canvas }) => canvas.getContext('2d'), [IMPL.BLOCKING]: blockingContextFactory, [IMPL.LOCKING]: blockingContextFactory, + [IMPL.UNLOCKED]: blockingContextFactory, }; export const RAYLIB_FACTORIES = { @@ -183,4 +197,10 @@ export const RAYLIB_FACTORIES = { eventsQueue, statusBuffer, }), + [IMPL.UNLOCKED]: ({ ctx, platform, eventsQueue }) => + new UnLockedRaylibJs({ + ctx, + platform, + eventsQueue, + }), }; diff --git a/raylib_worker.js b/raylib_worker.js index 17d2bb4..f0aff28 100644 --- a/raylib_worker.js +++ b/raylib_worker.js @@ -361,6 +361,7 @@ const RENDER_FACTORIES = { [IMPL.GAME_FRAME]: () => () => {}, [IMPL.BLOCKING]: blockingFactory, [IMPL.LOCKING]: blockingFactory, + [IMPL.UNLOCKED]: blockingFactory, }; export function makeRendererMessagesHandler() { @@ -404,6 +405,7 @@ const OFFSCREEN_CANVAS_FACTORIES = { [IMPL.GAME_FRAME]: (canvas) => canvas.transferControlToOffscreen(), [IMPL.BLOCKING]: blockingOffscreenCanvasFactory, [IMPL.LOCKING]: blockingOffscreenCanvasFactory, + [IMPL.UNLOCKED]: blockingOffscreenCanvasFactory, }; const blockingEventSenderFactory = @@ -422,6 +424,7 @@ const EVENT_SENDER_FACTORIES = { }), [IMPL.BLOCKING]: blockingEventSenderFactory, [IMPL.LOCKING]: blockingEventSenderFactory, + [IMPL.UNLOCKED]: blockingEventSenderFactory, }; function startEventsCommitter({ impl, eventsQueue, statusBuffer }) { @@ -432,6 +435,7 @@ function startEventsCommitter({ impl, eventsQueue, statusBuffer }) { case IMPL.GAME_FRAME: return () => {}; case IMPL.BLOCKING: + case IMPL.UNLOCKED: commitEvents = () => { eventsQueue.commit(); frameId = requestAnimationFrame(commitEvents); @@ -476,6 +480,7 @@ const UPDATE_WINDOW_FACTORIES = { }, [IMPL.BLOCKING]: blockingUpdateWindowFactory, [IMPL.LOCKING]: blockingUpdateWindowFactory, + [IMPL.UNLOCKED]: blockingUpdateWindowFactory, }; const BLOCKING_RENDERER_FACTORIES = { @@ -509,6 +514,7 @@ const RENDERER_FACTORIES = { [IMPL.GAME_FRAME]: () => () => {}, [IMPL.BLOCKING]: blockingRendererFactory, [IMPL.LOCKING]: blockingRendererFactory, + [IMPL.UNLOCKED]: blockingRendererFactory, }; export class RaylibJsWorker extends Service {