Skip to content

Commit

Permalink
Reorganise and rename config constants etc
Browse files Browse the repository at this point in the history
  • Loading branch information
majaha committed Dec 20, 2024
1 parent 1e80789 commit 77d2110
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 15 deletions.
7 changes: 7 additions & 0 deletions runtimes/web/src/config-constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/** True if the runtime is in Game Developer mode, with access to the devtools window. */
// WASM4_GAMEDEV_MODE is defined in vite.config.ts
export const GAMEDEV_MODE = WASM4_GAMEDEV_MODE;

/** True if we're developers of the WASM4 runtime itself, and are running the runtime
using `npm start` or `vite` etc. */
export const PLATFORM_DEVELOPER_MODE = import.meta.env.MODE === "development";
1 change: 0 additions & 1 deletion runtimes/web/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export const GAMEDEV_MODE = WASM4_GAMEDEV_MODE;
export const WIDTH = 160;
export const HEIGHT = 160;

Expand Down
11 changes: 5 additions & 6 deletions runtimes/web/src/devkit.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import * as constants from "./constants";
// True if we're developers of the WASM4 runtime itself, and are running the runtime
// using `npm start` or `vite` etc. In this case, there's no wasm4 CLI web socket to
// connect to, but we might be fighting for the Vite websocket.
const PLATFORM_DEVELOPER_MODE = import.meta.env.MODE === "development";
import { GAMEDEV_MODE, PLATFORM_DEVELOPER_MODE } from "./config-constants";

export const websocket = constants.GAMEDEV_MODE && !PLATFORM_DEVELOPER_MODE
// The w4 CLI web socket only exists for gamedev mode.
// But if we're running `npm start` or `vite` there is no w4 CLI to connect to,
// but there is a vite websocket we may be fighting for, causing issues with vite.
export const cli_websocket = GAMEDEV_MODE && !PLATFORM_DEVELOPER_MODE
? new WebSocket((location.protocol == "https:" ? "wss" : "ws") + "://" + location.host)
: null;
7 changes: 4 additions & 3 deletions runtimes/web/src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Framebuffer } from "./framebuffer";
import { WebGLCompositor } from "./compositor";
import * as devkit from "./devkit";
import { wasmPatchExportGlobals } from "./wasm-patch";
import * as configConstants from "./config-constants";

export class Runtime {
canvas: HTMLCanvasElement;
Expand Down Expand Up @@ -120,7 +121,7 @@ export class Runtime {
this.wasm = null;

if (wasmBuffer.byteLength > limit) {
if (constants.GAMEDEV_MODE) {
if (configConstants.GAMEDEV_MODE) {
if (!this.warnedFileSize) {
this.warnedFileSize = true;
this.print(`Warning: Cart is larger than ${limit} bytes. Ensure the release build of your cart is small enough to be bundled.`);
Expand Down Expand Up @@ -262,8 +263,8 @@ export class Runtime {
}

printToServer (str: string) {
if (devkit.websocket != null && devkit.websocket.readyState == 1) {
devkit.websocket.send(str);
if (devkit.cli_websocket != null && devkit.cli_websocket.readyState == 1) {
devkit.cli_websocket.send(str);
}
}

Expand Down
10 changes: 5 additions & 5 deletions runtimes/web/src/ui/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { LitElement, html, css } from "lit";
import { customElement, state, query } from 'lit/decorators.js';

import * as constants from "../constants";
import * as configConstants from "../config-constants";
import * as devkit from "../devkit";
import * as utils from "./utils";
import * as z85 from "../z85";
Expand Down Expand Up @@ -141,7 +142,7 @@ export class App extends LitElement {
// Nothing
},
};
if (constants.GAMEDEV_MODE) {
if (configConstants.GAMEDEV_MODE) {
devtoolsManager = await import('@wasm4/web-devtools').then(({ DevtoolsManager}) => new DevtoolsManager());
}

Expand All @@ -153,8 +154,8 @@ export class App extends LitElement {
this.copyNetplayLink();
}

if (constants.GAMEDEV_MODE) {
devkit.websocket?.addEventListener("message", async event => {
if (configConstants.GAMEDEV_MODE) {
devkit.cli_websocket?.addEventListener("message", async event => {
switch (event.data) {
case "reload":
this.resetCart(await loadCartWasm());
Expand Down Expand Up @@ -481,8 +482,7 @@ export class App extends LitElement {

runtime.composite();

if (constants.GAMEDEV_MODE) {
// FIXED(2023-12-13): Pass the correct FPS for display
if (configConstants.GAMEDEV_MODE) {
devtoolsManager.updateCompleted(runtime, timeFrameStart - lastTimeFrameStart);
lastTimeFrameStart = timeFrameStart;
}
Expand Down

0 comments on commit 77d2110

Please sign in to comment.