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 args: Vec<_> = env::args().collect();
let file_path = &args[1]; let file_path = &args[1];
let output_path = &args[2];
convert_image(&ImageConverterConfig { convert_image(&ImageConverterConfig {
transparent_colour: None, transparent_colour: None,
tile_size: TileSize::Tile8, tile_size: TileSize::Tile8,
input_image: file_path.into(), 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; use std::path::PathBuf;
mod colour; mod colour;
@ -29,6 +31,7 @@ pub struct ImageConverterConfig {
pub tile_size: TileSize, pub tile_size: TileSize,
pub input_image: PathBuf, pub input_image: PathBuf,
pub output_file: PathBuf, pub output_file: PathBuf,
pub output_name: String,
} }
pub fn convert_image(settings: &ImageConverterConfig) { 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 optimiser = optimiser_for_image(&image, tile_size);
let optimisation_results = optimiser.optimise_palettes(settings.transparent_colour); let optimisation_results = optimiser.optimise_palettes(settings.transparent_colour);
let stdout = std::io::stdout(); let output_file = File::create(&settings.output_file).expect("Failed to create file");
let handle = stdout.lock(); let mut writer = BufWriter::new(output_file);
let mut writer = std::io::BufWriter::new(handle);
rust_generator::generate_code( rust_generator::generate_code(
&mut writer, &mut writer,
&optimisation_results, &optimisation_results,
&image, &image,
settings.tile_size, settings.tile_size,
"HELLO", &settings.output_name,
) )
.expect("Failed to write data"); .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(()) Ok(())
} }