Skip to content

Commit

Permalink
Unlocked implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
x0k committed Sep 13, 2024
1 parent 73e6acd commit f1cef73
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
2 changes: 2 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@
return `wasm/${example}.wasm`
case IMPL.BLOCKING:
case IMPL.LOCKING:
case IMPL.UNLOCKED:
return `wasm/${example}.native.wasm`
}
}
Expand Down Expand Up @@ -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");
Expand Down
40 changes: 30 additions & 10 deletions raylib_factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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);
Expand All @@ -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 = {
Expand All @@ -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 = {
Expand All @@ -183,4 +197,10 @@ export const RAYLIB_FACTORIES = {
eventsQueue,
statusBuffer,
}),
[IMPL.UNLOCKED]: ({ ctx, platform, eventsQueue }) =>
new UnLockedRaylibJs({
ctx,
platform,
eventsQueue,
}),
};
6 changes: 6 additions & 0 deletions raylib_worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ const RENDER_FACTORIES = {
[IMPL.GAME_FRAME]: () => () => {},
[IMPL.BLOCKING]: blockingFactory,
[IMPL.LOCKING]: blockingFactory,
[IMPL.UNLOCKED]: blockingFactory,
};

export function makeRendererMessagesHandler() {
Expand Down Expand Up @@ -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 =
Expand All @@ -422,6 +424,7 @@ const EVENT_SENDER_FACTORIES = {
}),
[IMPL.BLOCKING]: blockingEventSenderFactory,
[IMPL.LOCKING]: blockingEventSenderFactory,
[IMPL.UNLOCKED]: blockingEventSenderFactory,
};

function startEventsCommitter({ impl, eventsQueue, statusBuffer }) {
Expand All @@ -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);
Expand Down Expand Up @@ -476,6 +480,7 @@ const UPDATE_WINDOW_FACTORIES = {
},
[IMPL.BLOCKING]: blockingUpdateWindowFactory,
[IMPL.LOCKING]: blockingUpdateWindowFactory,
[IMPL.UNLOCKED]: blockingUpdateWindowFactory,
};

const BLOCKING_RENDERER_FACTORIES = {
Expand Down Expand Up @@ -509,6 +514,7 @@ const RENDERER_FACTORIES = {
[IMPL.GAME_FRAME]: () => () => {},
[IMPL.BLOCKING]: blockingRendererFactory,
[IMPL.LOCKING]: blockingRendererFactory,
[IMPL.UNLOCKED]: blockingRendererFactory,
};

export class RaylibJsWorker extends Service {
Expand Down

0 comments on commit f1cef73

Please sign in to comment.