Moved to a GraphicsWriter trait

This commit is contained in:
Ryan Kennedy 2020-03-24 13:04:32 -05:00
parent 543eddbef7
commit 5a072f5382
2 changed files with 32 additions and 11 deletions

View file

@ -1,3 +1,4 @@
use super::GraphicsWriter;
use crate::{ use crate::{
colors::{Color16Bit, DEFAULT_PALETTE}, colors::{Color16Bit, DEFAULT_PALETTE},
drawing::{Bresenham, Point}, drawing::{Bresenham, Point},
@ -19,7 +20,7 @@ const WIDTH_IN_BYTES: usize = WIDTH / 8;
/// ///
/// ```no_run /// ```no_run
/// use vga::colors::Color16Bit; /// use vga::colors::Color16Bit;
/// use vga::writers::Graphics640x480x16; /// use vga::writers::{GraphicsWriter, Graphics640x480x16};
/// ///
/// let graphics_mode = Graphics640x480x16::new(); /// let graphics_mode = Graphics640x480x16::new();
/// ///
@ -29,14 +30,9 @@ const WIDTH_IN_BYTES: usize = WIDTH / 8;
#[derive(Default)] #[derive(Default)]
pub struct Graphics640x480x16; pub struct Graphics640x480x16;
impl Graphics640x480x16 { impl GraphicsWriter<Color16Bit> for Graphics640x480x16 {
/// Creates a new `Graphics640x480x16`.
pub fn new() -> Graphics640x480x16 {
Graphics640x480x16 {}
}
/// Clears the screen by setting all pixels to the specified `color`. /// Clears the screen by setting all pixels to the specified `color`.
pub fn clear_screen(&self, color: Color16Bit) { fn clear_screen(&self, color: Color16Bit) {
let (mut vga, frame_buffer) = self.get_frame_buffer(); let (mut vga, frame_buffer) = self.get_frame_buffer();
vga.graphics_controller_registers vga.graphics_controller_registers
.set_write_mode(WriteMode::Mode2); .set_write_mode(WriteMode::Mode2);
@ -51,7 +47,7 @@ impl Graphics640x480x16 {
} }
/// Draws a line from `start` to `end` with the specified `color`. /// Draws a line from `start` to `end` with the specified `color`.
pub fn draw_line(&self, start: Point<isize>, end: Point<isize>, color: Color16Bit) { fn draw_line(&self, start: Point<isize>, end: Point<isize>, color: Color16Bit) {
{ {
let (mut vga, _frame_buffer) = self.get_frame_buffer(); let (mut vga, _frame_buffer) = self.get_frame_buffer();
vga.graphics_controller_registers.write_set_reset(color); vga.graphics_controller_registers.write_set_reset(color);
@ -72,7 +68,7 @@ impl Graphics640x480x16 {
/// performance since it needs to ensure the correct `WriteMode` per pixel /// performance since it needs to ensure the correct `WriteMode` per pixel
/// drawn. If you need to draw more then one pixel, consider using a method /// drawn. If you need to draw more then one pixel, consider using a method
/// such as `draw_line`. /// such as `draw_line`.
pub fn set_pixel(&self, x: usize, y: usize, color: Color16Bit) { fn set_pixel(&self, x: usize, y: usize, color: Color16Bit) {
{ {
let (mut vga, _frame_buffer) = self.get_frame_buffer(); let (mut vga, _frame_buffer) = self.get_frame_buffer();
vga.graphics_controller_registers vga.graphics_controller_registers
@ -83,7 +79,7 @@ impl Graphics640x480x16 {
} }
/// Sets the graphics device to `VideoMode::Mode640x480x16`. /// Sets the graphics device to `VideoMode::Mode640x480x16`.
pub fn set_mode(&self) { fn set_mode(&self) {
let mut vga = VGA.lock(); let mut vga = VGA.lock();
vga.set_video_mode(VideoMode::Mode640x480x16); vga.set_video_mode(VideoMode::Mode640x480x16);
@ -91,6 +87,13 @@ impl Graphics640x480x16 {
// so explicitly set it. // so explicitly set it.
vga.color_palette_registers.load_palette(&DEFAULT_PALETTE); vga.color_palette_registers.load_palette(&DEFAULT_PALETTE);
} }
}
impl Graphics640x480x16 {
/// Creates a new `Graphics640x480x16`.
pub fn new() -> Graphics640x480x16 {
Graphics640x480x16 {}
}
/// Returns the start of the `FrameBuffer` as `*mut u8` as /// Returns the start of the `FrameBuffer` as `*mut u8` as
/// well as a lock to the vga driver. This ensures the vga /// well as a lock to the vga driver. This ensures the vga

View file

@ -6,6 +6,7 @@ mod text_80x25;
use super::{ use super::{
colors::{Color16Bit, TextModeColor}, colors::{Color16Bit, TextModeColor},
drawing::Point,
registers::CrtcControllerIndex, registers::CrtcControllerIndex,
vga::{Vga, VGA}, vga::{Vga, VGA},
}; };
@ -177,3 +178,20 @@ pub trait TextWriter: Screen {
} }
} }
} }
/// A helper trait used to interact with various vga graphics modes.
pub trait GraphicsWriter<Color> {
/// Clears the screen by setting all pixels to the specified `color`.
fn clear_screen(&self, color: Color);
/// /// Draws a line from `start` to `end` with the specified `color`.
fn draw_line(&self, start: Point<isize>, end: Point<isize>, color: Color);
/// Sets the given pixel at `(x, y)` to the given `color`.
///
/// **Note:** This method is provided for convenience, but has terrible
/// performance since it needs to ensure the correct `WriteMode` per pixel
/// drawn. If you need to draw more then one pixel, consider using a method
/// such as `draw_line`.
fn set_pixel(&self, x: usize, y: usize, color: Color);
/// Sets the graphics device to a `VideoMode`.
fn set_mode(&self);
}