a-game

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

commit ceab3def93dcbd257ba5708513b40e8f397a3e42
parent 20ad9e642e3cd76e8ba45cc6d4939dcea77be9f7
Author: amin <dev@aminmesbah.com>
Date:   Tue, 16 Jul 2019 03:57:19 +0000

Update wasm platform layer

FossilOrigin-Name: ad15d866acb6b2f30159de241ad5ee04477033a9117f3f4cd530345d30207056
Diffstat:
Msrc/platform_wasm.c | 4++--
Msrc/platform_wasm_js_symbols.txt | 2++
Msrc/platform_wasm_loader.js | 12++++++++++++
Msrc/webgl.h | 20++++++++++++++------
4 files changed, 30 insertions(+), 8 deletions(-)

diff --git a/src/platform_wasm.c b/src/platform_wasm.c @@ -29,9 +29,9 @@ global_variable struct GameMemory game_memory = {0}; global_variable struct GameInput game_input = {0}; global_variable i32 g_width = PLATFORM_SCR_WIDTH; global_variable i32 g_height = PLATFORM_SCR_HEIGHT; -// TODO: replace with allocator +// TODO: For the love of God, replace this with an allocator! // NOTE: If this buffer is too small, the browser will freeze up -global_variable u8 g_mem_buffer[10000] = {0}; +global_variable u8 g_mem_buffer[200000] = {0}; global_variable i32 g_mem_buffer_i = 0; global_variable u32 previous_time = 0; diff --git a/src/platform_wasm_js_symbols.txt b/src/platform_wasm_js_symbols.txt @@ -33,6 +33,8 @@ webglScissor webglShaderSource webglTexImage2D webglTexParameteri +webglTexStorage3D +webglTexSubImage3D webglUniform1f webglUniform1i webglUniform3f diff --git a/src/platform_wasm_loader.js b/src/platform_wasm_loader.js @@ -169,6 +169,14 @@ imports["webglTexImage2D"] = function(target, level, internalformat, width, heig imports["webglTexParameteri"] = function(target, pname, param) { gl.texParameteri(target, pname, param); } +imports["webglTexStorage3D"] = function(target, levels, internalformat, width, height, depth) { + gl.texStorage3D(target, levels, internalformat, width, height, depth); +} +imports["webglTexSubImage3D"] = function(target, level, xoffset, yoffset, zoffset, width, height, depth, format, type, pixels) { + const bytes_per_pixel = 4; + let dataslice = memory.subarray(pixels, pixels + (width * height * depth * bytes_per_pixel)); + gl.texSubImage3D(target, level, xoffset, yoffset, zoffset, width, height, depth, format, type, dataslice); +} imports["webglUniform1f"] = function(location_id, value) { let loc = gl_id_map[location_id]; gl['uniform1f'](loc, value); @@ -205,6 +213,8 @@ imports["js_read_entire_file"] = function(name, name_len, out_buf, out_len) { 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; } @@ -283,10 +293,12 @@ window.onload = async function() { 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]; } diff --git a/src/webgl.h b/src/webgl.h @@ -41,6 +41,8 @@ void webglScissor(i32 x, i32 y, i32 width, i32 height); void webglShaderSource(i32 shader, const char source[static 1], i32 source_len); void webglTexImage2D(i32 target, i32 level, i32 internalformat, i32 width, i32 height, i32 border, i32 format, i32 type, i32 pixels); void webglTexParameteri(i32 target, i32 pname, i32 param); +void webglTexStorage3D(i32 target, i32 levels, i32 internalformat, i32 width, i32 height, i32 depth); +void webglTexSubImage3D (i32 target, i32 level, i32 xoffset, i32 yoffset, i32 zoffset, i32 width, i32 height, i32 depth, i32 format, i32 type, i32 pixels); void webglUniform1f(i32 location, f32 value); void webglUniform1i(i32 location, i32 value); void webglUniform3f(i32 location, f32 x, f32 y, f32 z); @@ -74,12 +76,7 @@ void webglViewport(i32 x, i32 y, i32 width, i32 height); #define GL_TEXTURE_WRAP_S 0x2802 #define GL_TEXTURE_WRAP_T 0x2803 #define GL_CONSTANT_ALPHA 0x8003 -// NOTE(amin): this should normally be: `#define GL_BGRA 0x80E1`, however -// OpenGL ES 3.0, and consequently WebGL 2.0, doesn't support GL_BGRA, so we -// resort to this hideous hack just to get things working, along with a texture -// swizzle in the shader. -// TODO(amin): fix this in a better way -#define GL_BGRA GL_RGBA +#define GL_RGBA8 0x8058 #define GL_CLAMP_TO_EDGE 0x812F #define GL_TEXTURE0 0x84C0 #define GL_ARRAY_BUFFER 0x8892 @@ -89,6 +86,7 @@ void webglViewport(i32 x, i32 y, i32 width, i32 height); #define GL_VERTEX_SHADER 0x8B31 #define GL_COMPILE_STATUS 0x8B81 #define GL_LINK_STATUS 0x8B82 +#define GL_TEXTURE_2D_ARRAY 0x8C1A typedef unsigned int GLenum; typedef unsigned char GLboolean; @@ -320,6 +318,16 @@ void glTexParameteri(GLenum target, GLenum pname, GLint param) webglTexParameteri(WEBGL_CAST_I32(target), WEBGL_CAST_I32(pname), WEBGL_CAST_I32(param)); } +void glTexStorage3D(GLenum target, GLsizei levels, GLenum internalformat, GLsizei width, GLsizei height, GLsizei depth) +{ + webglTexStorage3D(target, levels, internalformat, width, height, depth); +} + +void glTexSubImage3D (GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLenum type, const void *pixels) +{ + webglTexSubImage3D(target, level, xoffset, yoffset, zoffset, width, height, depth, format, type, WEBGL_CAST_I32(pixels)); +} + inline void glUniform1f(GLint location, GLfloat v0) { webglUniform1f(WEBGL_CAST_I32(location), (f32)v0);