fix test images and add test sprite

This commit is contained in:
Max Bradbury 2020-04-06 08:27:08 +01:00
parent 2d2abcdd56
commit 4bfb82dba0
1 changed files with 70 additions and 52 deletions

View File

@ -4,29 +4,62 @@ use std::collections::HashMap;
const IMAGE_DIMENSION_SD: usize = 8; const IMAGE_DIMENSION_SD: usize = 8;
const IMAGE_DIMENSION_HD: usize = 16; const IMAGE_DIMENSION_HD: usize = 16;
const TEST_IMAGE_CHEQUERS_1: Image = Image { fn test_image_chequers_1() -> Image {
pixels: vec![ Image {
1,0,1,0,1,0,1,0, pixels: vec![
0,1,0,1,0,1,0,1, 1,0,1,0,1,0,1,0,
1,0,1,0,1,0,1,0, 0,1,0,1,0,1,0,1,
0,1,0,1,0,1,0,1, 1,0,1,0,1,0,1,0,
1,0,1,0,1,0,1,0, 0,1,0,1,0,1,0,1,
0,1,0,1,0,1,0,1, 1,0,1,0,1,0,1,0,
1,0,1,0,1,0,1,0, 0,1,0,1,0,1,0,1,
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![ fn test_image_chequers_2() -> Image {
0,1,0,1,0,1,0,1, Image {
1,0,1,0,1,0,1,0, pixels: vec![
0,1,0,1,0,1,0,1, 0,1,0,1,0,1,0,1,
1,0,1,0,1,0,1,0, 1,0,1,0,1,0,1,0,
0,1,0,1,0,1,0,1, 0,1,0,1,0,1,0,1,
1,0,1,0,1,0,1,0, 1,0,1,0,1,0,1,0,
0,1,0,1,0,1,0,1, 0,1,0,1,0,1,0,1,
1,0,1,0,1,0,1,0, 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)] #[derive(Debug, Eq, PartialEq)]
struct Colour { struct Colour {
@ -178,7 +211,7 @@ fn image_to_string_opts(image: Image, hd: bool) -> String {
#[test] #[test]
fn test_image_to_string() { 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(); let expected = "10101010\n01010101\n10101010\n01010101\n10101010\n01010101\n10101010\n01010101".to_string();
assert_eq!(output, expected); assert_eq!(output, expected);
@ -266,8 +299,8 @@ fn test_tile_to_string() {
name: Some("chequers".to_string()), name: Some("chequers".to_string()),
wall: false, wall: false,
animation_frames: vec![ animation_frames: vec![
TEST_IMAGE_CHEQUERS_1, test_image_chequers_1(),
TEST_IMAGE_CHEQUERS_2, test_image_chequers_2(),
] ]
}); });
@ -434,45 +467,30 @@ fn sprite_from_string(string: String) -> Sprite {
#[test] #[test]
fn test_sprite_from_string() { 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 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 { let expected = test_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
}
};
assert_eq!(output, expected); assert_eq!(output, expected);
} }
fn sprite_to_string(sprite: Sprite) -> String { fn sprite_to_string(sprite: Sprite) -> String {
format!( format!(
"SPR {}\n{}\n{}{}\nPOS {}", "SPR {}\n{}{}{}\nPOS {}",
sprite.id, sprite.id,
animation_frames_to_string(sprite.animation_frames), animation_frames_to_string(sprite.animation_frames),
if sprite.name.is_some() {format!("NAME {}", sprite.name.unwrap())} else {"".to_string()}, if sprite.name.is_some() {format!("\nNAME {}", sprite.name.unwrap())} else {"".to_string()},
if sprite.dialogue.is_some() {format!("DLG {}", sprite.dialogue.unwrap())} else {"".to_string()}, if sprite.dialogue.is_some() {format!("\nDLG {}", sprite.dialogue.unwrap())} else {"".to_string()},
position_to_string(sprite.position), 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 { // fn game_from_string(game: String ) -> Game {
// // probably needs to split the game data into different segments starting from the end // // probably needs to split the game data into different segments starting from the end
// // e.g. VAR... then END... then DLG... // // e.g. VAR... then END... then DLG...