From a3bfdc68b18cbccac6b6ec86e231d4f31fb5ed84 Mon Sep 17 00:00:00 2001 From: Max Bradbury Date: Sun, 12 Apr 2020 12:48:07 +0100 Subject: [PATCH] image module; mocks module --- src/image.rs | 67 ++++++++++++++++++++++++ src/lib.rs | 143 +++------------------------------------------------ src/mocks.rs | 69 +++++++++++++++++++++++++ 3 files changed, 144 insertions(+), 135 deletions(-) create mode 100644 src/image.rs create mode 100644 src/mocks.rs diff --git a/src/image.rs b/src/image.rs new file mode 100644 index 0000000..081e630 --- /dev/null +++ b/src/image.rs @@ -0,0 +1,67 @@ +use crate::mocks; + +#[derive(Debug, Eq, PartialEq)] +pub struct Image { + pub(crate) pixels: Vec, // 64 for SD, 256 for HD +} + +impl From for Image { + #[inline] + fn from(string: String) -> Image { + let string = string.replace("\n", ""); + let pixels: Vec<&str> = string.split("").collect(); + // the above seems to add an extra "" at the start and end of the vec, so strip them below + let pixels = &pixels[1..(pixels.len() - 1)]; + let pixels: Vec = pixels.iter().map(|&pixel| { + pixel.parse::().unwrap() + }).collect(); + + Image { pixels } + } +} + +impl ToString for Image { + #[inline] + fn to_string(&self) -> String { + let mut string = String::new(); + + let sqrt = (self.pixels.len() as f64).sqrt() as usize; // 8 for SD, 16 for HD + for line in self.pixels.chunks(sqrt) { + for pixel in line { + string.push_str(&format!("{}", *pixel)); + } + string.push('\n'); + } + + string.pop(); // remove trailing newline + + string + } +} + +#[test] +fn test_image_from_string() { + let output = Image::from(include_str!("../test/resources/image").to_string()); + + let expected = Image { + pixels: vec![ + 1,1,1,1,1,1,1,1, + 1,1,0,0,1,1,1,1, + 1,0,1,1,1,1,1,1, + 1,1,1,1,1,1,1,1, + 1,1,1,1,1,1,1,1, + 1,1,1,1,1,1,1,1, + 1,1,1,1,1,1,1,1, + 1,1,1,1,1,1,1,1, + ] + }; + + assert_eq!(output, expected) +} + +#[test] +fn test_image_to_string() { + let output = mocks::image::chequers_1().to_string(); + let expected = include_str!("../test/resources/image-chequers-1").to_string(); + assert_eq!(output, expected); +} diff --git a/src/lib.rs b/src/lib.rs index a3c47cc..eddc1ab 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,13 +1,11 @@ pub mod colour; pub mod palette; +pub mod image; +pub mod mocks; use colour::Colour; use palette::Palette; - -#[derive(Debug, Eq, PartialEq)] -struct Image { - pixels: Vec, // 64 for SD, 256 for HD -} +use image::Image; #[derive(Debug, Eq, PartialEq)] struct Tile { @@ -53,7 +51,7 @@ struct Sprite { /// avatar is a "sprite" in the game data but with a specific id #[derive(Debug, Eq, PartialEq)] -struct Avatar { +pub struct Avatar { animation_frames: Vec, room: String, /// room id position: Position, @@ -114,69 +112,6 @@ struct Game { variables: Vec, } -fn example_image_chequers_1() -> Image { - Image { - pixels: vec![ - 1,0,1,0,1,0,1,0, - 0,1,0,1,0,1,0,1, - 1,0,1,0,1,0,1,0, - 0,1,0,1,0,1,0,1, - 1,0,1,0,1,0,1,0, - 0,1,0,1,0,1,0,1, - 1,0,1,0,1,0,1,0, - 0,1,0,1,0,1,0,1, - ] - } -} - -fn example_image_chequers_2() -> Image { - Image { - pixels: vec![ - 0,1,0,1,0,1,0,1, - 1,0,1,0,1,0,1,0, - 0,1,0,1,0,1,0,1, - 1,0,1,0,1,0,1,0, - 0,1,0,1,0,1,0,1, - 1,0,1,0,1,0,1,0, - 0,1,0,1,0,1,0,1, - 1,0,1,0,1,0,1,0, - ] - } -} - -fn example_avatar() -> Avatar { - Avatar { - animation_frames: vec![ - Image { - pixels: vec![ - 0,0,0,0,0,0,0,0, - 0,0,1,1,1,1,0,0, - 0,1,1,1,1,1,1,0, - 1,1,1,0,1,1,1,0, - 1,0,0,1,1,0,0,1, - 1,1,1,1,1,1,1,1, - 1,1,1,1,1,1,1,1, - 0,1,1,1,1,1,1,0, - ] - }, - Image { - pixels: vec![ - 0,0,0,0,0,0,0,0, - 0,0,0,0,0,0,0,0, - 0,0,1,1,1,1,0,0, - 0,1,1,1,1,1,1,0, - 1,1,1,0,1,1,1,0, - 1,0,0,1,1,0,0,1, - 1,1,1,1,1,1,1,1, - 0,1,1,1,0,1,1,0, - ] - }, - ], - room: "0".to_string(), - position: Position { x: 2, y: 5 } - } -} - fn example_sprite() -> Sprite { Sprite { id: "a".to_string(), @@ -416,67 +351,6 @@ fn example_game_default() -> Game { } } -impl From for Image { - #[inline] - fn from(string: String) -> Image { - let string = string.replace("\n", ""); - let pixels: Vec<&str> = string.split("").collect(); - // the above seems to add an extra "" at the start and end of the vec, so strip them below - let pixels = &pixels[1..(pixels.len() - 1)]; - let pixels: Vec = pixels.iter().map(|&pixel| { - pixel.parse::().unwrap() - }).collect(); - - Image { pixels } - } -} - -#[test] -fn test_image_from_string() { - let output = Image::from(include_str!("../test/resources/image").to_string()); - - let expected = Image { - pixels: vec![ - 1,1,1,1,1,1,1,1, - 1,1,0,0,1,1,1,1, - 1,0,1,1,1,1,1,1, - 1,1,1,1,1,1,1,1, - 1,1,1,1,1,1,1,1, - 1,1,1,1,1,1,1,1, - 1,1,1,1,1,1,1,1, - 1,1,1,1,1,1,1,1, - ] - }; - - assert_eq!(output, expected) -} - -impl ToString for Image { - #[inline] - fn to_string(&self) -> String { - let mut string = String::new(); - - let sqrt = (self.pixels.len() as f64).sqrt() as usize; // 8 for SD, 16 for HD - for line in self.pixels.chunks(sqrt) { - for pixel in line { - string.push_str(&format!("{}", *pixel)); - } - string.push('\n'); - } - - string.pop(); // remove trailing newline - - string - } -} - -#[test] -fn test_image_to_string() { - let output = example_image_chequers_1().to_string(); - let expected = include_str!("../test/resources/image-chequers-1").to_string(); - assert_eq!(output, expected); -} - pub trait AnimationFrames { fn to_string(&self) -> String; } @@ -571,8 +445,8 @@ fn test_tile_to_string() { name: Some("chequers".to_string()), wall: false, animation_frames: vec![ - example_image_chequers_1(), - example_image_chequers_2(), + mocks::image::chequers_1(), + mocks::image::chequers_2(), ] }.to_string(); @@ -633,8 +507,7 @@ fn test_avatar_from_string() { include_str!("../test/resources/avatar").to_string() ); - let expected = example_avatar(); - assert_eq!(output, expected); + assert_eq!(output, mocks::avatar()); } impl ToString for Avatar { @@ -651,7 +524,7 @@ impl ToString for Avatar { #[test] fn test_avatar_to_string() { - assert_eq!(example_avatar().to_string(), include_str!("../test/resources/avatar")); + assert_eq!(mocks::avatar().to_string(), include_str!("../test/resources/avatar")); } impl From for Sprite { diff --git a/src/mocks.rs b/src/mocks.rs new file mode 100644 index 0000000..75991b6 --- /dev/null +++ b/src/mocks.rs @@ -0,0 +1,69 @@ +use crate::{Avatar, Position}; +use crate::image::Image; + +pub mod image { + use crate::image::Image; + + pub(crate) fn chequers_1() -> Image { + Image { + pixels: vec![ + 1, 0, 1, 0, 1, 0, 1, 0, + 0, 1, 0, 1, 0, 1, 0, 1, + 1, 0, 1, 0, 1, 0, 1, 0, + 0, 1, 0, 1, 0, 1, 0, 1, + 1, 0, 1, 0, 1, 0, 1, 0, + 0, 1, 0, 1, 0, 1, 0, 1, + 1, 0, 1, 0, 1, 0, 1, 0, + 0, 1, 0, 1, 0, 1, 0, 1, + ] + } + } + + pub fn chequers_2() -> Image { + Image { + pixels: vec![ + 0,1,0,1,0,1,0,1, + 1,0,1,0,1,0,1,0, + 0,1,0,1,0,1,0,1, + 1,0,1,0,1,0,1,0, + 0,1,0,1,0,1,0,1, + 1,0,1,0,1,0,1,0, + 0,1,0,1,0,1,0,1, + 1,0,1,0,1,0,1,0, + ] + } + } +} + +pub fn avatar() -> Avatar { + Avatar { + animation_frames: vec![ + Image { + pixels: vec![ + 0,0,0,0,0,0,0,0, + 0,0,1,1,1,1,0,0, + 0,1,1,1,1,1,1,0, + 1,1,1,0,1,1,1,0, + 1,0,0,1,1,0,0,1, + 1,1,1,1,1,1,1,1, + 1,1,1,1,1,1,1,1, + 0,1,1,1,1,1,1,0, + ] + }, + Image { + pixels: vec![ + 0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0, + 0,0,1,1,1,1,0,0, + 0,1,1,1,1,1,1,0, + 1,1,1,0,1,1,1,0, + 1,0,0,1,1,0,0,1, + 1,1,1,1,1,1,1,1, + 0,1,1,1,0,1,1,0, + ] + }, + ], + room: "0".to_string(), + position: Position { x: 2, y: 5 } + } +}