122 lines
4.5 KiB
JavaScript
122 lines
4.5 KiB
JavaScript
|
|
import { WASM_MEMORY_PAGE_SIZE, appGlobals } from './globals.js'
|
|
import { loadWasmModule, wasmPntrToJsString, wasmPntrAndLengthToJsString } from './wasm_functions.js'
|
|
import { jsStdFunctions } from './std_functions.js'
|
|
import { jsGlFunctions } from './gl_functions.js'
|
|
|
|
function AcquireCanvas(canvasWidth, canvasHeight)
|
|
{
|
|
var canvas = document.getElementsByTagName("canvas")[0];
|
|
// console.log(canvas);
|
|
|
|
// set the display size of the canvas.
|
|
canvas.style.width = canvasWidth + "px";
|
|
canvas.style.height = canvasHeight + "px";
|
|
|
|
// set the size of the drawingBuffer
|
|
var devicePixelRatio = window.devicePixelRatio || 1;
|
|
canvas.width = canvasWidth * devicePixelRatio;
|
|
canvas.height = canvasHeight * devicePixelRatio;
|
|
|
|
// canvasContainer = document.getElementById("canvas_container");
|
|
// console.assert(canvasContainer != null, "Couldn't find canvas container DOM element!");
|
|
appGlobals.canvas = canvas;
|
|
}
|
|
|
|
async function LoadWasmModule(wasmFilePath, initialWasmPageCount)
|
|
{
|
|
appGlobals.textDecoder = new TextDecoder("utf-8");
|
|
appGlobals.textEncoder = new TextEncoder("utf-8");
|
|
appGlobals.wasmModule = await loadWasmModule(wasmFilePath, { ...jsStdFunctions, ...jsGlFunctions });
|
|
appGlobals.memDataView = new DataView(new Uint8Array(appGlobals.wasmModule.exports.memory.buffer).buffer);
|
|
let memorySize = appGlobals.wasmModule.exports.memory.buffer.byteLength;
|
|
let numMemoryPagesAfterLoad = memorySize / WASM_MEMORY_PAGE_SIZE;
|
|
if ((memorySize % WASM_MEMORY_PAGE_SIZE) != 0)
|
|
{
|
|
console.warn("memorySize (" + memorySize + ") is not a multiple of WASM_MEMORY_PAGE_SIZE (" + WASM_MEMORY_PAGE_SIZE + ")");
|
|
numMemoryPagesAfterLoad++;
|
|
}
|
|
appGlobals.wasmModule.exports.init_mem(numMemoryPagesAfterLoad);
|
|
}
|
|
|
|
async function MainLoop()
|
|
{
|
|
console.log("Initializing WebGL Canvas...");
|
|
AcquireCanvas(600, 400);
|
|
|
|
var canvasContextGl = appGlobals.canvas.getContext("webgl2");
|
|
if (canvasContextGl === null) { console.error("Unable to initialize WebGL render context. Your browser or machine may not support it :("); return; }
|
|
// console.dir(canvasContextGl);
|
|
appGlobals.glContext = canvasContextGl;
|
|
|
|
console.log("Loading WASM Module...");
|
|
await LoadWasmModule("app.wasm", 4);
|
|
|
|
let initSuccess = appGlobals.wasmModule.exports.App_Initialize();
|
|
|
|
if (initSuccess)
|
|
{
|
|
var resourcePaths = [];
|
|
var resourceIndex = 0;
|
|
while (true)
|
|
{
|
|
let resourcePathPntr = appGlobals.wasmModule.exports.App_GetResourcePath(resourceIndex);
|
|
if (resourcePathPntr == 0) { break; }
|
|
let resourcePathStr = wasmPntrToJsString(resourcePathPntr);
|
|
resourcePaths.push(resourcePathStr);
|
|
resourceIndex++;
|
|
}
|
|
|
|
console.log("Running!");
|
|
function renderFrame(currentTime)
|
|
{
|
|
let shouldContinue = appGlobals.wasmModule.exports.App_UpdateAndRender(currentTime);
|
|
if (shouldContinue) { window.requestAnimationFrame(renderFrame); }
|
|
else { appGlobals.wasmModule.exports.App_Close(); }
|
|
}
|
|
window.requestAnimationFrame(renderFrame);
|
|
|
|
if (resourcePaths.length > 0)
|
|
{
|
|
console.log("Loading " + resourcePaths.length + " Resource" + (resourcePaths.length == 1 ? "" : "s") + "...");
|
|
// console.dir(resourcePaths);
|
|
for (var rIndex = 0; rIndex < resourcePaths.length; rIndex++)
|
|
{
|
|
const file = await fetch(resourcePaths[rIndex]);
|
|
if (file.ok)
|
|
{
|
|
const fileBytes = await file.arrayBuffer();
|
|
// console.log(fileBytes);
|
|
var fileBytesPntr = 0;
|
|
var scratchArenaPntr = 0;
|
|
var scratchArenaMark = 0;
|
|
if (fileBytes.byteLength > 0)
|
|
{
|
|
scratchArenaPntr = appGlobals.wasmModule.exports.cGetScratchArenaPntr(0);
|
|
scratchArenaMark = appGlobals.wasmModule.exports.cGetScratchArenaMark(0);
|
|
fileBytesPntr = appGlobals.wasmModule.exports.cAllocMem(scratchArenaPntr, fileBytes.byteLength);
|
|
if (fileBytesPntr != 0)
|
|
{
|
|
const writeArray = new Uint8Array(appGlobals.wasmModule.exports.memory.buffer);
|
|
writeArray.set(new Uint8Array(fileBytes), fileBytesPntr);
|
|
}
|
|
else { console.error("Failed to allocate " + fileBytes.byteLength + " byte resource[" + rIndex + "] from \"" + resourcePaths[rIndex] + "\""); }
|
|
}
|
|
appGlobals.wasmModule.exports.App_ResourceLoaded(rIndex, fileBytes.byteLength, fileBytesPntr);
|
|
if (fileBytes.byteLength > 0 && fileBytesPntr != 0) { appGlobals.wasmModule.exports.cEndScratchArena(scratchArenaPntr, scratchArenaMark); }
|
|
}
|
|
else
|
|
{
|
|
console.error("Failed to fetch resource[" + rIndex + "] from \"" + resourcePaths[rIndex] + "\"");
|
|
appGlobals.wasmModule.exports.App_ResourceLoaded(rIndex, 0, 0);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
console.error("Initialization failed!");
|
|
}
|
|
}
|
|
|
|
MainLoop(); |