From 14afe60f8e93e2ed6c066e9ed47aa20affa6c02e Mon Sep 17 00:00:00 2001 From: Zenn Date: Sun, 6 Aug 2023 20:37:24 +0200 Subject: [PATCH] added support for the collide commands --- .../src/library/arduboy/arduboy.cpp | 4 +++ Wrapper-Project/src/library/arduboy/arduboy.h | 1 + arduboy-rust/src/library/arduboy.rs | 29 +++++++++++++++++++ arduboy-rust/src/prelude.rs | 2 +- 4 files changed, 35 insertions(+), 1 deletion(-) diff --git a/Wrapper-Project/src/library/arduboy/arduboy.cpp b/Wrapper-Project/src/library/arduboy/arduboy.cpp index f19d9e3..93f6ec1 100644 --- a/Wrapper-Project/src/library/arduboy/arduboy.cpp +++ b/Wrapper-Project/src/library/arduboy/arduboy.cpp @@ -41,6 +41,10 @@ void arduboy_fill_rect(int16_t x, int16_t y, uint8_t w, uint8_t h, uint8_t color { arduboy.fillRect(x, y, w, h, color); } +void arduboy_draw_rect(int16_t x, int16_t y, uint8_t w, uint8_t h, uint8_t color) +{ + arduboy.drawRect(x, y, w, h, color); +} unsigned long arduboy_generate_random_seed() { return arduboy.generateRandomSeed(); diff --git a/Wrapper-Project/src/library/arduboy/arduboy.h b/Wrapper-Project/src/library/arduboy/arduboy.h index 723a989..710ab79 100644 --- a/Wrapper-Project/src/library/arduboy/arduboy.h +++ b/Wrapper-Project/src/library/arduboy/arduboy.h @@ -12,6 +12,7 @@ extern "C" void arduboy_draw_pixel(int16_t x, int16_t y, uint8_t color); void arduboy_draw_circle(int16_t x, int16_t y, uint8_t r, uint8_t color); void arduboy_fill_circle(int16_t x, int16_t y, uint8_t r, uint8_t color); + void arduboy_draw_rect(int16_t x, int16_t y, uint8_t w, uint8_t h, uint8_t color); void arduboy_fill_rect(int16_t x, int16_t y, uint8_t w, uint8_t h, uint8_t color); unsigned long arduboy_generate_random_seed(); uint8_t arduboy_get_pixel(uint8_t x, uint8_t y); diff --git a/arduboy-rust/src/library/arduboy.rs b/arduboy-rust/src/library/arduboy.rs index 2923427..58637f0 100644 --- a/arduboy-rust/src/library/arduboy.rs +++ b/arduboy-rust/src/library/arduboy.rs @@ -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); diff --git a/arduboy-rust/src/prelude.rs b/arduboy-rust/src/prelude.rs index d455bab..36e2b02 100644 --- a/arduboy-rust/src/prelude.rs +++ b/arduboy-rust/src/prelude.rs @@ -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::*;