begin work on affine regular compatability

This commit is contained in:
Corwin Kuiper 2021-08-28 23:02:35 +01:00
parent fe9461e5c5
commit 1ae3c34877
5 changed files with 43 additions and 28 deletions

View file

@ -3,7 +3,7 @@
extern crate agb; extern crate agb;
use agb::{ use agb::{
display::{object::ObjectStandard, tiled0::Map, HEIGHT, WIDTH}, display::{background::Map, object::ObjectStandard, HEIGHT, WIDTH},
input::Button, input::Button,
}; };
use core::convert::TryInto; use core::convert::TryInto;
@ -51,7 +51,7 @@ pub fn main() -> ! {
gfx.set_background_palette_raw(&MAP_PALETTE); gfx.set_background_palette_raw(&MAP_PALETTE);
gfx.set_background_tilemap(0, &MAP_TILES); gfx.set_background_tilemap(0, &MAP_TILES);
let mut background = gfx.get_background().unwrap(); let mut background = gfx.get_regular().unwrap();
background.set_map(Map::new(&MAP_MAP, (32_u32, 32_u32).into(), 0)); background.set_map(Map::new(&MAP_MAP, (32_u32, 32_u32).into(), 0));
background.show(); background.show();
background.commit(); background.commit();

View file

@ -79,7 +79,7 @@ impl<'a> MapStorage<'a> {
/// The map background is the method of drawing game maps to the screen. It /// The map background is the method of drawing game maps to the screen. It
/// automatically handles copying the correct portion of a provided map to the /// automatically handles copying the correct portion of a provided map to the
/// assigned block depending on given coordinates. /// assigned block depending on given coordinates.
pub struct Background<'a> { pub struct BackgroundRegular<'a> {
background: u8, background: u8,
block: u8, block: u8,
commited_position: Vector2D<i32>, commited_position: Vector2D<i32>,
@ -125,9 +125,9 @@ impl<'a> Map<'a> {
} }
} }
impl<'a> Background<'a> { impl<'a> BackgroundRegular<'a> {
unsafe fn new(background: u8, block: u8) -> Background<'a> { unsafe fn new(background: u8, block: u8) -> BackgroundRegular<'a> {
let mut b = Background { let mut b = BackgroundRegular {
background, background,
block, block,
commited_position: (0, 0).into(), commited_position: (0, 0).into(),
@ -281,18 +281,32 @@ impl<'a> Background<'a> {
} }
} }
pub struct Tiled0 { fn decide_background_mode(num_regular: u8, num_affine: u8) -> Option<DisplayMode> {
used_blocks: u32, if num_affine == 0 && num_regular <= 4 {
num_backgrounds: u8, Some(DisplayMode::Tiled0)
} else if num_affine == 1 && num_regular <= 2 {
Some(DisplayMode::Tiled1)
} else if num_affine == 2 && num_regular == 0 {
Some(DisplayMode::Tiled2)
} else {
None
}
} }
impl<'b> Tiled0 { pub struct BackgroundDistributor {
used_blocks: u32,
num_regular: u8,
num_affine: u8,
}
impl<'b> BackgroundDistributor {
pub(crate) unsafe fn new() -> Self { pub(crate) unsafe fn new() -> Self {
set_graphics_settings(GraphicsSettings::empty() | GraphicsSettings::SPRITE1_D); set_graphics_settings(GraphicsSettings::empty() | GraphicsSettings::SPRITE1_D);
set_graphics_mode(DisplayMode::Tiled0); set_graphics_mode(DisplayMode::Tiled0);
Tiled0 { BackgroundDistributor {
used_blocks: 0, used_blocks: 0,
num_backgrounds: 0, num_regular: 0,
num_affine: 0,
} }
} }
@ -321,10 +335,11 @@ impl<'b> Tiled0 {
} }
/// Gets a map background if possible and assigns an unused block to it. /// Gets a map background if possible and assigns an unused block to it.
pub fn get_background(&mut self) -> Result<Background<'b>, &'static str> { pub fn get_regular(&mut self) -> Result<BackgroundRegular<'b>, &'static str> {
if self.num_backgrounds >= 4 { let new_mode = decide_background_mode(self.num_regular + 1, self.num_affine)
return Err("too many backgrounds created, maximum is 4"); .ok_or("there is no mode compatible with the requested backgrounds")?;
}
unsafe { set_graphics_mode(new_mode) };
if !self.used_blocks == 0 { if !self.used_blocks == 0 {
return Err("all blocks are used"); return Err("all blocks are used");
@ -346,9 +361,9 @@ impl<'b> Tiled0 {
self.used_blocks |= 1 << availiable_block; self.used_blocks |= 1 << availiable_block;
let background = self.num_backgrounds; let background = self.num_regular;
self.num_backgrounds = background + 1; self.num_regular += 1;
Ok(unsafe { Background::new(background, availiable_block) }) Ok(unsafe { BackgroundRegular::new(background, availiable_block) })
} }
/// Copies tiles to tilemap starting at the starting tile. Cannot overwrite /// Copies tiles to tilemap starting at the starting tile. Cannot overwrite

View file

@ -1,13 +1,13 @@
use crate::display::tiled0::Tiled0; use crate::display::background::BackgroundDistributor;
crate::include_gfx!("gfx/agb_logo.toml"); crate::include_gfx!("gfx/agb_logo.toml");
pub fn display_logo(gfx: &mut Tiled0) { pub fn display_logo(gfx: &mut BackgroundDistributor) {
use super::tiled0::Map; use super::background::Map;
gfx.set_background_palettes(agb_logo::test_logo.palettes); gfx.set_background_palettes(agb_logo::test_logo.palettes);
gfx.set_background_tilemap(0, agb_logo::test_logo.tiles); gfx.set_background_tilemap(0, agb_logo::test_logo.tiles);
let mut back = gfx.get_background().unwrap(); let mut back = gfx.get_regular().unwrap();
let mut entries: [u16; 30 * 20] = [0; 30 * 20]; let mut entries: [u16; 30 * 20] = [0; 30 * 20];
for tile_id in 0..(30 * 20) { for tile_id in 0..(30 * 20) {

View file

@ -5,6 +5,8 @@ use video::Video;
use self::object::ObjectControl; use self::object::ObjectControl;
/// Graphics mode 0. Four regular backgrounds.
pub mod background;
/// Graphics mode 3. Bitmap mode that provides a 16-bit colour framebuffer. /// Graphics mode 3. Bitmap mode that provides a 16-bit colour framebuffer.
pub mod bitmap3; pub mod bitmap3;
/// Graphics mode 4. Bitmap 4 provides two 8-bit paletted framebuffers with page switching. /// Graphics mode 4. Bitmap 4 provides two 8-bit paletted framebuffers with page switching.
@ -17,8 +19,6 @@ pub mod object;
pub mod palette16; pub mod palette16;
/// Data produced by agb-image-converter /// Data produced by agb-image-converter
pub mod tile_data; pub mod tile_data;
/// Graphics mode 0. Four regular backgrounds.
pub mod tiled0;
/// Giving out graphics mode. /// Giving out graphics mode.
pub mod video; pub mod video;

View file

@ -1,4 +1,4 @@
use super::{bitmap3::Bitmap3, bitmap4::Bitmap4, tiled0::Tiled0}; use super::{background::BackgroundDistributor, bitmap3::Bitmap3, bitmap4::Bitmap4};
#[non_exhaustive] #[non_exhaustive]
pub struct Video {} pub struct Video {}
@ -14,7 +14,7 @@ impl Video {
unsafe { Bitmap4::new() } unsafe { Bitmap4::new() }
} }
pub fn tiled0(&mut self) -> Tiled0 { pub fn tiled0(&mut self) -> BackgroundDistributor {
unsafe { Tiled0::new() } unsafe { BackgroundDistributor::new() }
} }
} }