瀏覽代碼

initial commit!

master
Isabelle L. 5 年之前
當前提交
4c63fd4cae
共有 6 個文件被更改,包括 134 次插入0 次删除
  1. +2
    -0
      .gitignore
  2. +10
    -0
      Cargo.toml
  3. +7
    -0
      LICENSE
  4. +4
    -0
      README.md
  5. +22
    -0
      src/color.rs
  6. +89
    -0
      src/lib.rs

+ 2
- 0
.gitignore 查看文件

@@ -0,0 +1,2 @@
/target
Cargo.lock

+ 10
- 0
Cargo.toml 查看文件

@@ -0,0 +1,10 @@
[package]
name = "offbrand"
description = "we have brand at home"
version = "0.1.0"
authors = ["Isabelle L. <me@izzabelle.dev>"]
edition = "2018"

[dependencies]
minifb = {git = "https://github.com/emoon/rust_minifb" }
thiserror = "1.0.19"

+ 7
- 0
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.

+ 4
- 0
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.

+ 22
- 0
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 };

+ 89
- 0
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<T> = std::result::Result<T, Error>;

// pixel buffer for internal use
struct PixelBuffer {
buffer: Vec<u32>,
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<Vec<u32>> for PixelBuffer {
fn as_ref(&self) -> &Vec<u32> {
&self.buffer
}
}

impl std::convert::AsMut<Vec<u32>> for PixelBuffer {
fn as_mut(&mut self) -> &mut Vec<u32> {
&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<Context> {
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();
}
}

Loading…
取消
儲存