use super::{ColorFormat, Graphics, Image};
use crate::geom::Vector;
use crate::QuicksilverError;
pub struct Surface(pub(crate) golem::Surface);
impl Surface {
pub fn new(gfx: &Graphics, attachment: Image) -> Result<Surface, QuicksilverError> {
let tex = attachment
.into_raw()
.map_err(|_| QuicksilverError::SurfaceImageError)?;
Ok(Surface(golem::Surface::new(&gfx.ctx, tex)?))
}
pub fn attach(&mut self, attachment: Image) -> Result<(), QuicksilverError> {
let tex = attachment
.into_raw()
.map_err(|_| QuicksilverError::SurfaceImageError)?;
self.0.put_texture(tex);
Ok(())
}
pub fn detach(&mut self) -> Option<Image> {
Some(Image::new(self.0.take_texture()?))
}
pub fn screenshot(
&self,
gfx: &Graphics,
x: u32,
y: u32,
width: u32,
height: u32,
format: ColorFormat,
) -> Vec<u8> {
self.0.bind();
let mut buffer = vec![0; (width * height * format.bytes_per_pixel()) as usize];
self.0
.get_pixel_data(x, y, width, height, format, &mut buffer[..]);
golem::Surface::unbind(&gfx.ctx);
buffer
}
pub fn size(&self) -> Option<Vector> {
Some(Vector {
x: self.0.width()? as f32,
y: self.0.height()? as f32,
})
}
}