From dfcd5c5e99790b40bb1da1904d0dff61a161505f Mon Sep 17 00:00:00 2001 From: Evan Pratten Date: Thu, 22 Apr 2021 10:32:43 -0400 Subject: [PATCH] some pre-jam reorg --- Cargo.toml | 4 +--- src/lib/animation/mod.rs | 1 - src/lib/audio/mod.rs | 1 - src/lib/mod.rs | 3 +-- .../{animation/wrapper.rs => wrappers/animation.rs} | 10 +++++++++- src/lib/{audio/wrapper.rs => wrappers/audio.rs} | 12 +++++++++++- src/lib/wrappers/mod.rs | 2 ++ src/main.rs | 2 +- 8 files changed, 25 insertions(+), 10 deletions(-) delete mode 100644 src/lib/animation/mod.rs delete mode 100644 src/lib/audio/mod.rs rename src/lib/{animation/wrapper.rs => wrappers/animation.rs} (85%) rename src/lib/{audio/wrapper.rs => wrappers/audio.rs} (69%) create mode 100644 src/lib/wrappers/mod.rs diff --git a/Cargo.toml b/Cargo.toml index 9435421..6338d2a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,8 +5,6 @@ authors = ["Evan Pratten "] edition = "2018" description = "" -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - [dependencies] -raylib = { version = "3.5", git = "https://github.com/ewpratten/raylib-rs", branch = "cross_compile_fixes" } +raylib = { version = "3.5", git = "https://github.com/ewpratten/raylib-rs", branch = "master" } diff --git a/src/lib/animation/mod.rs b/src/lib/animation/mod.rs deleted file mode 100644 index 11c0c6d..0000000 --- a/src/lib/animation/mod.rs +++ /dev/null @@ -1 +0,0 @@ -pub mod wrapper; \ No newline at end of file diff --git a/src/lib/audio/mod.rs b/src/lib/audio/mod.rs deleted file mode 100644 index 11c0c6d..0000000 --- a/src/lib/audio/mod.rs +++ /dev/null @@ -1 +0,0 @@ -pub mod wrapper; \ No newline at end of file diff --git a/src/lib/mod.rs b/src/lib/mod.rs index 9af9ea2..4bafd13 100644 --- a/src/lib/mod.rs +++ b/src/lib/mod.rs @@ -1,2 +1 @@ -pub mod audio; -pub mod animation; \ No newline at end of file +pub mod wrappers; \ No newline at end of file diff --git a/src/lib/animation/wrapper.rs b/src/lib/wrappers/animation.rs similarity index 85% rename from src/lib/animation/wrapper.rs rename to src/lib/wrappers/animation.rs index e5bfff1..3632dee 100644 --- a/src/lib/animation/wrapper.rs +++ b/src/lib/wrappers/animation.rs @@ -5,6 +5,7 @@ use raylib::{ texture::Texture2D, }; +/// A wrapper around an animation spritesheet pub struct FrameAnimationWrapper { sprite_sheet: Texture2D, size: Vector2, @@ -14,6 +15,8 @@ pub struct FrameAnimationWrapper { } impl FrameAnimationWrapper { + + /// Create a new animation from a texture containing all frames pub fn new(sprite_sheet: Texture2D, frame_size: Vector2, frame_count: u32, fps: u8) -> Self { Self { sprite_sheet, @@ -24,14 +27,17 @@ impl FrameAnimationWrapper { } } + /// Start the animation pub fn start(&mut self, handle: &RaylibDrawHandle) { self.start_time_seconds = handle.get_time(); } + /// Stop (and reset) the animation pub fn stop(&mut self) { self.start_time_seconds = 0.0; } + /// Get the index of the currently displayed frame pub fn get_current_frame_id(&self, handle: &RaylibDrawHandle) -> u32 { // Get the time since start let time_since_start = handle.get_time() - self.start_time_seconds; @@ -41,11 +47,13 @@ impl FrameAnimationWrapper { as u32; } + /// Draw the next frame to the screen at `position` pub fn draw(&mut self, handle: &mut RaylibDrawHandle, position: Vector2) { let frame_id = self.get_current_frame_id(handle); self.draw_frame(handle, position, frame_id); } + /// Draw a specified frame to the screen at `position` pub fn draw_frame( &mut self, handle: &mut RaylibDrawHandle, @@ -67,7 +75,7 @@ impl FrameAnimationWrapper { width: self.size.x, height: self.size.y, }; - + // Render handle.draw_texture_rec(&mut self.sprite_sheet, frame_box, position, Color::WHITE); } diff --git a/src/lib/audio/wrapper.rs b/src/lib/wrappers/audio.rs similarity index 69% rename from src/lib/audio/wrapper.rs rename to src/lib/wrappers/audio.rs index ec2a159..67a76f1 100644 --- a/src/lib/audio/wrapper.rs +++ b/src/lib/wrappers/audio.rs @@ -1,30 +1,40 @@ -use raylib::{audio::{Music, RaylibAudio}, prelude::RaylibDrawHandle}; +use raylib::{ + audio::{Music, RaylibAudio}, + prelude::RaylibDrawHandle, +}; +/// A simple wrapper around a single audio clip. pub struct AudioWrapper { music: Music, } impl AudioWrapper { + /// Create a new AudioWrapper from a `Music` struct pub fn new(music: Music) -> Self { Self { music } } + /// Begin playing the audio pub fn play(&mut self, audio_handle: &mut RaylibAudio) { audio_handle.play_music_stream(&mut self.music); } + /// Stop playing the audio pub fn stop(&mut self, audio_handle: &mut RaylibAudio) { audio_handle.stop_music_stream(&mut self.music); } + /// Pause the audio pub fn pause(&mut self, audio_handle: &mut RaylibAudio) { audio_handle.pause_music_stream(&mut self.music); } + /// Call this every frame pub fn update(&mut self, audio_handle: &mut RaylibAudio) { audio_handle.update_music_stream(&mut self.music); } + /// Check if this audio clip is playing pub fn is_playing(&mut self, audio_handle: &mut RaylibAudio) -> bool { return audio_handle.is_music_playing(&mut self.music); } diff --git a/src/lib/wrappers/mod.rs b/src/lib/wrappers/mod.rs new file mode 100644 index 0000000..9af9ea2 --- /dev/null +++ b/src/lib/wrappers/mod.rs @@ -0,0 +1,2 @@ +pub mod audio; +pub mod animation; \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 43372e6..5d5615f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,6 @@ mod lib; -use lib::{animation::wrapper::FrameAnimationWrapper, audio::wrapper::AudioWrapper}; +use lib::wrappers::{animation::FrameAnimationWrapper, audio::AudioWrapper}; use raylib::prelude::*; fn main() {