implements PLN-0011
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
#![allow(clippy::collapsible_if)]
|
||||
use crate::memory_banks::{GlyphBankPoolInstaller, SoundBankPoolInstaller};
|
||||
use crate::memory_banks::{GlyphBankPoolInstaller, SceneBankPoolInstaller, SoundBankPoolInstaller};
|
||||
use prometeu_hal::AssetBridge;
|
||||
use prometeu_hal::asset::{
|
||||
AssetCodec, AssetEntry, AssetId, AssetLoadError, AssetOpStatus, BankTelemetry, BankType,
|
||||
@@ -9,6 +9,7 @@ use prometeu_hal::cartridge::AssetsPayloadSource;
|
||||
use prometeu_hal::color::Color;
|
||||
use prometeu_hal::glyph_bank::{GlyphBank, TileSize};
|
||||
use prometeu_hal::sample::Sample;
|
||||
use prometeu_hal::scene_bank::SceneBank;
|
||||
use prometeu_hal::sound_bank::SoundBank;
|
||||
use std::collections::HashMap;
|
||||
use std::io::Read;
|
||||
@@ -151,15 +152,19 @@ pub struct AssetManager {
|
||||
/// Narrow hardware interfaces
|
||||
gfx_installer: Arc<dyn GlyphBankPoolInstaller>,
|
||||
sound_installer: Arc<dyn SoundBankPoolInstaller>,
|
||||
scene_installer: Arc<dyn SceneBankPoolInstaller>,
|
||||
|
||||
/// Track what is installed in each hardware slot (for stats/info).
|
||||
gfx_slots: Arc<RwLock<[Option<AssetId>; 16]>>,
|
||||
sound_slots: Arc<RwLock<[Option<AssetId>; 16]>>,
|
||||
scene_slots: Arc<RwLock<[Option<AssetId>; 16]>>,
|
||||
|
||||
/// Residency policy for GFX glyph banks.
|
||||
gfx_policy: BankPolicy<GlyphBank>,
|
||||
/// Residency policy for sound banks.
|
||||
sound_policy: BankPolicy<SoundBank>,
|
||||
/// Residency policy for scene banks.
|
||||
scene_policy: BankPolicy<SceneBank>,
|
||||
|
||||
// Commits that are ready to be applied at the next frame boundary.
|
||||
pending_commits: Mutex<Vec<HandleId>>,
|
||||
@@ -275,6 +280,7 @@ impl AssetManager {
|
||||
match (entry.bank_type, entry.codec) {
|
||||
(BankType::GLYPH, AssetCodec::None) => Ok(AssetOpMode::StageInMemory),
|
||||
(BankType::SOUNDS, AssetCodec::None) => Ok(AssetOpMode::DirectFromSlice),
|
||||
(BankType::SCENE, AssetCodec::None) => Ok(AssetOpMode::DirectFromSlice),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -283,6 +289,7 @@ impl AssetManager {
|
||||
assets_data: AssetsPayloadSource,
|
||||
gfx_installer: Arc<dyn GlyphBankPoolInstaller>,
|
||||
sound_installer: Arc<dyn SoundBankPoolInstaller>,
|
||||
scene_installer: Arc<dyn SceneBankPoolInstaller>,
|
||||
) -> Self {
|
||||
let mut asset_map = HashMap::new();
|
||||
for entry in assets {
|
||||
@@ -293,10 +300,13 @@ impl AssetManager {
|
||||
assets: Arc::new(RwLock::new(asset_map)),
|
||||
gfx_installer,
|
||||
sound_installer,
|
||||
scene_installer,
|
||||
gfx_slots: Arc::new(RwLock::new(std::array::from_fn(|_| None))),
|
||||
sound_slots: Arc::new(RwLock::new(std::array::from_fn(|_| None))),
|
||||
scene_slots: Arc::new(RwLock::new(std::array::from_fn(|_| None))),
|
||||
gfx_policy: BankPolicy::new(),
|
||||
sound_policy: BankPolicy::new(),
|
||||
scene_policy: BankPolicy::new(),
|
||||
handles: Arc::new(RwLock::new(HashMap::new())),
|
||||
next_handle_id: Mutex::new(1),
|
||||
assets_data: Arc::new(RwLock::new(assets_data)),
|
||||
@@ -345,17 +355,7 @@ impl AssetManager {
|
||||
if slot_index < slots.len() {
|
||||
slots[slot_index] = Some(entry.asset_id);
|
||||
}
|
||||
// println!(
|
||||
// "[AssetManager] Preloaded tile asset '{}' (id: {}) into slot {}",
|
||||
// entry.asset_name, entry.asset_id, slot_index
|
||||
// );
|
||||
}
|
||||
// else {
|
||||
// eprintln!(
|
||||
// "[AssetManager] Failed to preload tile asset '{}'",
|
||||
// entry.asset_name
|
||||
// );
|
||||
// }
|
||||
}
|
||||
BankType::SOUNDS => {
|
||||
if let Ok(bank) =
|
||||
@@ -372,23 +372,27 @@ impl AssetManager {
|
||||
if slot_index < slots.len() {
|
||||
slots[slot_index] = Some(entry.asset_id);
|
||||
}
|
||||
// println!(
|
||||
// "[AssetManager] Preloaded sound asset '{}' (id: {}) into slot {}",
|
||||
// entry.asset_name, entry.asset_id, slot_index
|
||||
// );
|
||||
}
|
||||
// else {
|
||||
// eprintln!(
|
||||
// "[AssetManager] Failed to preload sound asset '{}'",
|
||||
// entry.asset_name
|
||||
// );
|
||||
// }
|
||||
}
|
||||
BankType::SCENE => {
|
||||
if let Ok(bank) =
|
||||
Self::perform_load_scene_bank(&entry, self.assets_data.clone())
|
||||
{
|
||||
let bank_arc = Arc::new(bank);
|
||||
self.scene_policy.put_resident(
|
||||
entry.asset_id,
|
||||
Arc::clone(&bank_arc),
|
||||
entry.decoded_size as usize,
|
||||
);
|
||||
self.scene_installer.install_scene_bank(slot_index, bank_arc);
|
||||
let mut slots = self.scene_slots.write().unwrap();
|
||||
if slot_index < slots.len() {
|
||||
slots[slot_index] = Some(entry.asset_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// else {
|
||||
// eprintln!("[AssetManager] Preload failed: asset id '{}' not found in table", item.asset_id);
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -403,6 +407,7 @@ impl AssetManager {
|
||||
let slot = match entry.bank_type {
|
||||
BankType::GLYPH => SlotRef::gfx(slot_index),
|
||||
BankType::SOUNDS => SlotRef::audio(slot_index),
|
||||
BankType::SCENE => SlotRef::scene(slot_index),
|
||||
};
|
||||
|
||||
let mut next_id = self.next_handle_id.lock().unwrap();
|
||||
@@ -427,6 +432,14 @@ impl AssetManager {
|
||||
false
|
||||
}
|
||||
}
|
||||
BankType::SCENE => {
|
||||
if let Some(bank) = self.scene_policy.get_resident(asset_id) {
|
||||
self.scene_policy.stage(handle_id, bank, entry.decoded_size as usize);
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
if already_resident {
|
||||
@@ -450,6 +463,7 @@ impl AssetManager {
|
||||
// Capture policies for the worker thread
|
||||
let gfx_policy = self.gfx_policy.clone();
|
||||
let sound_policy = self.sound_policy.clone();
|
||||
let scene_policy = self.scene_policy.clone();
|
||||
|
||||
thread::spawn(move || {
|
||||
// Update status to LOADING
|
||||
@@ -510,6 +524,34 @@ impl AssetManager {
|
||||
entry_clone.decoded_size as usize,
|
||||
);
|
||||
|
||||
let mut handles_map = handles.write().unwrap();
|
||||
if let Some(h) = handles_map.get_mut(&handle_id) {
|
||||
if h.status == LoadStatus::LOADING {
|
||||
h.status = LoadStatus::READY;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
let mut handles_map = handles.write().unwrap();
|
||||
if let Some(h) = handles_map.get_mut(&handle_id) {
|
||||
h.status = LoadStatus::ERROR;
|
||||
}
|
||||
}
|
||||
}
|
||||
BankType::SCENE => {
|
||||
let result = Self::perform_load_scene_bank(&entry_clone, assets_data);
|
||||
if let Ok(scenebank) = result {
|
||||
let bank_arc = Arc::new(scenebank);
|
||||
let resident_arc = scene_policy.put_resident(
|
||||
asset_id,
|
||||
bank_arc,
|
||||
entry_clone.decoded_size as usize,
|
||||
);
|
||||
scene_policy.stage(
|
||||
handle_id,
|
||||
resident_arc,
|
||||
entry_clone.decoded_size as usize,
|
||||
);
|
||||
|
||||
let mut handles_map = handles.write().unwrap();
|
||||
if let Some(h) = handles_map.get_mut(&handle_id) {
|
||||
if h.status == LoadStatus::LOADING {
|
||||
@@ -642,6 +684,17 @@ impl AssetManager {
|
||||
}
|
||||
}
|
||||
|
||||
fn perform_load_scene_bank(
|
||||
entry: &AssetEntry,
|
||||
assets_data: Arc<RwLock<AssetsPayloadSource>>,
|
||||
) -> Result<SceneBank, String> {
|
||||
let _ = (entry, assets_data);
|
||||
// TODO: SCENE payloads must not be decoded from ad-hoc JSON.
|
||||
// Close and document the binary scene-bank format first, then implement
|
||||
// the runtime decoder here using the canonical binary contract.
|
||||
todo!("SCENE binary payload format is still open")
|
||||
}
|
||||
|
||||
fn decode_sound_bank_from_buffer(
|
||||
entry: &AssetEntry,
|
||||
buffer: &[u8],
|
||||
@@ -709,6 +762,7 @@ impl AssetManager {
|
||||
}
|
||||
self.gfx_policy.take_staging(handle);
|
||||
self.sound_policy.take_staging(handle);
|
||||
self.scene_policy.take_staging(handle);
|
||||
final_status
|
||||
}
|
||||
|
||||
@@ -740,6 +794,16 @@ impl AssetManager {
|
||||
h.status = LoadStatus::COMMITTED;
|
||||
}
|
||||
}
|
||||
BankType::SCENE => {
|
||||
if let Some((bank, _)) = self.scene_policy.take_staging(handle_id) {
|
||||
self.scene_installer.install_scene_bank(h.slot.index, bank);
|
||||
let mut slots = self.scene_slots.write().unwrap();
|
||||
if h.slot.index < slots.len() {
|
||||
slots[h.slot.index] = Some(h._asset_id);
|
||||
}
|
||||
h.status = LoadStatus::COMMITTED;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -747,7 +811,11 @@ impl AssetManager {
|
||||
}
|
||||
|
||||
pub fn bank_telemetry(&self) -> Vec<BankTelemetry> {
|
||||
vec![self.bank_telemetry_for(BankType::GLYPH), self.bank_telemetry_for(BankType::SOUNDS)]
|
||||
vec![
|
||||
self.bank_telemetry_for(BankType::GLYPH),
|
||||
self.bank_telemetry_for(BankType::SOUNDS),
|
||||
self.bank_telemetry_for(BankType::SCENE),
|
||||
]
|
||||
}
|
||||
|
||||
fn bank_telemetry_for(&self, kind: BankType) -> BankTelemetry {
|
||||
@@ -758,6 +826,9 @@ impl AssetManager {
|
||||
BankType::SOUNDS => {
|
||||
self.sound_slots.read().unwrap().iter().filter(|slot| slot.is_some()).count()
|
||||
}
|
||||
BankType::SCENE => {
|
||||
self.scene_slots.read().unwrap().iter().filter(|slot| slot.is_some()).count()
|
||||
}
|
||||
};
|
||||
|
||||
BankTelemetry { bank_type: kind, used_slots, total_slots: 16 }
|
||||
@@ -805,6 +876,27 @@ impl AssetManager {
|
||||
(0, None)
|
||||
};
|
||||
|
||||
SlotStats { asset_id, asset_name, generation: 0, resident_bytes: bytes }
|
||||
}
|
||||
BankType::SCENE => {
|
||||
let slots = self.scene_slots.read().unwrap();
|
||||
let asset_id = slots.get(slot.index).and_then(|s| *s);
|
||||
|
||||
let (bytes, asset_name) = if let Some(id) = &asset_id {
|
||||
let bytes = self
|
||||
.scene_policy
|
||||
.resident
|
||||
.read()
|
||||
.unwrap()
|
||||
.get(id)
|
||||
.map(|entry| entry.bytes)
|
||||
.unwrap_or(0);
|
||||
let name = self.assets.read().unwrap().get(id).map(|e| e.asset_name.clone());
|
||||
(bytes, name)
|
||||
} else {
|
||||
(0, None)
|
||||
};
|
||||
|
||||
SlotStats { asset_id, asset_name, generation: 0, resident_bytes: bytes }
|
||||
}
|
||||
}
|
||||
@@ -813,10 +905,12 @@ impl AssetManager {
|
||||
pub fn shutdown(&self) {
|
||||
self.gfx_policy.clear();
|
||||
self.sound_policy.clear();
|
||||
self.scene_policy.clear();
|
||||
self.handles.write().unwrap().clear();
|
||||
self.pending_commits.lock().unwrap().clear();
|
||||
self.gfx_slots.write().unwrap().fill(None);
|
||||
self.sound_slots.write().unwrap().fill(None);
|
||||
self.scene_slots.write().unwrap().fill(None);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -933,11 +1027,28 @@ mod tests {
|
||||
assert_eq!(AssetManager::op_mode_for(&entry), Ok(AssetOpMode::DirectFromSlice));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_op_mode_for_scene_none_reads_direct_from_slice() {
|
||||
let entry = AssetEntry {
|
||||
asset_id: 2,
|
||||
asset_name: "scene".to_string(),
|
||||
bank_type: BankType::SCENE,
|
||||
offset: 0,
|
||||
size: 32,
|
||||
decoded_size: 32,
|
||||
codec: AssetCodec::None,
|
||||
metadata: serde_json::json!({}),
|
||||
};
|
||||
|
||||
assert_eq!(AssetManager::op_mode_for(&entry), Ok(AssetOpMode::DirectFromSlice));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_asset_loading_flow() {
|
||||
let banks = Arc::new(MemoryBanks::new());
|
||||
let gfx_installer = Arc::clone(&banks) as Arc<dyn GlyphBankPoolInstaller>;
|
||||
let sound_installer = Arc::clone(&banks) as Arc<dyn SoundBankPoolInstaller>;
|
||||
let scene_installer = Arc::clone(&banks) as Arc<dyn SceneBankPoolInstaller>;
|
||||
|
||||
let data = test_glyph_asset_data();
|
||||
let asset_entry = test_glyph_asset_entry("test_glyphs", 16, 16);
|
||||
@@ -947,6 +1058,7 @@ mod tests {
|
||||
AssetsPayloadSource::from_bytes(data),
|
||||
gfx_installer,
|
||||
sound_installer,
|
||||
scene_installer,
|
||||
);
|
||||
let handle = am.load(0, 0).expect("Should start loading");
|
||||
|
||||
@@ -976,6 +1088,7 @@ mod tests {
|
||||
let banks = Arc::new(MemoryBanks::new());
|
||||
let gfx_installer = Arc::clone(&banks) as Arc<dyn GlyphBankPoolInstaller>;
|
||||
let sound_installer = Arc::clone(&banks) as Arc<dyn SoundBankPoolInstaller>;
|
||||
let scene_installer = Arc::clone(&banks) as Arc<dyn SceneBankPoolInstaller>;
|
||||
|
||||
let data = test_glyph_asset_data();
|
||||
let asset_entry = test_glyph_asset_entry("test_glyphs", 16, 16);
|
||||
@@ -985,6 +1098,7 @@ mod tests {
|
||||
AssetsPayloadSource::from_bytes(data),
|
||||
gfx_installer,
|
||||
sound_installer,
|
||||
scene_installer,
|
||||
);
|
||||
|
||||
let handle1 = am.load(0, 0).unwrap();
|
||||
@@ -1007,6 +1121,7 @@ mod tests {
|
||||
let banks = Arc::new(MemoryBanks::new());
|
||||
let gfx_installer = Arc::clone(&banks) as Arc<dyn GlyphBankPoolInstaller>;
|
||||
let sound_installer = Arc::clone(&banks) as Arc<dyn SoundBankPoolInstaller>;
|
||||
let scene_installer = Arc::clone(&banks) as Arc<dyn SceneBankPoolInstaller>;
|
||||
|
||||
// 100 samples of 16-bit PCM (zeros)
|
||||
let data = vec![0u8; 200];
|
||||
@@ -1030,6 +1145,7 @@ mod tests {
|
||||
AssetsPayloadSource::from_bytes(data),
|
||||
gfx_installer,
|
||||
sound_installer,
|
||||
scene_installer,
|
||||
);
|
||||
let handle = am.load(1, 0).expect("Should start loading");
|
||||
|
||||
@@ -1052,6 +1168,7 @@ mod tests {
|
||||
let banks = Arc::new(MemoryBanks::new());
|
||||
let gfx_installer = Arc::clone(&banks) as Arc<dyn GlyphBankPoolInstaller>;
|
||||
let sound_installer = Arc::clone(&banks) as Arc<dyn SoundBankPoolInstaller>;
|
||||
let scene_installer = Arc::clone(&banks) as Arc<dyn SceneBankPoolInstaller>;
|
||||
|
||||
let data = vec![0u8; 200];
|
||||
|
||||
@@ -1071,8 +1188,13 @@ mod tests {
|
||||
|
||||
let preload = vec![PreloadEntry { asset_id: 2, slot: 5 }];
|
||||
|
||||
let am =
|
||||
AssetManager::new(vec![], AssetsPayloadSource::empty(), gfx_installer, sound_installer);
|
||||
let am = AssetManager::new(
|
||||
vec![],
|
||||
AssetsPayloadSource::empty(),
|
||||
gfx_installer,
|
||||
sound_installer,
|
||||
scene_installer,
|
||||
);
|
||||
|
||||
// Before init, slot 5 is empty
|
||||
assert!(banks.sound_bank_slot(5).is_none());
|
||||
@@ -1093,8 +1215,14 @@ mod tests {
|
||||
let banks = Arc::new(MemoryBanks::new());
|
||||
let gfx_installer = Arc::clone(&banks) as Arc<dyn GlyphBankPoolInstaller>;
|
||||
let sound_installer = Arc::clone(&banks) as Arc<dyn SoundBankPoolInstaller>;
|
||||
let am =
|
||||
AssetManager::new(vec![], AssetsPayloadSource::empty(), gfx_installer, sound_installer);
|
||||
let scene_installer = Arc::clone(&banks) as Arc<dyn SceneBankPoolInstaller>;
|
||||
let am = AssetManager::new(
|
||||
vec![],
|
||||
AssetsPayloadSource::empty(),
|
||||
gfx_installer,
|
||||
sound_installer,
|
||||
scene_installer,
|
||||
);
|
||||
|
||||
let result = am.load(999, 0);
|
||||
|
||||
@@ -1106,12 +1234,14 @@ mod tests {
|
||||
let banks = Arc::new(MemoryBanks::new());
|
||||
let gfx_installer = Arc::clone(&banks) as Arc<dyn GlyphBankPoolInstaller>;
|
||||
let sound_installer = Arc::clone(&banks) as Arc<dyn SoundBankPoolInstaller>;
|
||||
let scene_installer = Arc::clone(&banks) as Arc<dyn SceneBankPoolInstaller>;
|
||||
let data = test_glyph_asset_data();
|
||||
let am = AssetManager::new(
|
||||
vec![test_glyph_asset_entry("test_glyphs", 16, 16)],
|
||||
AssetsPayloadSource::from_bytes(data),
|
||||
gfx_installer,
|
||||
sound_installer,
|
||||
scene_installer,
|
||||
);
|
||||
|
||||
let result = am.load(0, 16);
|
||||
@@ -1124,8 +1254,14 @@ mod tests {
|
||||
let banks = Arc::new(MemoryBanks::new());
|
||||
let gfx_installer = Arc::clone(&banks) as Arc<dyn GlyphBankPoolInstaller>;
|
||||
let sound_installer = Arc::clone(&banks) as Arc<dyn SoundBankPoolInstaller>;
|
||||
let am =
|
||||
AssetManager::new(vec![], AssetsPayloadSource::empty(), gfx_installer, sound_installer);
|
||||
let scene_installer = Arc::clone(&banks) as Arc<dyn SceneBankPoolInstaller>;
|
||||
let am = AssetManager::new(
|
||||
vec![],
|
||||
AssetsPayloadSource::empty(),
|
||||
gfx_installer,
|
||||
sound_installer,
|
||||
scene_installer,
|
||||
);
|
||||
|
||||
assert_eq!(am.status(999), LoadStatus::UnknownHandle);
|
||||
}
|
||||
@@ -1135,12 +1271,14 @@ mod tests {
|
||||
let banks = Arc::new(MemoryBanks::new());
|
||||
let gfx_installer = Arc::clone(&banks) as Arc<dyn GlyphBankPoolInstaller>;
|
||||
let sound_installer = Arc::clone(&banks) as Arc<dyn SoundBankPoolInstaller>;
|
||||
let scene_installer = Arc::clone(&banks) as Arc<dyn SceneBankPoolInstaller>;
|
||||
let data = test_glyph_asset_data();
|
||||
let am = AssetManager::new(
|
||||
vec![test_glyph_asset_entry("test_glyphs", 16, 16)],
|
||||
AssetsPayloadSource::from_bytes(data),
|
||||
gfx_installer,
|
||||
sound_installer,
|
||||
scene_installer,
|
||||
);
|
||||
|
||||
assert_eq!(am.commit(999), AssetOpStatus::UnknownHandle);
|
||||
@@ -1162,6 +1300,7 @@ mod tests {
|
||||
let banks = Arc::new(MemoryBanks::new());
|
||||
let gfx_installer = Arc::clone(&banks) as Arc<dyn GlyphBankPoolInstaller>;
|
||||
let sound_installer = Arc::clone(&banks) as Arc<dyn SoundBankPoolInstaller>;
|
||||
let scene_installer = Arc::clone(&banks) as Arc<dyn SceneBankPoolInstaller>;
|
||||
|
||||
let width = 16;
|
||||
let height = 16;
|
||||
@@ -1172,6 +1311,7 @@ mod tests {
|
||||
AssetsPayloadSource::from_bytes(data),
|
||||
gfx_installer,
|
||||
sound_installer,
|
||||
scene_installer,
|
||||
);
|
||||
|
||||
// Initially zero
|
||||
|
||||
@@ -2,10 +2,8 @@ use crate::memory_banks::GlyphBankPoolAccess;
|
||||
use prometeu_hal::GfxBridge;
|
||||
use prometeu_hal::color::Color;
|
||||
use prometeu_hal::glyph::Glyph;
|
||||
use prometeu_hal::glyph_bank::{GlyphBank, TileSize};
|
||||
use prometeu_hal::glyph_bank::GlyphBank;
|
||||
use prometeu_hal::sprite::Sprite;
|
||||
use prometeu_hal::tile::Tile;
|
||||
use prometeu_hal::tile_layer::{HudTileLayer, ScrollableTileLayer, TileMap};
|
||||
use std::sync::Arc;
|
||||
|
||||
/// Blending modes inspired by classic 16-bit hardware.
|
||||
@@ -57,10 +55,6 @@ pub struct Gfx {
|
||||
/// Back buffer: the "Work RAM" where new frames are composed.
|
||||
back: Vec<u16>,
|
||||
|
||||
/// 4 scrollable background layers. Each can have its own scroll (X, Y) and GlyphBank.
|
||||
pub layers: [ScrollableTileLayer; 4],
|
||||
/// 1 fixed layer for User Interface (HUD). It doesn't scroll.
|
||||
pub hud: HudTileLayer,
|
||||
/// Shared access to graphical memory banks (tiles and palettes).
|
||||
pub glyph_banks: Arc<dyn GlyphBankPoolAccess>,
|
||||
/// Hardware sprite list (512 slots). Equivalent to OAM (Object Attribute Memory).
|
||||
@@ -214,12 +208,6 @@ impl GfxBridge for Gfx {
|
||||
fn render_all(&mut self) {
|
||||
self.render_all()
|
||||
}
|
||||
fn render_layer(&mut self, layer_idx: usize) {
|
||||
self.render_layer(layer_idx)
|
||||
}
|
||||
fn render_hud(&mut self) {
|
||||
self.render_hud()
|
||||
}
|
||||
fn draw_text(&mut self, x: i32, y: i32, text: &str, color: Color) {
|
||||
self.draw_text(x, y, text, color)
|
||||
}
|
||||
@@ -227,18 +215,6 @@ impl GfxBridge for Gfx {
|
||||
self.draw_char(x, y, c, color)
|
||||
}
|
||||
|
||||
fn layer(&self, index: usize) -> &ScrollableTileLayer {
|
||||
&self.layers[index]
|
||||
}
|
||||
fn layer_mut(&mut self, index: usize) -> &mut ScrollableTileLayer {
|
||||
&mut self.layers[index]
|
||||
}
|
||||
fn hud(&self) -> &HudTileLayer {
|
||||
&self.hud
|
||||
}
|
||||
fn hud_mut(&mut self) -> &mut HudTileLayer {
|
||||
&mut self.hud
|
||||
}
|
||||
fn sprite(&self, index: usize) -> &Sprite {
|
||||
&self.sprites[index]
|
||||
}
|
||||
@@ -289,20 +265,11 @@ impl Gfx {
|
||||
};
|
||||
|
||||
let len = w * h;
|
||||
let layers = [
|
||||
ScrollableTileLayer::new(64, 64, TileSize::Size16),
|
||||
ScrollableTileLayer::new(64, 64, TileSize::Size16),
|
||||
ScrollableTileLayer::new(64, 64, TileSize::Size16),
|
||||
ScrollableTileLayer::new(64, 64, TileSize::Size16),
|
||||
];
|
||||
|
||||
Self {
|
||||
w,
|
||||
h,
|
||||
front: vec![0; len],
|
||||
back: vec![0; len],
|
||||
layers,
|
||||
hud: HudTileLayer::new(64, 32),
|
||||
glyph_banks,
|
||||
sprites: [EMPTY_SPRITE; 512],
|
||||
scene_fade_level: 31,
|
||||
@@ -588,192 +555,171 @@ impl Gfx {
|
||||
|
||||
// 2. Main layers and prioritized sprites.
|
||||
// Order: Layer 0 -> Sprites 1 -> Layer 1 -> Sprites 2 ...
|
||||
for i in 0..self.layers.len() {
|
||||
let bank_id = self.layers[i].bank_id as usize;
|
||||
if let Some(bank) = self.glyph_banks.glyph_bank_slot(bank_id) {
|
||||
Self::draw_tile_map(
|
||||
&mut self.back,
|
||||
self.w,
|
||||
self.h,
|
||||
&self.layers[i].map,
|
||||
&bank,
|
||||
self.layers[i].scroll_x,
|
||||
self.layers[i].scroll_y,
|
||||
);
|
||||
}
|
||||
|
||||
// Draw sprites that belong to this depth level
|
||||
Self::draw_bucket_on_buffer(
|
||||
&mut self.back,
|
||||
self.w,
|
||||
self.h,
|
||||
&self.priority_buckets[i + 1],
|
||||
&self.sprites,
|
||||
&*self.glyph_banks,
|
||||
);
|
||||
}
|
||||
// for i in 0..self.layers.len() {
|
||||
// let bank_id = self.layers[i].bank_id as usize;
|
||||
// if let Some(bank) = self.glyph_banks.glyph_bank_slot(bank_id) {
|
||||
// Self::draw_tile_map(
|
||||
// &mut self.back,
|
||||
// self.w,
|
||||
// self.h,
|
||||
// &self.layers[i].map,
|
||||
// &bank,
|
||||
// self.layers[i].scroll_x,
|
||||
// self.layers[i].scroll_y,
|
||||
// );
|
||||
// }
|
||||
//
|
||||
// // Draw sprites that belong to this depth level
|
||||
// Self::draw_bucket_on_buffer(
|
||||
// &mut self.back,
|
||||
// self.w,
|
||||
// self.h,
|
||||
// &self.priority_buckets[i + 1],
|
||||
// &self.sprites,
|
||||
// &*self.glyph_banks,
|
||||
// );
|
||||
// }
|
||||
|
||||
// 4. Scene Fade: Applies a color blend to the entire world (excluding HUD).
|
||||
Self::apply_fade_to_buffer(&mut self.back, self.scene_fade_level, self.scene_fade_color);
|
||||
|
||||
// 5. HUD: The fixed interface layer, always drawn on top of the world.
|
||||
Self::render_hud_with_pool(&mut self.back, self.w, self.h, &self.hud, &*self.glyph_banks);
|
||||
// Self::render_hud_with_pool(&mut self.back, self.w, self.h, &self.hud, &*self.glyph_banks);
|
||||
|
||||
// 6. HUD Fade: Independent fade effect for the UI.
|
||||
Self::apply_fade_to_buffer(&mut self.back, self.hud_fade_level, self.hud_fade_color);
|
||||
}
|
||||
|
||||
/// Renders a specific game layer.
|
||||
pub fn render_layer(&mut self, layer_idx: usize) {
|
||||
if layer_idx >= self.layers.len() {
|
||||
return;
|
||||
}
|
||||
// /// Renders a specific game layer.
|
||||
// pub fn render_layer(&mut self, layer_idx: usize) {
|
||||
// if layer_idx >= self.layers.len() {
|
||||
// return;
|
||||
// }
|
||||
//
|
||||
// let bank_id = self.layers[layer_idx].bank_id as usize;
|
||||
// let scroll_x = self.layers[layer_idx].scroll_x;
|
||||
// let scroll_y = self.layers[layer_idx].scroll_y;
|
||||
//
|
||||
// let bank = match self.glyph_banks.glyph_bank_slot(bank_id) {
|
||||
// Some(b) => b,
|
||||
// _ => return,
|
||||
// };
|
||||
//
|
||||
// Self::draw_tile_map(
|
||||
// &mut self.back,
|
||||
// self.w,
|
||||
// self.h,
|
||||
// &self.layers[layer_idx].map,
|
||||
// &bank,
|
||||
// scroll_x,
|
||||
// scroll_y,
|
||||
// );
|
||||
// }
|
||||
|
||||
let bank_id = self.layers[layer_idx].bank_id as usize;
|
||||
let scroll_x = self.layers[layer_idx].scroll_x;
|
||||
let scroll_y = self.layers[layer_idx].scroll_y;
|
||||
// /// Rasterizes a TileMap into the provided pixel buffer using scrolling.
|
||||
// fn draw_tile_map(
|
||||
// back: &mut [u16],
|
||||
// screen_w: usize,
|
||||
// screen_h: usize,
|
||||
// map: &TileMap,
|
||||
// bank: &GlyphBank,
|
||||
// scroll_x: i32,
|
||||
// scroll_y: i32,
|
||||
// ) {
|
||||
// let tile_size = bank.tile_size as usize;
|
||||
//
|
||||
// // 1. Determine the range of visible tiles based on the scroll position.
|
||||
// // We add a margin of 1 tile to ensure smooth pixel-perfect scrolling at the borders.
|
||||
// let visible_tiles_x = (screen_w / tile_size) + 1;
|
||||
// let visible_tiles_y = (screen_h / tile_size) + 1;
|
||||
//
|
||||
// // 2. Calculate offsets within the tilemap.
|
||||
// let start_tile_x = scroll_x / tile_size as i32;
|
||||
// let start_tile_y = scroll_y / tile_size as i32;
|
||||
//
|
||||
// // 3. Fine scroll: how many pixels the tiles are shifted within the first visible cell.
|
||||
// let fine_scroll_x = scroll_x % tile_size as i32;
|
||||
// let fine_scroll_y = scroll_y % tile_size as i32;
|
||||
//
|
||||
// // 4. Iterate only through the tiles that are actually visible on screen.
|
||||
// for ty in 0..visible_tiles_y {
|
||||
// for tx in 0..visible_tiles_x {
|
||||
// let map_x = (start_tile_x + tx as i32) as usize;
|
||||
// let map_y = (start_tile_y + ty as i32) as usize;
|
||||
//
|
||||
// // Bounds check: don't draw if the camera is outside the map.
|
||||
// if map_x >= map.width || map_y >= map.height {
|
||||
// continue;
|
||||
// }
|
||||
//
|
||||
// let tile = map.tiles[map_y * map.width + map_x];
|
||||
//
|
||||
// // Optimized skip for empty (ID 0) tiles.
|
||||
// if !tile.active {
|
||||
// continue;
|
||||
// }
|
||||
//
|
||||
// // 5. Project the tile pixels to screen space.
|
||||
// let screen_tile_x = (tx as i32 * tile_size as i32) - fine_scroll_x;
|
||||
// let screen_tile_y = (ty as i32 * tile_size as i32) - fine_scroll_y;
|
||||
//
|
||||
// Self::draw_tile_pixels(
|
||||
// back,
|
||||
// screen_w,
|
||||
// screen_h,
|
||||
// screen_tile_x,
|
||||
// screen_tile_y,
|
||||
// tile,
|
||||
// bank,
|
||||
// );
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
let bank = match self.glyph_banks.glyph_bank_slot(bank_id) {
|
||||
Some(b) => b,
|
||||
_ => return,
|
||||
};
|
||||
|
||||
Self::draw_tile_map(
|
||||
&mut self.back,
|
||||
self.w,
|
||||
self.h,
|
||||
&self.layers[layer_idx].map,
|
||||
&bank,
|
||||
scroll_x,
|
||||
scroll_y,
|
||||
);
|
||||
}
|
||||
|
||||
/// Renders the HUD (fixed position, no scroll).
|
||||
pub fn render_hud(&mut self) {
|
||||
Self::render_hud_with_pool(&mut self.back, self.w, self.h, &self.hud, &*self.glyph_banks);
|
||||
}
|
||||
|
||||
fn render_hud_with_pool(
|
||||
back: &mut [u16],
|
||||
w: usize,
|
||||
h: usize,
|
||||
hud: &HudTileLayer,
|
||||
glyph_banks: &dyn GlyphBankPoolAccess,
|
||||
) {
|
||||
let bank_id = hud.bank_id as usize;
|
||||
let bank = match glyph_banks.glyph_bank_slot(bank_id) {
|
||||
Some(b) => b,
|
||||
_ => return,
|
||||
};
|
||||
|
||||
Self::draw_tile_map(back, w, h, &hud.map, &bank, 0, 0);
|
||||
}
|
||||
|
||||
/// Rasterizes a TileMap into the provided pixel buffer using scrolling.
|
||||
fn draw_tile_map(
|
||||
back: &mut [u16],
|
||||
screen_w: usize,
|
||||
screen_h: usize,
|
||||
map: &TileMap,
|
||||
bank: &GlyphBank,
|
||||
scroll_x: i32,
|
||||
scroll_y: i32,
|
||||
) {
|
||||
let tile_size = bank.tile_size as usize;
|
||||
|
||||
// 1. Determine the range of visible tiles based on the scroll position.
|
||||
// We add a margin of 1 tile to ensure smooth pixel-perfect scrolling at the borders.
|
||||
let visible_tiles_x = (screen_w / tile_size) + 1;
|
||||
let visible_tiles_y = (screen_h / tile_size) + 1;
|
||||
|
||||
// 2. Calculate offsets within the tilemap.
|
||||
let start_tile_x = scroll_x / tile_size as i32;
|
||||
let start_tile_y = scroll_y / tile_size as i32;
|
||||
|
||||
// 3. Fine scroll: how many pixels the tiles are shifted within the first visible cell.
|
||||
let fine_scroll_x = scroll_x % tile_size as i32;
|
||||
let fine_scroll_y = scroll_y % tile_size as i32;
|
||||
|
||||
// 4. Iterate only through the tiles that are actually visible on screen.
|
||||
for ty in 0..visible_tiles_y {
|
||||
for tx in 0..visible_tiles_x {
|
||||
let map_x = (start_tile_x + tx as i32) as usize;
|
||||
let map_y = (start_tile_y + ty as i32) as usize;
|
||||
|
||||
// Bounds check: don't draw if the camera is outside the map.
|
||||
if map_x >= map.width || map_y >= map.height {
|
||||
continue;
|
||||
}
|
||||
|
||||
let tile = map.tiles[map_y * map.width + map_x];
|
||||
|
||||
// Optimized skip for empty (ID 0) tiles.
|
||||
if !tile.active {
|
||||
continue;
|
||||
}
|
||||
|
||||
// 5. Project the tile pixels to screen space.
|
||||
let screen_tile_x = (tx as i32 * tile_size as i32) - fine_scroll_x;
|
||||
let screen_tile_y = (ty as i32 * tile_size as i32) - fine_scroll_y;
|
||||
|
||||
Self::draw_tile_pixels(
|
||||
back,
|
||||
screen_w,
|
||||
screen_h,
|
||||
screen_tile_x,
|
||||
screen_tile_y,
|
||||
tile,
|
||||
bank,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Internal helper to copy a single tile's pixels to the framebuffer.
|
||||
/// Handles flipping and palette resolution.
|
||||
fn draw_tile_pixels(
|
||||
back: &mut [u16],
|
||||
screen_w: usize,
|
||||
screen_h: usize,
|
||||
x: i32,
|
||||
y: i32,
|
||||
tile: Tile,
|
||||
bank: &GlyphBank,
|
||||
) {
|
||||
let size = bank.tile_size as usize;
|
||||
|
||||
for local_y in 0..size {
|
||||
let world_y = y + local_y as i32;
|
||||
if world_y < 0 || world_y >= screen_h as i32 {
|
||||
continue;
|
||||
}
|
||||
|
||||
for local_x in 0..size {
|
||||
let world_x = x + local_x as i32;
|
||||
if world_x < 0 || world_x >= screen_w as i32 {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Handle flip flags by reversing the fetch coordinates.
|
||||
let fetch_x = if tile.flip_x { size - 1 - local_x } else { local_x };
|
||||
let fetch_y = if tile.flip_y { size - 1 - local_y } else { local_y };
|
||||
|
||||
// 1. Get the pixel color index (0-15) from the bank.
|
||||
let px_index = bank.get_pixel_index(tile.glyph.glyph_id, fetch_x, fetch_y);
|
||||
|
||||
// 2. Hardware rule: Color index 0 is always fully transparent.
|
||||
if px_index == 0 {
|
||||
continue;
|
||||
}
|
||||
|
||||
// 3. Resolve the virtual index to a real RGB565 color using the tile's assigned palette.
|
||||
let color = bank.resolve_color(tile.glyph.palette_id, px_index);
|
||||
|
||||
back[world_y as usize * screen_w + world_x as usize] = color.raw();
|
||||
}
|
||||
}
|
||||
}
|
||||
// /// Internal helper to copy a single tile's pixels to the framebuffer.
|
||||
// /// Handles flipping and palette resolution.
|
||||
// fn draw_tile_pixels(
|
||||
// back: &mut [u16],
|
||||
// screen_w: usize,
|
||||
// screen_h: usize,
|
||||
// x: i32,
|
||||
// y: i32,
|
||||
// tile: Tile,
|
||||
// bank: &GlyphBank,
|
||||
// ) {
|
||||
// let size = bank.tile_size as usize;
|
||||
//
|
||||
// for local_y in 0..size {
|
||||
// let world_y = y + local_y as i32;
|
||||
// if world_y < 0 || world_y >= screen_h as i32 {
|
||||
// continue;
|
||||
// }
|
||||
//
|
||||
// for local_x in 0..size {
|
||||
// let world_x = x + local_x as i32;
|
||||
// if world_x < 0 || world_x >= screen_w as i32 {
|
||||
// continue;
|
||||
// }
|
||||
//
|
||||
// // Handle flip flags by reversing the fetch coordinates.
|
||||
// let fetch_x = if tile.flip_x { size - 1 - local_x } else { local_x };
|
||||
// let fetch_y = if tile.flip_y { size - 1 - local_y } else { local_y };
|
||||
//
|
||||
// // 1. Get the pixel color index (0-15) from the bank.
|
||||
// let px_index = bank.get_pixel_index(tile.glyph.glyph_id, fetch_x, fetch_y);
|
||||
//
|
||||
// // 2. Hardware rule: Color index 0 is always fully transparent.
|
||||
// if px_index == 0 {
|
||||
// continue;
|
||||
// }
|
||||
//
|
||||
// // 3. Resolve the virtual index to a real RGB565 color using the tile's assigned palette.
|
||||
// let color = bank.resolve_color(tile.glyph.palette_id, px_index);
|
||||
//
|
||||
// back[world_y as usize * screen_w + world_x as usize] = color.raw();
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
fn draw_bucket_on_buffer(
|
||||
back: &mut [u16],
|
||||
|
||||
@@ -2,8 +2,8 @@ use crate::asset::AssetManager;
|
||||
use crate::audio::Audio;
|
||||
use crate::gfx::Gfx;
|
||||
use crate::memory_banks::{
|
||||
GlyphBankPoolAccess, GlyphBankPoolInstaller, MemoryBanks, SoundBankPoolAccess,
|
||||
SoundBankPoolInstaller,
|
||||
GlyphBankPoolAccess, GlyphBankPoolInstaller, MemoryBanks, SceneBankPoolInstaller,
|
||||
SoundBankPoolAccess, SoundBankPoolInstaller,
|
||||
};
|
||||
use crate::pad::Pad;
|
||||
use crate::touch::Touch;
|
||||
@@ -102,6 +102,7 @@ impl Hardware {
|
||||
AssetsPayloadSource::empty(),
|
||||
Arc::clone(&memory_banks) as Arc<dyn GlyphBankPoolInstaller>,
|
||||
Arc::clone(&memory_banks) as Arc<dyn SoundBankPoolInstaller>,
|
||||
Arc::clone(&memory_banks) as Arc<dyn SceneBankPoolInstaller>,
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
use prometeu_hal::glyph_bank::GlyphBank;
|
||||
use prometeu_hal::scene_bank::SceneBank;
|
||||
use prometeu_hal::sound_bank::SoundBank;
|
||||
use std::sync::{Arc, RwLock};
|
||||
|
||||
@@ -30,6 +31,20 @@ pub trait SoundBankPoolInstaller: Send + Sync {
|
||||
fn install_sound_bank(&self, slot: usize, bank: Arc<SoundBank>);
|
||||
}
|
||||
|
||||
/// Non-generic interface for peripherals to access scene banks.
|
||||
pub trait SceneBankPoolAccess: Send + Sync {
|
||||
/// Returns a reference to the resident SceneBank in the specified slot, if any.
|
||||
fn scene_bank_slot(&self, slot: usize) -> Option<Arc<SceneBank>>;
|
||||
/// Returns the total number of slots available in this bank.
|
||||
fn scene_bank_slot_count(&self) -> usize;
|
||||
}
|
||||
|
||||
/// Non-generic interface for the AssetManager to install scene banks.
|
||||
pub trait SceneBankPoolInstaller: Send + Sync {
|
||||
/// Atomically swaps the resident SceneBank in the specified slot.
|
||||
fn install_scene_bank(&self, slot: usize, bank: Arc<SceneBank>);
|
||||
}
|
||||
|
||||
/// Centralized container for all hardware memory banks.
|
||||
///
|
||||
/// MemoryBanks represent the actual hardware slot state.
|
||||
@@ -38,6 +53,7 @@ pub trait SoundBankPoolInstaller: Send + Sync {
|
||||
pub struct MemoryBanks {
|
||||
glyph_bank_pool: Arc<RwLock<[Option<Arc<GlyphBank>>; 16]>>,
|
||||
sound_bank_pool: Arc<RwLock<[Option<Arc<SoundBank>>; 16]>>,
|
||||
scene_bank_pool: Arc<RwLock<[Option<Arc<SceneBank>>; 16]>>,
|
||||
}
|
||||
|
||||
impl Default for MemoryBanks {
|
||||
@@ -52,6 +68,7 @@ impl MemoryBanks {
|
||||
Self {
|
||||
glyph_bank_pool: Arc::new(RwLock::new(std::array::from_fn(|_| None))),
|
||||
sound_bank_pool: Arc::new(RwLock::new(std::array::from_fn(|_| None))),
|
||||
scene_bank_pool: Arc::new(RwLock::new(std::array::from_fn(|_| None))),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -95,3 +112,23 @@ impl SoundBankPoolInstaller for MemoryBanks {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl SceneBankPoolAccess for MemoryBanks {
|
||||
fn scene_bank_slot(&self, slot: usize) -> Option<Arc<SceneBank>> {
|
||||
let pool = self.scene_bank_pool.read().unwrap();
|
||||
pool.get(slot).and_then(|s| s.as_ref().map(Arc::clone))
|
||||
}
|
||||
|
||||
fn scene_bank_slot_count(&self) -> usize {
|
||||
16
|
||||
}
|
||||
}
|
||||
|
||||
impl SceneBankPoolInstaller for MemoryBanks {
|
||||
fn install_scene_bank(&self, slot: usize, bank: Arc<SceneBank>) {
|
||||
let mut pool = self.scene_bank_pool.write().unwrap();
|
||||
if slot < 16 {
|
||||
pool[slot] = Some(bank);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ pub type AssetId = i32;
|
||||
pub enum BankType {
|
||||
GLYPH,
|
||||
SOUNDS,
|
||||
SCENE,
|
||||
// TILEMAPS,
|
||||
// BLOBS,
|
||||
}
|
||||
@@ -133,4 +134,8 @@ impl SlotRef {
|
||||
pub fn audio(index: usize) -> Self {
|
||||
Self { asset_type: BankType::SOUNDS, index }
|
||||
}
|
||||
|
||||
pub fn scene(index: usize) -> Self {
|
||||
Self { asset_type: BankType::SCENE, index }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
use crate::color::Color;
|
||||
use crate::sprite::Sprite;
|
||||
use crate::tile_layer::{HudTileLayer, ScrollableTileLayer};
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum BlendMode {
|
||||
@@ -48,15 +47,9 @@ pub trait GfxBridge {
|
||||
fn draw_vertical_line(&mut self, x: i32, y0: i32, y1: i32, color: Color);
|
||||
fn present(&mut self);
|
||||
fn render_all(&mut self);
|
||||
fn render_layer(&mut self, layer_idx: usize);
|
||||
fn render_hud(&mut self);
|
||||
fn draw_text(&mut self, x: i32, y: i32, text: &str, color: Color);
|
||||
fn draw_char(&mut self, x: i32, y: i32, c: char, color: Color);
|
||||
|
||||
fn layer(&self, index: usize) -> &ScrollableTileLayer;
|
||||
fn layer_mut(&mut self, index: usize) -> &mut ScrollableTileLayer;
|
||||
fn hud(&self) -> &HudTileLayer;
|
||||
fn hud_mut(&mut self) -> &mut HudTileLayer;
|
||||
fn sprite(&self, index: usize) -> &Sprite;
|
||||
fn sprite_mut(&mut self, index: usize) -> &mut Sprite;
|
||||
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
#[derive(Clone, Copy, Debug, Default)]
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Clone, Copy, Debug, Default, Deserialize, Serialize)]
|
||||
pub struct Glyph {
|
||||
pub glyph_id: u16,
|
||||
pub palette_id: u8,
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
use crate::color::Color;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
pub const GLYPH_BANK_PALETTE_COUNT_V1: usize = 64;
|
||||
pub const GLYPH_BANK_COLORS_PER_PALETTE: usize = 16;
|
||||
|
||||
/// Standard sizes for square tiles.
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Deserialize, Serialize)]
|
||||
pub enum TileSize {
|
||||
/// 8x8 pixels.
|
||||
Size8 = 8,
|
||||
|
||||
@@ -18,12 +18,14 @@ pub mod native_helpers;
|
||||
pub mod native_interface;
|
||||
pub mod pad_bridge;
|
||||
pub mod sample;
|
||||
pub mod scene_bank;
|
||||
pub mod scene_layer;
|
||||
pub mod sound_bank;
|
||||
pub mod sprite;
|
||||
pub mod syscalls;
|
||||
pub mod telemetry;
|
||||
pub mod tile;
|
||||
pub mod tile_layer;
|
||||
pub mod tilemap;
|
||||
pub mod touch_bridge;
|
||||
pub mod vm_fault;
|
||||
pub mod window;
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
use crate::scene_layer::SceneLayer;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
pub struct SceneBank {
|
||||
pub layers: [SceneLayer; 4],
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::glyph::Glyph;
|
||||
use crate::glyph_bank::TileSize;
|
||||
use crate::scene_layer::MotionFactor;
|
||||
use crate::tile::Tile;
|
||||
use crate::tilemap::TileMap;
|
||||
|
||||
fn layer(glyph_bank_id: u8, motion_x: f32, motion_y: f32, glyph_id: u16) -> SceneLayer {
|
||||
SceneLayer {
|
||||
active: true,
|
||||
glyph_bank_id,
|
||||
tile_size: TileSize::Size16,
|
||||
motion_factor: MotionFactor { x: motion_x, y: motion_y },
|
||||
tilemap: TileMap {
|
||||
width: 1,
|
||||
height: 1,
|
||||
tiles: vec![Tile {
|
||||
active: true,
|
||||
glyph: Glyph { glyph_id, palette_id: glyph_bank_id },
|
||||
flip_x: false,
|
||||
flip_y: false,
|
||||
}],
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scene_bank_owns_exactly_four_layers() {
|
||||
let scene = SceneBank {
|
||||
layers: [
|
||||
layer(0, 1.0, 1.0, 10),
|
||||
layer(1, 0.5, 1.0, 11),
|
||||
layer(2, 1.0, 0.5, 12),
|
||||
layer(3, 0.25, 0.25, 13),
|
||||
],
|
||||
};
|
||||
|
||||
assert_eq!(scene.layers.len(), 4);
|
||||
assert_eq!(scene.layers[3].tilemap.tiles[0].glyph.glyph_id, 13);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
use crate::glyph_bank::TileSize;
|
||||
use crate::tilemap::TileMap;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
|
||||
pub struct MotionFactor {
|
||||
pub x: f32,
|
||||
pub y: f32,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
pub struct SceneLayer {
|
||||
pub active: bool,
|
||||
pub glyph_bank_id: u8,
|
||||
pub tile_size: TileSize,
|
||||
pub motion_factor: MotionFactor,
|
||||
pub tilemap: TileMap,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::glyph::Glyph;
|
||||
use crate::tile::Tile;
|
||||
|
||||
#[test]
|
||||
fn scene_layer_preserves_motion_factor_and_tilemap_ownership() {
|
||||
let layer = SceneLayer {
|
||||
active: true,
|
||||
glyph_bank_id: 7,
|
||||
tile_size: TileSize::Size16,
|
||||
motion_factor: MotionFactor { x: 0.5, y: 0.75 },
|
||||
tilemap: TileMap {
|
||||
width: 2,
|
||||
height: 1,
|
||||
tiles: vec![
|
||||
Tile {
|
||||
active: true,
|
||||
glyph: Glyph { glyph_id: 21, palette_id: 3 },
|
||||
flip_x: false,
|
||||
flip_y: false,
|
||||
},
|
||||
Tile {
|
||||
active: false,
|
||||
glyph: Glyph { glyph_id: 22, palette_id: 4 },
|
||||
flip_x: true,
|
||||
flip_y: false,
|
||||
},
|
||||
],
|
||||
},
|
||||
};
|
||||
|
||||
assert_eq!(layer.glyph_bank_id, 7);
|
||||
assert_eq!(layer.motion_factor.x, 0.5);
|
||||
assert_eq!(layer.motion_factor.y, 0.75);
|
||||
assert_eq!(layer.tilemap.width, 2);
|
||||
assert_eq!(layer.tilemap.tiles[1].glyph.glyph_id, 22);
|
||||
assert!(layer.tilemap.tiles[1].flip_x);
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,10 @@
|
||||
use crate::glyph::Glyph;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Clone, Copy, Debug, Default)]
|
||||
#[derive(Clone, Copy, Debug, Default, Deserialize, Serialize)]
|
||||
pub struct Tile {
|
||||
pub glyph: Glyph,
|
||||
pub active: bool,
|
||||
pub glyph: Glyph,
|
||||
pub flip_x: bool,
|
||||
pub flip_y: bool,
|
||||
}
|
||||
|
||||
@@ -1,94 +0,0 @@
|
||||
use crate::glyph_bank::TileSize;
|
||||
use crate::glyph_bank::TileSize::Size8;
|
||||
use crate::tile::Tile;
|
||||
|
||||
pub struct TileMap {
|
||||
pub width: usize,
|
||||
pub height: usize,
|
||||
pub tiles: Vec<Tile>,
|
||||
}
|
||||
|
||||
impl TileMap {
|
||||
fn create(width: usize, height: usize) -> Self {
|
||||
Self { width, height, tiles: vec![Tile::default(); width * height] }
|
||||
}
|
||||
|
||||
pub fn set_tile(&mut self, x: usize, y: usize, tile: Tile) {
|
||||
if x < self.width && y < self.height {
|
||||
self.tiles[y * self.width + x] = tile;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct TileLayer {
|
||||
pub bank_id: u8,
|
||||
pub tile_size: TileSize,
|
||||
pub map: TileMap,
|
||||
}
|
||||
|
||||
impl TileLayer {
|
||||
fn create(width: usize, height: usize, tile_size: TileSize) -> Self {
|
||||
Self { bank_id: 0, tile_size, map: TileMap::create(width, height) }
|
||||
}
|
||||
}
|
||||
|
||||
impl std::ops::Deref for TileLayer {
|
||||
type Target = TileMap;
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.map
|
||||
}
|
||||
}
|
||||
|
||||
impl std::ops::DerefMut for TileLayer {
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
&mut self.map
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ScrollableTileLayer {
|
||||
pub layer: TileLayer,
|
||||
pub scroll_x: i32,
|
||||
pub scroll_y: i32,
|
||||
}
|
||||
|
||||
impl ScrollableTileLayer {
|
||||
pub fn new(width: usize, height: usize, tile_size: TileSize) -> Self {
|
||||
Self { layer: TileLayer::create(width, height, tile_size), scroll_x: 0, scroll_y: 0 }
|
||||
}
|
||||
}
|
||||
|
||||
impl std::ops::Deref for ScrollableTileLayer {
|
||||
type Target = TileLayer;
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.layer
|
||||
}
|
||||
}
|
||||
|
||||
impl std::ops::DerefMut for ScrollableTileLayer {
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
&mut self.layer
|
||||
}
|
||||
}
|
||||
|
||||
pub struct HudTileLayer {
|
||||
pub layer: TileLayer,
|
||||
}
|
||||
|
||||
impl HudTileLayer {
|
||||
pub fn new(width: usize, height: usize) -> Self {
|
||||
Self { layer: TileLayer::create(width, height, Size8) }
|
||||
}
|
||||
}
|
||||
|
||||
impl std::ops::Deref for HudTileLayer {
|
||||
type Target = TileLayer;
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.layer
|
||||
}
|
||||
}
|
||||
|
||||
impl std::ops::DerefMut for HudTileLayer {
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
&mut self.layer
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
use crate::tile::Tile;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
pub struct TileMap {
|
||||
pub width: usize,
|
||||
pub height: usize,
|
||||
pub tiles: Vec<Tile>,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::glyph::Glyph;
|
||||
|
||||
#[test]
|
||||
fn tilemap_tile_write_and_read_remain_canonical() {
|
||||
let mut map = TileMap { width: 2, height: 2, tiles: vec![Tile::default(); 4] };
|
||||
|
||||
let index = 3;
|
||||
map.tiles[index] = Tile {
|
||||
active: true,
|
||||
glyph: Glyph { glyph_id: 99, palette_id: 5 },
|
||||
flip_x: true,
|
||||
flip_y: false,
|
||||
};
|
||||
|
||||
assert_eq!(map.tiles[index].glyph.glyph_id, 99);
|
||||
assert_eq!(map.tiles[index].glyph.palette_id, 5);
|
||||
assert!(map.tiles[index].flip_x);
|
||||
assert!(map.tiles[index].active);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user