diff --git a/.gitignore b/.gitignore index ea8c4bf..2a7f9d1 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /target +/temp \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index f95b85b..17002c0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,6 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "addr2line" @@ -288,7 +288,7 @@ checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724" [[package]] name = "chinchilib" -version = "0.2.1" +version = "0.3.1" dependencies = [ "array2d", "env_logger", diff --git a/Cargo.toml b/Cargo.toml index 5fdce71..9377e61 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "chinchilib" -version = "0.3.0" +version = "0.4.0" edition = "2021" license-file = "LICENSE.txt" description = "A graphical prototyping library for my students" diff --git a/README.md b/README.md index a4684d1..f302072 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,24 @@ It's mostly a wrapper arround pixels and winit that takes care of refresh rate, # Usage +`chinchilib` exposes two public functions for pixel access: + +- `get_pixel`\ + _Return the color `RGBA8` of a pixel at coordinates (x, y)_.\ + **The caller must ensure the coordinates are within bounds**\ + Parameters: + - `frame: &[u8]` - image buffer encoded in `RGBA8` + - `width: usize` - image width in pixels + - `x, y: usize` - pixel coordinates +- `put_pixel`\ + _Set the color of a pixel at coordinates (x, y)_.\ + **The caller must ensure the coordinates are within bounds**\ + Parameters: + - `frame: &mut [u8]` - mutable image buffer encoded in `RGBA8` + - `width: usize` - image width in pixels + - `x, y: usize` - pixel coordinates + - `color: rgb::RGBA8` - new pixel color + `WinitHandler` creates a window for you and manages events and timing. You can package your code into a `chinchilib::GfxApp` implementing struct such as `MovingPixel` in the example bellow. `WinitHandler` will make calls to: diff --git a/examples/moving_pixel.rs b/examples/moving_pixel.rs index 5367c4a..9ff97b6 100644 --- a/examples/moving_pixel.rs +++ b/examples/moving_pixel.rs @@ -39,7 +39,11 @@ const RED: rgb::RGBA8 = rgb::RGBA8 { }; impl GfxApp for MovingPixel { - fn on_tick(&mut self, pressed_keys: &std::collections::HashSet) -> bool { + fn on_tick( + &mut self, + pressed_keys: &std::collections::HashSet, + window_size: (usize, usize), + ) -> bool { let mut needs_redraw = true; for key in pressed_keys { match key { diff --git a/src/lib.rs b/src/lib.rs index e8f5d81..83ac1d7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -205,6 +205,12 @@ impl winit::application::ApplicationHandler for WinitHandler { } } +pub fn get_pixel(frame: &[u8], width: usize, x: usize, y: usize) -> rgb::RGBA8 { + use rgb::*; + let idx = y * width + x; + frame.as_rgba()[idx] +} + pub fn put_pixel(frame: &mut [u8], width: usize, x: usize, y: usize, color: rgb::RGBA8) { use rgb::*; let idx = width * y + x; @@ -278,7 +284,9 @@ impl WinFbx { fn on_tick(&mut self) { if self.app.done() == DoneStatus::NotDone { - self.needs_render = self.app.on_tick(&self.pressed_keys); + self.needs_render = self + .app + .on_tick(&self.pressed_keys, (self.width, self.height)); } self.pressed_keys .retain(|candidate| !self.released_keys.contains(candidate)); @@ -333,7 +341,11 @@ pub enum DoneStatus { pub trait GfxApp { /// Every tick, this method gets called with currently pressed keys. Released keys during the tick are considered still pressed. But will be removed after this call. - fn on_tick(&mut self, pressed_keys: &std::collections::HashSet) -> bool; + fn on_tick( + &mut self, + pressed_keys: &std::collections::HashSet, + window_size: (usize, usize), + ) -> bool; /// You get the pixel array, so you can draw on it before the render. fn draw(&mut self, pixels: &mut Pixels, width: usize);