From 42d2f4836e5196c23d02de9f2618a216613a4110 Mon Sep 17 00:00:00 2001 From: Ryan Kennedy Date: Tue, 24 Mar 2020 15:44:30 -0500 Subject: [PATCH] Drawing text --- Cargo.toml | 1 + src/writers/graphics_640x480x16.rs | 26 ++++++++++++++++++++++++++ src/writers/mod.rs | 2 ++ 3 files changed, 29 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index 653dc02..fd9d993 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,6 +21,7 @@ repository = "https://github.com/rust-osdev/vga" [dependencies] bitflags = "1.2.1" conquer-once = { version = "0.2.0", default-features = false } +font8x8 = { version = "0.2.5", default-features = false, features = ["unicode"] } spinning_top = { version = "0.1.0", features = ["nightly"] } x86_64 = "0.9.6" diff --git a/src/writers/graphics_640x480x16.rs b/src/writers/graphics_640x480x16.rs index b7e9de3..f9d955e 100644 --- a/src/writers/graphics_640x480x16.rs +++ b/src/writers/graphics_640x480x16.rs @@ -5,6 +5,7 @@ use crate::{ registers::{PlaneMask, WriteMode}, vga::{Vga, VideoMode, VGA}, }; +use font8x8::UnicodeFonts; use spinning_top::SpinlockGuard; const WIDTH: usize = 640; @@ -62,6 +63,31 @@ impl GraphicsWriter for Graphics640x480x16 { } } + fn draw_character(&self, x: usize, y: usize, character: char, color: Color16Bit) { + let character = match font8x8::BASIC_FONTS.get(character) { + Some(character) => character, + None => font8x8::unicode::BLOCK_UNICODE[8].byte_array(), + }; + + { + let (mut vga, _frame_buffer) = self.get_frame_buffer(); + vga.graphics_controller_registers + .set_write_mode(WriteMode::Mode2); + vga.graphics_controller_registers.set_bit_mask(0xFF); + vga.sequencer_registers + .set_plane_mask(PlaneMask::ALL_PLANES); + } + + for (y1, byte) in character.iter().enumerate() { + for bit in 0..8 { + match *byte & 1 << bit { + 0 => {} + _ => self._set_pixel(x + bit, y + y1, color), + } + } + } + } + /// Sets the given pixel at `(x, y)` to the given `color`. /// /// **Note:** This method is provided for convenience, but has terrible diff --git a/src/writers/mod.rs b/src/writers/mod.rs index 1f738a1..2965ec3 100644 --- a/src/writers/mod.rs +++ b/src/writers/mod.rs @@ -185,6 +185,8 @@ pub trait GraphicsWriter { fn clear_screen(&self, color: Color); /// /// Draws a line from `start` to `end` with the specified `color`. fn draw_line(&self, start: Point, end: Point, color: Color); + /// Draws a character at the given `(x, y)` coordinant to the specified `color`. + fn draw_character(&self, x: usize, y: usize, character: char, color: Color); /// Sets the given pixel at `(x, y)` to the given `color`. /// /// **Note:** This method is provided for convenience, but has terrible