2021-07-22 07:07:09 +10:00
|
|
|
use std::fmt::Write;
|
2021-04-21 05:41:04 +10:00
|
|
|
|
|
|
|
use crate::image_loader::Image;
|
|
|
|
use crate::palette16::Palette16OptimisationResults;
|
|
|
|
use crate::TileSize;
|
|
|
|
|
|
|
|
pub(crate) fn generate_code(
|
|
|
|
output: &mut dyn Write,
|
2021-07-22 06:01:24 +10:00
|
|
|
output_variable_name: &str,
|
2021-04-21 05:41:04 +10:00
|
|
|
results: &Palette16OptimisationResults,
|
|
|
|
image: &Image,
|
2021-07-22 07:07:09 +10:00
|
|
|
image_filename: &str,
|
2021-04-21 05:41:04 +10:00
|
|
|
tile_size: TileSize,
|
2021-06-06 02:47:13 +10:00
|
|
|
crate_prefix: String,
|
2021-07-22 07:07:09 +10:00
|
|
|
) {
|
|
|
|
writeln!(output, "#[allow(non_upper_case_globals)]").unwrap();
|
2021-07-22 07:46:22 +10:00
|
|
|
writeln!(
|
|
|
|
output,
|
|
|
|
"pub const {}: {}::display::tile_data::TileData = {{",
|
|
|
|
output_variable_name, crate_prefix
|
|
|
|
)
|
|
|
|
.unwrap();
|
2021-07-22 07:07:09 +10:00
|
|
|
|
2021-07-22 07:46:22 +10:00
|
|
|
writeln!(
|
|
|
|
output,
|
|
|
|
"const _: &[u8] = include_bytes!(\"{}\");",
|
|
|
|
image_filename
|
|
|
|
)
|
|
|
|
.unwrap();
|
2021-07-22 05:27:04 +10:00
|
|
|
|
2021-04-21 07:56:47 +10:00
|
|
|
writeln!(
|
2021-04-21 05:41:04 +10:00
|
|
|
output,
|
2021-07-22 05:27:04 +10:00
|
|
|
"const PALETTE_DATA: &[{}::display::palette16::Palette16] = &[",
|
2021-06-06 02:47:13 +10:00
|
|
|
crate_prefix,
|
2021-07-22 07:46:22 +10:00
|
|
|
)
|
|
|
|
.unwrap();
|
2021-04-21 05:41:04 +10:00
|
|
|
|
|
|
|
for palette in &results.optimised_palettes {
|
2021-06-06 02:47:13 +10:00
|
|
|
write!(
|
|
|
|
output,
|
|
|
|
" {}::display::palette16::Palette16::new([",
|
|
|
|
crate_prefix
|
2021-07-22 07:46:22 +10:00
|
|
|
)
|
|
|
|
.unwrap();
|
2021-04-21 05:41:04 +10:00
|
|
|
|
|
|
|
for colour in palette.clone() {
|
2021-07-22 07:07:09 +10:00
|
|
|
write!(output, "0x{:08x}, ", colour.to_rgb15()).unwrap();
|
2021-04-21 05:41:04 +10:00
|
|
|
}
|
|
|
|
|
2021-04-21 07:52:23 +10:00
|
|
|
for _ in palette.clone().into_iter().len()..16 {
|
2021-07-22 07:07:09 +10:00
|
|
|
write!(output, "0x00000000, ").unwrap();
|
2021-04-21 07:52:23 +10:00
|
|
|
}
|
|
|
|
|
2021-07-22 07:07:09 +10:00
|
|
|
writeln!(output, "]),").unwrap();
|
2021-04-21 05:41:04 +10:00
|
|
|
}
|
|
|
|
|
2021-07-22 07:07:09 +10:00
|
|
|
writeln!(output, "];").unwrap();
|
|
|
|
writeln!(output).unwrap();
|
2021-04-21 05:41:04 +10:00
|
|
|
|
2021-07-22 07:07:09 +10:00
|
|
|
writeln!(output, "const TILE_DATA: &[u32] = &[",).unwrap();
|
2021-04-21 05:41:04 +10:00
|
|
|
|
|
|
|
let tile_size = tile_size.to_size();
|
|
|
|
|
|
|
|
let tiles_x = image.width / tile_size;
|
|
|
|
let tiles_y = image.height / tile_size;
|
|
|
|
|
|
|
|
for y in 0..tiles_y {
|
|
|
|
for x in 0..tiles_x {
|
|
|
|
let palette_index = results.assignments[y * tiles_x + x];
|
|
|
|
let palette = &results.optimised_palettes[palette_index];
|
2021-04-21 07:56:47 +10:00
|
|
|
writeln!(
|
2021-04-21 05:41:04 +10:00
|
|
|
output,
|
2021-04-21 07:56:47 +10:00
|
|
|
" /* {}, {} (palette index {}) */",
|
2021-04-21 05:41:04 +10:00
|
|
|
x, y, palette_index
|
2021-07-22 07:46:22 +10:00
|
|
|
)
|
|
|
|
.unwrap();
|
2021-04-21 05:41:04 +10:00
|
|
|
|
|
|
|
for inner_y in 0..tile_size / 8 {
|
2021-07-22 07:07:09 +10:00
|
|
|
write!(output, " ").unwrap();
|
2021-04-21 05:41:04 +10:00
|
|
|
|
|
|
|
for inner_x in 0..tile_size / 8 {
|
|
|
|
for j in inner_y * 8..inner_y * 8 + 8 {
|
2021-07-22 07:07:09 +10:00
|
|
|
write!(output, "0x").unwrap();
|
2021-04-21 05:41:04 +10:00
|
|
|
|
2021-04-21 07:52:23 +10:00
|
|
|
for i in (inner_x * 8..inner_x * 8 + 8).rev() {
|
2021-04-21 05:41:04 +10:00
|
|
|
let colour = image.colour(x * tile_size + i, y * tile_size + j);
|
|
|
|
let colour_index = palette.colour_index(colour);
|
|
|
|
|
2021-07-22 07:07:09 +10:00
|
|
|
write!(output, "{:x}", colour_index).unwrap();
|
2021-04-21 05:41:04 +10:00
|
|
|
}
|
|
|
|
|
2021-07-22 07:07:09 +10:00
|
|
|
write!(output, ", ").unwrap();
|
2021-04-21 05:41:04 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-22 07:07:09 +10:00
|
|
|
writeln!(output).unwrap();
|
2021-04-21 05:41:04 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-22 07:07:09 +10:00
|
|
|
writeln!(output, "];").unwrap();
|
|
|
|
writeln!(output).unwrap();
|
2021-04-21 05:51:49 +10:00
|
|
|
|
2021-07-22 07:07:09 +10:00
|
|
|
write!(output, "const PALETTE_ASSIGNMENT: &[u8] = &[").unwrap();
|
2021-04-21 05:51:49 +10:00
|
|
|
|
|
|
|
for (i, assignment) in results.assignments.iter().enumerate() {
|
|
|
|
if i % 16 == 0 {
|
2021-07-22 07:07:09 +10:00
|
|
|
write!(output, "\n ").unwrap();
|
2021-04-21 05:51:49 +10:00
|
|
|
}
|
2021-07-22 07:07:09 +10:00
|
|
|
write!(output, "{}, ", assignment).unwrap();
|
2021-04-21 05:51:49 +10:00
|
|
|
}
|
|
|
|
|
2021-07-22 07:07:09 +10:00
|
|
|
writeln!(output, "\n];").unwrap();
|
2021-07-22 05:27:04 +10:00
|
|
|
|
2021-07-22 07:46:22 +10:00
|
|
|
writeln!(
|
|
|
|
output,
|
|
|
|
"{}::display::tile_data::TileData::new(PALETTE_DATA, TILE_DATA, PALETTE_ASSIGNMENT)\n}};",
|
|
|
|
crate_prefix
|
|
|
|
)
|
|
|
|
.unwrap();
|
2021-04-21 05:41:04 +10:00
|
|
|
}
|