From 4c63fd4cae6625ba3312b4a88b90a39e4ffa323b Mon Sep 17 00:00:00 2001 From: Isabelle L Date: Sat, 6 Jun 2020 18:57:37 -0500 Subject: [PATCH] initial commit! --- .gitignore | 2 ++ Cargo.toml | 10 ++++++ LICENSE | 7 +++++ README.md | 4 +++ src/color.rs | 22 +++++++++++++ src/lib.rs | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 134 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.toml create mode 100644 LICENSE create mode 100644 README.md create mode 100644 src/color.rs create mode 100644 src/lib.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..96ef6c0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target +Cargo.lock diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..7281073 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "offbrand" +description = "we have brand at home" +version = "0.1.0" +authors = ["Isabelle L. "] +edition = "2018" + +[dependencies] +minifb = {git = "https://github.com/emoon/rust_minifb" } +thiserror = "1.0.19" diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..9086a1c --- /dev/null +++ b/LICENSE @@ -0,0 +1,7 @@ +Copyright 2020 Isabelle L. + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..a0a8174 --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +# offbrand +### simple minifb wrapper + +uh i like the idea of minifb but wanted a little bit more of a rendering api built ontop of it. i'll probably add more shit down the line but right now it's literally just a pixel buf and colors. \ No newline at end of file diff --git a/src/color.rs b/src/color.rs new file mode 100644 index 0000000..b6b980a --- /dev/null +++ b/src/color.rs @@ -0,0 +1,22 @@ +// a 32 bit rgb color +pub struct Color { + pub r: u8, + pub g: u8, + pub b: u8, +} + +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 + } +} + +pub const BLACK: Color = Color { r: 0x00, g: 0x00, b: 0x00 }; +pub const WHITE: Color = Color { r: 0xff, g: 0xff, b: 0xff }; diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..50c807d --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,89 @@ +// modules +pub mod color; + +// namespacing +use color::Color; +use minifb::{Window, WindowOptions}; + +// error type +#[derive(Debug, thiserror::Error)] +pub enum Error { + #[error(transparent)] + MiniFb(#[from] minifb::Error), +} + +// result type +pub type Result = std::result::Result; + +// pixel buffer for internal use +struct PixelBuffer { + buffer: Vec, + width: usize, +} + +impl PixelBuffer { + // create a new pixel buffer + fn new(width: usize, height: usize) -> PixelBuffer { + PixelBuffer { width, buffer: vec![0; width * height] } + } +} + +impl std::convert::AsRef> for PixelBuffer { + fn as_ref(&self) -> &Vec { + &self.buffer + } +} + +impl std::convert::AsMut> for PixelBuffer { + fn as_mut(&mut self) -> &mut Vec { + &mut self.buffer + } +} + +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, + height: usize, + width: usize, + window: Window, +} + +impl Context { + /// create a new context + pub fn new(width: usize, height: usize, title: String) -> Result { + let pixel_buffer = PixelBuffer::new(height, width); + let mut window = Window::new(&title, width, height, WindowOptions::default())?; + window.limit_update_rate(Some(std::time::Duration::from_micros(16600))); + Ok(Context { pixel_buffer, window, height, width }) + } + + /// render the internal buffer to the screen + pub fn present(&mut self) -> Result<()> { + self.window.update_with_buffer(self.pixel_buffer.as_ref(), self.width, self.height)?; + Ok(()) + } + + /// clears the pixel buffer + pub fn clear(&mut self) { + self.pixel_buffer.as_mut().iter_mut().for_each(|pixel| *pixel = color::WHITE.as_u32()); + } + + /// set a pixel + pub fn set_pixel(&mut self, x: usize, y: usize, color: Color) { + self.pixel_buffer[(x, y)] = color.as_u32(); + } +}