diff options
| author | Raymaekers Luca <luca@spacehb.net> | 2025-10-12 15:20:31 +0200 |
|---|---|---|
| committer | Raymaekers Luca <luca@spacehb.net> | 2025-10-12 15:20:31 +0200 |
| commit | 59c7a90fae440b86a18560d55e9dca5d6c45d3cc (patch) | |
| tree | 6ebb6a52b60a3c60df602e8044f0014bbb67916f /code/platform.js | |
| parent | 7cb810089144d42d202e0b267235174d7869c5aa (diff) | |
checkpoint
Diffstat (limited to 'code/platform.js')
| -rw-r--r-- | code/platform.js | 77 |
1 files changed, 54 insertions, 23 deletions
diff --git a/code/platform.js b/code/platform.js index 7790297..35112a2 100644 --- a/code/platform.js +++ b/code/platform.js @@ -27,17 +27,13 @@ function ReadHeapString(ptr, length) return ret; } +function sleep(ms) { + return new Promise(resolve => setTimeout(resolve, ms)); +} + //- Platform async function main() { - const socket = new WebSocket('ws://localhost:1234'); - socket.onmessage = function incoming(message) - { - if(message.data === "reload") - { - window.location.reload(); - } - }; var env = { @@ -57,24 +53,38 @@ async function main() round: Math.round, }; - const { instance } = await WebAssembly.instantiateStreaming( + let wasm_instance = 0; + + let { instance } = await WebAssembly.instantiateStreaming( fetch("./game.wasm"), {env:env} ); - HEAPU8 = new Uint8Array(instance.exports.memory.buffer); + + wasm_instance = instance; + HEAPU8 = new Uint8Array(wasm_instance.exports.memory.buffer); + + let reload = false; + const socket = new WebSocket('ws://localhost:1234'); + socket.onmessage = async function incoming(message) + { + if(message.data === "reload") + { + reload = true; + } + }; //- Image - const width = instance.exports.GetBufferWidth(); - const height = instance.exports.GetBufferHeight(); - const bytes_per_pixel = instance.exports.GetBytesPerPixel(); + const width = wasm_instance.exports.GetBufferWidth(); + const height = wasm_instance.exports.GetBufferHeight(); + const bytes_per_pixel = wasm_instance.exports.GetBytesPerPixel(); const canvas = document.getElementById("graphics-canvas"); const ctx = canvas.getContext("2d"); canvas.width = width; canvas.height = height; - const buffer_address = instance.exports.GlobalImageBuffer.value; - const image = new ImageData( - new Uint8ClampedArray(instance.exports.memory.buffer, + let buffer_address = wasm_instance.exports.GlobalImageBuffer.value; + let image = new ImageData( + new Uint8ClampedArray(wasm_instance.exports.memory.buffer, buffer_address, bytes_per_pixel * width * height), width, @@ -108,20 +118,41 @@ async function main() mouse_down = false; }; - //- Game loop - let prev = 0; - let timestamp = 0; let update_hz = 30; - while(true) + let target_seconds_for_frame = 1/update_hz; + + //- Game loop + let running = true; + let end_counter = 0; + let work_time_elapsed = 0; + while(running) { - let dt = (timestamp - prev)*0.001; - prev = timestamp; + if(reload) + { + let { instance } = await WebAssembly.instantiateStreaming( + fetch("./game.wasm"), {env:env} + ); + HEAPU8 = new Uint8Array(instance.exports.memory.buffer); + image = new ImageData( + new Uint8ClampedArray(instance.exports.memory.buffer, + instance.exports.GlobalImageBuffer.value, + bytes_per_pixel * width * height), + width, height); + wasm_instance = instance; + reload = false; + } + + let last_counter = performance.now(); + + wasm_instance.exports.UpdateAndRender(width, height, bytes_per_pixel, + target_seconds_for_frame, + mouse_down, mouse_x, mouse_y); - instance.exports.UpdateAndRender(width, height, bytes_per_pixel, 1/update_hz, mouse_down, mouse_x, mouse_y); ctx.putImageData(image, 0, 0); await new Promise(requestAnimationFrame); } + } window.onload = main; |
