switch pixels to uints

This commit is contained in:
Max Bradbury 2020-04-05 22:28:23 +01:00
parent bdec7e5d4f
commit f27f31fac8
2 changed files with 51 additions and 56 deletions

View File

@ -17,12 +17,6 @@ a library for parsing Bitsy game data.
## todo ## todo
### fix pixels
I made the assumption that a pixel is "0" or "1". nope.
it's an unsigned int and can point to whatever colour you like
as long as it exists in the palette.
### functions ### functions
* position from * position from
* position to * position to

View File

@ -20,7 +20,7 @@ struct Palette {
#[derive(Eq, PartialEq)] #[derive(Eq, PartialEq)]
struct Image { struct Image {
pixels: Vec<bool>, // 64 for SD, 256 for HD pixels: Vec<u8>, // 64 for SD, 256 for HD
} }
#[derive(Eq, PartialEq)] #[derive(Eq, PartialEq)]
@ -142,11 +142,34 @@ fn image_from_string(string: String) -> Image {
let string = string.replace("\n", ""); let string = string.replace("\n", "");
let pixels: Vec<&str> = string.split("").collect(); 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 // the above seems to add an extra "" at the start and end of the vec, so strip them below
let pixels: Vec<bool> = pixels[1..(pixels.len() - 1)].iter().map(|&char| {char == "1"}).collect(); let pixels = &pixels[1..(pixels.len() - 1)];
let pixels: Vec<u8> = pixels.iter().map(|&pixel| { pixel.parse::<u8>().unwrap() }).collect();
Image { pixels } Image { pixels }
} }
#[test]
fn test_image_from_string() {
let output = image_from_string(
"11111111\n11001111\n10111111\n11111111\n11111111\n11111111\n11111111\n11111111".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)
}
fn image_to_string(image: Image) -> String { fn image_to_string(image: Image) -> String {
image_to_string_opts(image, true) image_to_string_opts(image, true)
} }
@ -167,40 +190,18 @@ fn image_to_string_opts(image: Image, hd: bool) -> String {
string string
} }
#[test]
fn test_image_from_string() {
let output = image_from_string(
"11111111\n11001111\n10111111\n11111111\n11111111\n11111111\n11111111\n11111111".to_string()
);
let expected = Image {
pixels: vec![
true,true,true,true,true,true,true,true,
true,true,false,false,true,true,true,true,
true,false,true,true,true,true,true,true,
true,true,true,true,true,true,true,true,
true,true,true,true,true,true,true,true,
true,true,true,true,true,true,true,true,
true,true,true,true,true,true,true,true,
true,true,true,true,true,true,true,true,
]
};
assert_eq!(output, expected)
}
#[test] #[test]
fn test_image_to_string() { fn test_image_to_string() {
let output = image_to_string(Image { let output = image_to_string(Image {
pixels: vec![ pixels: vec![
true,false,true,false,true,false,true,false, 1,0,1,0,1,0,1,0,
false,true,false,true,false,true,false,true, 0,1,0,1,0,1,0,1,
true,false,true,false,true,false,true,false, 1,0,1,0,1,0,1,0,
false,true,false,true,false,true,false,true, 0,1,0,1,0,1,0,1,
true,false,true,false,true,false,true,false, 1,0,1,0,1,0,1,0,
false,true,false,true,false,true,false,true, 0,1,0,1,0,1,0,1,
true,false,true,false,true,false,true,false, 1,0,1,0,1,0,1,0,
false,true,false,true,false,true,false,true, 0,1,0,1,0,1,0,1,
] ]
}); });
@ -251,7 +252,7 @@ fn test_tile_from_string() {
wall: true, wall: true,
animation_frames: vec![ animation_frames: vec![
Image { Image {
pixels: vec![true; 64] pixels: vec![1; 64]
} }
], ],
}; };
@ -288,24 +289,24 @@ fn test_tile_to_string() {
wall: false, wall: false,
animation_frames: vec![ animation_frames: vec![
Image { pixels: vec![ Image { pixels: vec![
true,false,true,false,true,false,true,false, 1,0,1,0,1,0,1,0,
false,true,false,true,false,true,false,true, 0,1,0,1,0,1,0,1,
true,false,true,false,true,false,true,false, 1,0,1,0,1,0,1,0,
false,true,false,true,false,true,false,true, 0,1,0,1,0,1,0,1,
true,false,true,false,true,false,true,false, 1,0,1,0,1,0,1,0,
false,true,false,true,false,true,false,true, 0,1,0,1,0,1,0,1,
true,false,true,false,true,false,true,false, 1,0,1,0,1,0,1,0,
false,true,false,true,false,true,false,true, 0,1,0,1,0,1,0,1,
]}, ]},
Image { pixels: vec![ Image { pixels: vec![
false,true,false,true,false,true,false,true, 0,1,0,1,0,1,0,1,
true,false,true,false,true,false,true,false, 1,0,1,0,1,0,1,0,
false,true,false,true,false,true,false,true, 0,1,0,1,0,1,0,1,
true,false,true,false,true,false,true,false, 1,0,1,0,1,0,1,0,
false,true,false,true,false,true,false,true, 0,1,0,1,0,1,0,1,
true,false,true,false,true,false,true,false, 1,0,1,0,1,0,1,0,
false,true,false,true,false,true,false,true, 0,1,0,1,0,1,0,1,
true,false,true,false,true,false,true,false, 1,0,1,0,1,0,1,0,
]}, ]},
] ]
}); });