Start fixing 256 colours

This commit is contained in:
Gwilym Inzani 2023-08-29 16:07:50 +01:00
parent 9db4230aee
commit a865240308
4 changed files with 31 additions and 43 deletions

View file

@ -10,7 +10,7 @@ use crate::memory_mapped::MemoryMapped;
use super::{ use super::{
AffineBackgroundSize, BackgroundID, BackgroundSize, BackgroundSizePrivate, AffineBackgroundSize, BackgroundID, BackgroundSize, BackgroundSizePrivate,
RegularBackgroundSize, Tile, TileFormat, TileIndex, TileSet, TileSetting, VRamManager, RegularBackgroundSize, Tile, TileFormat, TileSet, TileSetting, VRamManager,
}; };
use alloc::{vec, vec::Vec}; use alloc::{vec, vec::Vec};
@ -20,10 +20,9 @@ pub trait TiledMapTypes: private::Sealed {
} }
trait TiledMapPrivate: TiledMapTypes { trait TiledMapPrivate: TiledMapTypes {
type TileType: Into<TileIndex> + Copy + Default + Eq + PartialEq;
type AffineMatrix; type AffineMatrix;
fn tiles_mut(&mut self) -> &mut [Self::TileType]; fn tiles_mut(&mut self) -> &mut [Tile];
fn tiles_dirty(&mut self) -> &mut bool; fn tiles_dirty(&mut self) -> &mut bool;
fn colours(&self) -> TileFormat; fn colours(&self) -> TileFormat;
@ -59,9 +58,11 @@ where
T::Size: BackgroundSizePrivate, T::Size: BackgroundSizePrivate,
{ {
fn clear(&mut self, vram: &mut VRamManager) { fn clear(&mut self, vram: &mut VRamManager) {
let colours = self.colours();
for tile in self.tiles_mut() { for tile in self.tiles_mut() {
if *tile != Default::default() { if *tile != Default::default() {
vram.remove_tile((*tile).into()); vram.remove_tile(tile.tile_index(colours));
} }
*tile = Default::default(); *tile = Default::default();
@ -134,10 +135,9 @@ impl TiledMapTypes for RegularMap {
} }
impl TiledMapPrivate for RegularMap { impl TiledMapPrivate for RegularMap {
type TileType = Tile;
type AffineMatrix = (); type AffineMatrix = ();
fn tiles_mut(&mut self) -> &mut [Self::TileType] { fn tiles_mut(&mut self) -> &mut [Tile] {
&mut self.tiles &mut self.tiles
} }
fn tiles_dirty(&mut self) -> &mut bool { fn tiles_dirty(&mut self) -> &mut bool {
@ -195,11 +195,12 @@ impl RegularMap {
tileset: &TileSet<'_>, tileset: &TileSet<'_>,
tile_setting: TileSetting, tile_setting: TileSetting,
) { ) {
if tileset.format() != self.colours() { let colours = self.colours();
if tileset.format() != colours {
panic!( panic!(
"Cannot set a {:?} colour tile on a {:?} colour background", "Cannot set a {:?} colour tile on a {:?} colour background",
tileset.format(), tileset.format(),
self.colours() colours
); );
} }
@ -207,7 +208,7 @@ impl RegularMap {
let old_tile = self.tiles_mut()[pos]; let old_tile = self.tiles_mut()[pos];
if old_tile != Tile::default() { if old_tile != Tile::default() {
vram.remove_tile(old_tile.into()); vram.remove_tile(old_tile.tile_index(colours));
} }
let tile_index = tile_setting.index(); let tile_index = tile_setting.index();
@ -254,7 +255,7 @@ pub struct AffineMap {
transform: AffineMatrixBackground, transform: AffineMatrixBackground,
tiles: Vec<u8>, tiles: Vec<Tile>,
tiles_dirty: bool, tiles_dirty: bool,
} }
@ -263,10 +264,9 @@ impl TiledMapTypes for AffineMap {
} }
impl TiledMapPrivate for AffineMap { impl TiledMapPrivate for AffineMap {
type TileType = u8;
type AffineMatrix = AffineMatrixBackground; type AffineMatrix = AffineMatrixBackground;
fn tiles_mut(&mut self) -> &mut [Self::TileType] { fn tiles_mut(&mut self) -> &mut [Tile] {
&mut self.tiles &mut self.tiles
} }
fn tiles_dirty(&mut self) -> &mut bool { fn tiles_dirty(&mut self) -> &mut bool {
@ -320,19 +320,20 @@ impl AffineMap {
tile_id: u8, tile_id: u8,
) { ) {
let pos = self.map_size().gba_offset(pos); let pos = self.map_size().gba_offset(pos);
let colours = self.colours();
let old_tile = self.tiles_mut()[pos]; let old_tile = self.tiles_mut()[pos];
if old_tile != 0 { if old_tile != Tile::default() {
vram.remove_tile(old_tile.into()); vram.remove_tile(old_tile.tile_index(colours));
} }
let tile_index = tile_id as u16; let tile_index = tile_id as u16;
let new_tile = if tile_index != TRANSPARENT_TILE_INDEX { let new_tile = if tile_index != TRANSPARENT_TILE_INDEX {
let new_tile_idx = vram.add_tile(tileset, tile_index); let new_tile_idx = vram.add_tile(tileset, tile_index);
new_tile_idx.raw_index() as u8 Tile::new(new_tile_idx, TileSetting(0))
} else { } else {
0 Tile::default()
}; };
if old_tile == new_tile { if old_tile == new_tile {

View file

@ -156,8 +156,8 @@ impl Tile {
Self(idx.raw_index() | setting.setting()) Self(idx.raw_index() | setting.setting())
} }
fn tile_index(self) -> TileIndex { fn tile_index(self, format: TileFormat) -> TileIndex {
TileIndex::new(self.0 as usize & ((1 << 10) - 1), TileFormat::FourBpp) TileIndex::new(self.0 as usize & ((1 << 10) - 1), format)
} }
} }

View file

@ -2,7 +2,6 @@ use core::{alloc::Layout, ptr::NonNull};
use alloc::{slice, vec::Vec}; use alloc::{slice, vec::Vec};
use crate::display::tiled::Tile;
use crate::{ use crate::{
agb_alloc::{block_allocator::BlockAllocator, bump_allocator::StartEnd}, agb_alloc::{block_allocator::BlockAllocator, bump_allocator::StartEnd},
display::palette16, display::palette16,
@ -66,21 +65,21 @@ impl<'a> TileSet<'a> {
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub enum TileIndex { pub enum TileIndex {
FourBpp(u16), FourBpp(u16),
EightBpp(u8), EightBpp(u16),
} }
impl TileIndex { impl TileIndex {
pub(crate) const fn new(index: usize, format: TileFormat) -> Self { pub(crate) const fn new(index: usize, format: TileFormat) -> Self {
match format { match format {
TileFormat::FourBpp => Self::FourBpp(index as u16), TileFormat::FourBpp => Self::FourBpp(index as u16),
TileFormat::EightBpp => Self::EightBpp(index as u8), TileFormat::EightBpp => Self::EightBpp(index as u16),
} }
} }
pub(crate) const fn raw_index(self) -> u16 { pub(crate) const fn raw_index(self) -> u16 {
match self { match self {
TileIndex::FourBpp(x) => x, TileIndex::FourBpp(x) => x,
TileIndex::EightBpp(x) => x as u16, TileIndex::EightBpp(x) => x,
} }
} }
@ -99,18 +98,6 @@ impl TileIndex {
} }
} }
impl From<Tile> for TileIndex {
fn from(tile: Tile) -> Self {
tile.tile_index()
}
}
impl From<u8> for TileIndex {
fn from(index: u8) -> TileIndex {
TileIndex::new(usize::from(index), TileFormat::EightBpp)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
struct TileReference(NonNull<u32>); struct TileReference(NonNull<u32>);

View file

@ -47,10 +47,10 @@ impl Game {
include_background_gfx!( include_background_gfx!(
games, "121105", games, "121105",
hat => deduplicate "gfx/hat.png", hat => 256 deduplicate "gfx/hat.png",
purple => deduplicate "gfx/purple.png", purple => 256 deduplicate "gfx/purple.png",
hyperspace => deduplicate "gfx/hyperspace.png", hyperspace => 256 deduplicate "gfx/hyperspace.png",
amplitude => deduplicate "gfx/amplitude.png" amplitude => 256 deduplicate "gfx/amplitude.png"
); );
fn get_game(gba: &mut agb::Gba) -> Game { fn get_game(gba: &mut agb::Gba) -> Game {
@ -59,10 +59,10 @@ fn get_game(gba: &mut agb::Gba) -> Game {
let (tile, mut vram) = gba.display.video.tiled0(); let (tile, mut vram) = gba.display.video.tiled0();
let hat = TileSet::new(games::hat.tiles, TileFormat::FourBpp); let hat = TileSet::new(games::hat.tiles, TileFormat::EightBpp);
let purple = TileSet::new(games::purple.tiles, TileFormat::FourBpp); let purple = TileSet::new(games::purple.tiles, TileFormat::EightBpp);
let hyperspace = TileSet::new(games::hyperspace.tiles, TileFormat::FourBpp); let hyperspace = TileSet::new(games::hyperspace.tiles, TileFormat::EightBpp);
let amplitude = TileSet::new(games::amplitude.tiles, TileFormat::FourBpp); let amplitude = TileSet::new(games::amplitude.tiles, TileFormat::EightBpp);
let tiles = [hat, purple, hyperspace, amplitude]; let tiles = [hat, purple, hyperspace, amplitude];
@ -79,7 +79,7 @@ fn get_game(gba: &mut agb::Gba) -> Game {
tile.background( tile.background(
Priority::P0, Priority::P0,
RegularBackgroundSize::Background32x32, RegularBackgroundSize::Background32x32,
TileFormat::FourBpp, TileFormat::EightBpp,
), ),
Box::new(|pos| { Box::new(|pos| {
let y = pos.y.rem_euclid(20); let y = pos.y.rem_euclid(20);