agb_eb_ext 0.25.3

AGB Extension methods
Documentation
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 bg
pub fn create_bg(priority: Priority) -> RegularBackground {
    RegularBackground::new(
        priority,
        RegularBackgroundSize::Background32x32,
        TileFormat::FourBpp,
    )
}

/// Create a blank 32x32 4bpp P0 bg for text
pub fn create_text_bg() -> RegularBackground {
    create_bg(Priority::P0)
}

/// 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 = create_bg(priority);
    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 = create_bg(priority);
    full_fill(&mut background, data);
    background
}

/// Background first
/// then extras
/// then ui
///
/// All at 4bpp
///
/// (calls bg.fill_with internally so limited to 30x20, see [create_filled_bg])
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
    );

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