agb_eb_ext 0.25.0

AGB Extension methods
use agb::display::Priority;
use agb::display::tile_data::TileData;
use agb::display::tiled::{RegularBackground, RegularBackgroundSize, TileFormat};
use agb::fixnum::vec2;

/// Create a blank 32x32 4bpp P0 bg for text
pub fn create_text_bg() -> RegularBackground {
    RegularBackground::new(
        Priority::P0,
        RegularBackgroundSize::Background32x32,
        TileFormat::FourBpp,
    )
}

/// Create and fill a 4bpp bg
///
/// (calls bg.fill_with internally so limited to 30x20)
pub fn create_filled_bg(data: &'static TileData, priority: Priority) -> RegularBackground {
    let mut background = RegularBackground::new(
        priority,
        RegularBackgroundSize::Background32x32,
        TileFormat::FourBpp,
    );

    background.fill_with(data);

    background
}

/// Create and fully fill a 4bpp bg (32x32)
///
/// see [full_fill]
pub fn create_full_filled_bg(data: &'static TileData, priority: Priority) -> RegularBackground {
    let mut background = RegularBackground::new(
        priority,
        RegularBackgroundSize::Background32x32,
        TileFormat::FourBpp,
    );

    full_fill(&mut background, data);

    background
}

/// Background first
/// then extras
/// then ui
///
/// All at 4bpp
///
/// (calls bg.fill internally so limited to 32x32)
pub fn background_stack<const N: usize>(layers: [&'static TileData; N]) -> [RegularBackground; N] {
    const {
        assert!(N >= 1 && N <= 4, "between 1 and 4 layers required");
    }

    let priorities = [Priority::P3, Priority::P2, Priority::P1, Priority::P0];

    core::array::from_fn(|i| create_filled_bg(layers[i], priorities[i]))
}

/// fill an entire 32x32 background
///
/// `tile_data` must be at least 32x32 tiles
pub fn full_fill(bg: &mut RegularBackground, tile_data: &TileData) {
    assert!(
        tile_data.width >= 32 && tile_data.height >= 32,
        "full_fill requires at least 32x32 tiles of data, got {}x{}",
        tile_data.width,
        tile_data.height
    );

    for y in 0..32 {
        for x in 0..32 {
            let tile_id = y * tile_data.width + x;
            bg.set_tile(
                vec2(x as i32, y as i32),
                &tile_data.tiles,
                tile_data.tile_settings[tile_id],
            );
        }
    }
}