From 75401cb5c7292291a1df9c5fd28da86a502c7829 Mon Sep 17 00:00:00 2001 From: gered Date: Thu, 9 Mar 2023 16:32:46 -0500 Subject: [PATCH] move repeated 'PixelType' type constraints to new Pixel trait --- ggdt/src/graphics/bitmap.rs | 8 +++----- ggdt/src/graphics/font.rs | 8 +++----- ggdt/src/graphics/mod.rs | 6 ++++++ 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/ggdt/src/graphics/bitmap.rs b/ggdt/src/graphics/bitmap.rs index 5bd68cc..767d2a1 100644 --- a/ggdt/src/graphics/bitmap.rs +++ b/ggdt/src/graphics/bitmap.rs @@ -8,13 +8,11 @@ use std::error::Error; -use num_traits::{PrimInt, Unsigned}; - -use crate::graphics::indexed; +use crate::graphics::{indexed, Pixel}; use crate::math::Rect; #[derive(Clone, PartialEq)] -pub enum GeneralBlitMethod { +pub enum GeneralBlitMethod { Solid, Transparent(PixelType), } @@ -23,7 +21,7 @@ pub enum GeneralBlitMethod { /// drawing functionality that is to be made generic across all supported bitmap types and is not specific to /// any one pixel-depth. Note that this does not provide cross-bit-depth drawing support. pub trait GeneralBitmap: Sized + Clone { - type PixelType: PrimInt + Unsigned; + type PixelType: Pixel; type ErrorType: Error; /// Creates a new bitmap with the specified dimensions, in pixels. diff --git a/ggdt/src/graphics/font.rs b/ggdt/src/graphics/font.rs index f31f324..895d4fb 100644 --- a/ggdt/src/graphics/font.rs +++ b/ggdt/src/graphics/font.rs @@ -4,11 +4,9 @@ use std::io::{BufReader, BufWriter, Cursor}; use std::path::Path; use byteorder::{ReadBytesExt, WriteBytesExt}; -use num_traits::{PrimInt, Unsigned}; use thiserror::Error; use crate::graphics::*; -use crate::graphics::indexed::*; use crate::math::*; pub static VGA_FONT_BYTES: &[u8] = include_bytes!("../../assets/vga.fnt"); @@ -27,7 +25,7 @@ pub enum FontError { } #[derive(Debug, Copy, Clone, Eq, PartialEq)] -pub enum FontRenderOpts { +pub enum FontRenderOpts { Color(PixelType), None, } @@ -47,7 +45,7 @@ pub trait Font { fn line_height(&self) -> u8; fn measure(&self, text: &str, opts: FontRenderOpts) -> (u32, u32) where - PixelType: PrimInt + Unsigned; + PixelType: Pixel; } #[derive(Debug, Clone, Eq, PartialEq)] @@ -210,7 +208,7 @@ impl Font for BitmaskFont { fn measure(&self, text: &str, _opts: FontRenderOpts) -> (u32, u32) where - PixelType: PrimInt + Unsigned + PixelType: Pixel { if text.is_empty() { return (0, 0); diff --git a/ggdt/src/graphics/mod.rs b/ggdt/src/graphics/mod.rs index f4c614c..f1325f8 100644 --- a/ggdt/src/graphics/mod.rs +++ b/ggdt/src/graphics/mod.rs @@ -1,3 +1,5 @@ +use num_traits::{PrimInt, Unsigned}; + pub use self::bitmap::*; pub use self::bitmapatlas::*; pub use self::font::*; @@ -7,3 +9,7 @@ pub mod bitmapatlas; pub mod font; pub mod indexed; pub mod rgb; + +/// Common trait to represent single pixel/colour values. +pub trait Pixel: PrimInt + Unsigned {} +impl Pixel for T where T: PrimInt + Unsigned {}