Simplify tiled0-2

This commit is contained in:
Gwilym Kuiper 2022-08-11 20:51:15 +01:00
parent d59e2672c0
commit 31f85cf6b9
4 changed files with 36 additions and 33 deletions

View file

@ -210,15 +210,21 @@ pub(self) fn find_screenblock_gap(screenblocks: &Bitarray<1>, gap: usize) -> usi
); );
} }
pub(self) trait TiledMode { trait TiledMode {
const REGULAR_BACKGROUNDS: usize;
const AFFINE_BACKGROUNDS: usize;
fn screenblocks(&self) -> &RefCell<Bitarray<1>>; fn screenblocks(&self) -> &RefCell<Bitarray<1>>;
}
trait CreatableRegularTiledMode: TiledMode {
const REGULAR_BACKGROUNDS: usize;
fn regular(&self) -> &RefCell<Bitarray<1>>; fn regular(&self) -> &RefCell<Bitarray<1>>;
}
trait CreatableAffineTiledMode: TiledMode {
const AFFINE_BACKGROUNDS: usize;
fn affine(&self) -> &RefCell<Bitarray<1>>; fn affine(&self) -> &RefCell<Bitarray<1>>;
} }
pub trait RegularTiledMode { trait RegularTiledMode {
fn regular_background( fn regular_background(
&self, &self,
priority: Priority, priority: Priority,
@ -226,7 +232,7 @@ pub trait RegularTiledMode {
) -> MapLoan<'_, RegularMap>; ) -> MapLoan<'_, RegularMap>;
} }
pub trait AffineTiledMode { trait AffineTiledMode {
fn affine_background( fn affine_background(
&self, &self,
priority: Priority, priority: Priority,
@ -234,15 +240,9 @@ pub trait AffineTiledMode {
) -> MapLoan<'_, AffineMap>; ) -> MapLoan<'_, AffineMap>;
} }
// withoutboats: https://internals.rust-lang.org/t/const-generics-where-restrictions/12742/6
pub struct If<const B: bool>;
pub trait True {}
impl True for If<true> {}
impl<T> RegularTiledMode for T impl<T> RegularTiledMode for T
where where
T: TiledMode, T: CreatableRegularTiledMode,
If<{ T::REGULAR_BACKGROUNDS > 0 }>: True,
{ {
fn regular_background( fn regular_background(
&self, &self,
@ -283,8 +283,7 @@ where
impl<T> AffineTiledMode for T impl<T> AffineTiledMode for T
where where
T: TiledMode, T: CreatableAffineTiledMode,
If<{ T::AFFINE_BACKGROUNDS > 0 }>: True,
{ {
fn affine_background( fn affine_background(
&self, &self,

View file

@ -1,6 +1,9 @@
use core::cell::RefCell; use core::cell::RefCell;
use super::{MapLoan, RegularBackgroundSize, RegularMap, RegularTiledMode, TiledMode}; use super::{
CreatableRegularTiledMode, MapLoan, RegularBackgroundSize, RegularMap, RegularTiledMode,
TiledMode,
};
use crate::{ use crate::{
bitarray::Bitarray, bitarray::Bitarray,
display::{set_graphics_mode, DisplayMode, Priority}, display::{set_graphics_mode, DisplayMode, Priority},
@ -31,18 +34,15 @@ impl Tiled0 {
} }
impl TiledMode for Tiled0 { impl TiledMode for Tiled0 {
const REGULAR_BACKGROUNDS: usize = 4;
const AFFINE_BACKGROUNDS: usize = 0;
fn screenblocks(&self) -> &RefCell<Bitarray<1>> { fn screenblocks(&self) -> &RefCell<Bitarray<1>> {
&self.screenblocks &self.screenblocks
} }
}
impl CreatableRegularTiledMode for Tiled0 {
const REGULAR_BACKGROUNDS: usize = 4;
fn regular(&self) -> &RefCell<Bitarray<1>> { fn regular(&self) -> &RefCell<Bitarray<1>> {
&self.regular &self.regular
} }
fn affine(&self) -> &RefCell<Bitarray<1>> {
unimplemented!()
}
} }

View file

@ -1,6 +1,6 @@
use core::cell::RefCell; use core::cell::RefCell;
use super::TiledMode; use super::{CreatableAffineTiledMode, CreatableRegularTiledMode, TiledMode};
use crate::{ use crate::{
bitarray::Bitarray, bitarray::Bitarray,
display::{set_graphics_mode, tiled::AFFINE_BG_ID_OFFSET, DisplayMode}, display::{set_graphics_mode, tiled::AFFINE_BG_ID_OFFSET, DisplayMode},
@ -30,16 +30,21 @@ impl Tiled1 {
} }
impl TiledMode for Tiled1 { impl TiledMode for Tiled1 {
const REGULAR_BACKGROUNDS: usize = 2;
const AFFINE_BACKGROUNDS: usize = 1;
fn screenblocks(&self) -> &RefCell<Bitarray<1>> { fn screenblocks(&self) -> &RefCell<Bitarray<1>> {
&self.screenblocks &self.screenblocks
} }
}
impl CreatableRegularTiledMode for Tiled1 {
const REGULAR_BACKGROUNDS: usize = 2;
fn regular(&self) -> &RefCell<Bitarray<1>> { fn regular(&self) -> &RefCell<Bitarray<1>> {
&self.regular &self.regular
} }
}
impl CreatableAffineTiledMode for Tiled1 {
const AFFINE_BACKGROUNDS: usize = 1;
fn affine(&self) -> &RefCell<Bitarray<1>> { fn affine(&self) -> &RefCell<Bitarray<1>> {
&self.affine &self.affine

View file

@ -1,6 +1,8 @@
use core::cell::RefCell; use core::cell::RefCell;
use super::{AffineBackgroundSize, AffineMap, AffineTiledMode, MapLoan, TiledMode}; use super::{
AffineBackgroundSize, AffineMap, AffineTiledMode, CreatableAffineTiledMode, MapLoan, TiledMode,
};
use crate::{ use crate::{
bitarray::Bitarray, bitarray::Bitarray,
display::{set_graphics_mode, tiled::AFFINE_BG_ID_OFFSET, DisplayMode, Priority}, display::{set_graphics_mode, tiled::AFFINE_BG_ID_OFFSET, DisplayMode, Priority},
@ -36,16 +38,13 @@ impl Tiled2 {
} }
impl TiledMode for Tiled2 { impl TiledMode for Tiled2 {
const REGULAR_BACKGROUNDS: usize = 0;
const AFFINE_BACKGROUNDS: usize = 2;
fn screenblocks(&self) -> &RefCell<Bitarray<1>> { fn screenblocks(&self) -> &RefCell<Bitarray<1>> {
&self.screenblocks &self.screenblocks
} }
}
fn regular(&self) -> &RefCell<Bitarray<1>> { impl CreatableAffineTiledMode for Tiled2 {
unimplemented!() const AFFINE_BACKGROUNDS: usize = 2;
}
fn affine(&self) -> &RefCell<Bitarray<1>> { fn affine(&self) -> &RefCell<Bitarray<1>> {
&self.affine &self.affine