add macro to import a palette from a sprite

This commit is contained in:
Corwin 2023-05-06 22:42:03 +01:00
parent 13f0eaf5b0
commit e8479c093b
No known key found for this signature in database
3 changed files with 40 additions and 1 deletions

View file

@ -239,9 +239,36 @@ impl ToTokens for ByteString<'_> {
}
}
#[proc_macro]
pub fn include_colours_inner(input: TokenStream) -> TokenStream {
let input_filename = parse_macro_input!(input as LitStr);
let input_filename = input_filename.value();
let root = std::env::var("CARGO_MANIFEST_DIR").expect("Failed to get cargo manifest dir");
let input_filename = Path::new(&root).join(input_filename);
let image = Image::load_from_file(Path::new(&input_filename));
let mut palette_data = Vec::with_capacity(image.width * image.height);
for y in 0..image.height {
for x in 0..image.width {
palette_data.push(image.colour(x, y).to_rgb15())
}
}
let filename = input_filename.to_string_lossy();
TokenStream::from(quote! {
{
const _: &[u8] = include_bytes!(#filename);
[#(#palette_data),*]
}
})
}
#[proc_macro]
pub fn include_aseprite_inner(input: TokenStream) -> TokenStream {
let parser = Punctuated::<LitStr, syn::Token![,]>::parse_separated_nonempty;
let parser = Punctuated::<LitStr, syn::Token![,]>::parse_terminated;
let parsed = match parser.parse(input) {
Ok(e) => e,
Err(e) => return e.to_compile_error().into(),

View file

@ -27,3 +27,12 @@ impl Palette16 {
Layout::new::<Self>()
}
}
#[macro_export]
macro_rules! include_palette {
($palette:literal) => {
$crate::include_colours_inner!($palette)
};
}
pub use include_palette;

View file

@ -104,6 +104,9 @@ pub use agb_image_converter::include_aseprite_inner;
#[doc(hidden)]
pub use agb_image_converter::include_font as include_font_inner;
#[doc(hidden)]
pub use agb_image_converter::include_colours_inner;
#[macro_export]
macro_rules! include_font {
($font_path: literal, $font_size: literal) => {{