diff --git a/CWasm.sublime-project b/CWasm.sublime-project index 5dab2f0..4ae5efd 100644 --- a/CWasm.sublime-project +++ b/CWasm.sublime-project @@ -39,6 +39,7 @@ "CWASM_ENABLE_DBG_LEVEL_INFO", "CWASM_ENABLE_DBG_LEVEL_WARNING", "CWASM_ENABLE_DBG_LEVEL_WARNING", + "CWASM_NO_STB_IMAGE", "DbgLevel_Count", "DbgLevel_Debug", "DbgLevel_Error", diff --git a/build.bat b/build.bat index 539836f..0b57d94 100644 --- a/build.bat +++ b/build.bat @@ -18,9 +18,8 @@ if not "%2"=="" ( if not exist build mkdir build pushd build -rem TODO: Take the app.c file path as an argument? echo [Compiling %app_source_path% to %app_module_name%...] -clang %app_source_path% -o "%app_module_name%" -I ".." -I "..\std" -std=gnu2x --target=wasm32-unknown-unknown -mbulk-memory -Wl,--no-entry,--export-dynamic,--allow-undefined,--export=__heap_base --no-standard-libraries --no-standard-includes +clang %app_source_path% -o "%app_module_name%" -I ".." -I "..\std" -std=gnu2x -O0 --target=wasm32-unknown-unknown -mbulk-memory -Wl,--no-entry,--export-dynamic,--allow-undefined,--export=__heap_base --no-standard-libraries --no-standard-includes if %ERRORLEVEL% NEQ 0 ( echo [FAILED to build %app_module_name%!] goto :build_end diff --git a/cwasm.c b/cwasm.c index ceeb8b4..069f3cd 100644 --- a/cwasm.c +++ b/cwasm.c @@ -15,9 +15,7 @@ Description: #include "cwasm_matrices.c" #include "cwasm_arena.c" -#include "cwasm_webgl_js_imports.h" -#include "cwasm_webgl_constants.h" - +#if !CWASM_NO_STB_IMAGE #define STB_IMAGE_IMPLEMENTATION #define STBIDEF static #define STBI_NO_STDIO @@ -29,6 +27,10 @@ Description: #define STBI_ONLY_PNG #define STBI_NO_THREAD_LOCALS #include "stb_image.h" +#endif + +#include "cwasm_webgl_js_imports.h" +#include "cwasm_webgl_constants.h" void InitializeCWasm(u32 scratchArenasSize) { diff --git a/cwasm.h b/cwasm.h index 42c7ee8..aa6d9db 100644 --- a/cwasm.h +++ b/cwasm.h @@ -49,7 +49,7 @@ Description: #define CWASM_DEBUG_OUTPUT_PRINT_BUFFER_SIZE 1024 //chars #endif #ifndef CWASM_ENABLE_DBG_LEVEL_DEBUG -#define CWASM_ENABLE_DBG_LEVEL_DEBUG 1 +#define CWASM_ENABLE_DBG_LEVEL_DEBUG CWASM_DEBUG #endif #ifndef CWASM_ENABLE_DBG_LEVEL_INFO #define CWASM_ENABLE_DBG_LEVEL_INFO 1 @@ -61,6 +61,10 @@ Description: #define CWASM_ENABLE_DBG_LEVEL_ERROR 1 #endif +#ifndef CWASM_NO_STB_IMAGE +#define CWASM_NO_STB_IMAGE 0 +#endif + // +--------------------------------------------------------------+ // | Macros | // +--------------------------------------------------------------+ diff --git a/data/parallax-mountain-bg.png b/data/parallax-mountain-bg.png new file mode 100644 index 0000000..a4a4805 Binary files /dev/null and b/data/parallax-mountain-bg.png differ diff --git a/data/parallax-mountain-foreground-trees.png b/data/parallax-mountain-foreground-trees.png new file mode 100644 index 0000000..66bbcae Binary files /dev/null and b/data/parallax-mountain-foreground-trees.png differ diff --git a/data/parallax-mountain-montain-far.png b/data/parallax-mountain-montain-far.png new file mode 100644 index 0000000..745ef83 Binary files /dev/null and b/data/parallax-mountain-montain-far.png differ diff --git a/data/parallax-mountain-mountains.png b/data/parallax-mountain-mountains.png new file mode 100644 index 0000000..90ec816 Binary files /dev/null and b/data/parallax-mountain-mountains.png differ diff --git a/data/parallax-mountain-trees.png b/data/parallax-mountain-trees.png new file mode 100644 index 0000000..936c5f7 Binary files /dev/null and b/data/parallax-mountain-trees.png differ diff --git a/test_app.c b/test_app.c index 9bb5f25..89b4901 100644 --- a/test_app.c +++ b/test_app.c @@ -6,6 +6,7 @@ Description: ** When no other application is present, this serves as a simple test case for the CWasm layer */ +#define CWASM_DEBUG 1 #include "cwasm.c" struct @@ -25,6 +26,8 @@ struct GlId viewMatrixLocation; GlId projMatrixLocation; + GlId backgroundTextures[5]; + u32 frameIndex; r64 prevProgramTimeR64; } app; @@ -45,16 +48,25 @@ WASM_EXPORT(App_GetResourcePath) const char* App_GetResourcePath(int resourceInd { switch (resourceIndex) { - case 0: return "icon_64.png"; + case 0: return "parallax-mountain-bg.png"; + case 1: return "parallax-mountain-montain-far.png"; + case 2: return "parallax-mountain-mountains.png"; + case 3: return "parallax-mountain-trees.png"; + case 4: return "parallax-mountain-foreground-trees.png"; default: return nullptr; } } WASM_EXPORT(App_ResourceLoaded) void App_ResourceLoaded(int resourceIndex, int fileSize, u8* fileBytes) { + switch (resourceIndex) { case 0: + case 1: + case 2: + case 3: + case 4: { PrintLine_D("Got resource %d bytes: %p %02X %02X %02X %02X", fileSize, fileBytes, fileBytes[0], fileBytes[1], fileBytes[2], fileBytes[3]); ScratchBegin(scratch); //NOTE: stbi_load_from_memory implicitly allocates from the first scratch arena @@ -95,10 +107,10 @@ WASM_EXPORT(App_ResourceLoaded) void App_ResourceLoaded(int resourceIndex, int f } #endif - if (app.testTexture != 0) { jsGlDeleteTexture(app.testTexture); } - app.testTexture = jsGlCreateTexture(); + // if (app.testTexture != 0) { jsGlDeleteTexture(app.testTexture); } + app.backgroundTextures[resourceIndex] = jsGlCreateTexture(); jsGlActiveTexture(GL_TEXTURE0); - jsGlBindTexture(GL_TEXTURE_2D, app.testTexture); + jsGlBindTexture(GL_TEXTURE_2D, app.backgroundTextures[resourceIndex]); // jsGlPixelStorei(GL_UNPACK_PREMULTIPLY_ALPHA_WEBGL, 1); jsGlTexImage2D( GL_TEXTURE_2D, //bound texture type @@ -112,8 +124,8 @@ WASM_EXPORT(App_ResourceLoaded) void App_ResourceLoaded(int resourceIndex, int f imageWidth*imageHeight*sizeof(u32), //dataLength textureBytes //dataPntr ); - jsGlTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); - jsGlTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + jsGlTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST); + jsGlTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); jsGlTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); jsGlTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); jsGlGenerateMipmap(GL_TEXTURE_2D); @@ -346,8 +358,6 @@ WASM_EXPORT(App_UpdateAndRender) bool App_UpdateAndRender(r64 programTimeR64) jsGlClear(GL_COLOR_BUFFER_BIT); jsGlBindVertexArray(app.vao); // our vertex array object jsGlUseProgram(app.testShader); // our shader program - jsGlActiveTexture(GL_TEXTURE0); - jsGlBindTexture(GL_TEXTURE_2D, app.testTexture); mat4 identityMatrix = Mat4_Identity; jsGlUniformMatrix4fv(app.viewMatrixLocation, &identityMatrix); @@ -379,6 +389,19 @@ WASM_EXPORT(App_UpdateAndRender) bool App_UpdateAndRender(r64 programTimeR64) } #endif + for (int bIndex = 0; bIndex < ArrayCount(app.backgroundTextures); bIndex++) + { + mat4 worldMatrix = Mat4_Identity_Const; + worldMatrix = MulMat4(MakeScaleMat4(3.4f, 2.0f, 1.0f), worldMatrix); + r32 parrallax = ((r32)bIndex / (r32)ArrayCount(app.backgroundTextures)); + worldMatrix = MulMat4(MakeTranslateMat4(-1.7f + OscillateBy(programTime, -0.2f*parrallax, 0.2f*parrallax, 15000, 0), -1.0f, 0.0f), worldMatrix); + jsGlUniformMatrix4fv(app.worldMatrixLocation, &worldMatrix); + jsGlActiveTexture(GL_TEXTURE0); + jsGlBindTexture(GL_TEXTURE_2D, app.backgroundTextures[bIndex]); + jsGlDrawArrays(GL_TRIANGLES, 0, 6); + } + + #if 1 { r32 offsetX = -0.4f; r32 offsetY = -0.4f; @@ -397,8 +420,11 @@ WASM_EXPORT(App_UpdateAndRender) bool App_UpdateAndRender(r64 programTimeR64) ); #endif jsGlUniformMatrix4fv(app.worldMatrixLocation, &worldMatrix); + jsGlActiveTexture(GL_TEXTURE0); + jsGlBindTexture(GL_TEXTURE_2D, app.testTexture); jsGlDrawArrays(GL_TRIANGLES, 0, 6); } + #endif // if (programTime > 5000) { shouldContinue = false; }