summaryrefslogtreecommitdiff
path: root/chapter32/src/main/smp/sys/gfx.smp
diff options
context:
space:
mode:
authorCliff Click <cliffc@acm.org>2026-11-15 16:11:39 -0800
committerCliff Click <cliffc@acm.org>2026-11-15 16:11:39 -0800
commit9fe7c6f9697b5bbf3041261fdc60040e75cc674f (patch)
treed92a8ff20ed984938d90ad8917b4d67fba9fb745 /chapter32/src/main/smp/sys/gfx.smp
parentf0a6c6757a795b8e186de0076836a87acef59fa7 (diff)
downloadSimple-ch32.tar.gz
Simple-ch32.zip
Split off chapter 31ch32
Ch 32 will be focused on compiler performance issues.
Diffstat (limited to 'chapter32/src/main/smp/sys/gfx.smp')
-rw-r--r--chapter32/src/main/smp/sys/gfx.smp61
1 files changed, 61 insertions, 0 deletions
diff --git a/chapter32/src/main/smp/sys/gfx.smp b/chapter32/src/main/smp/sys/gfx.smp
new file mode 100644
index 00000000..60505ac9
--- /dev/null
+++ b/chapter32/src/main/smp/sys/gfx.smp
@@ -0,0 +1,61 @@
+// Minimal pixel-window interface; drawing belongs in Simple.
+// API and launch reference: gfx.md beside this file.
+// One resizable window, main thread only. Pixel format is 0x00RRGGBB.
+// open/present/poll return OK, CLOSED (normal user close/Esc), or ERROR.
+int OK = 0;
+int CLOSED = 1;
+int ERROR = -1;
+int LEFT = 1;
+int ESCAPE = 256;
+
+{ i64 u32 u32 -> i32 } simple_gfx_open = "C";
+{ i64 u32 -> i32 } simple_gfx_present = "C";
+{ i64 u32 u32 u32 -> i32 } simple_gfx_present_size = "C";
+{ u32 } simple_gfx_width = "C";
+{ u32 } simple_gfx_height = "C";
+{ i32 -> i32 } simple_gfx_poll = "C";
+{ i32 } simple_gfx_mouse_x = "C";
+{ i32 } simple_gfx_mouse_y = "C";
+{ i32 -> i32 } simple_gfx_button = "C";
+{ i32 -> i32 } simple_gfx_key = "C";
+{ i32 } simple_gfx_close = "C";
+{ i64 -> i32 } simple_gfx_wait = "C";
+
+// Failure prints a diagnostic to stderr and returns ERROR; callers decide
+// whether to exit or fall back to text. No native library is loaded until here.
+val open = { u8[~] title, u32 width, u32 height ->
+ i64 ptr = aryu8.str0(title);
+ simple_gfx_open(ptr,width,height);
+};
+
+// Array length must be width*height. The native layer copies for OS repaints;
+// it never keeps a pointer into the Simple heap after this call returns.
+val present = { u32[~] pixels ->
+ i64 ptr = pixels;
+ simple_gfx_present(ptr,pixels#);
+};
+
+// Drawable size after the last event dispatch; zero when closed.
+val width = { -> simple_gfx_width(); };
+val height = { -> simple_gfx_height(); };
+// Explicit dimensions keep a resize arriving during presentation harmless.
+val presentSize = { u32[~] pixels, u32 width, u32 height ->
+ if( width == 0 || height == 0 || width > pixels# || height > pixels# ) return ERROR;
+ i64 count = width; count *= height;
+ if( count > pixels# ) return ERROR;
+ i64 ptr = pixels;
+ u32 size = count;
+ simple_gfx_present_size(ptr,size,width,height);
+};
+
+// wait=true waits up to 17ms, waking early for input to avoid a busy loop.
+val poll = { bool wait -> simple_gfx_poll(wait); };
+val mouseX = { -> simple_gfx_mouse_x(); };
+val mouseY = { -> simple_gfx_mouse_y(); };
+val button = { int button -> simple_gfx_button(button); };
+// Printable keys use uppercase ASCII; special keys use MiniFB's key codes.
+val key = { int key -> simple_gfx_key(key); };
+val close = { -> simple_gfx_close(); };
+// Wait for input or at most timeout milliseconds; 0 polls, -1 waits indefinitely.
+// This blocks the game thread, dispatches window events, and may wake early.
+val wait = { i64 timeout_ms -> simple_gfx_wait(timeout_ms); };