refactor mega-commit.
- reorganised the entire project so that the entire kernel is a single codebase rather than a kernel and a libk.
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
use super::{render::RenderError, window::Window};
|
||||
use crate::arch::x86_64::drivers::framebuffer::colour::Colour;
|
||||
use crate::std::maths::geometry::Vec2;
|
||||
use alloc::{vec, vec::Vec};
|
||||
|
||||
pub struct Frame<'f> {
|
||||
data: Vec<Vec<Colour>>,
|
||||
window: &'f Window,
|
||||
}
|
||||
|
||||
impl<'a> Frame<'a> {
|
||||
pub fn new(window: &'a Window) -> Self {
|
||||
Self {
|
||||
data: vec![vec![Colour::Black; window.dimensions().x()]; window.dimensions().y()],
|
||||
window,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn render(&self) -> Result<(), RenderError> {
|
||||
let data: Vec<&[Colour]> = self.data.iter().map(|v| v.as_slice()).collect::<Vec<_>>();
|
||||
self.window
|
||||
.render(data.as_slice())
|
||||
.map_err(|_| RenderError::Generic)
|
||||
}
|
||||
|
||||
pub fn write_pixel(&mut self, x: usize, y: usize, color: Colour) -> Result<(), RenderError> {
|
||||
if x >= self.window.dimensions().x() || y >= self.window.dimensions().y() {
|
||||
return Err(RenderError::Generic);
|
||||
}
|
||||
self.data[y][x] = color;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub const fn dimensions(&self) -> Vec2<usize> {
|
||||
self.window.dimensions()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
use crate::arch::x86_64::drivers::framebuffer::colour::Colour;
|
||||
use core::fmt::Display;
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum RenderError {
|
||||
Generic,
|
||||
}
|
||||
|
||||
impl Display for RenderError {
|
||||
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
||||
match self {
|
||||
Self::Generic => write!(f, "Generic render error"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl core::error::Error for RenderError {}
|
||||
|
||||
pub struct ColouredChar {
|
||||
ch: u8,
|
||||
colour: Colour,
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
use super::render::RenderError;
|
||||
use crate::arch::x86_64::drivers::framebuffer::colour::Colour;
|
||||
use crate::arch::x86_64::drivers::framebuffer::display::FRAMEBUFFER_WRITER;
|
||||
use crate::std::maths::geometry::Vec2;
|
||||
use alloc::string::String;
|
||||
|
||||
pub struct Window {
|
||||
dimensions: Vec2<usize>,
|
||||
position: Vec2<usize>,
|
||||
bordered: bool,
|
||||
opened: bool,
|
||||
title: String,
|
||||
}
|
||||
|
||||
impl Window {
|
||||
pub const fn new() -> Self {
|
||||
Self {
|
||||
dimensions: Vec2::new(0, 0),
|
||||
position: Vec2::new(0, 0),
|
||||
bordered: true,
|
||||
opened: false,
|
||||
title: String::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn render(&self, _data: &[&[Colour]]) -> Result<(), RenderError> {
|
||||
// TODO: error handling!! the kernel should return an error in some cases
|
||||
if let Some(fb) = FRAMEBUFFER_WRITER.lock().as_mut() {
|
||||
fb.render_frame(_data);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub const fn is_bordered(&self) -> bool {
|
||||
self.bordered
|
||||
}
|
||||
|
||||
pub const fn is_open(&self) -> bool {
|
||||
self.opened
|
||||
}
|
||||
|
||||
pub const fn open(&mut self) {
|
||||
self.opened = true;
|
||||
}
|
||||
|
||||
pub const fn close(&mut self) {
|
||||
self.opened = false;
|
||||
}
|
||||
|
||||
// some basic getters and setters for utility.
|
||||
pub fn title(&'static self) -> &'static str {
|
||||
self.title.as_str()
|
||||
}
|
||||
|
||||
pub const fn dimensions(&self) -> Vec2<usize> {
|
||||
self.dimensions
|
||||
}
|
||||
|
||||
pub const fn position(&self) -> Vec2<usize> {
|
||||
self.position
|
||||
}
|
||||
|
||||
pub fn set_title(&mut self, title: String) {
|
||||
self.title = title;
|
||||
}
|
||||
|
||||
pub fn move_window(&mut self, offset: Vec2<usize>) {
|
||||
self.position += offset;
|
||||
}
|
||||
|
||||
pub const fn set_position(&mut self, position: Vec2<usize>) {
|
||||
self.position = position;
|
||||
}
|
||||
|
||||
pub const fn set_dimensions(&mut self, dimensions: Vec2<usize>) {
|
||||
self.dimensions = dimensions;
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Window {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Window {
|
||||
fn drop(&mut self) {
|
||||
if self.opened {
|
||||
self.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user