Add some helpers, minor tweaks

This commit is contained in:
Stephen Seo 2023-04-29 11:41:05 +09:00
parent 0961572000
commit 1febaba023
2 changed files with 31 additions and 1 deletions

24
src/helpers.rs Normal file
View file

@ -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
}
}

View file

@ -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() {}
}