switch pixels to uints
This commit is contained in:
parent
bdec7e5d4f
commit
f27f31fac8
|
@ -17,12 +17,6 @@ a library for parsing Bitsy game data.
|
|||
|
||||
## 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
|
||||
* position from
|
||||
* position to
|
||||
|
|
101
src/main.rs
101
src/main.rs
|
@ -20,7 +20,7 @@ struct Palette {
|
|||
|
||||
#[derive(Eq, PartialEq)]
|
||||
struct Image {
|
||||
pixels: Vec<bool>, // 64 for SD, 256 for HD
|
||||
pixels: Vec<u8>, // 64 for SD, 256 for HD
|
||||
}
|
||||
|
||||
#[derive(Eq, PartialEq)]
|
||||
|
@ -142,11 +142,34 @@ fn image_from_string(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: 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 }
|
||||
}
|
||||
|
||||
#[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 {
|
||||
image_to_string_opts(image, true)
|
||||
}
|
||||
|
@ -167,40 +190,18 @@ fn image_to_string_opts(image: Image, hd: bool) -> 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]
|
||||
fn test_image_to_string() {
|
||||
let output = image_to_string(Image {
|
||||
pixels: vec![
|
||||
true,false,true,false,true,false,true,false,
|
||||
false,true,false,true,false,true,false,true,
|
||||
true,false,true,false,true,false,true,false,
|
||||
false,true,false,true,false,true,false,true,
|
||||
true,false,true,false,true,false,true,false,
|
||||
false,true,false,true,false,true,false,true,
|
||||
true,false,true,false,true,false,true,false,
|
||||
false,true,false,true,false,true,false,true,
|
||||
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,
|
||||
]
|
||||
});
|
||||
|
||||
|
@ -251,7 +252,7 @@ fn test_tile_from_string() {
|
|||
wall: true,
|
||||
animation_frames: vec![
|
||||
Image {
|
||||
pixels: vec![true; 64]
|
||||
pixels: vec![1; 64]
|
||||
}
|
||||
],
|
||||
};
|
||||
|
@ -288,24 +289,24 @@ fn test_tile_to_string() {
|
|||
wall: false,
|
||||
animation_frames: vec![
|
||||
Image { pixels: vec![
|
||||
true,false,true,false,true,false,true,false,
|
||||
false,true,false,true,false,true,false,true,
|
||||
true,false,true,false,true,false,true,false,
|
||||
false,true,false,true,false,true,false,true,
|
||||
true,false,true,false,true,false,true,false,
|
||||
false,true,false,true,false,true,false,true,
|
||||
true,false,true,false,true,false,true,false,
|
||||
false,true,false,true,false,true,false,true,
|
||||
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,
|
||||
]},
|
||||
Image { pixels: vec![
|
||||
false,true,false,true,false,true,false,true,
|
||||
true,false,true,false,true,false,true,false,
|
||||
false,true,false,true,false,true,false,true,
|
||||
true,false,true,false,true,false,true,false,
|
||||
false,true,false,true,false,true,false,true,
|
||||
true,false,true,false,true,false,true,false,
|
||||
false,true,false,true,false,true,false,true,
|
||||
true,false,true,false,true,false,true,false,
|
||||
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,
|
||||
]},
|
||||
]
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue