From 89e5e2c0ad06e83df50cba1597e8c89b767a7d2b Mon Sep 17 00:00:00 2001
From: koniifer <koniifer@proton.me>
Date: Sun, 9 Feb 2025 01:25:05 +0000
Subject: [PATCH] intouch optimisation & new render example using intouch
 features

---
 sysdata/libraries/intouch/src/events.hb       | 16 ++++----
 sysdata/libraries/intouch/src/lib.hb          | 23 ++++++-----
 .../render_example/src/examples/intouch.hb    | 38 +++++++++++++++++++
 sysdata/programs/render_example/src/main.hb   |  2 +-
 .../programs/test/src/tests/stn/process.hb    |  8 ++--
 sysdata/system_config.toml                    |  8 ++--
 6 files changed, 68 insertions(+), 27 deletions(-)
 create mode 100644 sysdata/programs/render_example/src/examples/intouch.hb

diff --git a/sysdata/libraries/intouch/src/events.hb b/sysdata/libraries/intouch/src/events.hb
index 546cb52..ac87d7f 100644
--- a/sysdata/libraries/intouch/src/events.hb
+++ b/sysdata/libraries/intouch/src/events.hb
@@ -2,17 +2,17 @@ keycodes := @use("keycodes.hb");
 .{KeyCode} := keycodes
 
 KeyEvent := packed struct {
-	up: bool,
-	just_triggered: bool,
-	key: KeyCode,
+	up: bool = false,
+	just_triggered: bool = false,
+	key: KeyCode = 0,
 }
 
 MouseEvent := packed struct {
-	x_change: i8,
-	y_change: i8,
-	left: bool,
-	middle: bool,
-	right: bool,
+	x_change: i8 = 0,
+	y_change: i8 = 0,
+	left: bool = false,
+	middle: bool = false,
+	right: bool = false,
 }
 
 GamepadEvent := struct {}
\ No newline at end of file
diff --git a/sysdata/libraries/intouch/src/lib.hb b/sysdata/libraries/intouch/src/lib.hb
index 6130c7a..090bf79 100644
--- a/sysdata/libraries/intouch/src/lib.hb
+++ b/sysdata/libraries/intouch/src/lib.hb
@@ -5,15 +5,19 @@ keycodes := @use("keycodes.hb")
 events := @use("events.hb");
 .{KeyEvent, MouseEvent} := events
 
-recieve_key_event := fn(): ?KeyEvent {
-	kevent := KeyEvent.(false, false, 0)
+key_buf := 0
+mouse_buf := 0
 
-	buf_id := buffer.search("PS/2 Keyboard")
+recieve_key_event := fn(): ?KeyEvent {
+	kevent := KeyEvent.{}
+
+	if key_buf == 0 key_buf = buffer.search("PS/2 Keyboard")
+	if key_buf == 0 return null
 
 	// Read out of the Keyboard buffer here
-	buffer.recv(KeyEvent, buf_id, &kevent)
+	buffer.recv(KeyEvent, key_buf, &kevent)
 
-	if kevent.just_triggered {
+	if kevent != .{} {
 		return kevent
 	}
 
@@ -21,14 +25,15 @@ recieve_key_event := fn(): ?KeyEvent {
 }
 
 recieve_mouse_event := fn(): ?MouseEvent {
-	mevent := MouseEvent.(0, 0, false, false, false)
+	mevent := MouseEvent.{}
 
-	buf_id := buffer.search("PS/2 Mouse")
+	if mouse_buf == 0 mouse_buf = buffer.search("PS/2 Mouse")
+	if mouse_buf == 0 return null
 
 	// Read out of the Mouse buffer here
-	buffer.recv(MouseEvent, buf_id, &mevent)
+	buffer.recv(MouseEvent, mouse_buf, &mevent)
 
-	if mevent.x_change != 0 | mevent.y_change != 0 | mevent.left | mevent.middle | mevent.right {
+	if mevent != .{} {
 		return mevent
 	}
 
diff --git a/sysdata/programs/render_example/src/examples/intouch.hb b/sysdata/programs/render_example/src/examples/intouch.hb
new file mode 100644
index 0000000..79ee022
--- /dev/null
+++ b/sysdata/programs/render_example/src/examples/intouch.hb
@@ -0,0 +1,38 @@
+intouch := @use("lib:intouch")
+render := @use("lib:render")
+stn := @use("stn")
+
+example := fn(): void {
+	screen := render.init(true)
+	mouse_pos := stn.math.Vec2(int).(0, 0)
+	prev_pos := mouse_pos
+	bg_colour := render.BLACK
+	pen_colour := render.WHITE
+	loop {
+		mouse := intouch.recieve_mouse_event()
+		if mouse != null {
+			mouse_pos.x = stn.math.clamp(int, mouse_pos.x + mouse.x_change, 10, @bitcast(screen.width) - 10)
+			mouse_pos.y = stn.math.clamp(int, mouse_pos.y - mouse.y_change, 10, @bitcast(screen.height) - 10)
+
+			prev_pos = mouse_pos
+
+			if mouse.left {
+				screen.put_filled_circle(@bitcast(mouse_pos), 10, pen_colour)
+			} else if mouse.right {
+				screen.put_filled_circle(@bitcast(mouse_pos), 10, bg_colour)
+			} else if mouse.middle {
+				pen_colour = stn.random.any(render.Color)
+			}
+		}
+
+		kb := intouch.recieve_key_event()
+		if kb != null {
+			if kb.just_triggered {
+				bg_colour = stn.random.any(render.Color)
+				screen.clear(bg_colour)
+			}
+		}
+
+		screen.sync()
+	}
+}
\ No newline at end of file
diff --git a/sysdata/programs/render_example/src/main.hb b/sysdata/programs/render_example/src/main.hb
index 5273ce2..c3e299f 100644
--- a/sysdata/programs/render_example/src/main.hb
+++ b/sysdata/programs/render_example/src/main.hb
@@ -1 +1 @@
-.{example: main} := @use("./examples/mandelbrot.hb")
\ No newline at end of file
+.{example: main} := @use("./examples/intouch.hb")
\ No newline at end of file
diff --git a/sysdata/programs/test/src/tests/stn/process.hb b/sysdata/programs/test/src/tests/stn/process.hb
index 0f01c94..0053057 100644
--- a/sysdata/programs/test/src/tests/stn/process.hb
+++ b/sysdata/programs/test/src/tests/stn/process.hb
@@ -5,13 +5,11 @@
  * assume that `*a` never changes (program doesnt work otherwise)
  * (this is a reason why memory sharing is bad)
  */
-opaque := fn(ptr: ^bool): bool {
-	return *ptr
-}
 
 // axe := @embed("assets/lily.axe")
 
 test := fn(): uint {
+	// causes segfault on fakern due to hblang soundness :thumbsup:
 	// process.spawn(@bitcast(&axe), @sizeof(@TypeOf(axe)))
 
 	a: ^bool = @bitcast(memory.request_page(1, true))
@@ -22,8 +20,8 @@ test := fn(): uint {
 	if x == 0 {
 		// ! NOTE: NEVER DO THIS!!! USE BUFFERS INSTEAD!!! :)
 		// acts as a lock. when parent is done, this can go ahead.
-		loop if opaque(a) break else {
-		}
+		loop if *a break else {
+		};
 		log.info("child done.")
 	} else {
 		*a = true
diff --git a/sysdata/system_config.toml b/sysdata/system_config.toml
index b5d8d44..31c3f63 100644
--- a/sysdata/system_config.toml
+++ b/sysdata/system_config.toml
@@ -23,8 +23,8 @@ resolution = "1024x768x24"
 
 [boot.limine.ableos.modules]
 
-# [boot.limine.ableos.modules.render_example]
-# path = "boot:///render_example.hbf"
+[boot.limine.ableos.modules.render_example]
+path = "boot:///render_example.hbf"
 
 # [boot.limine.ableos.modules.sunset_server]
 # path = "boot:///sunset_server.hbf"
@@ -50,8 +50,8 @@ resolution = "1024x768x24"
 # [boot.limine.ableos.modules.angels_halo]
 # path = "boot:///angels_halo.hbf"
 
-[boot.limine.ableos.modules.test]
-path = "boot:///test.hbf"
+# [boot.limine.ableos.modules.test]
+# path = "boot:///test.hbf"
 
 # [boot.limine.ableos.modules.vfsaur]
 # path = "boot:///vfsaur.hbf"