Actually write to a file and also include palette assignments

This commit is contained in:
Gwilym Kuiper 2021-04-20 20:51:49 +01:00
parent 4daafdec20
commit d1412fb709
3 changed files with 25 additions and 6 deletions

View file

@ -6,10 +6,12 @@ fn main() -> () {
let args: Vec<_> = env::args().collect();
let file_path = &args[1];
let output_path = &args[2];
convert_image(&ImageConverterConfig {
transparent_colour: None,
tile_size: TileSize::Tile8,
input_image: file_path.into(),
output_file: "".into(),
output_file: output_path.into(),
output_name: "HELLO".to_owned(),
});
}

View file

@ -1,3 +1,5 @@
use std::fs::File;
use std::io::BufWriter;
use std::path::PathBuf;
mod colour;
@ -29,6 +31,7 @@ pub struct ImageConverterConfig {
pub tile_size: TileSize,
pub input_image: PathBuf,
pub output_file: PathBuf,
pub output_name: String,
}
pub fn convert_image(settings: &ImageConverterConfig) {
@ -42,16 +45,15 @@ pub fn convert_image(settings: &ImageConverterConfig) {
let optimiser = optimiser_for_image(&image, tile_size);
let optimisation_results = optimiser.optimise_palettes(settings.transparent_colour);
let stdout = std::io::stdout();
let handle = stdout.lock();
let mut writer = std::io::BufWriter::new(handle);
let output_file = File::create(&settings.output_file).expect("Failed to create file");
let mut writer = BufWriter::new(output_file);
rust_generator::generate_code(
&mut writer,
&optimisation_results,
&image,
settings.tile_size,
"HELLO",
&settings.output_name,
)
.expect("Failed to write data");
}

View file

@ -74,7 +74,22 @@ pub(crate) fn generate_code(
}
}
write!(output, "];\n")?;
write!(output, "];\n\n")?;
write!(
output,
"pub const {}_PALETTE_ASSIGNMENT: &'static [u8] = &[",
tile_name
)?;
for (i, assignment) in results.assignments.iter().enumerate() {
if i % 16 == 0 {
write!(output, "\n ")?;
}
write!(output, "{}, ", assignment)?;
}
write!(output, "\n];\n")?;
Ok(())
}