mirror of
https://github.com/Xevion/Pac-Man.git
synced 2025-12-06 07:15:41 -06:00
66 lines
2.2 KiB
Rust
66 lines
2.2 KiB
Rust
#![allow(dead_code)]
|
|
//! Cross-platform asset loading abstraction.
|
|
//! On desktop, assets are embedded using include_bytes!; on Emscripten, assets are loaded from the filesystem.
|
|
|
|
use std::borrow::Cow;
|
|
use strum_macros::EnumIter;
|
|
|
|
/// Enumeration of all game assets with cross-platform loading support.
|
|
///
|
|
/// Each variant corresponds to a specific file that can be loaded either from
|
|
/// binary-embedded data or embedded filesystem (Emscripten).
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, EnumIter)]
|
|
pub enum Asset {
|
|
Wav1,
|
|
Wav2,
|
|
Wav3,
|
|
Wav4,
|
|
/// Main sprite atlas containing all game graphics (atlas.png)
|
|
AtlasImage,
|
|
/// Terminal Vector font for text rendering (TerminalVector.ttf)
|
|
Font,
|
|
}
|
|
|
|
impl Asset {
|
|
/// Returns the relative file path for this asset within the game's asset directory.
|
|
///
|
|
/// Paths are consistent across platforms and used by the Emscripten backend
|
|
/// for filesystem loading. Desktop builds embed assets directly and don't
|
|
/// use these paths at runtime.
|
|
#[allow(dead_code)]
|
|
pub fn path(&self) -> &str {
|
|
use Asset::*;
|
|
match self {
|
|
Wav1 => "sound/waka/1.ogg",
|
|
Wav2 => "sound/waka/2.ogg",
|
|
Wav3 => "sound/waka/3.ogg",
|
|
Wav4 => "sound/waka/4.ogg",
|
|
AtlasImage => "atlas.png",
|
|
Font => "TerminalVector.ttf",
|
|
}
|
|
}
|
|
}
|
|
|
|
mod imp {
|
|
use super::*;
|
|
use crate::error::AssetError;
|
|
use crate::platform::get_platform;
|
|
|
|
/// Loads asset bytes using the appropriate platform-specific method.
|
|
///
|
|
/// On desktop platforms, returns embedded compile-time data via `include_bytes!`.
|
|
/// On Emscripten, loads from the filesystem using the asset's path. The returned
|
|
/// `Cow` allows zero-copy access to embedded data while supporting owned data
|
|
/// when loaded from disk.
|
|
///
|
|
/// # Errors
|
|
///
|
|
/// Returns `AssetError::NotFound` if the asset file cannot be located (Emscripten only),
|
|
/// or `AssetError::Io` for filesystem I/O failures.
|
|
pub fn get_asset_bytes(asset: Asset) -> Result<Cow<'static, [u8]>, AssetError> {
|
|
get_platform().get_asset_bytes(asset)
|
|
}
|
|
}
|
|
|
|
pub use imp::get_asset_bytes;
|