a-game

2D platformer written from scratch.
git clone git://git.amin.space/a-game.git
Log | Files | Refs | README | LICENSE

commit 51cb312527969d63ebfcc22319583498d00c317f
parent b687b427c267ffbfbc2c9a614c5a6c823b9aa0a9
Author: amin <dev@aminmesbah.com>
Date:   Tue,  2 Jul 2019 00:13:08 +0000

Configure allocator with properly sized buffer

FossilOrigin-Name: 8157231a74da6c8f29b37bef56a2f26b6d9db56d866d15a67d7e6fd040035554
Diffstat:
Msrc/platform_wasm.c | 5+++--
Msrc/platform_wasm_loader.js | 9++++++---
2 files changed, 9 insertions(+), 5 deletions(-)

diff --git a/src/platform_wasm.c b/src/platform_wasm.c @@ -51,11 +51,12 @@ int wasm_print(const char *format, ...) return 0; } -bool init(void) +bool init(i32 num_wasm_memory_pages) { bool init_successful = true; game_memory = (struct GameMemory) { - .buffer_size = MEBIBYTES(64), + // NOTE(amin): each wasm memory page is 64KiB + .buffer_size = num_wasm_memory_pages * KIBIBYTES(64), .platform = { &wasm_read_entire_file, &wasm_memory_free, diff --git a/src/platform_wasm_loader.js b/src/platform_wasm_loader.js @@ -11,6 +11,8 @@ let gl_id_map = [null]; // By the way: Thanks a lot, W3C, for not having any standardized _numeric_ // representation of a key. const key_numeric_codes = { + // Code strings are from: + // https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/code/code_values ArrowLeft: 0, ArrowRight: 1, ArrowUp: 2, @@ -248,11 +250,12 @@ window.onload = async function() { files[0] = file_load("binary.wasm"); files[1] = file_load("shader/main_f.glsl"); files[2] = file_load("shader/main_v.glsl"); - for(var i=0; i<files.length; i++) { + for(var i = 0; i < files.length; i++) { files[i] = await files[i]; } let binary = files[0]; - imports['memory'] = new WebAssembly.Memory({'initial':32}); + let num_memory_pages = 64; // 64pages * 64KiB/page = 4096KiB = 4MiB + imports['memory'] = new WebAssembly.Memory({initial: num_memory_pages}); memory = new Uint8Array(imports['memory']['buffer']); let program = await WebAssembly.instantiate(binary, {"env":imports}); let instance = program['instance']; @@ -261,7 +264,7 @@ window.onload = async function() { window.addEventListener("resize", canvas_resize); window.addEventListener("keyup", (event) => {on_key_event(event, false)}, false); window.addEventListener("keydown", (event) => {on_key_event(event, true)}, false); - if(!exports['init']()) { + if(!exports['init'](num_memory_pages)) { error_fatal("Game initialization failed."); } canvas_render();