From e3b2a3e3572ce15c7f4f384353ed555b3049df03 Mon Sep 17 00:00:00 2001 From: Olle Wreede Date: Wed, 22 Jun 2022 21:06:14 +0200 Subject: [PATCH] Refactor and update docs --- src/lib.rs | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 3506cc2..70c5f75 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -23,16 +23,23 @@ use macroquad::prelude::*; use rgb::ComponentBytes; +/// Struct containing textures to display every frame. pub struct GifAnimation { frames: Vec, - width: u16, - height: u16, + pub width: u16, + pub height: u16, current_frame: usize, elapsed_time: f32, paused: bool, } impl GifAnimation { + /// Instantiate with a vector of [`AnimationFrame`], width and height. + /// + /// Can be used to create an animation from your own textures instead + /// of loading a GIF file. + /// + /// [`AnimationFrame`]: struct.AnimationFrame pub fn new(frames: Vec, width: u16, height: u16) -> Self { Self { frames, @@ -51,6 +58,16 @@ impl GifAnimation { /// ``` pub async fn load(filename: String) -> Self { let file_bytes = load_file(&filename).await.expect("Couldn't load file"); + Self::from_gif_bytes(&file_bytes) + } + + /// Instantiate a new `GifAnimation` from bytes. + /// + /// ```rust + /// let bytes: [u8] = ... + /// let mut gif_animation = GifAnimation::from_gif_bytes(&bytes); + /// ``` + pub fn from_gif_bytes(file_bytes: &[u8]) -> GifAnimation { let (frames, width, height) = Self::decode_gif(&file_bytes); GifAnimation::new(frames, width, height) }