AnimationLoop

Struct AnimationLoop 

Source
pub struct AnimationLoop<F>
where F: FnMut(u64, &mut BrailleGrid) -> Result<bool, DotmaxError>,
{ /* private fields */ }
Expand description

High-level animation loop abstraction.

AnimationLoop provides a simple builder-pattern API for creating terminal animations. It handles all the complexity of double-buffering, frame timing, and terminal management automatically.

§Type Parameter

  • F: The frame callback type, which must implement FnMut(u64, &mut BrailleGrid) -> Result<bool, DotmaxError>

§Examples

Basic animation with default 60 FPS:

use dotmax::animation::AnimationLoop;

let mut frame_count = 0;
AnimationLoop::new(80, 24)
    .on_frame(|frame, buffer| {
        buffer.set_dot((frame as usize * 2) % 160, 48)?;
        Ok(frame < 100)  // Run for 100 frames
    })
    .run()?;

Animation with custom FPS:

use dotmax::animation::AnimationLoop;

AnimationLoop::new(80, 24)
    .fps(30)  // 30 FPS for less CPU usage
    .on_frame(|frame, buffer| {
        // Draw something each frame
        Ok(true)
    })
    .run()?;

Implementations§

Source§

impl AnimationLoop<fn(u64, &mut BrailleGrid) -> Result<bool, DotmaxError>>

Source

pub const fn new(width: usize, height: usize) -> AnimationLoopBuilder

Creates a new animation loop builder with the specified dimensions.

The dimensions specify the size of the braille grid in terminal cells (characters wide × lines tall). Each cell contains an 8-dot braille character (2×4 dots), so a 80×24 grid provides 160×96 dot resolution.

§Arguments
  • width - Width in terminal cells (characters)
  • height - Height in terminal cells (lines)
§Returns

An AnimationLoopBuilder with default settings (60 FPS).

§Examples
use dotmax::animation::AnimationLoop;

// Standard terminal size (80×24)
let builder = AnimationLoop::new(80, 24);

// Larger buffer for detailed graphics
let large = AnimationLoop::new(120, 40);
Source§

impl<F> AnimationLoop<F>
where F: FnMut(u64, &mut BrailleGrid) -> Result<bool, DotmaxError>,

Source

pub fn run(&mut self) -> Result<(), DotmaxError>

Runs the animation loop until stopped.

This method blocks until:

  • The callback returns Ok(false)
  • The callback returns Err(...)
  • Ctrl+C is pressed

The method handles all terminal setup (raw mode, alternate screen, cursor hiding) and cleanup automatically, even on error.

§Returns
  • Ok(()) on normal exit (callback returned false or Ctrl+C)
  • Err(...) if the callback returns an error or terminal I/O fails
§Errors

Returns DotmaxError::Terminal if:

  • Terminal initialization fails (raw mode, alternate screen)
  • Rendering to terminal fails during animation
  • Terminal cleanup fails on exit

Returns the error from the callback if it returns Err(...).

§Terminal State

On entry:

  • Enables raw mode for unbuffered input
  • Enters alternate screen to preserve original content
  • Hides cursor for clean animation

On exit (any path):

  • Shows cursor
  • Leaves alternate screen
  • Disables raw mode
§Examples
use dotmax::animation::AnimationLoop;

AnimationLoop::new(80, 24)
    .fps(60)
    .on_frame(|frame, buffer| {
        buffer.set_dot(frame as usize % 160, 48)?;
        Ok(frame < 100)  // Stop after 100 frames
    })
    .run()?;
Source

pub const fn width(&self) -> usize

Returns the animation width in terminal cells.

§Examples
use dotmax::animation::AnimationLoop;

let anim = AnimationLoop::new(80, 24)
    .on_frame(|_, _| Ok(false));
assert_eq!(anim.width(), 80);
Source

pub const fn height(&self) -> usize

Returns the animation height in terminal cells.

§Examples
use dotmax::animation::AnimationLoop;

let anim = AnimationLoop::new(80, 24)
    .on_frame(|_, _| Ok(false));
assert_eq!(anim.height(), 24);
Source

pub const fn target_fps(&self) -> u32

Returns the target FPS for this animation.

This is the FPS set via the builder, not the actual achieved FPS (which depends on system performance).

§Examples
use dotmax::animation::AnimationLoop;

// Default FPS is 60
let anim = AnimationLoop::new(80, 24)
    .on_frame(|_, _| Ok(false));
assert_eq!(anim.target_fps(), 60);

// Custom FPS
let anim = AnimationLoop::new(80, 24)
    .fps(30)
    .on_frame(|_, _| Ok(false));
assert_eq!(anim.target_fps(), 30);

Auto Trait Implementations§

§

impl<F> Freeze for AnimationLoop<F>
where F: Freeze,

§

impl<F> RefUnwindSafe for AnimationLoop<F>
where F: RefUnwindSafe,

§

impl<F> Send for AnimationLoop<F>
where F: Send,

§

impl<F> Sync for AnimationLoop<F>
where F: Sync,

§

impl<F> Unpin for AnimationLoop<F>
where F: Unpin,

§

impl<F> UnwindSafe for AnimationLoop<F>
where F: UnwindSafe,

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
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

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

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

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
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.
Source§

impl<T> WithSubscriber for T

Source§

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
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

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