1
0
Fork 0
forked from AbleOS/ableos
ableos-idl/ableos/src/graphics/mod.rs
2022-01-18 08:30:09 -06:00

67 lines
1.6 KiB
Rust

use alloc::{boxed::Box, vec, vec::Vec};
use shadeable::pixel_format::{new_rgba64, Rgba64};
use vga::{colors::Color16, writers::GraphicsWriter};
use crate::vga_e::VGAE;
#[derive(Debug)]
pub struct ScreenSize {
pub x: usize,
pub y: usize,
}
impl ScreenSize {
pub fn new(x: usize, y: usize) -> Self {
Self { x, y }
}
}
pub enum GraphicsReturn {
Ok,
ImproperScreenSize,
}
pub struct ScreenBuffer {
pub size: ScreenSize,
pub clear_color: Rgba64,
pub buff: Box<[Rgba64]>, // Vec<Rgba64>,
}
impl ScreenBuffer {
// Add optional size later
pub fn new(x: usize, y: usize) -> Self {
Self {
size: ScreenSize::new(x, y),
clear_color: 0,
buff: vec![0u64; x * y].into_boxed_slice(),
}
}
#[inline]
pub fn set_pixel(&mut self, x: usize, y: usize, color: Rgba64) {
self.buff[y * self.size.x + x] = color;
}
pub fn clear(&mut self) {
self.buff = vec![0u64; self.buff.len()].into_boxed_slice();
}
pub fn blit(&mut self, _width: usize, _height: usize) {}
}
pub trait VgaBuffer {
fn copy_to_buffer(&self) -> GraphicsReturn;
}
impl VgaBuffer for ScreenBuffer {
fn copy_to_buffer(&self) -> GraphicsReturn {
let mode = VGAE.lock();
for y in 0..self.size.y {
for x in 0..self.size.x {
use shadeable::pixel_format::{get_color16, into_vga_16};
let vga_color = get_color16(self.buff[y * self.size.x + x]);
// let vga_color = into_vga_16(self.buff[y * self.size.x + x]);
mode.set_pixel(x, y, vga_color);
}
}
GraphicsReturn::Ok
}
}