Struct minifb::Window [] [src]

pub struct Window(_);

Window used for displaying a 32-bit RGB buffer. Here is a small example on how to use it: (without error checking


const WIDTH: usize = 640;
const HEIGHT: usize = 360;

let mut buffer: Vec<u32> = vec![0; WIDTH * HEIGHT];

let mut window = match Window::new("Test - Press ESC to exit", WIDTH, HEIGHT, Scale::X1).unwrap()

while window.is_open() && !window.is_key_down(Key::Escape) {
    for i in buffer.iter_mut() {
        *i = 0; // write something interesting here
    }
    window.update(&buffer);
}

Methods

impl Window
[src]

fn new(name: &str, width: usize, height: usize, scale: Scale) -> Result<Window, &str>

Opens up a new window

 let mut window = match Window::new("Test", 640, 400, Scale::X1) {
    Ok(win) => win,
    Err(err) => {
        println!("Unable to create window {}", err);
        return;
    }
};

fn update(&mut self, buffer: &[u32])

Updates the window with a 32-bit pixel buffer. Notice that the buffer needs to be at least the size of the created window

Examples

let mut buffer: Vec<u32> = vec![0; 640 * 400];

let mut window = match Window::new("Test", 640, 400, Scale::X1).unwrap();

window.update(&buffer);

fn is_open(&self) -> bool

Checks if the window is still open. A window can be closed by the user (by for example pressing the close button on the window) It's up to the user to make sure that this is being checked and take action depending on the state.

Examples

while window.is_open() {
    window.update(...)
}

fn set_position(&mut self, x: isize, y: isize)

Sets the position of the window. This is useful if you have more than one window and want to align them up on the screen

Examples

// Moves the window to pixel postion 20, 20 on the screen
window.set_position(20, 20);
}

fn get_keys(&self) -> Option<Vec<Key>>

Get the current keys that are down.

Examples

window.get_keys().map(|keys| {
    for t in keys {
        match t {
            Key::W => println!("holding w"),
            Key::T => println!("holding t"),
            _ => (),
        }
    }
});

fn get_keys_pressed(&self, repeat: KeyRepeat) -> Option<Vec<Key>>

Get the current pressed keys. Repeat can be used to control if keys should be repeated if down or not.

Examples

window.get_keys_pressed(KeyRepeat::No).map(|keys| {
    for t in keys {
        match t {
            Key::W => println!("pressed w"),
            Key::T => println!("pressed t"),
            _ => (),
        }
    }
});

fn is_key_down(&self, key: Key) -> bool

Check if a single key is down.

Examples

if window.is_key_down(Key::A) {
    println!("Key A is down");
}

fn is_key_pressed(&self, key: Key, repeat: KeyRepeat) -> bool

Check if a single key is pressed. KeyRepeat will control if the key should be repeated or not while being pressed.

Examples

if window.is_key_pressed(KeyRepeat::No) {
    println!("Key A is down");
}

fn set_key_repeat_delay(&mut self, delay: f32)

Sets the delay for when a key is being held before it starts being repeated the default value is 0.25 sec

Examples

window.set_key_repeat_delay(0.5) // 0.5 sec before repeat starts

fn set_key_repeat_rate(&mut self, rate: f32)

Sets the rate in between when the keys has passed the intital repeat_delay. The default value is 0.05 sec

Examples

window.set_key_repeat_rate(0.01) // 0.01 sec between keys