added support for the collide commands

This commit is contained in:
Zenn 2023-08-06 20:37:24 +02:00
parent 50819abc50
commit 14afe60f8e
4 changed files with 35 additions and 1 deletions

View file

@ -39,6 +39,17 @@ impl Not for Color {
}
}
}
pub struct Rect {
x: i16,
y: i16,
width: u8,
height: u8,
}
pub struct Point {
x: i16,
y: i16,
}
/// This is the struct to interact in a save way with the Arduboy2 C++ library.
pub struct Arduboy {}
impl Arduboy {
@ -123,6 +134,9 @@ impl Arduboy {
pub fn fill_rect(&self, x: i16, y: i16, w: u8, h: u8, color: Color) {
unsafe { fill_rect_raw(x, y, w, h, color as u8) }
}
pub fn draw_rect(&self, x: i16, y: i16, w: u8, h: u8, color: Color) {
unsafe { draw_rect_raw(x, y, w, h, color as u8) }
}
pub fn draw_circle(&self, x: i16, y: i16, r: u8, color: Color) {
unsafe { draw_circle_raw(x, y, r, color as u8) }
}
@ -322,6 +336,18 @@ impl Arduboy {
pub fn invert(&self, inverse: bool) {
unsafe { arduboy_invert(inverse) }
}
pub fn collide_point(point: Point, rect: Rect) -> bool {
point.x >= rect.x
&& point.x < rect.x + rect.width as i16
&& point.y >= rect.y
&& point.y < rect.y + rect.height as i16
}
pub fn collide_rect(rect1: Rect, rect2: Rect) -> bool {
!(rect2.x >= rect1.x + rect1.width as i16
|| rect2.x + rect2.width as i16 <= rect1.x
|| rect2.y >= rect1.y + rect1.height as i16
|| rect2.y + rect2.height as i16 <= rect1.y)
}
}
extern "C" {
@ -349,6 +375,9 @@ extern "C" {
#[link_name = "arduboy_draw_circle"]
fn draw_circle_raw(x: i16, y: i16, r: u8, color: u8);
#[link_name = "arduboy_draw_rect"]
fn draw_rect_raw(x: i16, y: i16, w: u8, h: u8, color: u8);
#[link_name = "arduboy_fill_circle"]
fn fill_circle_raw(x: i16, y: i16, r: u8, color: u8);

View file

@ -12,7 +12,7 @@ pub const arduboy: Arduboy = Arduboy {};
#[allow(non_upper_case_globals)]
pub const sound: Sound = Sound {};
pub use crate::hardware::buttons::*;
pub use crate::library::arduboy::{Color, FONT_SIZE, HEIGHT, WIDTH};
pub use crate::library::arduboy::{Color, Point, Rect, FONT_SIZE, HEIGHT, WIDTH};
pub use crate::library::arduboy_tone::*;
pub use crate::library::arduino::*;
pub use crate::library::c::*;