From 4bfb82dba0ba6e52645e3eca8c06766baa493475 Mon Sep 17 00:00:00 2001 From: Max Bradbury Date: Mon, 6 Apr 2020 08:27:08 +0100 Subject: [PATCH] fix test images and add test sprite --- src/main.rs | 122 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 70 insertions(+), 52 deletions(-) diff --git a/src/main.rs b/src/main.rs index 4bd8117..23c4de5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,29 +4,62 @@ use std::collections::HashMap; const IMAGE_DIMENSION_SD: usize = 8; const IMAGE_DIMENSION_HD: usize = 16; -const TEST_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 test_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, + ] + } +} -const TEST_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 test_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 test_sprite() -> Sprite { + Sprite { + id: "a".to_string(), + name: Some("hatch".to_string()), + animation_frames: vec![ + Image { + pixels: vec![ + 0,0,0,0,0,0,0,0, + 0,1,1,1,1,0,0,0, + 0,1,0,0,1,0,0,0, + 0,0,1,1,1,1,0,0, + 0,0,1,1,1,1,0,0, + 0,1,0,1,1,1,1,0, + 0,1,0,1,1,1,1,0, + 0,1,1,0,1,1,1,1, + ] + } + ], + dialogue: Some("SPR_0".to_string()), + position: Position { + room: "4".to_string(), + x: 9, + y: 7 + } + } +} #[derive(Debug, Eq, PartialEq)] struct Colour { @@ -178,7 +211,7 @@ fn image_to_string_opts(image: Image, hd: bool) -> String { #[test] fn test_image_to_string() { - let output = image_to_string(TEST_IMAGE_CHEQUERS_1); + let output = image_to_string(test_image_chequers_1()); let expected = "10101010\n01010101\n10101010\n01010101\n10101010\n01010101\n10101010\n01010101".to_string(); assert_eq!(output, expected); @@ -266,8 +299,8 @@ fn test_tile_to_string() { name: Some("chequers".to_string()), wall: false, animation_frames: vec![ - TEST_IMAGE_CHEQUERS_1, - TEST_IMAGE_CHEQUERS_2, + test_image_chequers_1(), + test_image_chequers_2(), ] }); @@ -434,45 +467,30 @@ fn sprite_from_string(string: String) -> Sprite { #[test] fn test_sprite_from_string() { let output = sprite_from_string("SPR a\n00000000\n01111000\n01001000\n00111100\n00111100\n01011110\n01011110\n01101111\nNAME hatch\nDLG SPR_0\nPOS 4 9,7".to_string()); - let expected = Sprite { - id: "a".to_string(), - name: Some("hatch".to_string()), - animation_frames: vec![ - Image { - pixels: vec![ - 0,0,0,0,0,0,0,0, - 0,1,1,1,1,0,0,0, - 0,1,0,0,1,0,0,0, - 0,0,1,1,1,1,0,0, - 0,0,1,1,1,1,0,0, - 0,1,0,1,1,1,1,0, - 0,1,0,1,1,1,1,0, - 0,1,1,0,1,1,1,1, - ] - } - ], - dialogue: Some("SPR_0".to_string()), - position: Position { - room: "4".to_string(), - x: 9, - y: 7 - } - }; + let expected = test_sprite(); assert_eq!(output, expected); } fn sprite_to_string(sprite: Sprite) -> String { format!( - "SPR {}\n{}\n{}{}\nPOS {}", + "SPR {}\n{}{}{}\nPOS {}", sprite.id, animation_frames_to_string(sprite.animation_frames), - if sprite.name.is_some() {format!("NAME {}", sprite.name.unwrap())} else {"".to_string()}, - if sprite.dialogue.is_some() {format!("DLG {}", sprite.dialogue.unwrap())} else {"".to_string()}, + if sprite.name.is_some() {format!("\nNAME {}", sprite.name.unwrap())} else {"".to_string()}, + if sprite.dialogue.is_some() {format!("\nDLG {}", sprite.dialogue.unwrap())} else {"".to_string()}, position_to_string(sprite.position), ) } +#[test] +fn test_sprite_to_string() { + let output = sprite_to_string(test_sprite()); + let expected = "SPR a\n00000000\n01111000\n01001000\n00111100\n00111100\n01011110\n01011110\n01101111\nNAME hatch\nDLG SPR_0\nPOS 4 9,7".to_string(); + + assert_eq!(output, expected); +} + // fn game_from_string(game: String ) -> Game { // // probably needs to split the game data into different segments starting from the end // // e.g. VAR... then END... then DLG...