Files
wldash/src/buffer.rs
T

111 lines
3.5 KiB
Rust
Raw Normal View History

2019-07-30 16:31:42 +02:00
use memmap::MmapMut;
use crate::color::Color;
pub struct Buffer<'a> {
buf: &'a mut MmapMut,
dimensions: (u32, u32),
subdimensions: Option<(u32, u32, u32, u32)>,
}
impl<'a> Buffer<'a> {
pub fn new(buf: &'a mut MmapMut, dimensions: (u32, u32)) -> Buffer {
Buffer {
buf: buf,
dimensions: dimensions,
subdimensions: None,
}
}
pub fn get_bounds(&self) -> (u32, u32, u32, u32) {
if let Some(subdim) = self.subdimensions {
subdim
} else {
(0, 0, self.dimensions.0, self.dimensions.1)
}
}
2019-07-31 00:42:03 +02:00
pub fn get_signed_bounds(&self) -> (i32, i32, i32, i32) {
if let Some(subdim) = self.subdimensions {
(
subdim.0 as i32,
subdim.1 as i32,
subdim.2 as i32,
subdim.3 as i32,
)
} else {
(0, 0, self.dimensions.0 as i32, self.dimensions.1 as i32)
}
}
2019-08-02 15:59:10 +02:00
pub fn subdimensions(&mut self, subdimensions: (u32, u32, u32, u32)) -> Result<Buffer, ::std::io::Error> {
2019-07-30 16:31:42 +02:00
let bounds = self.get_bounds();
if subdimensions.0 + subdimensions.2 > bounds.2
|| subdimensions.1 + subdimensions.3 > bounds.3
{
2019-08-02 15:59:10 +02:00
return Err(::std::io::Error::new(::std::io::ErrorKind::Other, format!("cannot create subdimensions larger than buffer: {:?} > {:?}",
2019-07-30 16:31:42 +02:00
subdimensions, bounds
2019-08-02 15:59:10 +02:00
)));
2019-07-30 16:31:42 +02:00
}
2019-08-02 15:59:10 +02:00
Ok(Buffer {
2019-07-30 16:31:42 +02:00
buf: self.buf,
dimensions: self.dimensions,
subdimensions: Some((
subdimensions.0 + bounds.0,
subdimensions.1 + bounds.1,
subdimensions.2,
subdimensions.3,
)),
2019-08-02 15:59:10 +02:00
})
2019-07-30 16:31:42 +02:00
}
pub fn memset(&mut self, c: &Color) {
2019-07-31 00:42:03 +02:00
if let Some(subdim) = self.subdimensions {
unsafe {
let ptr = self.buf.as_mut_ptr();
for y in subdim.1..(subdim.1 + subdim.3) {
for x in subdim.0..(subdim.0 + subdim.2) {
*((ptr as *mut u32).offset((x + y * self.dimensions.0) as isize)) =
c.as_argb8888();
}
}
}
} else {
unsafe {
let ptr = self.buf.as_mut_ptr();
for p in 0..(self.dimensions.0 * self.dimensions.1) {
*((ptr as *mut u32).offset(p as isize)) = c.as_argb8888();
}
2019-07-30 16:31:42 +02:00
}
}
}
2019-08-02 15:59:10 +02:00
pub fn put(&mut self, pos: (u32, u32), c: &Color) -> Result<(), ::std::io::Error> {
2019-07-30 16:31:42 +02:00
let true_pos = if let Some(subdim) = self.subdimensions {
if pos.0 > subdim.2 || pos.1 > subdim.3 {
2019-08-02 15:59:10 +02:00
return Err(::std::io::Error::new(::std::io::ErrorKind::Other, format!("put({:?}) is not within subdimensions of buffer ({:?})",
pos, subdim)));
2019-07-30 16:31:42 +02:00
}
(pos.0 + subdim.0, pos.1 + subdim.1)
} else {
if pos.0 >= self.dimensions.0 || pos.1 >= self.dimensions.1 {
2019-08-02 15:59:10 +02:00
return Err(::std::io::Error::new(::std::io::ErrorKind::Other, format!("put({:?}) is not within dimensions of buffer ({:?})",
2019-07-30 16:31:42 +02:00
pos, self.dimensions
2019-08-02 15:59:10 +02:00
)));
2019-07-30 16:31:42 +02:00
}
pos
};
unsafe {
let ptr = self
.buf
.as_mut_ptr()
.offset(4 * (true_pos.0 + (true_pos.1 * self.dimensions.0)) as isize);
*(ptr as *mut u32) = c.as_argb8888();
2019-08-02 15:59:10 +02:00
};
Ok(())
2019-07-30 16:31:42 +02:00
}
}