use alloc::vec::Vec;
use core::cell::RefCell;
use tishlang_core::SingleCore;
static GBA: SingleCore<RefCell<Option<agb::Gba>>> = SingleCore::new(RefCell::new(None));
pub fn init(gba: agb::Gba) {
GBA.with(|c| {
*c.borrow_mut() = Some(gba);
});
}
pub fn take_gba() -> Option<agb::Gba> {
GBA.with(|c| c.borrow_mut().take())
}
pub fn halt() -> ! {
loop {
agb::halt();
}
}
static PRE_COMMIT: SingleCore<RefCell<Vec<fn()>>> = SingleCore::new(RefCell::new(Vec::new()));
pub fn register_pre_commit(f: fn()) {
PRE_COMMIT.with(|c| c.borrow_mut().push(f));
}
pub fn run_pre_commit() {
PRE_COMMIT.with(|c| {
let hooks: Vec<fn()> = c.borrow().clone();
for f in hooks {
f();
}
});
}
static ASSET_SHEETS: SingleCore<RefCell<Vec<&'static [agb::display::object::Sprite]>>> =
SingleCore::new(RefCell::new(Vec::new()));
pub fn __asset_register_sheet(sheet: &'static [agb::display::object::Sprite]) -> i32 {
ASSET_SHEETS.with(|c| {
let mut v = c.borrow_mut();
let idx = v.len() as i32;
v.push(sheet);
idx
})
}
pub fn asset_sheet(handle: i32) -> Option<&'static [agb::display::object::Sprite]> {
ASSET_SHEETS.with(|c| c.borrow().get(handle as usize).copied())
}
type BgAsset = (
&'static [agb::display::Palette16],
&'static agb::display::tile_data::TileData,
);
const MAX_BGS: usize = 256;
static ASSET_BGS: SingleCore<RefCell<[Option<BgAsset>; MAX_BGS]>> =
SingleCore::new(RefCell::new([None; MAX_BGS]));
static ASSET_BGS_N: SingleCore<RefCell<usize>> = SingleCore::new(RefCell::new(0));
pub fn __asset_register_bg(bg: BgAsset) -> i32 {
ASSET_BGS_N.with(|n| {
let mut n = n.borrow_mut();
if *n >= MAX_BGS {
return -1;
}
let idx = *n as i32;
ASSET_BGS.with(|c| c.borrow_mut()[*n] = Some(bg));
*n += 1;
idx
})
}
pub fn asset_bg(handle: i32) -> Option<BgAsset> {
if handle < 0 || handle as usize >= MAX_BGS {
return None;
}
ASSET_BGS.with(|c| c.borrow()[handle as usize])
}
static ASSET_FONTS: SingleCore<RefCell<Vec<&'static agb::display::font::Font>>> =
SingleCore::new(RefCell::new(Vec::new()));
pub fn __asset_register_font(font: &'static agb::display::font::Font) -> i32 {
ASSET_FONTS.with(|c| {
let mut v = c.borrow_mut();
let idx = v.len() as i32;
v.push(font);
idx
})
}
pub fn asset_font(handle: i32) -> Option<&'static agb::display::font::Font> {
ASSET_FONTS.with(|c| c.borrow().get(handle as usize).copied())
}
type EmojiAtlas = (
&'static [agb::display::object::Sprite],
&'static [(u32, u16)],
);
static ASSET_EMOJI: SingleCore<RefCell<Option<EmojiAtlas>>> = SingleCore::new(RefCell::new(None));
pub fn __asset_register_emoji(
sprites: &'static [agb::display::object::Sprite],
table: &'static [(u32, u16)],
) -> i32 {
ASSET_EMOJI.with(|c| *c.borrow_mut() = Some((sprites, table)));
0
}
pub fn emoji_sprite(cp: u32) -> Option<&'static agb::display::object::Sprite> {
ASSET_EMOJI.with(|c| {
let (sprites, table) = (*c.borrow())?;
let i = table.binary_search_by_key(&cp, |&(c, _)| c).ok()?;
sprites.get(table[i].1 as usize)
})
}
static ASSET_MAPS: SingleCore<RefCell<Vec<&'static [u8]>>> =
SingleCore::new(RefCell::new(Vec::new()));
pub fn __asset_register_map(data: &'static [u8]) -> i32 {
ASSET_MAPS.with(|c| {
let mut v = c.borrow_mut();
let idx = v.len() as i32;
v.push(data);
idx
})
}
pub fn asset_map(handle: i32) -> Option<&'static [u8]> {
ASSET_MAPS.with(|c| c.borrow().get(handle as usize).copied())
}
static ASSET_WAVS: SingleCore<RefCell<Vec<agb::sound::mixer::SoundData>>> =
SingleCore::new(RefCell::new(Vec::new()));
pub fn __asset_register_wav(data: agb::sound::mixer::SoundData) -> i32 {
ASSET_WAVS.with(|c| {
let mut v = c.borrow_mut();
let idx = v.len() as i32;
v.push(data);
idx
})
}
pub fn asset_wav(handle: i32) -> Option<agb::sound::mixer::SoundData> {
ASSET_WAVS.with(|c| c.borrow().get(handle as usize).copied())
}