Added some simple background textures from OpenGameArt.org and rendered them with a little parallax effect

This commit is contained in:
2025-09-02 12:20:11 -07:00
parent 3aee6123c6
commit f45c2d88e0
10 changed files with 46 additions and 14 deletions

View File

@@ -39,6 +39,7 @@
"CWASM_ENABLE_DBG_LEVEL_INFO", "CWASM_ENABLE_DBG_LEVEL_INFO",
"CWASM_ENABLE_DBG_LEVEL_WARNING", "CWASM_ENABLE_DBG_LEVEL_WARNING",
"CWASM_ENABLE_DBG_LEVEL_WARNING", "CWASM_ENABLE_DBG_LEVEL_WARNING",
"CWASM_NO_STB_IMAGE",
"DbgLevel_Count", "DbgLevel_Count",
"DbgLevel_Debug", "DbgLevel_Debug",
"DbgLevel_Error", "DbgLevel_Error",

View File

@@ -18,9 +18,8 @@ if not "%2"=="" (
if not exist build mkdir build if not exist build mkdir build
pushd build pushd build
rem TODO: Take the app.c file path as an argument?
echo [Compiling %app_source_path% to %app_module_name%...] 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 ( if %ERRORLEVEL% NEQ 0 (
echo [FAILED to build %app_module_name%!] echo [FAILED to build %app_module_name%!]
goto :build_end goto :build_end

View File

@@ -15,9 +15,7 @@ Description:
#include "cwasm_matrices.c" #include "cwasm_matrices.c"
#include "cwasm_arena.c" #include "cwasm_arena.c"
#include "cwasm_webgl_js_imports.h" #if !CWASM_NO_STB_IMAGE
#include "cwasm_webgl_constants.h"
#define STB_IMAGE_IMPLEMENTATION #define STB_IMAGE_IMPLEMENTATION
#define STBIDEF static #define STBIDEF static
#define STBI_NO_STDIO #define STBI_NO_STDIO
@@ -29,6 +27,10 @@ Description:
#define STBI_ONLY_PNG #define STBI_ONLY_PNG
#define STBI_NO_THREAD_LOCALS #define STBI_NO_THREAD_LOCALS
#include "stb_image.h" #include "stb_image.h"
#endif
#include "cwasm_webgl_js_imports.h"
#include "cwasm_webgl_constants.h"
void InitializeCWasm(u32 scratchArenasSize) void InitializeCWasm(u32 scratchArenasSize)
{ {

View File

@@ -49,7 +49,7 @@ Description:
#define CWASM_DEBUG_OUTPUT_PRINT_BUFFER_SIZE 1024 //chars #define CWASM_DEBUG_OUTPUT_PRINT_BUFFER_SIZE 1024 //chars
#endif #endif
#ifndef CWASM_ENABLE_DBG_LEVEL_DEBUG #ifndef CWASM_ENABLE_DBG_LEVEL_DEBUG
#define CWASM_ENABLE_DBG_LEVEL_DEBUG 1 #define CWASM_ENABLE_DBG_LEVEL_DEBUG CWASM_DEBUG
#endif #endif
#ifndef CWASM_ENABLE_DBG_LEVEL_INFO #ifndef CWASM_ENABLE_DBG_LEVEL_INFO
#define CWASM_ENABLE_DBG_LEVEL_INFO 1 #define CWASM_ENABLE_DBG_LEVEL_INFO 1
@@ -61,6 +61,10 @@ Description:
#define CWASM_ENABLE_DBG_LEVEL_ERROR 1 #define CWASM_ENABLE_DBG_LEVEL_ERROR 1
#endif #endif
#ifndef CWASM_NO_STB_IMAGE
#define CWASM_NO_STB_IMAGE 0
#endif
// +--------------------------------------------------------------+ // +--------------------------------------------------------------+
// | Macros | // | Macros |
// +--------------------------------------------------------------+ // +--------------------------------------------------------------+

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB

View File

@@ -6,6 +6,7 @@ Description:
** When no other application is present, this serves as a simple test case for the CWasm layer ** When no other application is present, this serves as a simple test case for the CWasm layer
*/ */
#define CWASM_DEBUG 1
#include "cwasm.c" #include "cwasm.c"
struct struct
@@ -25,6 +26,8 @@ struct
GlId viewMatrixLocation; GlId viewMatrixLocation;
GlId projMatrixLocation; GlId projMatrixLocation;
GlId backgroundTextures[5];
u32 frameIndex; u32 frameIndex;
r64 prevProgramTimeR64; r64 prevProgramTimeR64;
} app; } app;
@@ -45,16 +48,25 @@ WASM_EXPORT(App_GetResourcePath) const char* App_GetResourcePath(int resourceInd
{ {
switch (resourceIndex) 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; default: return nullptr;
} }
} }
WASM_EXPORT(App_ResourceLoaded) void App_ResourceLoaded(int resourceIndex, int fileSize, u8* fileBytes) WASM_EXPORT(App_ResourceLoaded) void App_ResourceLoaded(int resourceIndex, int fileSize, u8* fileBytes)
{ {
switch (resourceIndex) switch (resourceIndex)
{ {
case 0: 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]); 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 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 #endif
if (app.testTexture != 0) { jsGlDeleteTexture(app.testTexture); } // if (app.testTexture != 0) { jsGlDeleteTexture(app.testTexture); }
app.testTexture = jsGlCreateTexture(); app.backgroundTextures[resourceIndex] = jsGlCreateTexture();
jsGlActiveTexture(GL_TEXTURE0); jsGlActiveTexture(GL_TEXTURE0);
jsGlBindTexture(GL_TEXTURE_2D, app.testTexture); jsGlBindTexture(GL_TEXTURE_2D, app.backgroundTextures[resourceIndex]);
// jsGlPixelStorei(GL_UNPACK_PREMULTIPLY_ALPHA_WEBGL, 1); // jsGlPixelStorei(GL_UNPACK_PREMULTIPLY_ALPHA_WEBGL, 1);
jsGlTexImage2D( jsGlTexImage2D(
GL_TEXTURE_2D, //bound texture type 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 imageWidth*imageHeight*sizeof(u32), //dataLength
textureBytes //dataPntr textureBytes //dataPntr
); );
jsGlTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); jsGlTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
jsGlTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 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_S, GL_CLAMP_TO_EDGE);
jsGlTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); jsGlTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
jsGlGenerateMipmap(GL_TEXTURE_2D); jsGlGenerateMipmap(GL_TEXTURE_2D);
@@ -346,8 +358,6 @@ WASM_EXPORT(App_UpdateAndRender) bool App_UpdateAndRender(r64 programTimeR64)
jsGlClear(GL_COLOR_BUFFER_BIT); jsGlClear(GL_COLOR_BUFFER_BIT);
jsGlBindVertexArray(app.vao); // our vertex array object jsGlBindVertexArray(app.vao); // our vertex array object
jsGlUseProgram(app.testShader); // our shader program jsGlUseProgram(app.testShader); // our shader program
jsGlActiveTexture(GL_TEXTURE0);
jsGlBindTexture(GL_TEXTURE_2D, app.testTexture);
mat4 identityMatrix = Mat4_Identity; mat4 identityMatrix = Mat4_Identity;
jsGlUniformMatrix4fv(app.viewMatrixLocation, &identityMatrix); jsGlUniformMatrix4fv(app.viewMatrixLocation, &identityMatrix);
@@ -379,6 +389,19 @@ WASM_EXPORT(App_UpdateAndRender) bool App_UpdateAndRender(r64 programTimeR64)
} }
#endif #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 offsetX = -0.4f;
r32 offsetY = -0.4f; r32 offsetY = -0.4f;
@@ -397,8 +420,11 @@ WASM_EXPORT(App_UpdateAndRender) bool App_UpdateAndRender(r64 programTimeR64)
); );
#endif #endif
jsGlUniformMatrix4fv(app.worldMatrixLocation, &worldMatrix); jsGlUniformMatrix4fv(app.worldMatrixLocation, &worldMatrix);
jsGlActiveTexture(GL_TEXTURE0);
jsGlBindTexture(GL_TEXTURE_2D, app.testTexture);
jsGlDrawArrays(GL_TRIANGLES, 0, 6); jsGlDrawArrays(GL_TRIANGLES, 0, 6);
} }
#endif
// if (programTime > 5000) { shouldContinue = false; } // if (programTime > 5000) { shouldContinue = false; }