diff options
| author | Raymaekers Luca <luca@spacehb.net> | 2025-10-03 02:05:42 +0200 |
|---|---|---|
| committer | Raymaekers Luca <luca@spacehb.net> | 2025-10-03 02:05:42 +0200 |
| commit | 732e6a47ded496cd224e1cfadc2106b04dcf6859 (patch) | |
| tree | 3ff22b5a6808e0970f8b94b15d162df2054078d9 | |
| parent | 449514853850adca190d7eba679f386c024aa929 (diff) | |
checkpoint
| -rwxr-xr-x | code/build.sh | 25 | ||||
| -rw-r--r-- | code/game.c | 122 | ||||
| -rw-r--r-- | code/platform.js | 22 | ||||
| -rw-r--r-- | data/favicon.ico | 0 | ||||
| -rwxr-xr-x | misc/tmux.sh | 6 | ||||
| -rw-r--r-- | todo.txt | 17 |
6 files changed, 102 insertions, 90 deletions
diff --git a/code/build.sh b/code/build.sh index ed819b6..dd02a94 100755 --- a/code/build.sh +++ b/code/build.sh @@ -28,11 +28,17 @@ WarningFlags=" -Wno-null-dereference " -LinkerFlags=" +WasmCompilerFlags=" +-nostdlib +-mbulk-memory +--target=wasm32 +" +WasmLinkerFlags=" -Wl,--allow-undefined -Wl,--no-entry -Wl,--export-all -Wl,--export=LogMessage +-Wl,-z,stack-size=$((64 * 1024)) " if [ "$Mode" = "debug" ] @@ -44,21 +50,26 @@ then elif [ "$Mode" = "release" ] then CompilerFlags="$CompilerFlags - -O2 + -O3 + " + WasmCompilerFlags="$WasmCompilerFlags + -flto + " + WasmLinkerFlags="$WasmLinkerFlags + -Wl,--lto-O3 " fi printf 'game.c\n' clang \ $CompilerFlags \ - -nostdlib \ - -mbulk-memory --target=wasm32 \ + $WasmCompilerFlags $WasmLinkerFlags \ $WarningFlags \ - $LinkerFlags \ -o ../build/game.wasm \ game.c -printf 'index.html platform.js\n' +printf 'index.html platform.js favicon.ico\n' ln -f index.html platform.js ../build +cp ../data/favicon.ico ../build if false then @@ -72,4 +83,4 @@ then ws.c fi -printf '%s\n' "update" | websocat 'ws://localhost:1234/'
\ No newline at end of file +printf '%s\n' "update" | websocat 'ws://localhost:1234/' diff --git a/code/game.c b/code/game.c index edcc75c..1159c1c 100644 --- a/code/game.c +++ b/code/game.c @@ -1,12 +1,17 @@ +// NOTE(luca): Image buffer format is AA BB GG RR +//~ Types // TODO: figure out how to get those from javascript, because they are going to be different in WASM64 -typedef int s32; -typedef unsigned int u32; -typedef unsigned char u8; -typedef float r32; -typedef int b32; -#define true 1 -#define false 0 +typedef signed char s8; +typedef signed short s16; +typedef signed int s32; +typedef unsigned char u8; +typedef unsigned short u16; +typedef unsigned int u32; +typedef float r32; +typedef int b32; +#define true 1 +#define false 0 #define local_persist static #define global static @@ -16,28 +21,37 @@ typedef int b32; #define HEIGHT (1080/2) #define BYTES_PER_PIXEL 4 +//~ Libraries #define STB_SPRINTF_IMPLEMENTATION #include "libs/stb_sprintf.h" -//- Global variables +//~ Global variables +// From WASM extern u8 __heap_base; + +// Memory u32 BumpPointer = (u32)&__heap_base; +u32 BumpAllocated = 0; + +// Image u8 Buffer[WIDTH*HEIGHT*BYTES_PER_PIXEL]; -//- Extern (js) functions +//~ Functions +//- Platform (js) extern void LogMessage(u32 Length, char* message); //- Memory void* Malloc(u32 Size) { - u32 Result = BumpPointer; - Result += Size; + u32 Result = BumpPointer + BumpAllocated; + BumpAllocated += Size; + return (void *)Result; } void Free(u32 Size) { - BumpPointer -= Size; + BumpAllocated -= Size; } //- Game @@ -56,55 +70,10 @@ void Logf(char *Format, ...) LogMessage(MessageLength, MessageBuffer); } -void RenderGradient(s32 Width, s32 Height, s32 BytesPerPixel, - r32 dtForFrame, b32 MouseDown, s32 MouseX, s32 MouseY) +void +RenderRectangle(u8 *Buffer, s32 Pitch, s32 Width, s32 Height, s32 BytesPerPixel, + s32 MinX, s32 MinY, s32 MaxX, s32 MaxY, u32 Color) { - local_persist s32 Counter = 0; - local_persist b32 Toggle = true; - for(s32 Y = 0; Y < Height; Y++) - { - for(s32 X = 0; X < Width; X++) - { - r32 R = 0; - r32 G = 0; - r32 B = 0; - - if(Toggle) - { - R = 1.0f - ((r32)Counter/(r32)Width); - G = 1.0f - ((r32)Counter/(r32)Width); - } - else - { - G = (r32)Counter/(r32)Width; - R = (r32)Counter/(r32)Width; - } - - // AA BB GG RR - u32 Color = ((0xFF << 24) | - ((u32)(0xFF * B) << 16) | - ((u32)(0xFF * G) << 8) | - ((u32)(0xFF * R) << 0)); - - ((u32 *)Buffer)[Y*Width + X] = Color; - } - } - - u32 Color = 0; - - if(MouseDown) - { - Color = 0xFFFF0000; - } - - u32 Pitch = BytesPerPixel * Width; - - s32 Size = 5; - s32 MinX = MouseX - Size; - s32 MaxX = MouseX + Size; - s32 MinY = MouseY - Size; - s32 MaxY = MouseY + Size; - if(MinX < 0) { MinX = 0; @@ -140,15 +109,38 @@ void RenderGradient(s32 Width, s32 Height, s32 BytesPerPixel, } Row += Pitch; } +} + +void +UpdateAndRender(s32 Width, s32 Height, s32 BytesPerPixel, + r32 dtForFrame, b32 MouseDown, s32 MouseX, s32 MouseY) +{ +#if 1 + // Clear the buffer to black. + { + u32 *Clear = (u32 *)Buffer; + s32 ClearSize = Width*Height; + while(ClearSize--) *Clear++ = 0xFF000000; + } +#endif - - Counter += 1000 * dtForFrame * 0.5; - if(Counter > Width) + u32 Color = 0; + if(MouseDown) { - Counter -= Width; - Toggle = !Toggle; + Color = 0xFF00FF00; } + u32 Pitch = BytesPerPixel * Width; + + s32 Size = 5; + s32 MinX = MouseX - Size; + s32 MaxX = MouseX + Size; + s32 MinY = MouseY - Size; + s32 MaxY = MouseY + Size; + + RenderRectangle(Buffer, Pitch, Width, Height, BytesPerPixel, + MinX, MinY, MaxX, MaxY, Color); + #if 1 Logf("(%d, %d) / %s", MouseX, MouseY, ((MouseDown) ? "down" : "up")); diff --git a/code/platform.js b/code/platform.js index fda3a49..163b0ad 100644 --- a/code/platform.js +++ b/code/platform.js @@ -2,7 +2,6 @@ var HEAPU8; //- Helper functions - function ReadHeapString(ptr, length) { if (length === 0 || !ptr) return ''; @@ -101,24 +100,19 @@ async function main() }; //- Game loop - var prev = null; - function frame(timestamp) + let prev = 0; + let timestamp = 0; + let update_hz = 30; + while(true) { - var dt = (timestamp - prev)*0.001; + let dt = (timestamp - prev)*0.001; prev = timestamp; - // TODO(luca): How to get this??? - var update_hz = 30; - instance.exports.RenderGradient(width, height, bytes_per_pixel, 1/update_hz, 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); - window.requestAnimationFrame(frame); - } - window.requestAnimationFrame((timestamp) => { - prev = timestamp; - window.requestAnimationFrame(frame); - }); + await new Promise(requestAnimationFrame); + } } - window.onload = main; diff --git a/data/favicon.ico b/data/favicon.ico new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/data/favicon.ico diff --git a/misc/tmux.sh b/misc/tmux.sh new file mode 100755 index 0000000..96ce592 --- /dev/null +++ b/misc/tmux.sh @@ -0,0 +1,6 @@ +#!/bin/sh + +tmux new-window './build/ws' +tmux split-pane -h 'zsh' +tmux select-pane -L +tmux split-pane './misc/deploy.sh' @@ -1,5 +1,14 @@ -mouse input. +Web platform +- Make an approximation of the screen's refreshrate. +- Audio +- Keyboard input +- Font? +- Profiling? +WASM +- Initialize the image memory from the heap. +- Debugger? +- Profiling? -reload game on build - -create uint8 buffer in js and pass it to the game.
\ No newline at end of file +ARCHIVED +x reload game on build +x create uint8 buffer in js and pass it to the game. |
