summaryrefslogtreecommitdiff
path: root/code/game.c
diff options
context:
space:
mode:
authorRaymaekers Luca <luca@spacehb.net>2025-10-01 16:53:49 +0200
committerRaymaekers Luca <luca@spacehb.net>2025-10-01 16:53:49 +0200
commitaadff4d1cf17cc23f6f38ab0da51baabfeb9f9d1 (patch)
treef2755f80c5067ae97b667b4c5607d4502805d078 /code/game.c
parent4ea5261f32ec8acd4cdad7c364f57e6ebc86866a (diff)
checkpoint
Diffstat (limited to 'code/game.c')
-rw-r--r--code/game.c103
1 files changed, 103 insertions, 0 deletions
diff --git a/code/game.c b/code/game.c
new file mode 100644
index 0000000..8b37c99
--- /dev/null
+++ b/code/game.c
@@ -0,0 +1,103 @@
+
+// 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
+
+#define local_persist static
+#define global static
+#define internal static
+
+#define WIDTH (1920/2)
+#define HEIGHT (1080/2)
+#define BYTES_PER_PIXEL 4
+
+#define STB_SPRINTF_IMPLEMENTATION
+#include "libs/stb_sprintf.h"
+
+//- Global variables
+extern u8 __heap_base;
+u32 BumpPointer = (u32)&__heap_base;
+u8 Buffer[WIDTH*HEIGHT*BYTES_PER_PIXEL];
+
+//- Extern (js) functions
+extern void LogMessage(u32 Length, char* message);
+
+//- Memory
+void* Malloc(u32 Size)
+{
+ u32 Result = BumpPointer;
+ Result += Size;
+ return (void *)Result;
+}
+
+void Free(u32 Size)
+{
+ BumpPointer -= Size;
+}
+
+//- Game
+u32 GetBufferWidth() { return WIDTH; }
+u32 GetBufferHeight() { return HEIGHT; }
+u32 GetBytesPerPixel() { return BYTES_PER_PIXEL; }
+
+void Logf(char *Format, ...)
+{
+ char MessageBuffer[256] = {0};
+
+ va_list Args;
+ va_start(Args, Format);
+ u32 MessageLength = stbsp_vsprintf(MessageBuffer, Format, Args);
+
+ LogMessage(MessageLength, MessageBuffer);
+}
+
+void RenderGradient(s32 Width, s32 Height, r32 dtForFrame)
+{
+ 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 = ((r32)Counter/(r32)Width);
+ }
+ else
+ {
+ G = 1.0f - (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;
+ }
+ }
+
+ Counter += (dtForFrame * 1000) * .3;
+ if(Counter > Width)
+ {
+ Counter -= Width;
+ Toggle = !Toggle;
+ }
+
+#if 1
+ Logf("%d", Width);
+#endif
+
+} \ No newline at end of file