diff --git a/MarathonData.md b/MarathonData.md index c229c58..d57aeb7 100644 --- a/MarathonData.md +++ b/MarathonData.md @@ -388,6 +388,7 @@ Map tags: | `ambi` | Array of Ambient Sound | | `bonk` | Array of Random Sound | | `term` | Array of Terminal | +| `NAME` | Unknown | | `påth` | Unused, supposed to be guardpaths (å is $8C) | | `door` | Unused, supposed to be extra door data | diff --git a/source/durandal/image.rs b/source/durandal/image.rs index aa539ba..637ea50 100644 --- a/source/durandal/image.rs +++ b/source/durandal/image.rs @@ -65,6 +65,8 @@ pub trait Image /// Returns the color of the pixel at column `x` at row `y`. /// + /// # Panics + /// /// Panics if `x` is greater than the width of the image or `y` is greater /// than the height of the image. fn index(&self, x: usize, y: usize) -> &Self::Output; @@ -80,6 +82,28 @@ pub trait Image } } +/// A generic color matrix image, which may be mutated. +pub trait ImageMut: Image +{ + /// Returns the color of the pixel at column `x` at row `y`. + /// + /// # Panics + /// + /// Panics if `x` is greater than the width of the image or `y` is greater + /// than the height of the image. + fn index_mut(&mut self, x: usize, y: usize) -> &mut Self::Output; + + /// The same as `index_mut`, but will not panic if out of bounds. + fn get_mut(&mut self, x: usize, y: usize) -> Option<&mut Self::Output> + { + if x < self.w() && y < self.h() { + Some(self.index_mut(x, y)) + } else { + None + } + } +} + /// Any color which may be represented as RGBA16. pub trait Color: Sized + Copy + Clone + Eq + PartialEq { @@ -98,11 +122,17 @@ pub trait Color: Sized + Copy + Clone + Eq + PartialEq impl Image16 { - /// Creates a new Image16. + /// Creates a new Image16 with no canvas. pub fn new(w: usize, h: usize) -> Self { Self{w, h, cr: Vec::with_capacity(w * h)} } + + /// Creates a new Image16 with an empty canvas. + pub fn new_empty(w: usize, h: usize) -> Self + { + Self{w, h, cr: vec![Color16::new(0, 0, 0); w * h]} + } } impl Image for Image16 @@ -118,13 +148,27 @@ impl Image for Image16 } } +impl ImageMut for Image16 +{ + fn index_mut(&mut self, x: usize, y: usize) -> &mut Self::Output + { + &mut self.cr[x + y * self.w] + } +} + impl Image8 { - /// Creates a new Image8. + /// Creates a new Image8 with no canvas. pub fn new(w: usize, h: usize) -> Self { Self{w, h, cr: Vec::with_capacity(w * h)} } + + /// Creates a new Image8 with an empty canvas. + pub fn new_empty(w: usize, h: usize) -> Self + { + Self{w, h, cr: vec![Color8::new(0, 0, 0); w * h]} + } } impl Image for Image8 @@ -140,6 +184,14 @@ impl Image for Image8 } } +impl ImageMut for Image8 +{ + fn index_mut(&mut self, x: usize, y: usize) -> &mut Self::Output + { + &mut self.cr[x + y * self.w] + } +} + impl Color16 { pub const fn new(r: u16, g: u16, b: u16) -> Self {Self(r, g, b)} diff --git a/source/durandal/sound.rs b/source/durandal/sound.rs index 075aaab..756ae3d 100644 --- a/source/durandal/sound.rs +++ b/source/durandal/sound.rs @@ -11,7 +11,9 @@ pub trait Sound /// Returns the `n`th sample. /// - /// May panic if `n` exceeds the length of this sound. + /// # Panics + /// + /// Panics if `n` exceeds the length of this sound. fn index(&self, n: usize) -> i16; /// Returns the number of the first sample of the loop, or `0`.