diff options
| author | Raymaekers Luca <luca@spacehb.net> | 2025-10-12 15:20:31 +0200 |
|---|---|---|
| committer | Raymaekers Luca <luca@spacehb.net> | 2025-10-12 15:20:31 +0200 |
| commit | 59c7a90fae440b86a18560d55e9dca5d6c45d3cc (patch) | |
| tree | 6ebb6a52b60a3c60df602e8044f0014bbb67916f | |
| parent | 7cb810089144d42d202e0b267235174d7869c5aa (diff) | |
checkpoint
| -rwxr-xr-x | code/build.sh | 2 | ||||
| -rw-r--r-- | code/game.cpp | 69 | ||||
| -rw-r--r-- | code/platform.js | 77 | ||||
| -rwxr-xr-x | game.so | bin | 2670432 -> 0 bytes | |||
| -rwxr-xr-x | misc/tmux.sh | 2 |
5 files changed, 71 insertions, 79 deletions
diff --git a/code/build.sh b/code/build.sh index f892d39..e3aa269 100755 --- a/code/build.sh +++ b/code/build.sh @@ -72,7 +72,7 @@ printf 'index.html platform.js favicon.ico\n' ln -f index.html platform.js ../build cp ../data/favicon.ico ../build -if false +if [ ! -s "../build/ws" ] then cd ../ws printf 'ws.c\n' diff --git a/code/game.cpp b/code/game.cpp index 7203347..1a1cebf 100644 --- a/code/game.cpp +++ b/code/game.cpp @@ -31,6 +31,16 @@ u8 GlobalImageBuffer[WIDTH*HEIGHT*BYTES_PER_PIXEL]; external void LogMessage(u32 Length, char* message); #define S_Len(String) (sizeof(String) - 1), (String) +external r32 floor(r32 X); +external r32 ceil(r32 X); +external r32 sqrt(r32 X); +external r32 pow(r32 X, r32 Y); +external r32 fmod(r32 X, r32 Y); +external r32 cos(r32 X); +external r32 acos(r32 X); +external r32 fabs(r32 X); +external r32 round(r32 X); + //- Memory void* Malloc(psize Size) { @@ -106,57 +116,6 @@ RenderRectangle(u8 *Buffer, s32 Pitch, s32 Width, s32 Height, s32 BytesPerPixel, Row += Pitch; } } -#if 0 -void memcpy(void *Source, void *Dest, u32 Size) -{ - -} - -void memset(void *Source, u32 Value, u32 Size) -{ - -} - -u32 strlen(char *String) -{ - return 0; -} - -external r32 floor(r32 X); -external r32 ceil(r32 X); -external r32 sqrt(r32 X); -external r32 pow(r32 X, r32 Y); -external r32 fmod(r32 X, r32 Y); -external r32 cos(r32 X); -external r32 acos(r32 X); -external r32 fabs(r32 X); -external r32 round(r32 X); - -#define STBTT_ifloor(x) ((int) floor(x)) -#define STBTT_iceil(x) ((int) ceil(x)) -#define STBTT_sqrt(x) sqrt(x) -#define STBTT_pow(x,y) pow(x,y) -#define STBTT_fmod(x,y) fmod(x,y) -#define STBTT_cos(x) cos(x) -#define STBTT_acos(x) acos(x) -#define STBTT_fabs(x) fabs(x) - -#define STBTT_malloc(x,u) ((void)(u),Malloc(x)) -#define STBTT_free(x,u) ((void)(u),Free(x)) - -#define STBTT_assert(x) Assert(x) -#define STBTT_strlen(x) strlen(x) - -#define STBTT_memcpy memcpy -#define STBTT_memset memset - -#define HANDMADE_FONT_IMPLEMENTATION -#define STBTT_RASTERIZER_VERSION 1 -#include "libs/handmade_font.h" - -global game_font GlobalFont; -#include "../build/font.c" -#endif external void UpdateAndRender(s32 Width, s32 Height, s32 BytesPerPixel, @@ -196,9 +155,11 @@ UpdateAndRender(s32 Width, s32 Height, s32 BytesPerPixel, u32 Color = 0; if(MouseDown) { - Color = 0xFF00FF00; + Color = 0xFF00FF; } + v2 Min = {}; + s32 ColumnsCount = 6; s32 ColumnWidth = Width/ColumnsCount; @@ -265,9 +226,9 @@ UpdateAndRender(s32 Width, s32 Height, s32 BytesPerPixel, } #endif -#if 0 +#if 1 Logf("(%d, %d) / %s", MouseX, MouseY, ((MouseDown) ? "down" : "up")); #endif -}
\ No newline at end of file +} diff --git a/code/platform.js b/code/platform.js index 7790297..35112a2 100644 --- a/code/platform.js +++ b/code/platform.js @@ -27,17 +27,13 @@ function ReadHeapString(ptr, length) return ret; } +function sleep(ms) { + return new Promise(resolve => setTimeout(resolve, ms)); +} + //- Platform async function main() { - const socket = new WebSocket('ws://localhost:1234'); - socket.onmessage = function incoming(message) - { - if(message.data === "reload") - { - window.location.reload(); - } - }; var env = { @@ -57,24 +53,38 @@ async function main() round: Math.round, }; - const { instance } = await WebAssembly.instantiateStreaming( + let wasm_instance = 0; + + let { instance } = await WebAssembly.instantiateStreaming( fetch("./game.wasm"), {env:env} ); - HEAPU8 = new Uint8Array(instance.exports.memory.buffer); + + wasm_instance = instance; + HEAPU8 = new Uint8Array(wasm_instance.exports.memory.buffer); + + let reload = false; + const socket = new WebSocket('ws://localhost:1234'); + socket.onmessage = async function incoming(message) + { + if(message.data === "reload") + { + reload = true; + } + }; //- Image - const width = instance.exports.GetBufferWidth(); - const height = instance.exports.GetBufferHeight(); - const bytes_per_pixel = instance.exports.GetBytesPerPixel(); + const width = wasm_instance.exports.GetBufferWidth(); + const height = wasm_instance.exports.GetBufferHeight(); + const bytes_per_pixel = wasm_instance.exports.GetBytesPerPixel(); const canvas = document.getElementById("graphics-canvas"); const ctx = canvas.getContext("2d"); canvas.width = width; canvas.height = height; - const buffer_address = instance.exports.GlobalImageBuffer.value; - const image = new ImageData( - new Uint8ClampedArray(instance.exports.memory.buffer, + let buffer_address = wasm_instance.exports.GlobalImageBuffer.value; + let image = new ImageData( + new Uint8ClampedArray(wasm_instance.exports.memory.buffer, buffer_address, bytes_per_pixel * width * height), width, @@ -108,20 +118,41 @@ async function main() mouse_down = false; }; - //- Game loop - let prev = 0; - let timestamp = 0; let update_hz = 30; - while(true) + let target_seconds_for_frame = 1/update_hz; + + //- Game loop + let running = true; + let end_counter = 0; + let work_time_elapsed = 0; + while(running) { - let dt = (timestamp - prev)*0.001; - prev = timestamp; + if(reload) + { + let { instance } = await WebAssembly.instantiateStreaming( + fetch("./game.wasm"), {env:env} + ); + HEAPU8 = new Uint8Array(instance.exports.memory.buffer); + image = new ImageData( + new Uint8ClampedArray(instance.exports.memory.buffer, + instance.exports.GlobalImageBuffer.value, + bytes_per_pixel * width * height), + width, height); + wasm_instance = instance; + reload = false; + } + + let last_counter = performance.now(); + + wasm_instance.exports.UpdateAndRender(width, height, bytes_per_pixel, + target_seconds_for_frame, + mouse_down, mouse_x, mouse_y); - instance.exports.UpdateAndRender(width, height, bytes_per_pixel, 1/update_hz, mouse_down, mouse_x, mouse_y); ctx.putImageData(image, 0, 0); await new Promise(requestAnimationFrame); } + } window.onload = main; diff --git a/game.so b/game.so Binary files differdeleted file mode 100755 index ec92d73..0000000 --- a/game.so +++ /dev/null diff --git a/misc/tmux.sh b/misc/tmux.sh index 15377b8..84e5063 100755 --- a/misc/tmux.sh +++ b/misc/tmux.sh @@ -4,7 +4,7 @@ ThisDir="$(dirname "$(readlink -f "$0")")" cd "$ThisDir" cd .. -start 4ed . +setsid 4ed . tmux new-window './build/ws' tmux split-pane -h 'zsh' tmux select-pane -L |
