Used __builtin_wasm_memory_size(0) instead of having the Javascript code call init_mem with the initial page count. Added a simple <div> below the canvas that can hold any number of labels that can be filled/updated through jsSetLabel which lives in a new file misc_functions.js.

This commit is contained in:
2025-09-02 16:05:53 -07:00
parent f45c2d88e0
commit 04028b29d3
10 changed files with 117 additions and 30 deletions

30
cwasm.c
View File

@@ -29,6 +29,7 @@ Description:
#include "stb_image.h"
#endif
#include "cwasm_misc_js_imports.h"
#include "cwasm_webgl_js_imports.h"
#include "cwasm_webgl_constants.h"
@@ -37,6 +38,35 @@ void InitializeCWasm(u32 scratchArenasSize)
InitGlobalArenas(scratchArenasSize);
}
static inline void SetLabel(const char* labelName, const char* displayStr) { jsSetLabel(labelName, displayStr); }
void SetLabelPrint(const char* labelName, const char* formatStr, ...)
{
char* displayStr = nullptr;
va_list args;
va_start(args, formatStr);
int length = vsnprintf(displayStr, 0, formatStr, args); //Measure first
Assert(length >= 0);
va_end(args);
if (length == 0) { SetLabel(labelName, ""); return; }
ScratchBegin(scratch);
{
displayStr = AllocArray(char, scratch, length+1);
if (displayStr == nullptr) { SetLabel(labelName, ""); return; }
va_start(args, formatStr);
vsnprintf(displayStr, (size_t)(length+1), formatStr, args); //Real printf
va_end(args);
displayStr[length] = '\0';
SetLabel(labelName, displayStr);
}
ScratchEnd(scratch);
}
// +--------------------------------------------------------------+
// | Memory Allocation Functions for Javascript |
// +--------------------------------------------------------------+
WASM_EXPORT(cAllocMem) void* cAllocMem(Arena* arenaPntr, int numBytes, int alignment)
{
NotNull(arenaPntr);