make seperate struct for buffer

This commit is contained in:
Simon Gardling
2021-04-01 13:56:10 -04:00
parent d887b13579
commit af0b9f9222
6 changed files with 63 additions and 24 deletions

39
src/buffer.rs Normal file
View File

@@ -0,0 +1,39 @@
#[derive(Debug)]
pub struct Buf {
pub width: usize,
pub height: usize,
pub buf: Vec<f32>,
}
impl Clone for Buf {
fn clone(&self) -> Buf {
Buf {
width: self.width,
height: self.height,
buf: self.buf.clone(),
}
}
}
impl Buf {
pub fn new(width: usize, height: usize, buf: Vec<f32>) -> Self {
Buf {
width,
height,
buf,
}
}
// Truncate x and y and return a corresponding index into the data slice.
fn index(&self, x: f32, y: f32) -> usize {
// x/y can come in negative, hence we shift them by width/height.
let i = (x + self.width as f32) as usize & (self.width - 1);
let j = (y + self.height as f32) as usize & (self.height - 1);
j * self.width + i
}
// Get the buffer value at a given position. The implementation effectively treats data as periodic, hence any finite position will produce a value.
pub fn get_buf(&self, x: f32, y: f32) -> f32 {
self.buf[self.index(x, y)]
}
}