From 5a072f53820dd8e1b6cef4e24379a4eab049bd80 Mon Sep 17 00:00:00 2001 From: Ryan Kennedy Date: Tue, 24 Mar 2020 13:04:32 -0500 Subject: [PATCH] Moved to a GraphicsWriter trait --- src/writers/graphics_640x480x16.rs | 25 ++++++++++++++----------- src/writers/mod.rs | 18 ++++++++++++++++++ 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/src/writers/graphics_640x480x16.rs b/src/writers/graphics_640x480x16.rs index 58d77a3..b7e9de3 100644 --- a/src/writers/graphics_640x480x16.rs +++ b/src/writers/graphics_640x480x16.rs @@ -1,3 +1,4 @@ +use super::GraphicsWriter; use crate::{ colors::{Color16Bit, DEFAULT_PALETTE}, drawing::{Bresenham, Point}, @@ -19,7 +20,7 @@ const WIDTH_IN_BYTES: usize = WIDTH / 8; /// /// ```no_run /// use vga::colors::Color16Bit; -/// use vga::writers::Graphics640x480x16; +/// use vga::writers::{GraphicsWriter, Graphics640x480x16}; /// /// let graphics_mode = Graphics640x480x16::new(); /// @@ -29,14 +30,9 @@ const WIDTH_IN_BYTES: usize = WIDTH / 8; #[derive(Default)] pub struct Graphics640x480x16; -impl Graphics640x480x16 { - /// Creates a new `Graphics640x480x16`. - pub fn new() -> Graphics640x480x16 { - Graphics640x480x16 {} - } - +impl GraphicsWriter for Graphics640x480x16 { /// 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(); vga.graphics_controller_registers .set_write_mode(WriteMode::Mode2); @@ -51,7 +47,7 @@ impl Graphics640x480x16 { } /// Draws a line from `start` to `end` with the specified `color`. - pub fn draw_line(&self, start: Point, end: Point, color: Color16Bit) { + fn draw_line(&self, start: Point, end: Point, color: Color16Bit) { { let (mut vga, _frame_buffer) = self.get_frame_buffer(); 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 /// drawn. If you need to draw more then one pixel, consider using a method /// 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(); vga.graphics_controller_registers @@ -83,7 +79,7 @@ impl Graphics640x480x16 { } /// Sets the graphics device to `VideoMode::Mode640x480x16`. - pub fn set_mode(&self) { + fn set_mode(&self) { let mut vga = VGA.lock(); vga.set_video_mode(VideoMode::Mode640x480x16); @@ -91,6 +87,13 @@ impl Graphics640x480x16 { // so explicitly set it. 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 /// well as a lock to the vga driver. This ensures the vga diff --git a/src/writers/mod.rs b/src/writers/mod.rs index 5dc74af..1f738a1 100644 --- a/src/writers/mod.rs +++ b/src/writers/mod.rs @@ -6,6 +6,7 @@ mod text_80x25; use super::{ colors::{Color16Bit, TextModeColor}, + drawing::Point, registers::CrtcControllerIndex, 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 { + /// 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, end: Point, 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); +}