mirror of
https://github.com/italicsjenga/agb.git
synced 2024-12-23 00:01:34 +11:00
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:
commit
84a3c0b6a0
|
@ -19,6 +19,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||
### Fixed
|
||||
- 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
|
||||
|
||||
### Fixed
|
||||
|
|
|
@ -109,6 +109,7 @@ impl Parse for BackgroundGfxOption {
|
|||
|
||||
struct IncludeBackgroundGfxInput {
|
||||
module_name: syn::Ident,
|
||||
as_pub: bool,
|
||||
crate_prefix: String,
|
||||
transparent_colour: Colour,
|
||||
background_gfx_options: Vec<BackgroundGfxOption>,
|
||||
|
@ -126,6 +127,15 @@ impl Parse for IncludeBackgroundGfxInput {
|
|||
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 _: Token![,] = input.parse()?;
|
||||
|
||||
|
@ -146,6 +156,7 @@ impl Parse for IncludeBackgroundGfxInput {
|
|||
|
||||
Ok(Self {
|
||||
module_name,
|
||||
as_pub,
|
||||
crate_prefix: crate_prefix.to_string(),
|
||||
transparent_colour,
|
||||
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 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(
|
||||
config: Box<dyn config::Config>,
|
||||
as_pub: bool,
|
||||
module_name: syn::Ident,
|
||||
parent: &Path,
|
||||
) -> TokenStream {
|
||||
|
@ -255,11 +268,21 @@ fn include_gfx_from_config(
|
|||
let palette_code =
|
||||
rust_generator::generate_palette_code(&optimisation_results, &config.crate_prefix());
|
||||
|
||||
let module = quote! {
|
||||
mod #module_name {
|
||||
#palette_code
|
||||
let module = if as_pub {
|
||||
quote! {
|
||||
pub mod #module_name {
|
||||
#palette_code
|
||||
|
||||
#(#image_code)*
|
||||
#(#image_code)*
|
||||
}
|
||||
}
|
||||
} else {
|
||||
quote! {
|
||||
mod #module_name {
|
||||
#palette_code
|
||||
|
||||
#(#image_code)*
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -105,6 +105,15 @@
|
|||
/// # use agb::include_background_gfx;
|
||||
/// 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;
|
||||
|
||||
#[doc(hidden)]
|
||||
|
|
Loading…
Reference in a new issue