Skip to main content

WindowAble

Trait WindowAble 

Source
pub trait WindowAble {
    // Required methods
    fn update(&mut self, context: Context) -> Option<WLibRequest>;
    fn draw(&mut self, pixel_buffer: &mut [u8], frame_info: WindowSize);
}
Expand description

The trait you must implement to create a window

Required Methods§

Source

fn update(&mut self, context: Context) -> Option<WLibRequest>

Ran before draw so you can set up your scene with information from context You can include any requests you want wlib to do in in the returned output

Source

fn draw(&mut self, pixel_buffer: &mut [u8], frame_info: WindowSize)

Write your pixels to this buffer Since the window size is controlled by compositor, the width and height is given here. Foramt is always ARGB little endian. (So real byte order is BGRA)

§Example
fn draw(buffer: &mut [u8], frame: wlib::WindowSize) {
    let width = frame.width;
    let height = frame.height;

    buffer
        .chunks_exact_mut(4)
        .enumerate()
        .for_each(|(index, chunk)| {
            let x = index as u32 % width;
            let y = index as u32 / width;

            let a: u8 = 0xFF;
            let r: u8 = ((x as f32 / (width as f32)) * 255.0) as u8;
            let g: u8 = 0;
            let b: u8 = ((y as f32 / (height as f32)) * 255.0) as u8;

            let array: &mut [u8; 4] = chunk.try_into().unwrap();
            *array = [b, g, r, a]
        });
}

Implementors§