diff --git a/agb-image-converter/src/lib.rs b/agb-image-converter/src/lib.rs index c844673a..26c9a582 100644 --- a/agb-image-converter/src/lib.rs +++ b/agb-image-converter/src/lib.rs @@ -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::::parse_separated_nonempty; + let parser = Punctuated::::parse_terminated; let parsed = match parser.parse(input) { Ok(e) => e, Err(e) => return e.to_compile_error().into(), diff --git a/agb/src/display/palette16.rs b/agb/src/display/palette16.rs index 7296b5bd..06383f93 100644 --- a/agb/src/display/palette16.rs +++ b/agb/src/display/palette16.rs @@ -27,3 +27,12 @@ impl Palette16 { Layout::new::() } } + +#[macro_export] +macro_rules! include_palette { + ($palette:literal) => { + $crate::include_colours_inner!($palette) + }; +} + +pub use include_palette; diff --git a/agb/src/lib.rs b/agb/src/lib.rs index 073971cf..5849a192 100644 --- a/agb/src/lib.rs +++ b/agb/src/lib.rs @@ -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) => {{