From 1febaba023590ed349ad96104e238220d1992fe9 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Sat, 29 Apr 2023 11:41:05 +0900 Subject: [PATCH] Add some helpers, minor tweaks --- src/helpers.rs | 24 ++++++++++++++++++++++++ src/lib.rs | 8 +++++++- 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 src/helpers.rs diff --git a/src/helpers.rs b/src/helpers.rs new file mode 100644 index 0000000..3047e78 --- /dev/null +++ b/src/helpers.rs @@ -0,0 +1,24 @@ +pub fn fill(color: u8) { + if color > 3 { + crate::trace("ERROR: Invalid value passed to helper \"fill\""); + return; + } + unsafe { + (&mut *crate::FRAMEBUFFER).fill(color | (color << 2) | (color << 4) | (color << 6)); + } +} + +pub fn draw_mouse() -> Option<(i16, i16)> { + let mouse = unsafe { *crate::MOUSE_BUTTONS }; + let mouse_x = unsafe { *crate::MOUSE_X }; + let mouse_y = unsafe { *crate::MOUSE_Y }; + if mouse & crate::MOUSE_LEFT != 0 { + unsafe { *crate::DRAW_COLORS = 2 } + crate::rect(i32::from(mouse_x) - 4, i32::from(mouse_y) - 4, 8, 8); + Some((mouse_x, mouse_y)) + } else { + unsafe { *crate::DRAW_COLORS = 1 } + crate::rect(i32::from(mouse_x) - 2, i32::from(mouse_y) - 2, 4, 4); + None + } +} diff --git a/src/lib.rs b/src/lib.rs index d1c7f30..86c1fd6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,6 +3,8 @@ mod alloc; mod wasm4; use wasm4::*; +mod helpers; + #[rustfmt::skip] const SMILEY: [u8; 8] = [ 0b11000011, @@ -17,14 +19,18 @@ const SMILEY: [u8; 8] = [ #[no_mangle] fn update() { + helpers::fill(3); + unsafe { *DRAW_COLORS = 2 } text("Hello from Rust!", 10, 10); let gamepad = unsafe { *GAMEPAD1 }; if gamepad & BUTTON_1 != 0 { - unsafe { *DRAW_COLORS = 4 } + unsafe { *DRAW_COLORS = 3 } } blit(&SMILEY, 76, 76, 8, 8, BLIT_1BPP); text("Press X to blink", 16, 90); + + if let Some((x, y)) = helpers::draw_mouse() {} }