clean up
This commit is contained in:
parent
e2e1ee2db9
commit
b09922e0ea
@ -1,4 +1,4 @@
|
||||
use crate::hardware::memory_banks::GfxTileBankPoolInstaller;
|
||||
use crate::hardware::memory_banks::TileBankPoolInstaller;
|
||||
use crate::model::{AssetEntry, BankStats, BankType, Color, HandleId, LoadStatus, SlotRef, SlotStats, TileBank, TileSize};
|
||||
use std::collections::HashMap;
|
||||
use std::sync::{Arc, Mutex, RwLock};
|
||||
@ -100,7 +100,7 @@ pub struct AssetManager {
|
||||
assets_data: Arc<RwLock<Vec<u8>>>,
|
||||
|
||||
/// Narrow hardware interfaces
|
||||
gfx_installer: Arc<dyn GfxTileBankPoolInstaller>,
|
||||
gfx_installer: Arc<dyn TileBankPoolInstaller>,
|
||||
|
||||
/// Track what is installed in each hardware slot (for stats/info).
|
||||
gfx_slots: Arc<RwLock<[Option<String>; 16]>>,
|
||||
@ -120,9 +120,9 @@ struct LoadHandleInfo {
|
||||
|
||||
impl AssetManager {
|
||||
pub fn new(
|
||||
assets: Vec<AssetEntry>,
|
||||
assets_data: Vec<u8>,
|
||||
gfx_installer: Arc<dyn GfxTileBankPoolInstaller>,
|
||||
assets: Vec<AssetEntry>,
|
||||
assets_data: Vec<u8>,
|
||||
gfx_installer: Arc<dyn TileBankPoolInstaller>,
|
||||
) -> Self {
|
||||
let mut asset_map = HashMap::new();
|
||||
for entry in assets {
|
||||
@ -430,12 +430,12 @@ impl AssetManager {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::hardware::memory_banks::{GfxTileBankPoolAccess, MemoryBanks};
|
||||
use crate::hardware::memory_banks::{TileBankPoolAccess, MemoryBanks};
|
||||
|
||||
#[test]
|
||||
fn test_asset_loading_flow() {
|
||||
let banks = Arc::new(MemoryBanks::new());
|
||||
let gfx_installer = Arc::clone(&banks) as Arc<dyn GfxTileBankPoolInstaller>;
|
||||
let gfx_installer = Arc::clone(&banks) as Arc<dyn TileBankPoolInstaller>;
|
||||
|
||||
let mut data = vec![1u8; 256];
|
||||
data.extend_from_slice(&[0u8; 2048]);
|
||||
@ -483,7 +483,7 @@ mod tests {
|
||||
#[test]
|
||||
fn test_asset_dedup() {
|
||||
let banks = Arc::new(MemoryBanks::new());
|
||||
let gfx_installer = Arc::clone(&banks) as Arc<dyn GfxTileBankPoolInstaller>;
|
||||
let gfx_installer = Arc::clone(&banks) as Arc<dyn TileBankPoolInstaller>;
|
||||
|
||||
let mut data = vec![1u8; 256];
|
||||
data.extend_from_slice(&[0u8; 2048]);
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
use crate::hardware::memory_banks::GfxTileBankPoolAccess;
|
||||
use crate::hardware::memory_banks::TileBankPoolAccess;
|
||||
use crate::model::{Color, HudTileLayer, ScrollableTileLayer, Sprite, TileBank, TileMap, TileSize};
|
||||
use std::sync::Arc;
|
||||
|
||||
@ -48,7 +48,7 @@ pub struct Gfx {
|
||||
/// 1 fixed layer for User Interface.
|
||||
pub hud: HudTileLayer,
|
||||
/// Interface to access graphical memory banks.
|
||||
pub tile_banks: Arc<dyn GfxTileBankPoolAccess>,
|
||||
pub tile_banks: Arc<dyn TileBankPoolAccess>,
|
||||
/// Hardware sprites (Object Attribute Memory equivalent).
|
||||
pub sprites: [Sprite; 512],
|
||||
|
||||
@ -67,7 +67,7 @@ pub struct Gfx {
|
||||
|
||||
impl Gfx {
|
||||
/// Initializes the graphics system with a specific resolution and shared memory banks.
|
||||
pub fn new(w: usize, h: usize, tile_banks: Arc<dyn GfxTileBankPoolAccess>) -> Self {
|
||||
pub fn new(w: usize, h: usize, tile_banks: Arc<dyn TileBankPoolAccess>) -> Self {
|
||||
const EMPTY_SPRITE: Sprite = Sprite {
|
||||
tile: crate::model::Tile { id: 0, flip_x: false, flip_y: false, palette_id: 0 },
|
||||
x: 0,
|
||||
@ -373,7 +373,7 @@ impl Gfx {
|
||||
Self::render_hud_with_pool(&mut self.back, self.w, self.h, &self.hud, &*self.tile_banks);
|
||||
}
|
||||
|
||||
fn render_hud_with_pool(back: &mut [u16], w: usize, h: usize, hud: &HudTileLayer, tile_banks: &dyn GfxTileBankPoolAccess) {
|
||||
fn render_hud_with_pool(back: &mut [u16], w: usize, h: usize, hud: &HudTileLayer, tile_banks: &dyn TileBankPoolAccess) {
|
||||
let bank_id = hud.bank_id as usize;
|
||||
let bank = match tile_banks.tile_bank_slot(bank_id) {
|
||||
Some(b) => b,
|
||||
@ -468,7 +468,7 @@ impl Gfx {
|
||||
screen_h: usize,
|
||||
bucket: &[usize],
|
||||
sprites: &[Sprite],
|
||||
tile_banks: &dyn GfxTileBankPoolAccess,
|
||||
tile_banks: &dyn TileBankPoolAccess,
|
||||
) {
|
||||
for &idx in bucket {
|
||||
let s = &sprites[idx];
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
use crate::hardware::{AssetManager, Audio, Gfx, HardwareBridge, Pad, Touch, MemoryBanks};
|
||||
use crate::hardware::memory_banks::{GfxTileBankPoolAccess, GfxTileBankPoolInstaller};
|
||||
use crate::hardware::memory_banks::{TileBankPoolAccess, TileBankPoolInstaller};
|
||||
use std::sync::Arc;
|
||||
|
||||
/// Aggregate structure for all virtual hardware peripherals.
|
||||
@ -51,14 +51,14 @@ impl Hardware {
|
||||
let memory_banks = Arc::new(MemoryBanks::new());
|
||||
Self {
|
||||
memory_banks: Arc::clone(&memory_banks),
|
||||
gfx: Gfx::new(Self::W, Self::H, Arc::clone(&memory_banks) as Arc<dyn GfxTileBankPoolAccess>),
|
||||
gfx: Gfx::new(Self::W, Self::H, Arc::clone(&memory_banks) as Arc<dyn TileBankPoolAccess>),
|
||||
audio: Audio::new(),
|
||||
pad: Pad::default(),
|
||||
touch: Touch::default(),
|
||||
assets: AssetManager::new(
|
||||
vec![],
|
||||
vec![],
|
||||
Arc::clone(&memory_banks) as Arc<dyn GfxTileBankPoolInstaller>,
|
||||
vec![],
|
||||
vec![],
|
||||
Arc::clone(&memory_banks) as Arc<dyn TileBankPoolInstaller>,
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
@ -2,7 +2,7 @@ use std::sync::{Arc, RwLock};
|
||||
use crate::model::TileBank;
|
||||
|
||||
/// Non-generic interface for peripherals to access graphical tile banks.
|
||||
pub trait GfxTileBankPoolAccess: Send + Sync {
|
||||
pub trait TileBankPoolAccess: Send + Sync {
|
||||
/// Returns a reference to the resident TileBank in the specified slot, if any.
|
||||
fn tile_bank_slot(&self, slot: usize) -> Option<Arc<TileBank>>;
|
||||
/// Returns the total number of slots available in this bank.
|
||||
@ -10,7 +10,7 @@ pub trait GfxTileBankPoolAccess: Send + Sync {
|
||||
}
|
||||
|
||||
/// Non-generic interface for the AssetManager to install graphical tile banks.
|
||||
pub trait GfxTileBankPoolInstaller: Send + Sync {
|
||||
pub trait TileBankPoolInstaller: Send + Sync {
|
||||
/// Atomically swaps the resident TileBank in the specified slot.
|
||||
fn install_tile_bank(&self, slot: usize, bank: Arc<TileBank>);
|
||||
}
|
||||
@ -21,21 +21,21 @@ pub trait GfxTileBankPoolInstaller: Send + Sync {
|
||||
/// Peripherals consume this state via narrow, non-generic traits.
|
||||
/// AssetManager coordinates residency and installs assets into these slots.
|
||||
pub struct MemoryBanks {
|
||||
gfx_tile_bank_pool: Arc<RwLock<[Option<Arc<TileBank>>; 16]>>,
|
||||
tile_bank_pool: Arc<RwLock<[Option<Arc<TileBank>>; 16]>>,
|
||||
}
|
||||
|
||||
impl MemoryBanks {
|
||||
/// Creates a new set of memory banks with empty slots.
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
gfx_tile_bank_pool: Arc::new(RwLock::new(std::array::from_fn(|_| None))),
|
||||
tile_bank_pool: Arc::new(RwLock::new(std::array::from_fn(|_| None))),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl GfxTileBankPoolAccess for MemoryBanks {
|
||||
impl TileBankPoolAccess for MemoryBanks {
|
||||
fn tile_bank_slot(&self, slot: usize) -> Option<Arc<TileBank>> {
|
||||
let pool = self.gfx_tile_bank_pool.read().unwrap();
|
||||
let pool = self.tile_bank_pool.read().unwrap();
|
||||
pool.get(slot).and_then(|s| s.as_ref().map(Arc::clone))
|
||||
}
|
||||
|
||||
@ -44,9 +44,9 @@ impl GfxTileBankPoolAccess for MemoryBanks {
|
||||
}
|
||||
}
|
||||
|
||||
impl GfxTileBankPoolInstaller for MemoryBanks {
|
||||
impl TileBankPoolInstaller for MemoryBanks {
|
||||
fn install_tile_bank(&self, slot: usize, bank: Arc<TileBank>) {
|
||||
let mut pool = self.gfx_tile_bank_pool.write().unwrap();
|
||||
let mut pool = self.tile_bank_pool.write().unwrap();
|
||||
if slot < 16 {
|
||||
pool[slot] = Some(bank);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user