diff --git a/examples/game_of_life/src/main.rs b/examples/game_of_life/src/main.rs index 555d5ca..49cc4ca 100644 --- a/examples/game_of_life/src/main.rs +++ b/examples/game_of_life/src/main.rs @@ -39,8 +39,8 @@ fn main() { fn iteration(field: PixelGrid) -> PixelGrid { let mut next = field.clone(); - for x in 0..field.width { - for y in 0..field.height { + for x in 0..field.width() { + for y in 0..field.height() { let old_state = field.get(x, y); let neighbors = count_neighbors(&field, x as i32, y as i32); @@ -64,8 +64,8 @@ fn count_neighbors(field: &PixelGrid, x: i32, y: i32) -> i32 { if nx < 0 || ny < 0 - || nx >= field.width as i32 - || ny >= field.height as i32 + || nx >= field.width() as i32 + || ny >= field.height() as i32 { continue; // pixels outside the grid do not count } @@ -85,8 +85,8 @@ fn make_random_field(probability: f64) -> PixelGrid { let mut field = PixelGrid::max_sized(); let mut rng = rand::thread_rng(); let d = distributions::Bernoulli::new(probability).unwrap(); - for x in 0..field.width { - for y in 0..field.height { + for x in 0..field.width() { + for y in 0..field.height() { field.set(x, y, rng.sample(d)); } } diff --git a/servicepoint2/src/byte_grid.rs b/servicepoint2/src/byte_grid.rs index a54dd3a..674c208 100644 --- a/servicepoint2/src/byte_grid.rs +++ b/servicepoint2/src/byte_grid.rs @@ -1,10 +1,8 @@ /// A 2D grid of bytes #[derive(Debug, Clone, PartialEq)] pub struct ByteGrid { - /// Size in the x-axis - pub width: usize, - /// Size in the y-axis - pub height: usize, + width: usize, + height: usize, data: Vec, } @@ -57,6 +55,16 @@ impl ByteGrid { pub fn mut_data_ref(&mut self) -> &mut [u8] { self.data.as_mut_slice() } + + /// the size in x-direction + pub fn width(&self) -> usize { + self.width + } + + /// the height in y-direction + pub fn height(&self) -> usize { + self.height + } } impl From for Vec { diff --git a/servicepoint2/src/command.rs b/servicepoint2/src/command.rs index 0e89440..635b1a2 100644 --- a/servicepoint2/src/command.rs +++ b/servicepoint2/src/command.rs @@ -74,8 +74,8 @@ impl From for Packet { CommandCode::CharBrightness.into(), x, y, - grid.width as u16, - grid.height as u16, + grid.width() as u16, + grid.height() as u16, ), grid.into(), ), @@ -95,11 +95,11 @@ impl From for Packet { compression, ) => { debug_assert_eq!(pixel_x % 8, 0); - debug_assert_eq!(pixels.width % 8, 0); + debug_assert_eq!(pixels.width() % 8, 0); let tile_x = pixel_x / TILE_SIZE; - let tile_w = pixels.width as u16 / TILE_SIZE; - let pixel_h = pixels.height as u16; + let tile_w = pixels.width() as u16 / TILE_SIZE; + let pixel_h = pixels.height() as u16; let payload = into_compressed(compression, pixels.into()); let command = match compression { CompressionCode::Uncompressed => { @@ -153,8 +153,8 @@ impl From for Packet { CommandCode::Cp437Data.into(), x, y, - grid.width as u16, - grid.height as u16, + grid.width() as u16, + grid.height() as u16, ), grid.into(), ), diff --git a/servicepoint2/src/pixel_grid.rs b/servicepoint2/src/pixel_grid.rs index 2ab8595..0ee4894 100644 --- a/servicepoint2/src/pixel_grid.rs +++ b/servicepoint2/src/pixel_grid.rs @@ -3,10 +3,8 @@ use crate::{BitVec, PIXEL_HEIGHT, PIXEL_WIDTH}; /// A grid of pixels stored in packed bytes. #[derive(Debug, Clone, PartialEq)] pub struct PixelGrid { - /// the width in pixels - pub width: usize, - /// the height in pixels - pub height: usize, + width: usize, + height: usize, bit_vec: BitVec, } @@ -80,6 +78,16 @@ impl PixelGrid { pub fn mut_data_ref(&mut self) -> &mut [u8] { self.bit_vec.mut_data_ref() } + + /// the size in x-direction in pixels + pub fn width(&self) -> usize { + self.width + } + + /// the height in y-direction in pixels + pub fn height(&self) -> usize { + self.height + } } impl From for Vec {