Skip to content

Commit aaabb4a

Browse files
committed
Refactor: Reorder the renderer setup code so that WebAudio works
Since BJS provides the facilities for both WebGL and WebAudio, the C_WebAudio API relies on it being available when it is loaded. This wasn't the case with the original design, since the UI is loaded asynchronously and only then was the render loop started. Since there's no reason or even benefit to doing so, this should change the render loop to start immediately and allow the WebAudio initialization to create its SoundTracks via BJS' API.
1 parent c2096b7 commit aaabb4a

File tree

4 files changed

+169
-170
lines changed

4 files changed

+169
-170
lines changed

Core/APIs/C_Rendering.js

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ const C_Rendering = {
1313
fogColor: Color.GREY,
1414
meshes: [],
1515
lightSources: [],
16+
renderCanvas: window["WorldFrame"],
17+
renderer: new Renderer(window["WorldFrame"]),
1618
};
1719

1820
C_Rendering.loadScene = function () {};
@@ -38,18 +40,10 @@ C_Rendering.isSwitchingScenes = function () {};
3840
// Creates a new renderer for the WorldFrame canvas and immediately renders the active scene.
3941
// Note: Only one renderer/scene/canvas is currently supported.
4042
C_Rendering.startRenderLoop = function () {
41-
if (this.renderer) {
42-
NOTICE("Failed to start render loop (renderer already exists and only one scene is currently supported)");
43-
return;
44-
}
45-
const renderCanvas = window["WorldFrame"];
46-
const renderer = new Renderer(renderCanvas);
47-
this.renderer = renderer;
48-
4943
function onCurrentFrameFinishedRendering() {
50-
const deltaTime = renderer.deltaTime;
44+
const deltaTime = C_Rendering.renderer.deltaTime;
5145
C_EventSystem.triggerEvent("RENDER_LOOP_UPDATE", deltaTime);
52-
renderer.renderNextFrame();
46+
C_Rendering.renderer.renderNextFrame();
5347
}
5448
this.switchScene();
5549
this.createDefaultLightSource(); // for easier debugging
Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,45 @@
11
var format = require("util").format;
22

3-
// Shorthand because I'm lazy (must be set after the localization tables have been read)
4-
let L = {};
53
function StartWebClient() {
64
C_Profiling.startTimer("StartWebClient");
75

86
C_Settings.loadSettingsCache();
97

8+
// WebAudio Setup: Requires settings to be loaded
9+
// We can do this here as long as the C_Decoding API was loaded first
10+
C_Decoding.addDecoder(new BuiltinAudioDecoder());
11+
// Ensure stored settings are applied to any new audio source right away
12+
C_WebAudio.setMusicVolume(C_Settings.getValue("musicVolume"));
13+
C_WebAudio.setEffectsVolume(C_Settings.getValue("sfxVolume"));
14+
C_WebAudio.setAmbienceVolume(C_Settings.getValue("ambienceVolume"));
15+
C_WebAudio.setGlobalVolume(C_Settings.getValue("globalVolume"));
16+
1017
WebClient.initializeLocalizationTables();
1118
L = C_Locales.getLocalizationTable(C_Settings.getValue("activeLocale"));
1219
WebClient.setWindowTitle(L["Loading..."]);
1320

1421
C_Macro.restoreMacroCache(); // Needs to be done before addons are loaded, as they may want to interact with the cache?
1522

16-
WebClient.createUserInterface();
1723
C_Addons.loadAddonCache();
1824
C_Addons.loadEnabledAddons();
1925

2026
const windowTitle = format("%s (%s)", WebClient.titleString, WebClient.versionString);
2127
WebClient.setWindowTitle(windowTitle);
2228

2329
C_EventSystem.registerEvent("SCRIPT_EXECUTION_FINISHED", "WebClient", WebClient.onScriptExecutionFinished);
30+
31+
window.onbeforeunload = function () {
32+
C_EventSystem.triggerEvent("APPLICATION_SHUTDOWN");
33+
};
34+
35+
C_EventSystem.registerEvent("APPLICATION_SHUTDOWN", "WebClient", function () {
36+
DEBUG("Application shutting down; performing cleanup tasks");
37+
C_Addons.saveAddonCache();
38+
C_Macro.saveMacroCache();
39+
C_Settings.saveSettingsCache();
40+
});
41+
42+
WebClient.run();
43+
44+
C_Profiling.endTimer("StartWebClient");
2445
}

Core/WebClient.js

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,6 @@ class WebClient {
55
static titleString = "Revival WebClient";
66
static versionString = "v" + require("./package.json").version;
77

8-
static defaultFrames = [
9-
"ViewportContainer",
10-
"WorldFrame",
11-
"UIParent",
12-
"FpsCounterFrame",
13-
"KeyboardInputFrame",
14-
"GameMenuFrame",
15-
"AddonOptionsFrame",
16-
"SystemOptionsFrame",
17-
];
188
static settings = {};
199
static nextAvailableGUID = 1;
2010
// Sets the window title of the application window
@@ -77,37 +67,16 @@ class WebClient {
7767
DEBUG("Loading application manifest from " + filePath);
7868
this.metadata = C_FileSystem.readJSON(filePath);
7969
}
80-
// Load basic interface
81-
static createUserInterface() {
82-
for (const fileName of this.defaultFrames) {
83-
DEBUG(format("Creating default interface component %s", fileName));
84-
this.loadScript(format(WEBCLIENT_INTERFACE_DIR + "/Frames/" + fileName + ".js"));
85-
}
86-
}
8770
// Defer render loop until the WorldFrame (canvas) exists
8871
static onScriptExecutionFinished(event, URL) {
8972
// Kinda ugly, but alas. It's better than entering callback hell and sync loading doesn't work at all for this
9073
DEBUG(format("SCRIPT_EXECUTION_FINISHED triggered for URL %s", URL));
9174
if (URL !== WEBCLIENT_INTERFACE_DIR + "/Frames/WorldFrame.js") return;
9275

9376
// process.on("exit", function () {
94-
window.onbeforeunload = function () {
95-
C_EventSystem.triggerEvent("APPLICATION_SHUTDOWN");
96-
};
97-
98-
C_EventSystem.registerEvent("APPLICATION_SHUTDOWN", "WebClient", function () {
99-
DEBUG("Application shutting down; performing cleanup tasks");
100-
C_Addons.saveAddonCache();
101-
C_Macro.saveMacroCache();
102-
C_Settings.saveSettingsCache();
103-
});
104-
105-
WebClient.run();
10677
}
10778
// Starts the client application
10879
static run() {
109-
C_Profiling.endTimer("StartWebClient");
110-
11180
function processMessageQueue() {
11281
let numProcessedMessages = 0;
11382
let nextMessage = null;

0 commit comments

Comments
 (0)