73 lines
2.6 KiB
JavaScript
73 lines
2.6 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)
|
|
{
|
|
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);
|
|
}
|
|
else
|
|
{
|
|
console.error("Initialization failed!");
|
|
}
|
|
}
|
|
|
|
MainLoop(); |