implements PLN-0088
This commit is contained in:
@@ -12,7 +12,7 @@ pub use crate::audio::{Audio, AudioCommand, Channel, MAX_CHANNELS, OUTPUT_SAMPLE
|
||||
pub use crate::frame_composer::{ComposerBuffer, FrameComposer, SceneStatus, SpriteController};
|
||||
pub use crate::gfx::Gfx;
|
||||
pub use crate::memory_banks::{
|
||||
GlyphBankPoolAccess, GlyphBankPoolInstaller, MemoryBanks, SceneBankPoolAccess,
|
||||
SceneBankPoolInstaller, SoundBankPoolAccess, SoundBankPoolInstaller,
|
||||
GlyphBankPoolAccess, GlyphBankPoolInstaller, MemoryBanks, RenderResourceAccess,
|
||||
SceneBankPoolAccess, SceneBankPoolInstaller, SoundBankPoolAccess, SoundBankPoolInstaller,
|
||||
};
|
||||
pub use crate::pad::Pad;
|
||||
|
||||
@@ -1,3 +1,11 @@
|
||||
//! Memory bank access boundary.
|
||||
//!
|
||||
//! Render consumers must cross this boundary through small, read-only traits.
|
||||
//! Frame packets carry resource IDs; large resident resources stay in banks and
|
||||
//! are returned as `Arc` snapshots of the slot contents. Installation and slot
|
||||
//! replacement remain separate owner-side operations and are not part of the
|
||||
//! render read surface.
|
||||
|
||||
use prometeu_hal::glyph_bank::GlyphBank;
|
||||
use prometeu_hal::scene_bank::SceneBank;
|
||||
use prometeu_hal::sound_bank::SoundBank;
|
||||
@@ -45,6 +53,15 @@ pub trait SceneBankPoolInstaller: Send + Sync {
|
||||
fn install_scene_bank(&self, slot: usize, bank: Arc<SceneBank>);
|
||||
}
|
||||
|
||||
/// Read-only resource surface needed by render consumers.
|
||||
///
|
||||
/// This is intentionally narrower than `MemoryBanks`: render code can resolve
|
||||
/// stable packet IDs to resident glyph/scene banks, but cannot install or swap
|
||||
/// resources through this trait.
|
||||
pub trait RenderResourceAccess: GlyphBankPoolAccess + SceneBankPoolAccess {}
|
||||
|
||||
impl<T> RenderResourceAccess for T where T: GlyphBankPoolAccess + SceneBankPoolAccess {}
|
||||
|
||||
/// Centralized container for all hardware memory banks.
|
||||
///
|
||||
/// MemoryBanks represent the actual hardware slot state.
|
||||
@@ -132,3 +149,68 @@ impl SceneBankPoolInstaller for MemoryBanks {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use prometeu_hal::color::Color;
|
||||
use prometeu_hal::glyph::Glyph;
|
||||
use prometeu_hal::glyph_bank::TileSize;
|
||||
use prometeu_hal::scene_layer::{ParallaxFactor, SceneLayer};
|
||||
use prometeu_hal::tile::Tile;
|
||||
use prometeu_hal::tilemap::TileMap;
|
||||
|
||||
fn assert_send_sync<T: Send + Sync>() {}
|
||||
|
||||
fn make_glyph_bank() -> GlyphBank {
|
||||
let mut bank = GlyphBank::new(TileSize::Size8, 8, 8);
|
||||
bank.palettes[0][1] = Color::WHITE;
|
||||
bank
|
||||
}
|
||||
|
||||
fn make_scene_bank() -> SceneBank {
|
||||
let layer = SceneLayer {
|
||||
active: true,
|
||||
glyph_asset_id: 0,
|
||||
tile_size: TileSize::Size8,
|
||||
parallax_factor: ParallaxFactor { x: 1.0, y: 1.0 },
|
||||
tilemap: TileMap {
|
||||
width: 1,
|
||||
height: 1,
|
||||
tiles: vec![Tile {
|
||||
active: true,
|
||||
glyph: Glyph { glyph_id: 0, palette_id: 0 },
|
||||
flip_x: false,
|
||||
flip_y: false,
|
||||
}],
|
||||
},
|
||||
};
|
||||
SceneBank { layers: std::array::from_fn(|_| layer.clone()) }
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn render_resource_access_traits_are_thread_safe() {
|
||||
assert_send_sync::<MemoryBanks>();
|
||||
assert_send_sync::<Arc<dyn GlyphBankPoolAccess>>();
|
||||
assert_send_sync::<Arc<dyn SceneBankPoolAccess>>();
|
||||
assert_send_sync::<Arc<dyn RenderResourceAccess>>();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn render_resource_access_exposes_read_only_glyph_and_scene_banks() {
|
||||
let banks = Arc::new(MemoryBanks::new());
|
||||
banks.install_glyph_bank(0, Arc::new(make_glyph_bank()));
|
||||
banks.install_scene_bank(1, Arc::new(make_scene_bank()));
|
||||
|
||||
let render_resources: Arc<dyn RenderResourceAccess> = banks;
|
||||
let glyph_bank =
|
||||
render_resources.glyph_bank_slot(0).expect("glyph bank slot should be readable");
|
||||
let scene_bank =
|
||||
render_resources.scene_bank_slot(1).expect("scene bank slot should be readable");
|
||||
|
||||
assert_eq!(glyph_bank.tile_size, TileSize::Size8);
|
||||
assert_eq!(scene_bank.layers[0].tile_size, TileSize::Size8);
|
||||
assert!(render_resources.glyph_bank_slot(15).is_none());
|
||||
assert!(render_resources.scene_bank_slot(15).is_none());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user