Skip to main content

SpriteCache

Struct SpriteCache 

Source
pub struct SpriteCache { /* private fields */ }
Expand description

Cache of decoded sprites, keyed by Unicode codepoint.

§Reload / hot-swap is not supported

load is append-only: it decodes a tileset and merges its sprites into the existing map, with later registrations winning on codepoint collision (see load docs). There is no unload or clear, and nothing observes or invalidates sprites already handed out via get.

This is a deliberate scope decision, not an oversight: games generally don’t hot-swap tilesets at runtime, and a SpriteCache is only ever populated once, when a backend is built. If you need to iterate on a sprite sheet (e.g. during dev-mode asset editing) or otherwise want a tileset change to take effect, rebuild the whole renderer from a fresh backend configuration rather than mutating an existing cache in place.

Implementations§

Source§

impl SpriteCache

Source

pub const fn new() -> Self

Creates an empty sprite cache.

Source

pub fn get(&self, ch: char) -> Option<&Sprite>

Returns the sprite for ch, if registered.

Source

pub fn iter(&self) -> impl ExactSizeIterator<Item = (char, &Sprite)>

Iterates every registered (codepoint, sprite) in codepoint order.

Used by GPU backends to build a sprite atlas from the whole decoded set (the software backend only ever needs per-glyph get at blit time).

Source

pub fn is_empty(&self) -> bool

Whether any sprite is registered.

Source

pub fn from_tilesets(opts: &[TilesetOptions]) -> Result<Self, TilesetError>

Builds a cache by loading every tileset in opts, in order.

This is what both pixel backends call from their builder’s build/into_renderer instead of each looping over their own configured tilesets by hand; later tilesets win on codepoint collision, same as calling load directly in a loop.

§Errors

Returns the first TilesetError any tileset’s load call fails with; no later tileset is loaded once one fails.

Source

pub fn load(&mut self, opts: &TilesetOptions) -> Result<(), TilesetError>

Loads a tileset, decoding the sprite sheet and inserting all sprites.

On codepoint collision, the new sprite replaces the old one and a message is logged via log::warn. Unlike warn_sprite_needs_span and warn_tint_needs_sprite, this warning is not gated behind dev_only!: it fires at most once per tileset load rather than once per frame, so it needs no seen dedup table and has no redraw-loop cost, and it reports a tileset/codepage authoring mistake a consumer may want visible even in a shipped build. See the “Load-time versus per-frame” section of retroglyph_core::dev’s module docs.

§Errors

Returns TilesetError::ImageDecode if the bytes are not a valid image, TilesetError::ZeroTileSize if opts.tile_width or opts.tile_height is 0, TilesetError::InvalidDimensions if the decoded image dimensions are not evenly divisible by the tile size, or TilesetError::TooManyColumns if opts.columns declares more columns than the image actually has at opts.tile_width.

Trait Implementations§

Source§

impl Debug for SpriteCache

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for SpriteCache

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> Downcast for T
where T: Any,

§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,