Allowing imported graphics to be exposed as pub mod (#738)

- [X] Changelog updated

This PR adds the possibility for graphics imported through
`include_background_gfx!` to be used as public modules, e.g.:
```rust
/// graphics.rs
agb::include_background_gfx!(pub background, "d77bba", tiles256 => 256 "map.aseprite");

/// main.rs
#![no_std]
#![no_main]

use agb::display::Priority;
use agb::display::tiled::RegularBackgroundSize::Background64x64;
use agb::display::tiled::{TiledMap, TileFormat};

mod graphics;


#[agb::entry]
fn main(mut gba: agb::Gba) -> ! {
    let (tiled, mut vram) = gba.display.video.tiled1();
    let vblank = agb::interrupt::VBlank::get();
    let tileset = &graphics::background::tiles256.tiles;

    vram.set_background_palettes(graphics::background::PALETTES);

    let mut bg = tiled.regular(Priority::P2, Background64x64, TileFormat::EightBpp);

    for y in 0..64u16 {
        for x in 0..64u16 {
            bg.set_tile(&mut vram, (x, y), tileset, graphics::background::tiles256.tile_settings[1]);
        }
    }
    bg.set_visible(true);
    bg.commit(&mut vram);

    loop {
        vblank.wait_for_vblank();
    }
}
```
This commit is contained in:
Gwilym Inzani 2024-08-28 15:02:41 +01:00 committed by GitHub
commit 84a3c0b6a0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 40 additions and 5 deletions

View file

@ -19,6 +19,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed ### Fixed
- There are no longer gaps between tiles in affine graphics modes. - There are no longer gaps between tiles in affine graphics modes.
### Added
- Added option to export imported background graphics from `include_background_gfx` as pub.
## [0.20.5] - 2024/06/18 ## [0.20.5] - 2024/06/18
### Fixed ### Fixed

View file

@ -109,6 +109,7 @@ impl Parse for BackgroundGfxOption {
struct IncludeBackgroundGfxInput { struct IncludeBackgroundGfxInput {
module_name: syn::Ident, module_name: syn::Ident,
as_pub: bool,
crate_prefix: String, crate_prefix: String,
transparent_colour: Colour, transparent_colour: Colour,
background_gfx_options: Vec<BackgroundGfxOption>, background_gfx_options: Vec<BackgroundGfxOption>,
@ -126,6 +127,15 @@ impl Parse for IncludeBackgroundGfxInput {
format_ident!("agb") format_ident!("agb")
}; };
let lookahead = input.lookahead1();
let as_pub = if lookahead.peek(Token![pub]) {
let _: Token![pub] = input.parse()?;
true
} else {
false
};
let module_name: syn::Ident = input.parse()?; let module_name: syn::Ident = input.parse()?;
let _: Token![,] = input.parse()?; let _: Token![,] = input.parse()?;
@ -146,6 +156,7 @@ impl Parse for IncludeBackgroundGfxInput {
Ok(Self { Ok(Self {
module_name, module_name,
as_pub,
crate_prefix: crate_prefix.to_string(), crate_prefix: crate_prefix.to_string(),
transparent_colour, transparent_colour,
background_gfx_options: background_gfx_options.into_iter().collect(), background_gfx_options: background_gfx_options.into_iter().collect(),
@ -186,11 +197,13 @@ pub fn include_background_gfx(input: TokenStream) -> TokenStream {
let root = std::env::var("CARGO_MANIFEST_DIR").expect("Failed to get cargo manifest dir"); let root = std::env::var("CARGO_MANIFEST_DIR").expect("Failed to get cargo manifest dir");
let module_name = config.module_name.clone(); let module_name = config.module_name.clone();
include_gfx_from_config(config, module_name, Path::new(&root)) let as_pub = config.as_pub;
include_gfx_from_config(config, as_pub, module_name, Path::new(&root))
} }
fn include_gfx_from_config( fn include_gfx_from_config(
config: Box<dyn config::Config>, config: Box<dyn config::Config>,
as_pub: bool,
module_name: syn::Ident, module_name: syn::Ident,
parent: &Path, parent: &Path,
) -> TokenStream { ) -> TokenStream {
@ -255,11 +268,21 @@ fn include_gfx_from_config(
let palette_code = let palette_code =
rust_generator::generate_palette_code(&optimisation_results, &config.crate_prefix()); rust_generator::generate_palette_code(&optimisation_results, &config.crate_prefix());
let module = quote! { let module = if as_pub {
mod #module_name { quote! {
#palette_code pub mod #module_name {
#palette_code
#(#image_code)* #(#image_code)*
}
}
} else {
quote! {
mod #module_name {
#palette_code
#(#image_code)*
}
} }
}; };

View file

@ -105,6 +105,15 @@
/// # use agb::include_background_gfx; /// # use agb::include_background_gfx;
/// include_background_gfx!(generated_background, "000000", DATA => "$OUT_DIR/generated_background.aseprite"); /// include_background_gfx!(generated_background, "000000", DATA => "$OUT_DIR/generated_background.aseprite");
/// ``` /// ```
///
/// You can also make the exported background a public module which will allow other modules access them. The following
/// will declare `water_tiles` as a `pub mod` rather than a `mod`.
///
/// ```rust,no_run
/// ##![no_std]
/// ##![no_main]
/// agb::include_background_gfx!(pub water_tiles, tiles => "examples/water_tiles.png");
/// ```
pub use agb_image_converter::include_background_gfx; pub use agb_image_converter::include_background_gfx;
#[doc(hidden)] #[doc(hidden)]