From 6547019382e2fe881c48c41c42f8c673488591f0 Mon Sep 17 00:00:00 2001 From: Daniel Beckwith Date: Tue, 31 Mar 2020 18:29:32 -0400 Subject: [PATCH] Make screen width and height constant --- src/writers/graphics_320x200x256.rs | 17 +++-------------- src/writers/graphics_640x480x16.rs | 12 +++--------- src/writers/mod.rs | 24 +++++++++++------------- src/writers/text_40x25.rs | 15 ++++----------- src/writers/text_40x50.rs | 15 ++++----------- src/writers/text_80x25.rs | 15 ++++----------- 6 files changed, 29 insertions(+), 69 deletions(-) diff --git a/src/writers/graphics_320x200x256.rs b/src/writers/graphics_320x200x256.rs index fd582f4..35ed689 100644 --- a/src/writers/graphics_320x200x256.rs +++ b/src/writers/graphics_320x200x256.rs @@ -37,20 +37,9 @@ const SIZE: usize = WIDTH * HEIGHT; pub struct Graphics320x200x256; impl Screen for Graphics320x200x256 { - #[inline] - fn get_width(&self) -> usize { - WIDTH - } - - #[inline] - fn get_height(&self) -> usize { - HEIGHT - } - - #[inline] - fn get_size(&self) -> usize { - SIZE - } + const WIDTH: usize = WIDTH; + const HEIGHT: usize = HEIGHT; + const SIZE: usize = SIZE; } impl GraphicsWriter for Graphics320x200x256 { diff --git a/src/writers/graphics_640x480x16.rs b/src/writers/graphics_640x480x16.rs index e287ce8..6a35125 100644 --- a/src/writers/graphics_640x480x16.rs +++ b/src/writers/graphics_640x480x16.rs @@ -40,15 +40,9 @@ const WIDTH_IN_BYTES: usize = WIDTH / 8; pub struct Graphics640x480x16; impl Screen for Graphics640x480x16 { - fn get_width(&self) -> usize { - WIDTH - } - fn get_height(&self) -> usize { - HEIGHT - } - fn get_size(&self) -> usize { - SIZE - } + const WIDTH: usize = WIDTH; + const HEIGHT: usize = HEIGHT; + const SIZE: usize = SIZE; } impl GraphicsWriter for Graphics640x480x16 { diff --git a/src/writers/mod.rs b/src/writers/mod.rs index 499b031..1759a29 100644 --- a/src/writers/mod.rs +++ b/src/writers/mod.rs @@ -51,12 +51,12 @@ static BLANK_CHARACTER: ScreenCharacter = ScreenCharacter { /// A helper trait used to interact with various vga screens. pub trait Screen { - /// Returns the width of the `Screen`. - fn get_width(&self) -> usize; - /// Returns the height of the `Screen`. - fn get_height(&self) -> usize; - /// Returns the size of the `Screen`. - fn get_size(&self) -> usize; + /// The width of the `Screen`. + const WIDTH: usize; + /// The height of the `Screen`. + const HEIGHT: usize; + /// The size (total area) of the `Screen`. + const SIZE: usize; } /// A helper trait used to interact with various vga text modes. @@ -79,8 +79,7 @@ pub trait TextWriter: Screen { /// color of `Color16::Yellow`. fn clear_screen(&self) { let (_vga, frame_buffer) = self.get_frame_buffer(); - let screen_size = self.get_width() * self.get_height(); - for i in 0..screen_size { + for i in 0..Self::SIZE { unsafe { frame_buffer.add(i).write_volatile(BLANK_CHARACTER); } @@ -94,8 +93,7 @@ pub trait TextWriter: Screen { character: b' ', color, }; - let screen_size = self.get_width() * self.get_height(); - for i in 0..screen_size { + for i in 0..Self::SIZE { unsafe { frame_buffer.add(i).write_volatile(character); } @@ -133,7 +131,7 @@ pub trait TextWriter: Screen { /// Returns the `ScreenCharacter` at the given `(x, y)` position. fn read_character(&self, x: usize, y: usize) -> ScreenCharacter { let (_vga, frame_buffer) = self.get_frame_buffer(); - let offset = self.get_width() * y + x; + let offset = Self::WIDTH * y + x; unsafe { frame_buffer.add(offset).read_volatile() } } @@ -169,7 +167,7 @@ pub trait TextWriter: Screen { /// Sets the current text cursor to the position specified by /// `x` and `y`. fn set_cursor_position(&self, x: usize, y: usize) { - let offset = self.get_width() * y + x; + let offset = Self::WIDTH * y + x; let (mut vga, _frame_buffer) = self.get_frame_buffer(); let emulation_mode = vga.get_emulation_mode(); let cursor_start = offset & 0xFF; @@ -189,7 +187,7 @@ pub trait TextWriter: Screen { /// Prints the given `character` and `color` at `(x, y)`. fn write_character(&self, x: usize, y: usize, screen_character: ScreenCharacter) { let (_vga, frame_buffer) = self.get_frame_buffer(); - let offset = self.get_width() * y + x; + let offset = Self::WIDTH * y + x; unsafe { frame_buffer.add(offset).write_volatile(screen_character); } diff --git a/src/writers/text_40x25.rs b/src/writers/text_40x25.rs index 2ead79f..91e547f 100644 --- a/src/writers/text_40x25.rs +++ b/src/writers/text_40x25.rs @@ -7,6 +7,7 @@ use crate::{ const WIDTH: usize = 40; const HEIGHT: usize = 25; +const SIZE: usize = WIDTH * HEIGHT; /// A basic interface for interacting with vga text mode 40x25 /// @@ -30,17 +31,9 @@ const HEIGHT: usize = 25; pub struct Text40x25; impl Screen for Text40x25 { - fn get_width(&self) -> usize { - WIDTH - } - - fn get_height(&self) -> usize { - HEIGHT - } - - fn get_size(&self) -> usize { - WIDTH * HEIGHT - } + const WIDTH: usize = WIDTH; + const HEIGHT: usize = HEIGHT; + const SIZE: usize = SIZE; } impl TextWriter for Text40x25 { diff --git a/src/writers/text_40x50.rs b/src/writers/text_40x50.rs index f327905..b1a8a68 100644 --- a/src/writers/text_40x50.rs +++ b/src/writers/text_40x50.rs @@ -7,6 +7,7 @@ use crate::{ const WIDTH: usize = 40; const HEIGHT: usize = 50; +const SIZE: usize = WIDTH * HEIGHT; /// A basic interface for interacting with vga text mode 40x50 /// @@ -30,17 +31,9 @@ const HEIGHT: usize = 50; pub struct Text40x50; impl Screen for Text40x50 { - fn get_width(&self) -> usize { - WIDTH - } - - fn get_height(&self) -> usize { - HEIGHT - } - - fn get_size(&self) -> usize { - WIDTH * HEIGHT - } + const WIDTH: usize = WIDTH; + const HEIGHT: usize = HEIGHT; + const SIZE: usize = SIZE; } impl TextWriter for Text40x50 { diff --git a/src/writers/text_80x25.rs b/src/writers/text_80x25.rs index 94aef4f..19811e2 100644 --- a/src/writers/text_80x25.rs +++ b/src/writers/text_80x25.rs @@ -7,6 +7,7 @@ use crate::{ const WIDTH: usize = 80; const HEIGHT: usize = 25; +const SIZE: usize = WIDTH * HEIGHT; /// A basic interface for interacting with vga text mode 80x25 /// @@ -30,17 +31,9 @@ const HEIGHT: usize = 25; pub struct Text80x25; impl Screen for Text80x25 { - fn get_width(&self) -> usize { - WIDTH - } - - fn get_height(&self) -> usize { - HEIGHT - } - - fn get_size(&self) -> usize { - WIDTH * HEIGHT - } + const WIDTH: usize = WIDTH; + const HEIGHT: usize = HEIGHT; + const SIZE: usize = SIZE; } impl TextWriter for Text80x25 {