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

@ -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); 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() unsigned long arduboy_generate_random_seed()
{ {
return arduboy.generateRandomSeed(); return arduboy.generateRandomSeed();

View file

@ -12,6 +12,7 @@ extern "C"
void arduboy_draw_pixel(int16_t x, int16_t y, uint8_t color); 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_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_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); 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(); unsigned long arduboy_generate_random_seed();
uint8_t arduboy_get_pixel(uint8_t x, uint8_t y); uint8_t arduboy_get_pixel(uint8_t x, uint8_t y);

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. /// This is the struct to interact in a save way with the Arduboy2 C++ library.
pub struct Arduboy {} pub struct Arduboy {}
impl Arduboy { impl Arduboy {
@ -123,6 +134,9 @@ impl Arduboy {
pub fn fill_rect(&self, x: i16, y: i16, w: u8, h: u8, color: Color) { 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) } 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) { pub fn draw_circle(&self, x: i16, y: i16, r: u8, color: Color) {
unsafe { draw_circle_raw(x, y, r, color as u8) } unsafe { draw_circle_raw(x, y, r, color as u8) }
} }
@ -322,6 +336,18 @@ impl Arduboy {
pub fn invert(&self, inverse: bool) { pub fn invert(&self, inverse: bool) {
unsafe { arduboy_invert(inverse) } 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" { extern "C" {
@ -349,6 +375,9 @@ extern "C" {
#[link_name = "arduboy_draw_circle"] #[link_name = "arduboy_draw_circle"]
fn draw_circle_raw(x: i16, y: i16, r: u8, color: u8); 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"] #[link_name = "arduboy_fill_circle"]
fn fill_circle_raw(x: i16, y: i16, r: u8, color: u8); 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)] #[allow(non_upper_case_globals)]
pub const sound: Sound = Sound {}; pub const sound: Sound = Sound {};
pub use crate::hardware::buttons::*; 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::arduboy_tone::*;
pub use crate::library::arduino::*; pub use crate::library::arduino::*;
pub use crate::library::c::*; pub use crate::library::c::*;