use crate::memory_mapped::MemoryMapped;
use bilge::prelude::*;
use bitflags::bitflags;
use video::Video;
use self::{
blend::Blend,
object::{initilise_oam, OamManaged, OamUnmanaged, SpriteLoader},
window::Windows,
};
pub mod bitmap3;
pub mod bitmap4;
pub mod example_logo;
pub mod object;
pub mod palette16;
pub mod tile_data;
pub mod tiled;
pub mod video;
pub mod affine;
pub mod blend;
pub mod window;
pub mod font;
pub use font::{Font, FontLetter};
const DISPLAY_CONTROL: MemoryMapped<u16> = unsafe { MemoryMapped::new(0x0400_0000) };
pub(crate) const DISPLAY_STATUS: MemoryMapped<u16> = unsafe { MemoryMapped::new(0x0400_0004) };
const VCOUNT: MemoryMapped<u16> = unsafe { MemoryMapped::new(0x0400_0006) };
bitflags! {
#[derive(PartialEq, Eq, Hash, Debug, Clone, Copy)]
struct GraphicsSettings: u16 {
const PAGE_SELECT = 1 << 0x4;
const OAM_HBLANK = 1 << 0x5;
const SPRITE1_D = 1 << 0x6;
const SCREEN_BLANK = 1 << 0x7;
const LAYER_BG0 = 1 << 0x8;
const LAYER_BG1 = 1 << 0x9;
const LAYER_BG2 = 1 << 0xA;
const LAYER_BG3 = 1 << 0xB;
const LAYER_OBJ = 1 << 0xC;
const WINDOW0 = 1 << 0xD;
const WINDOW1 = 1 << 0xE;
const WINDOW_OBJECT = 1 << 0xF;
}
}
pub const WIDTH: i32 = 240;
pub const HEIGHT: i32 = 160;
#[allow(dead_code)]
enum DisplayMode {
Tiled0 = 0,
Tiled1 = 1,
Tiled2 = 2,
Bitmap3 = 3,
Bitmap4 = 4,
Bitmap5 = 5,
}
#[non_exhaustive]
pub struct Display {
pub video: Video,
pub object: ObjectDistribution,
pub window: WindowDist,
pub blend: BlendDist,
}
#[non_exhaustive]
pub struct ObjectDistribution;
impl ObjectDistribution {
pub fn get_unmanaged(&mut self) -> (OamUnmanaged<'_>, SpriteLoader) {
unsafe { initilise_oam() };
(OamUnmanaged::new(), SpriteLoader::new())
}
pub fn get_managed(&mut self) -> OamManaged<'_> {
unsafe { initilise_oam() };
OamManaged::new()
}
#[deprecated = "use get_managed to get the managed oam instead"]
pub fn get(&mut self) -> OamManaged<'_> {
self.get_managed()
}
}
#[non_exhaustive]
pub struct WindowDist;
impl WindowDist {
pub fn get(&mut self) -> Windows<'_> {
Windows::new()
}
}
#[non_exhaustive]
pub struct BlendDist;
impl BlendDist {
pub fn get(&mut self) -> Blend<'_> {
Blend::new()
}
}
impl Display {
pub(crate) const unsafe fn new() -> Self {
Display {
video: Video,
object: ObjectDistribution,
window: WindowDist,
blend: BlendDist,
}
}
}
unsafe fn set_graphics_mode(mode: DisplayMode) {
let current = DISPLAY_CONTROL.get();
let current = current & (!0b111);
let s = current | (mode as u16 & 0b111);
let s = s & !(1 << 7);
DISPLAY_CONTROL.set(s);
}
unsafe fn set_graphics_settings(settings: GraphicsSettings) {
let current = DISPLAY_CONTROL.get();
let current = current & 0b111;
let s = settings.bits() | current;
DISPLAY_CONTROL.set(s);
}
pub fn busy_wait_for_vblank() {
while VCOUNT.get() >= 160 {}
while VCOUNT.get() < 160 {}
}
#[bitsize(2)]
#[derive(FromBits, PartialEq, Eq, Clone, Copy, Debug, Default)]
pub enum Priority {
#[default]
P0 = 0,
P1 = 1,
P2 = 2,
P3 = 3,
}