make Bitmap::new private to force creation via concrete aliased types

This commit is contained in:
Gered 2023-03-11 17:24:53 -05:00
parent c1dcc39bc1
commit 503f822a87
4 changed files with 29 additions and 14 deletions

View file

@ -9,6 +9,17 @@ pub mod primitives;
pub type IndexedBitmap = Bitmap<u8>; pub type IndexedBitmap = Bitmap<u8>;
impl IndexedBitmap { impl IndexedBitmap {
/// Creates a new Bitmap with the specified dimensions.
///
/// # Arguments
///
/// * `width`: the width of the bitmap in pixels
/// * `height`: the height of the bitmap in pixels
///
/// returns: `Result<Bitmap, BitmapError>`
pub fn new(width: u32, height: u32) -> Result<Self, BitmapError> {
Self::internal_new(width, height)
}
pub fn load_file(path: &Path) -> Result<(Self, Palette), BitmapError> { pub fn load_file(path: &Path) -> Result<(Self, Palette), BitmapError> {
if let Some(extension) = path.extension() { if let Some(extension) = path.extension() {

View file

@ -60,15 +60,9 @@ impl<PixelType: Pixel> std::fmt::Debug for Bitmap<PixelType> {
impl<PixelType: Pixel> Bitmap<PixelType> { impl<PixelType: Pixel> Bitmap<PixelType> {
pub const PIXEL_SIZE: usize = std::mem::size_of::<PixelType>(); pub const PIXEL_SIZE: usize = std::mem::size_of::<PixelType>();
/// Creates a new Bitmap with the specified dimensions. // not public to force creation via one of the concrete types (even though this
/// // would technically work and be fine-ish)
/// # Arguments fn internal_new(width: u32, height: u32) -> Result<Self, BitmapError> {
///
/// * `width`: the width of the bitmap in pixels
/// * `height`: the height of the bitmap in pixels
///
/// returns: `Result<Bitmap, BitmapError>`
pub fn new(width: u32, height: u32) -> Result<Self, BitmapError> {
if width == 0 || height == 0 { if width == 0 || height == 0 {
return Err(BitmapError::InvalidDimensions); return Err(BitmapError::InvalidDimensions);
} }
@ -100,7 +94,7 @@ impl<PixelType: Pixel> Bitmap<PixelType> {
return Err(BitmapError::OutOfBounds); return Err(BitmapError::OutOfBounds);
} }
let mut bmp = Self::new(region.width, region.height)?; let mut bmp = Self::internal_new(region.width, region.height)?;
unsafe { bmp.solid_blit(source, region, 0, 0) }; unsafe { bmp.solid_blit(source, region, 0, 0) };
Ok(bmp) Ok(bmp)
} }

View file

@ -1,9 +1,19 @@
use crate::graphics::bitmap::Bitmap; use crate::graphics::bitmap::{Bitmap, BitmapError};
pub mod blit; pub mod blit;
pub type RgbaBitmap = Bitmap<u32>; pub type RgbaBitmap = Bitmap<u32>;
impl RgbaBitmap { impl RgbaBitmap {
/// Creates a new Bitmap with the specified dimensions.
///
/// # Arguments
///
/// * `width`: the width of the bitmap in pixels
/// * `height`: the height of the bitmap in pixels
///
/// returns: `Result<Bitmap, BitmapError>`
pub fn new(width: u32, height: u32) -> Result<Self, BitmapError> {
Self::internal_new(width, height)
}
} }

View file

@ -6,7 +6,7 @@ use ggdt::prelude::*;
fn setup() -> (IndexedBitmap, Palette) { fn setup() -> (IndexedBitmap, Palette) {
let palette = Palette::new_vga_palette().unwrap(); let palette = Palette::new_vga_palette().unwrap();
let screen = Bitmap::new(SCREEN_WIDTH, SCREEN_HEIGHT).unwrap(); let screen = IndexedBitmap::new(SCREEN_WIDTH, SCREEN_HEIGHT).unwrap();
(screen, palette) (screen, palette)
} }
@ -523,7 +523,7 @@ fn generate_bitmap(width: i32, height: i32) -> IndexedBitmap {
let x_third = width / 3; let x_third = width / 3;
let y_third = height / 3; let y_third = height / 3;
let mut bitmap = Bitmap::new(width as u32, height as u32).unwrap(); let mut bitmap = IndexedBitmap::new(width as u32, height as u32).unwrap();
bitmap.filled_rect(0, 0, x_third, y_third, 1); bitmap.filled_rect(0, 0, x_third, y_third, 1);
bitmap.filled_rect(x_third * 2 + 1, y_third * 2 + 1, width - 1, height - 1, 2); bitmap.filled_rect(x_third * 2 + 1, y_third * 2 + 1, width - 1, height - 1, 2);