video: Add a Color enum to make it easier to work with colors

This commit is contained in:
apio 2022-10-31 14:34:09 +01:00
parent ab4d34ecdb
commit 9902506264

View File

@ -1,27 +1,51 @@
#![allow(dead_code)]
use crate::bootboot::{BOOTBOOT, BOOTBOOT_FB}; use crate::bootboot::{BOOTBOOT, BOOTBOOT_FB};
use crate::util::get_bootboot; use crate::util::get_bootboot;
use BOOTBOOT_FB as FB; use BOOTBOOT_FB as FB;
pub fn put_pixel(x: u32, y: u32, color: u32) -> () { #[allow(dead_code)]
let boot: BOOTBOOT = get_bootboot(); #[derive(Debug, Clone, Copy, PartialEq, Eq)]
let ptr: u64 = FB + (boot.fb_scanline * y) as u64 + (x * 4) as u64; #[repr(u32)]
unsafe { *(ptr as *mut u32) = color }; pub enum Color {
Black = 0x00000000,
Blue = 0xff0000ff,
Green = 0xff00ff00,
Cyan = 0xff00ffff,
Red = 0xffff0000,
Magenta = 0xffff00ff,
Brown = 0xff231709,
LightGray = 0xffb9b8b5,
DarkGray = 0xff808080,
LightBlue = 0xff317ecc,
LightGreen = 0xff6aab8e,
LightCyan = 0xffe0ffff,
LightRed = 0xffff7377,
Pink = 0xffffc0cb,
Yellow = 0xffffff00,
White = 0xffffffff,
} }
pub fn draw_rect(x: u32, y: u32, width: u32, height: u32, color: u32) -> () { pub fn put_pixel(x: u32, y: u32, color: Color) -> () {
let boot: BOOTBOOT = get_bootboot();
let ptr: u64 = FB + (boot.fb_scanline * y) as u64 + (x * 4) as u64;
unsafe { *(ptr as *mut u32) = color as u32 };
}
pub fn draw_rect(x: u32, y: u32, width: u32, height: u32, color: Color) -> () {
let boot: BOOTBOOT = get_bootboot(); let boot: BOOTBOOT = get_bootboot();
for i in y..(y + height) for i in y..(y + height)
{ {
let addr: u64 = FB + (boot.fb_scanline * i) as u64 + (x * 4) as u64; let addr: u64 = FB + (boot.fb_scanline * i) as u64 + (x * 4) as u64;
for ptr in (addr..(addr + (width*4) as u64)).step_by(4) for ptr in (addr..(addr + (width*4) as u64)).step_by(4)
{ {
unsafe { *(ptr as *mut u32) = color }; unsafe { *(ptr as *mut u32) = color as u32 };
} }
} }
} }
pub fn clear(color: u32) -> () { pub fn clear(color: Color) -> () {
let boot: BOOTBOOT = get_bootboot(); let boot: BOOTBOOT = get_bootboot();
draw_rect(0, 0, boot.fb_width, boot.fb_height, color) draw_rect(0, 0, boot.fb_width, boot.fb_height, color)
} }