Skip to content

Commit

Permalink
Implement Screen in terms of ndarray
Browse files Browse the repository at this point in the history
  • Loading branch information
maghoff committed Feb 13, 2018
1 parent 193103a commit 6c86fce
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 14 deletions.
58 changes: 58 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ version = "0.1.0"

[dependencies]
cgmath = "0.16.0"
ndarray = "0.11.1"

[profile.release]
opt-level = "s"
8 changes: 7 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![feature(allocator_api)]

extern crate cgmath;
extern crate ndarray;

mod core;
mod screen;
Expand Down Expand Up @@ -33,7 +34,12 @@ pub fn fill(
cx: f64, cy: f64,
dx: f64, dy: f64
) {
let screen_buf = unsafe { slice::from_raw_parts_mut(std::mem::transmute(screen_ptr), screen_width * screen_height) };
let screen_buf = unsafe {
slice::from_raw_parts_mut(
std::mem::transmute(screen_ptr),
screen_width * screen_height
)
};
let mut screen = screen::Screen::new(screen_buf, screen_width, screen_height);

let pos = Vector2::new(cx, cy);
Expand Down
21 changes: 8 additions & 13 deletions src/screen.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use ndarray::ArrayViewMut2;

#[repr(C)]
#[derive(Clone, Copy)]
pub struct Pixel {
Expand All @@ -7,27 +9,20 @@ pub struct Pixel {
pub a: u8,
}

pub struct Screen<'a> {
buf: &'a mut [Pixel],
width: usize,
height: usize,
}
pub struct Screen<'a>(ArrayViewMut2<'a, Pixel>);

impl<'a> Screen<'a> {
pub fn new(buf: &mut [Pixel], width: usize, height: usize) -> Screen {
assert!(buf.len() == width * height);
let buf = ArrayViewMut2::from_shape((height, width), buf).unwrap();

Screen {
buf,
width,
height,
}
Screen(buf)
}

pub fn width(&self) -> usize { self.width }
pub fn height(&self) -> usize { self.height }
pub fn width(&self) -> usize { self.0.dim().1 }
pub fn height(&self) -> usize { self.0.dim().0 }

pub fn px(&mut self, x: usize, y: usize) -> &mut Pixel {
&mut self.buf[y * self.width + x]
&mut self.0[[y, x]]
}
}

0 comments on commit 6c86fce

Please sign in to comment.