From 02841e3c67c92006f9fe08efcda0d15f3d7283c5 Mon Sep 17 00:00:00 2001 From: Raymaekers Luca Date: Thu, 2 Oct 2025 12:42:29 +0200 Subject: checkpoint --- code/build.sh | 21 ++++++++++++++--- code/game.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++---- code/platform.js | 58 ++++++++++++++++++++++++++++++++++++---------- 3 files changed, 129 insertions(+), 20 deletions(-) (limited to 'code') diff --git a/code/build.sh b/code/build.sh index be1cbc2..96b9516 100755 --- a/code/build.sh +++ b/code/build.sh @@ -14,8 +14,6 @@ fi CompilerFlags=" -fdiagnostics-absolute-paths ---target=wasm32 --nostdlib " WarningFlags=" @@ -27,6 +25,7 @@ WarningFlags=" -Wno-pointer-arith -Wno-unused-parameter -Wno-unused-function +-Wno-null-dereference " LinkerFlags=" @@ -52,9 +51,25 @@ fi printf 'game.c\n' clang \ $CompilerFlags \ + -nostdlib \ + --target=wasm32 \ $WarningFlags \ $LinkerFlags \ -o ../build/game.wasm \ game.c printf 'index.html platform.js\n' -cp index.html platform.js ../build +ln -f index.html platform.js ../build + +if true +then + cd ../ws + printf 'ws.c\n' + clang \ + -I./libs/wsServer/include -I./libs/wsServer/src \ + $CompilerFlags \ + $WarningFlags \ + -o ../build/ws \ + ws.c +fi + +printf '%s\n' "update" | websocat 'ws://localhost:1234/' \ No newline at end of file diff --git a/code/game.c b/code/game.c index 8b37c99..39420b4 100644 --- a/code/game.c +++ b/code/game.c @@ -56,7 +56,8 @@ void Logf(char *Format, ...) LogMessage(MessageLength, MessageBuffer); } -void RenderGradient(s32 Width, s32 Height, r32 dtForFrame) +void RenderGradient(s32 Width, s32 Height, r32 dtForFrame, + b32 MouseDown, s32 MouseX, s32 MouseY) { local_persist s32 Counter = 0; local_persist b32 Toggle = true; @@ -71,14 +72,19 @@ void RenderGradient(s32 Width, s32 Height, r32 dtForFrame) if(Toggle) { R = 1.0f - ((r32)Counter/(r32)Width); - G = ((r32)Counter/(r32)Width); + G = 1.0f - ((r32)Counter/(r32)Width); } else { - G = 1.0f - (r32)Counter/(r32)Width; + G = (r32)Counter/(r32)Width; R = (r32)Counter/(r32)Width; } + if(MouseDown) + { + B = 0.5f; + } + // AA BB GG RR u32 Color = ((0xFF << 24) | ((u32)(0xFF * B) << 16) | @@ -89,7 +95,61 @@ void RenderGradient(s32 Width, s32 Height, r32 dtForFrame) } } - Counter += (dtForFrame * 1000) * .3; + if(MouseDown) + { + u32 Color = 0; + + u32 Pitch = BYTES_PER_PIXEL * Width; + + s32 Width = 5; + s32 MinX = MouseX - Width; + s32 MaxX = MouseX + Width; + s32 MinY = MouseY - Width; + s32 MaxY = MouseY + Width; + +#if 0 + if(MinX < 0) + { + MinX = 0; + } + + if(MaxX > Width) + { + MaxX = Width; + } + + if(MinY < 0) + { + MinY = 0; + } + + if(MaxY > Height) + { + MaxY = Height; + } +#endif + + u8 *Row = ((u8 *)Buffer) + ((BYTES_PER_PIXEL*MinX) + (Pitch*MinY)); + + for(s32 Y = MinY; + Y < MaxY; + Y++) + { + u32 *Pixel = (u32 *)Row; + for(s32 X = MinX; + X < MaxX; + X++) + { + *Pixel++ = Color; + } + Row += Pitch; + } + + + } + + + Counter += 1000 * dtForFrame * 0.5; if(Counter > Width) { Counter -= Width; @@ -97,7 +157,7 @@ void RenderGradient(s32 Width, s32 Height, r32 dtForFrame) } #if 1 - Logf("%d", Width); + Logf("%.3f / (%d, %d) / %s", dtForFrame, MouseX, MouseY, ((MouseDown) ? "down" : "up")); #endif } \ No newline at end of file diff --git a/code/platform.js b/code/platform.js index 883c915..30034a4 100644 --- a/code/platform.js +++ b/code/platform.js @@ -1,11 +1,12 @@ //- Global variables -let HEAPU8; +var HEAPU8; //- Helper functions function ReadHeapString(ptr, length) { if (length === 0 || !ptr) return ''; + var hasutf = 0; for (var hasUtf = 0, t, i = 0; !length || i != length; i++) { t = HEAPU8[((ptr)+(i))>>0]; @@ -30,11 +31,20 @@ function ReadHeapString(ptr, length) //- Platform async function main() { - let env = + const socket = new WebSocket('ws://localhost:1234'); + socket.onmessage = function incoming(message) + { + if(message.data === "reload") + { + window.location.reload(); + } + }; + + var env = { LogMessage: function(length, message) { - let messageJS = ReadHeapString(message, length); + var messageJS = ReadHeapString(message, length); console.log(messageJS); }, }; @@ -44,6 +54,7 @@ async function main() ); HEAPU8 = new Uint8Array(instance.exports.memory.buffer); + //- Image const width = instance.exports.GetBufferWidth(); const height = instance.exports.GetBufferHeight(); const bytes_per_pixel = instance.exports.GetBytesPerPixel(); @@ -52,11 +63,7 @@ async function main() const ctx = canvas.getContext("2d"); canvas.width = width; canvas.height = height; - // - //while(true) - //{ - //} - // + const buffer_address = instance.exports.Buffer.value; const image = new ImageData( new Uint8ClampedArray(instance.exports.memory.buffer, @@ -65,15 +72,42 @@ async function main() width, height); - let prev = null; + //- Mouse Input + var mouse_x = -1; + var mouse_y = -1; + var mouse_down = false; + var mouse_clicked = 0; + + console.log(canvas); + document.onmousemove = function(event) + { + var x_offset = (window.innerWidth - width)/2; + var y_offset = (window.innerHeight - height)/2; + + mouse_x = event.clientX - x_offset; + mouse_y = event.clientY - y_offset; + }; + + document.onmousedown = function(event) + { + mouse_down = true; + }; + + document.onmouseup = function(event) + { + mouse_down = false; + }; + + //- Game loop + var prev = null; function frame(timestamp) { - let dt = (timestamp - prev)*0.001; + var dt = (timestamp - prev)*0.001; prev = timestamp; // TODO(luca): How to get this??? - let update_hz = 144; - instance.exports.RenderGradient(width, height, 1/update_hz); + var update_hz = 144; + instance.exports.RenderGradient(width, height, 1/update_hz, mouse_down, mouse_x, mouse_y); ctx.putImageData(image, 0, 0); window.requestAnimationFrame(frame); } -- cgit v1.2.3-70-g09d2