Rust: Color pub u8
This commit is contained in:
@@ -6,12 +6,10 @@ pub const HEIGHT: usize = 240;
|
||||
|
||||
#[repr(transparent)]
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct Color(pub(crate) u8);
|
||||
pub struct Color(pub u8);
|
||||
|
||||
impl Color
|
||||
{
|
||||
pub const fn from_rgb(r: u8, g: u8, b: u8) -> Self
|
||||
{
|
||||
impl Color {
|
||||
pub const fn from_rgb(r: u8, g: u8, b: u8) -> Self {
|
||||
Self(((r >> 6) << 6) | ((g >> 5) << 3) | (b >> 5))
|
||||
}
|
||||
}
|
||||
@@ -26,12 +24,10 @@ pub const BLUE: Color = Color::from_rgb(0, 0, 255);
|
||||
|
||||
pub struct VGA {}
|
||||
|
||||
impl VGA
|
||||
{
|
||||
impl VGA {
|
||||
/// # Safety
|
||||
/// `x` must be less than `WIDTH` and `y` must be less than `HEIGHT`
|
||||
pub unsafe fn write_pixel_unsafe(x: u16, y: u16, color: Color)
|
||||
{
|
||||
pub unsafe fn write_pixel_unsafe(x: u16, y: u16, color: Color) {
|
||||
let pixel_index = x as usize + y as usize * WIDTH;
|
||||
let word_index = pixel_index / 4; // Get index of the word in which the pixel is
|
||||
let byte_index = pixel_index % 4; // Get index of the byte within the word for the pixel
|
||||
@@ -41,14 +37,10 @@ impl VGA
|
||||
|
||||
/// # Safety
|
||||
/// `x` must be less than `WIDTH` and `y` must be less than `HEIGHT`
|
||||
pub unsafe fn draw_char(x: u16, y: u16, c: char, color: Color)
|
||||
{
|
||||
let c = if (c as u8 > b'~') || ((c as u8) < b' ')
|
||||
{
|
||||
pub unsafe fn draw_char(x: u16, y: u16, c: char, color: Color) {
|
||||
let c = if (c as u8 > b'~') || ((c as u8) < b' ') {
|
||||
b'/' - b' '
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
c as u8 - b' '
|
||||
};
|
||||
|
||||
@@ -56,10 +48,8 @@ impl VGA
|
||||
let char_x = (c as usize % 32) * FONT_WIDTH;
|
||||
let char_y = (c as usize / 32) * FONT_HEIGHT;
|
||||
|
||||
for i in 0..(FONT_WIDTH as u16)
|
||||
{
|
||||
for j in 0..(FONT_HEIGHT as u16)
|
||||
{
|
||||
for i in 0..(FONT_WIDTH as u16) {
|
||||
for j in 0..(FONT_HEIGHT as u16) {
|
||||
let xx = x + i;
|
||||
let yy = y + j;
|
||||
|
||||
@@ -73,14 +63,10 @@ impl VGA
|
||||
}
|
||||
}
|
||||
|
||||
pub unsafe fn draw_char_bg(x: u16, y: u16, c: char, color: Color, bg_color: Color)
|
||||
{
|
||||
let c = if (c as u8 > b'~') || ((c as u8) < b' ')
|
||||
{
|
||||
pub unsafe fn draw_char_bg(x: u16, y: u16, c: char, color: Color, bg_color: Color) {
|
||||
let c = if (c as u8 > b'~') || ((c as u8) < b' ') {
|
||||
b'/' - b' '
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
c as u8 - b' '
|
||||
};
|
||||
|
||||
@@ -88,21 +74,15 @@ impl VGA
|
||||
let char_x = (c as usize % 32) * FONT_WIDTH;
|
||||
let char_y = (c as usize / 32) * FONT_HEIGHT;
|
||||
|
||||
for i in 0..(FONT_WIDTH as u16)
|
||||
{
|
||||
for j in 0..(FONT_HEIGHT as u16)
|
||||
{
|
||||
for i in 0..(FONT_WIDTH as u16) {
|
||||
for j in 0..(FONT_HEIGHT as u16) {
|
||||
let xx = x + i;
|
||||
let yy = y + j;
|
||||
|
||||
if xx < (WIDTH as u16) && yy < (HEIGHT as u16)
|
||||
{
|
||||
if unsafe { Self::font_plate_index(char_x as u16 + i, char_y as u16 + j) }
|
||||
{
|
||||
if xx < (WIDTH as u16) && yy < (HEIGHT as u16) {
|
||||
if unsafe { Self::font_plate_index(char_x as u16 + i, char_y as u16 + j) } {
|
||||
unsafe { Self::write_pixel_unsafe(xx, yy, color) }
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
unsafe { Self::write_pixel_unsafe(xx, yy, bg_color) }
|
||||
}
|
||||
}
|
||||
@@ -112,18 +92,15 @@ impl VGA
|
||||
|
||||
/// # Safety
|
||||
/// The text must have a length that can fit within a `u16`
|
||||
pub unsafe fn draw_string(x: u16, y: u16, str: &str, color: Color)
|
||||
{
|
||||
pub unsafe fn draw_string(x: u16, y: u16, str: &str, color: Color) {
|
||||
str.chars().enumerate().for_each(|(i, c)| unsafe {
|
||||
Self::draw_char(x + i as u16 * (FONT_WIDTH as u16), y, c as char, color)
|
||||
});
|
||||
}
|
||||
|
||||
fn digit_count(mut val: u64) -> u64
|
||||
{
|
||||
fn digit_count(mut val: u64) -> u64 {
|
||||
let mut i = 0;
|
||||
while val != 0
|
||||
{
|
||||
while val != 0 {
|
||||
val /= 10;
|
||||
i += 1;
|
||||
}
|
||||
@@ -131,17 +108,14 @@ impl VGA
|
||||
i
|
||||
}
|
||||
|
||||
pub unsafe fn draw_u64(x: u16, y: u16, mut val: u64, color: Color, bg_color: Color)
|
||||
{
|
||||
if val == 0
|
||||
{
|
||||
pub unsafe fn draw_u64(x: u16, y: u16, mut val: u64, color: Color, bg_color: Color) {
|
||||
if val == 0 {
|
||||
unsafe { Self::draw_char_bg(x, y, '0', color, bg_color) }
|
||||
return;
|
||||
}
|
||||
let digit_count = Self::digit_count(val) - 1;
|
||||
let mut i = digit_count as u16;
|
||||
while val != 0
|
||||
{
|
||||
while val != 0 {
|
||||
let digit = val % 10;
|
||||
val /= 10;
|
||||
|
||||
@@ -160,8 +134,7 @@ impl VGA
|
||||
}
|
||||
}
|
||||
|
||||
unsafe fn font_plate_index(x: u16, y: u16) -> bool
|
||||
{
|
||||
unsafe fn font_plate_index(x: u16, y: u16) -> bool {
|
||||
let pixel_index = (y as usize) * FONTPLATE_WIDTH + (x as usize);
|
||||
let byte_index = pixel_index / 8;
|
||||
let bit_index = pixel_index % 8;
|
||||
|
||||
Reference in New Issue
Block a user