Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/target
/temp
4 changes: 2 additions & 2 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 5 additions & 1 deletion examples/moving_pixel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ const RED: rgb::RGBA8 = rgb::RGBA8 {
};

impl GfxApp for MovingPixel {
fn on_tick(&mut self, pressed_keys: &std::collections::HashSet<Key>) -> bool {
fn on_tick(
&mut self,
pressed_keys: &std::collections::HashSet<Key>,
window_size: (usize, usize),
) -> bool {
let mut needs_redraw = true;
for key in pressed_keys {
match key {
Expand Down
16 changes: 14 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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<Key>) -> bool;
fn on_tick(
&mut self,
pressed_keys: &std::collections::HashSet<Key>,
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);
Expand Down