From 619bbf3b89d132f60837d3f6156049992194d447 Mon Sep 17 00:00:00 2001 From: Isabelle L Date: Wed, 10 Jun 2020 02:38:11 -0500 Subject: [PATCH] idfk bro --- src/color.rs | 5 ++--- src/lib.rs | 35 +++++++++++++++++++---------------- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/src/color.rs b/src/color.rs index b6b980a..8cec646 100644 --- a/src/color.rs +++ b/src/color.rs @@ -1,4 +1,5 @@ -// a 32 bit rgb color +// a 32 bit rgb +#[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct Color { pub r: u8, pub g: u8, @@ -6,12 +7,10 @@ pub struct Color { } impl Color { - /// create a new color pub fn new(r: u8, g: u8, b: u8) -> Color { Color { r, g, b } } - /// return the color as a u32 pub fn as_u32(&self) -> u32 { let (r, g, b) = (self.r as u32, self.g as u32, self.b as u32); (r << 16) | (g << 8) | b diff --git a/src/lib.rs b/src/lib.rs index 50c807d..9babfbf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,6 +3,7 @@ pub mod color; // namespacing use color::Color; +pub use minifb::{Key, MouseButton}; use minifb::{Window, WindowOptions}; // error type @@ -19,12 +20,13 @@ pub type Result = std::result::Result; struct PixelBuffer { buffer: Vec, width: usize, + height: usize, } impl PixelBuffer { // create a new pixel buffer fn new(width: usize, height: usize) -> PixelBuffer { - PixelBuffer { width, buffer: vec![0; width * height] } + PixelBuffer { width, height, buffer: vec![0; width * height] } } } @@ -40,20 +42,6 @@ impl std::convert::AsMut> for PixelBuffer { } } -impl std::ops::Index<(usize, usize)> for PixelBuffer { - type Output = u32; - - fn index(&self, index: (usize, usize)) -> &u32 { - &self.buffer[index.1 * self.width + index.0] - } -} - -impl std::ops::IndexMut<(usize, usize)> for PixelBuffer { - fn index_mut(&mut self, index: (usize, usize)) -> &mut u32 { - &mut self.buffer[index.1 * self.width + index.0] - } -} - /// context data pub struct Context { pixel_buffer: PixelBuffer, @@ -79,11 +67,26 @@ impl Context { /// clears the pixel buffer pub fn clear(&mut self) { - self.pixel_buffer.as_mut().iter_mut().for_each(|pixel| *pixel = color::WHITE.as_u32()); + self.pixel_buffer.as_mut().iter_mut().for_each(|pixel| *pixel = color::BLACK.as_u32()); + } + + /// checks if window is open + pub fn is_open(&self) -> bool { + self.window.is_open() } /// set a pixel pub fn set_pixel(&mut self, x: usize, y: usize, color: Color) { self.pixel_buffer[(x, y)] = color.as_u32(); } + + /// get mouse position + pub fn get_mouse_pos(&self) -> Option<(f32, f32)> { + self.window.get_mouse_pos(minifb::MouseMode::Discard) + } + + /// get mouse down + pub fn get_mouse_down(&self, button: MouseButton) -> bool { + self.window.get_mouse_down(button) + } }