Another emoji reaction bot(this one is for Discord)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

43 lines
1.0 KiB

#[derive(Clone)]
pub(crate) struct FrameBuffer {
width: u32,
height: u32,
buffer: Vec<u8>,
}
impl FrameBuffer {
pub fn new(width: u32, height: u32) -> Self {
Self {
width,
height,
buffer: vec![0; width as usize * height as usize * 4],
}
}
pub fn set_pixel(&mut self, x: u32, y: u32, red: u8, green: u8, blue: u8, alpha: u8) {
if x >= self.width || y >= self.height {
return;
}
let x = x as usize;
let y = y as usize;
self.buffer[(x + self.width as usize * y) * 4] = red;
self.buffer[(x + self.width as usize * y) * 4 + 1] = green;
self.buffer[(x + self.width as usize * y) * 4 + 2] = blue;
self.buffer[(x + self.width as usize * y) * 4 + 3] = alpha;
}
pub fn as_png_vec(&self) -> Vec<u8> {
let mut buffer: Vec<u8> = Vec::new();
{
let mut encoder = png::Encoder::new(&mut buffer, self.width, self.height);
encoder.set_color(png::ColorType::RGBA);
encoder.set_depth(png::BitDepth::Eight);
let mut writer = encoder.write_header().unwrap();
writer.write_image_data(&self.buffer).unwrap();
}
buffer
}
}