a-game

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

commit 37f0619d402427a277fb78632dfb26c0546cfb3f
parent a546ccb942ab37ea4d03ff16207936669319724b
Author: amin <dev@aminmesbah.com>
Date:   Wed, 17 Jul 2019 03:18:25 +0000

Clean up js file loading

FossilOrigin-Name: e382c8447643d075054136d2d5f596a5b05a042ff61a48d14daa0a02871217b2
Diffstat:
Msrc/platform_wasm_loader.js | 58++++++++++++++++++++++++----------------------------------
1 file changed, 24 insertions(+), 34 deletions(-)

diff --git a/src/platform_wasm_loader.js b/src/platform_wasm_loader.js @@ -3,7 +3,7 @@ let utf8decoder = new TextDecoder("utf-8"); let memory = null; let exports = {}; let imports = {}; -let files = []; +let files = new Map; let gl = null; let gl_id_freelist = []; let gl_id_map = [null]; @@ -207,36 +207,24 @@ imports["webglViewport"] = function(x, y, width, height) { imports["js_get_file_size"] = function(name, name_len) { let file_name = utf8decoder.decode(memory.subarray(name, name + name_len)) let file_size = 0; - if (file_name == "shader/main_f.glsl") { - var file = files[1]; - } else if (file_name == "shader/main_v.glsl") { - var file = files[2]; - } else if (file_name == "assets/tile0.tga") { - var file = files[3]; - } else if (file_name == "assets/tileset0.tga") { - var file = files[4]; - } else { - return 0; + let file = files[file_name]; + if (file !== undefined) { + file_size = file.byteLength; } - file_size = file.byteLength; return file_size; } imports["js_read_entire_file"] = function(name, name_len, out_buf) { let file_name = utf8decoder.decode(memory.subarray(name, name + name_len)) let file_size = 0; - if (file_name == "shader/main_f.glsl") { - var file = files[1]; - } else if (file_name == "shader/main_v.glsl") { - var file = files[2]; - } else if (file_name == "assets/tile0.tga") { - var file = files[3]; - } else if (file_name == "assets/tileset0.tga") { - var file = files[4]; + let file = files[file_name]; + let success = true; + if (file !== undefined) { + let arr = memory.subarray(out_buf, out_buf + file.byteLength); + arr.set(new Uint8Array(file)); } else { - return 0; + success = false; } - let arr = memory.subarray(out_buf, out_buf + file.byteLength); - arr.set(new Uint8Array(file)); + return success; } imports["js_print"] = function(s, len) { let arr = memory.subarray(s, s + len); @@ -296,27 +284,29 @@ function on_key_event(e, key_is_down) { } } window.onload = async function() { - let ctxopts = { + let webgl_options = { alpha: false, depth: true, stencil: false, antialias: true, preserveDrawingBuffer: false }; - gl = document.getElementById("webglcanvas").getContext("webgl2", ctxopts); + gl = document.getElementById("webglcanvas").getContext("webgl2", webgl_options); if(!gl) { error_fatal("Your browser does not support WebGL 2."); } - // TODO: Use a hash table (lol) - files[0] = file_load("binary.wasm"); - files[1] = file_load("shader/main_f.glsl"); - files[2] = file_load("shader/main_v.glsl"); - files[3] = file_load("assets/tile0.tga"); - files[4] = file_load("assets/tileset0.tga"); - for(var i = 0; i < files.length; i++) { - files[i] = await files[i]; + + let file_names = [ + 'binary.wasm', + 'shader/main_f.glsl', + 'shader/main_v.glsl', + 'assets/tile0.tga', + 'assets/tileset0.tga', + ]; + for (const name of file_names.values()) { + files[name] = await file_load(name); } - let binary = files[0]; + let binary = files['binary.wasm']; 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']);