Commit 059ae059 authored by Astro's avatar Astro
Browse files

l0dable: spin framebuffer, add RawColor

parent 29a6b351
use core::mem::{transmute, uninitialized}; use core::mem::{transmute, uninitialized};
use core::ops::{Deref, DerefMut}; use core::ops::{Index, IndexMut};
use crate::bindings::*; use crate::bindings::*;
use crate::{Color, Display}; use crate::Display;
mod font; mod font;
pub use font::*; pub use font::*;
...@@ -30,7 +30,7 @@ impl<'d> FrameBuffer<'d> { ...@@ -30,7 +30,7 @@ impl<'d> FrameBuffer<'d> {
} }
} }
pub fn text<'a, 'f>(&'a mut self, x: isize, y: isize, font: &'f Font, color: Color) -> TextRenderer<'a, 'd, 'f> { pub fn text<'a, 'f>(&'a mut self, x: isize, y: isize, font: &'f Font, color: RawColor) -> TextRenderer<'a, 'd, 'f> {
TextRenderer { TextRenderer {
framebuffer: self, framebuffer: self,
x, y, font, color, x, y, font, color,
...@@ -38,15 +38,59 @@ impl<'d> FrameBuffer<'d> { ...@@ -38,15 +38,59 @@ impl<'d> FrameBuffer<'d> {
} }
} }
impl<'d> Deref for FrameBuffer<'d> { impl<'d> Index<(u16, u16)> for FrameBuffer<'d> {
type Target = [[Color; Display::W as usize]; Display::H as usize]; type Output = RawColor;
fn deref(&self) -> &Self::Target { fn index(&self, (x, y): (u16, u16)) -> &Self::Output {
unsafe { transmute(&self.buffer.raw) } let x = usize::from(Display::W - 1 - x);
let y = usize::from(Display::H - 1 - y);
unsafe {
transmute(&self.buffer.fb[y][x])
}
}
}
impl<'d> IndexMut<(u16, u16)> for FrameBuffer<'d> {
fn index_mut(&mut self, (x, y): (u16, u16)) -> &mut Self::Output {
let x = usize::from(Display::W - 1 - x);
let y = usize::from(Display::H - 1 - y);
unsafe {
transmute(&mut self.buffer.fb[y][x])
}
} }
} }
impl<'d> DerefMut for FrameBuffer<'d> { #[derive(Debug, Clone, Copy)]
fn deref_mut(&mut self) -> &mut Self::Target { #[repr(C)]
unsafe { transmute(&mut self.buffer.raw) } pub struct RawColor([u8; 2]);
impl RawColor {
pub fn red() -> Self {
Self::rgb8(0xff, 0, 0)
}
pub fn blue() -> Self {
Self::rgb8(0, 0, 0xff)
}
pub fn green() -> Self {
Self::rgb8(0, 0xff, 0)
}
pub fn black() -> Self {
Self::rgb8(0, 0, 0)
}
pub fn white() -> Self {
Self::rgb8(0xff, 0xff, 0xff)
}
pub fn yellow() -> Self {
Self::rgb8(0xff, 0xff, 0)
}
pub fn rgb8(r8: u8, g8: u8, b8: u8) -> Self {
let b1 = (r8 & 0xF8) | (g8 >> 5);
let b2 = ((g8 & 0xFA) << 3) | (b8 >> 3);
RawColor([b1, b2])
} }
} }
use core::fmt::Write; use core::fmt::Write;
use super::{FrameBuffer, Font}; use super::{FrameBuffer, Font, RawColor};
use crate::{Color, Display}; use crate::{Display};
pub struct TextRenderer<'a, 'd, 'f> { pub struct TextRenderer<'a, 'd, 'f> {
pub framebuffer: &'a mut FrameBuffer<'d>, pub framebuffer: &'a mut FrameBuffer<'d>,
pub x: isize, pub x: isize,
pub y: isize, pub y: isize,
pub font: &'f Font, pub font: &'f Font,
pub color: Color, pub color: RawColor,
} }
impl<'a, 'd, 'f> Write for TextRenderer<'a, 'd, 'f> { impl<'a, 'd, 'f> Write for TextRenderer<'a, 'd, 'f> {
...@@ -23,13 +23,13 @@ impl<'a, 'd, 'f> Write for TextRenderer<'a, 'd, 'f> { ...@@ -23,13 +23,13 @@ impl<'a, 'd, 'f> Write for TextRenderer<'a, 'd, 'f> {
None => Ok(()), None => Ok(()),
Some(glyph) => { Some(glyph) => {
for y in 0..self.font.h { for y in 0..self.font.h {
let y1 = self.y + y as isize; let y1 = (self.y + y as isize) as u16;
if y1 >= 0 && y1 < Display::H as isize { if y1 < Display::H {
for x in 0..self.font.w { for x in 0..self.font.w {
let x1 = self.x + x as isize; let x1 = (self.x + x as isize) as u16;
if x1 >= 0 && x1 < Display::W as isize { if x1 < Display::W {
if glyph.get_pixel(x as usize, y as usize) { if glyph.get_pixel(x as usize, y as usize) {
self.framebuffer[y1 as usize][x1 as usize] = self.color; self.framebuffer[(x1, y1)] = self.color;
} }
} }
} }
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment