From a3e8b1387b35222bf599cdb4d23ead45cc5b5418 Mon Sep 17 00:00:00 2001 From: Jarrod Doyle Date: Mon, 29 Jan 2024 15:03:41 +0000 Subject: [PATCH] Move DirtyRect to utility module --- src/falling_sand/mod.rs | 53 ++--------------------------------------- src/main.rs | 1 + src/util.rs | 50 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 51 deletions(-) create mode 100644 src/util.rs diff --git a/src/falling_sand/mod.rs b/src/falling_sand/mod.rs index 444290f..97f66d2 100644 --- a/src/falling_sand/mod.rs +++ b/src/falling_sand/mod.rs @@ -13,6 +13,8 @@ use bevy::{ }; use rand::Rng; +use crate::util::DirtyRect; + pub struct FallingSandPlugin; impl Plugin for FallingSandPlugin { @@ -58,57 +60,6 @@ fn setup(mut commands: Commands, mut images: ResMut>) { }); } -#[derive(Debug, Default)] -struct DirtyRect { - dirty: bool, - rect: (usize, usize, usize, usize), -} - -impl DirtyRect { - fn new() -> Self { - Self { - dirty: false, - rect: (usize::MAX, usize::MAX, usize::MIN, usize::MIN), - } - } - - fn range_x(&self) -> std::ops::RangeInclusive { - self.rect.0..=self.rect.2 - } - - fn range_y(&self) -> std::ops::RangeInclusive { - self.rect.1..=self.rect.3 - } - - fn reset(&mut self) { - self.dirty = false; - self.rect = (usize::MAX, usize::MAX, usize::MIN, usize::MIN); - } - - fn add_point(&mut self, x: usize, y: usize) { - if x < self.rect.0 { - self.rect.0 = x; - self.dirty = true; - } - if x > self.rect.2 { - self.rect.2 = x; - self.dirty = true; - } - if y < self.rect.1 { - self.rect.1 = y; - self.dirty = true; - } - if y > self.rect.3 { - self.rect.3 = y; - self.dirty = true; - } - } - - fn is_dirty(&self) -> bool { - self.dirty - } -} - #[derive(Component)] pub struct Chunk { width: usize, diff --git a/src/main.rs b/src/main.rs index f007546..743ec89 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ mod falling_sand; +mod util; use bevy::{ diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin}, diff --git a/src/util.rs b/src/util.rs new file mode 100644 index 0000000..3580c34 --- /dev/null +++ b/src/util.rs @@ -0,0 +1,50 @@ +#[derive(Debug, Default)] +pub struct DirtyRect { + dirty: bool, + rect: (usize, usize, usize, usize), +} + +impl DirtyRect { + pub fn new() -> Self { + Self { + dirty: false, + rect: (usize::MAX, usize::MAX, usize::MIN, usize::MIN), + } + } + + pub fn range_x(&self) -> std::ops::RangeInclusive { + self.rect.0..=self.rect.2 + } + + pub fn range_y(&self) -> std::ops::RangeInclusive { + self.rect.1..=self.rect.3 + } + + pub fn reset(&mut self) { + self.dirty = false; + self.rect = (usize::MAX, usize::MAX, usize::MIN, usize::MIN); + } + + pub fn add_point(&mut self, x: usize, y: usize) { + if x < self.rect.0 { + self.rect.0 = x; + self.dirty = true; + } + if x > self.rect.2 { + self.rect.2 = x; + self.dirty = true; + } + if y < self.rect.1 { + self.rect.1 = y; + self.dirty = true; + } + if y > self.rect.3 { + self.rect.3 = y; + self.dirty = true; + } + } + + pub fn is_dirty(&self) -> bool { + self.dirty + } +}