2022-02-16 08:33:48 +11:00
|
|
|
use palette16::Palette16OptimisationResults;
|
2021-07-22 07:46:22 +10:00
|
|
|
use proc_macro::TokenStream;
|
2022-02-23 07:16:34 +11:00
|
|
|
use proc_macro2::Literal;
|
|
|
|
use syn::parse::Parser;
|
|
|
|
use syn::{parse_macro_input, punctuated::Punctuated, LitStr};
|
2021-04-20 08:01:28 +10:00
|
|
|
|
2022-02-23 07:24:32 +11:00
|
|
|
use std::path::PathBuf;
|
2022-02-23 07:16:34 +11:00
|
|
|
use std::{iter, path::Path, str};
|
2021-06-06 02:45:21 +10:00
|
|
|
|
2022-02-23 07:16:34 +11:00
|
|
|
use quote::{format_ident, quote, ToTokens};
|
2021-07-23 04:03:49 +10:00
|
|
|
|
2022-02-16 08:28:21 +11:00
|
|
|
mod aseprite;
|
2021-04-20 08:15:03 +10:00
|
|
|
mod colour;
|
2021-07-22 07:46:22 +10:00
|
|
|
mod config;
|
2021-04-20 09:40:07 +10:00
|
|
|
mod image_loader;
|
|
|
|
mod palette16;
|
2021-04-21 05:41:04 +10:00
|
|
|
mod rust_generator;
|
2021-04-20 09:40:07 +10:00
|
|
|
|
2022-02-24 05:05:48 +11:00
|
|
|
use image::GenericImageView;
|
2021-04-20 09:40:07 +10:00
|
|
|
use image_loader::Image;
|
2021-04-20 08:15:03 +10:00
|
|
|
|
2021-07-22 07:07:09 +10:00
|
|
|
use colour::Colour;
|
2021-04-20 08:01:28 +10:00
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy)]
|
2021-07-22 07:07:09 +10:00
|
|
|
pub(crate) enum TileSize {
|
2021-04-20 08:01:28 +10:00
|
|
|
Tile8,
|
|
|
|
Tile16,
|
2021-10-31 04:22:09 +11:00
|
|
|
Tile32,
|
2021-04-20 08:01:28 +10:00
|
|
|
}
|
|
|
|
|
2021-04-20 09:40:07 +10:00
|
|
|
impl TileSize {
|
2021-04-21 07:56:47 +10:00
|
|
|
fn to_size(self) -> usize {
|
|
|
|
match self {
|
2021-04-20 09:40:07 +10:00
|
|
|
TileSize::Tile8 => 8,
|
|
|
|
TileSize::Tile16 => 16,
|
2021-10-31 04:22:09 +11:00
|
|
|
TileSize::Tile32 => 32,
|
2021-04-20 09:40:07 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-22 07:07:09 +10:00
|
|
|
#[proc_macro]
|
|
|
|
pub fn include_gfx(input: TokenStream) -> TokenStream {
|
2021-07-23 03:43:27 +10:00
|
|
|
let input = parse_macro_input!(input as syn::LitStr);
|
2021-06-06 02:47:13 +10:00
|
|
|
|
2021-07-23 03:43:27 +10:00
|
|
|
let filename = input.value();
|
2021-07-22 07:07:09 +10:00
|
|
|
|
|
|
|
let root = std::env::var("CARGO_MANIFEST_DIR").expect("Failed to get cargo manifest dir");
|
|
|
|
let path = Path::new(&root).join(&*filename);
|
2021-07-22 07:46:22 +10:00
|
|
|
let parent = path
|
|
|
|
.parent()
|
|
|
|
.expect("Expected a parent directory for the path");
|
2021-07-22 07:07:09 +10:00
|
|
|
|
|
|
|
let config = config::parse(&path.to_string_lossy());
|
|
|
|
|
2021-07-27 08:27:16 +10:00
|
|
|
let module_name = format_ident!(
|
|
|
|
"{}",
|
|
|
|
path.file_stem()
|
|
|
|
.expect("Expected a file stem")
|
|
|
|
.to_string_lossy()
|
|
|
|
);
|
2021-07-23 04:03:49 +10:00
|
|
|
let include_path = path.to_string_lossy();
|
|
|
|
|
|
|
|
let images = config.images();
|
2021-07-27 08:27:16 +10:00
|
|
|
let image_code = images.iter().map(|(image_name, &image)| {
|
2022-02-16 08:29:16 +11:00
|
|
|
convert_image(image, parent, image_name, &config.crate_prefix())
|
2021-07-27 08:27:16 +10:00
|
|
|
});
|
2021-07-22 07:07:09 +10:00
|
|
|
|
2021-07-23 04:03:49 +10:00
|
|
|
let module = quote! {
|
2022-01-03 04:54:44 +11:00
|
|
|
mod #module_name {
|
2021-07-23 04:03:49 +10:00
|
|
|
const _: &[u8] = include_bytes!(#include_path);
|
|
|
|
|
|
|
|
#(#image_code)*
|
|
|
|
}
|
|
|
|
};
|
2021-07-22 07:07:09 +10:00
|
|
|
|
2021-07-23 04:03:49 +10:00
|
|
|
TokenStream::from(module)
|
2021-04-20 09:40:07 +10:00
|
|
|
}
|
|
|
|
|
2022-02-23 07:16:34 +11:00
|
|
|
use quote::TokenStreamExt;
|
|
|
|
struct ByteString<'a>(&'a [u8]);
|
|
|
|
impl ToTokens for ByteString<'_> {
|
|
|
|
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
|
|
|
|
tokens.append(Literal::byte_string(self.0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-16 08:33:48 +11:00
|
|
|
#[proc_macro]
|
|
|
|
pub fn include_aseprite_inner(input: TokenStream) -> TokenStream {
|
2022-02-23 07:16:34 +11:00
|
|
|
let parser = Punctuated::<LitStr, syn::Token![,]>::parse_separated_nonempty;
|
|
|
|
let parsed = match parser.parse(input) {
|
|
|
|
Ok(e) => e,
|
|
|
|
Err(e) => return e.to_compile_error().into(),
|
|
|
|
};
|
2022-02-16 08:33:48 +11:00
|
|
|
|
2022-02-23 07:16:34 +11:00
|
|
|
let mut optimiser = palette16::Palette16Optimiser::new();
|
|
|
|
let mut images = Vec::new();
|
|
|
|
let mut tags = Vec::new();
|
2022-02-16 08:33:48 +11:00
|
|
|
|
2022-02-23 07:16:34 +11:00
|
|
|
let root = std::env::var("CARGO_MANIFEST_DIR").expect("Failed to get cargo manifest dir");
|
2022-02-16 08:33:48 +11:00
|
|
|
|
2022-02-23 07:24:32 +11:00
|
|
|
let filenames: Vec<PathBuf> = parsed
|
2022-02-23 07:16:34 +11:00
|
|
|
.iter()
|
|
|
|
.map(|s| s.value())
|
2022-02-23 07:24:32 +11:00
|
|
|
.map(|s| Path::new(&root).join(&*s))
|
2022-02-23 07:16:34 +11:00
|
|
|
.collect();
|
2022-02-16 08:33:48 +11:00
|
|
|
|
2022-02-23 07:16:34 +11:00
|
|
|
for filename in filenames.iter() {
|
2022-02-24 05:05:48 +11:00
|
|
|
let (frames, tag) = aseprite::generate_from_file(filename);
|
|
|
|
|
|
|
|
tags.push((tag, images.len()));
|
2022-02-16 08:33:48 +11:00
|
|
|
|
2022-02-24 05:05:48 +11:00
|
|
|
for frame in frames {
|
|
|
|
let width = frame.width();
|
|
|
|
assert!(width == frame.height() && width.is_power_of_two() && width <= 32);
|
2022-02-16 08:33:48 +11:00
|
|
|
|
2022-02-24 05:05:48 +11:00
|
|
|
let image = Image::load_from_dyn_image(frame);
|
|
|
|
add_to_optimiser(&mut optimiser, &image, width as usize);
|
|
|
|
images.push(image);
|
|
|
|
}
|
2022-02-23 07:16:34 +11:00
|
|
|
}
|
2022-02-16 08:33:48 +11:00
|
|
|
|
2022-02-23 07:16:34 +11:00
|
|
|
let optimised_results = optimiser.optimise_palettes(None);
|
2022-02-16 08:33:48 +11:00
|
|
|
|
2022-02-23 07:16:34 +11:00
|
|
|
let (palette_data, tile_data, assignments) = palete_tile_data(&optimised_results, &images);
|
2022-02-16 08:33:48 +11:00
|
|
|
|
|
|
|
let palette_data = palette_data.iter().map(|colours| {
|
|
|
|
quote! {
|
|
|
|
Palette16::new([
|
|
|
|
#(#colours),*
|
|
|
|
])
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let mut pre = 0;
|
2022-02-24 05:05:48 +11:00
|
|
|
let sprites = images
|
2022-02-16 08:33:48 +11:00
|
|
|
.iter()
|
|
|
|
.zip(assignments.iter())
|
|
|
|
.map(|(f, assignment)| {
|
|
|
|
let start: usize = pre;
|
2022-02-24 05:05:48 +11:00
|
|
|
let end: usize = pre + (f.width / 8) * (f.height / 8) * 32;
|
2022-02-23 07:16:34 +11:00
|
|
|
let data = ByteString(&tile_data[start..end]);
|
2022-02-16 08:33:48 +11:00
|
|
|
pre = end;
|
2022-02-24 05:05:48 +11:00
|
|
|
let width = f.width;
|
|
|
|
let height = f.height;
|
2022-02-16 08:33:48 +11:00
|
|
|
quote! {
|
|
|
|
Sprite::new(
|
|
|
|
&PALETTES[#assignment],
|
2022-02-23 07:16:34 +11:00
|
|
|
#data,
|
2022-02-16 08:33:48 +11:00
|
|
|
Size::from_width_height(#width, #height)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-02-23 07:16:34 +11:00
|
|
|
let tags = tags
|
|
|
|
.iter()
|
2022-02-24 05:05:48 +11:00
|
|
|
.map(|(tag, num_images)| {
|
|
|
|
tag.iter().map(move |tag| {
|
|
|
|
let start = tag.from_frame() as usize + num_images;
|
|
|
|
let end = tag.to_frame() as usize + num_images;
|
|
|
|
let direction = tag.animation_direction() as usize;
|
|
|
|
|
|
|
|
let name = tag.name();
|
2022-02-23 07:16:34 +11:00
|
|
|
assert!(start <= end, "Tag {} has start > end", name);
|
|
|
|
|
|
|
|
quote! {
|
|
|
|
#name => Tag::new(SPRITES, #start, #end, #direction)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.flatten();
|
2022-02-16 08:33:48 +11:00
|
|
|
|
2022-02-23 07:16:34 +11:00
|
|
|
let include_paths = filenames.iter().map(|s| {
|
2022-02-23 07:24:32 +11:00
|
|
|
let s = s.as_os_str().to_string_lossy();
|
2022-02-16 08:33:48 +11:00
|
|
|
quote! {
|
2022-02-23 07:16:34 +11:00
|
|
|
const _: &[u8] = include_bytes!(#s);
|
2022-02-16 08:33:48 +11:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let module = quote! {
|
2022-02-23 07:16:34 +11:00
|
|
|
#(#include_paths)*
|
2022-02-16 08:33:48 +11:00
|
|
|
|
|
|
|
|
|
|
|
const PALETTES: &[Palette16] = &[
|
|
|
|
#(#palette_data),*
|
|
|
|
];
|
|
|
|
|
|
|
|
pub const SPRITES: &[Sprite] = &[
|
|
|
|
#(#sprites),*
|
|
|
|
];
|
|
|
|
|
|
|
|
const TAGS: &TagMap = &TagMap::new(
|
|
|
|
phf::phf_map! {
|
|
|
|
#(#tags),*
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
TokenStream::from(module)
|
|
|
|
}
|
|
|
|
|
2021-07-22 07:46:22 +10:00
|
|
|
fn convert_image(
|
|
|
|
settings: &dyn config::Image,
|
|
|
|
parent: &Path,
|
|
|
|
variable_name: &str,
|
|
|
|
crate_prefix: &str,
|
2021-07-23 04:04:30 +10:00
|
|
|
) -> proc_macro2::TokenStream {
|
2021-07-22 07:07:09 +10:00
|
|
|
let image_filename = &parent.join(&settings.filename());
|
|
|
|
let image = Image::load_from_file(image_filename);
|
2021-04-20 09:40:07 +10:00
|
|
|
|
2021-07-22 07:07:09 +10:00
|
|
|
let tile_size = settings.tilesize().to_size();
|
2021-04-20 09:40:07 +10:00
|
|
|
if image.width % tile_size != 0 || image.height % tile_size != 0 {
|
|
|
|
panic!("Image size not a multiple of tile size");
|
|
|
|
}
|
|
|
|
|
|
|
|
let optimiser = optimiser_for_image(&image, tile_size);
|
2021-07-22 07:07:09 +10:00
|
|
|
let optimisation_results = optimiser.optimise_palettes(settings.transparent_colour());
|
2021-04-20 09:40:07 +10:00
|
|
|
|
2021-04-21 05:41:04 +10:00
|
|
|
rust_generator::generate_code(
|
2021-07-22 07:07:09 +10:00
|
|
|
variable_name,
|
2021-04-21 05:41:04 +10:00
|
|
|
&optimisation_results,
|
|
|
|
&image,
|
2021-07-22 07:07:09 +10:00
|
|
|
&image_filename.to_string_lossy(),
|
|
|
|
settings.tilesize(),
|
|
|
|
crate_prefix.to_owned(),
|
2021-07-27 08:27:16 +10:00
|
|
|
)
|
2021-04-20 08:01:28 +10:00
|
|
|
}
|
|
|
|
|
2021-04-20 09:40:07 +10:00
|
|
|
fn optimiser_for_image(image: &Image, tile_size: usize) -> palette16::Palette16Optimiser {
|
2022-02-23 07:16:34 +11:00
|
|
|
let mut palette_optimiser = palette16::Palette16Optimiser::new();
|
|
|
|
add_to_optimiser(&mut palette_optimiser, image, tile_size);
|
|
|
|
palette_optimiser
|
|
|
|
}
|
|
|
|
|
|
|
|
fn add_to_optimiser(
|
|
|
|
palette_optimiser: &mut palette16::Palette16Optimiser,
|
|
|
|
image: &Image,
|
|
|
|
tile_size: usize,
|
|
|
|
) {
|
2021-04-20 09:40:07 +10:00
|
|
|
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 mut palette = palette16::Palette16::new();
|
|
|
|
|
|
|
|
for j in 0..tile_size {
|
|
|
|
for i in 0..tile_size {
|
|
|
|
let colour = image.colour(x * tile_size + i, y * tile_size + j);
|
|
|
|
|
2021-04-20 09:57:47 +10:00
|
|
|
palette.add_colour(colour);
|
2021-04-20 09:40:07 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
palette_optimiser.add_palette(palette);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-16 08:33:48 +11:00
|
|
|
|
|
|
|
fn palete_tile_data(
|
|
|
|
optimiser: &Palette16OptimisationResults,
|
2022-02-23 07:16:34 +11:00
|
|
|
images: &[Image],
|
2022-02-16 08:33:48 +11:00
|
|
|
) -> (Vec<Vec<u16>>, Vec<u8>, Vec<usize>) {
|
|
|
|
let palette_data: Vec<Vec<u16>> = optimiser
|
|
|
|
.optimised_palettes
|
|
|
|
.iter()
|
|
|
|
.map(|palette| {
|
|
|
|
palette
|
|
|
|
.clone()
|
|
|
|
.into_iter()
|
|
|
|
.map(|colour| colour.to_rgb15())
|
|
|
|
.chain(iter::repeat(0))
|
|
|
|
.take(16)
|
|
|
|
.map(|colour| colour as u16)
|
|
|
|
.collect()
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
2022-02-23 07:16:34 +11:00
|
|
|
let mut tile_data = Vec::new();
|
|
|
|
|
|
|
|
for image in images {
|
|
|
|
let tile_size = image.height;
|
|
|
|
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 = optimiser.assignments[y * tiles_x + x];
|
|
|
|
let palette = &optimiser.optimised_palettes[palette_index];
|
|
|
|
|
|
|
|
for inner_y in 0..tile_size / 8 {
|
|
|
|
for inner_x in 0..tile_size / 8 {
|
|
|
|
for j in inner_y * 8..inner_y * 8 + 8 {
|
|
|
|
for i in inner_x * 8..inner_x * 8 + 8 {
|
|
|
|
let colour = image.colour(x * tile_size + i, y * tile_size + j);
|
|
|
|
tile_data.push(palette.colour_index(colour));
|
|
|
|
}
|
2022-02-16 08:33:48 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let tile_data = tile_data
|
|
|
|
.chunks(2)
|
|
|
|
.map(|chunk| chunk[0] | (chunk[1] << 4))
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let assignments = optimiser.assignments.clone();
|
|
|
|
|
|
|
|
(palette_data, tile_data, assignments)
|
|
|
|
}
|