Changed keys api and added check if window is open

Also removed ugly CLOSE_APP global
This commit is contained in:
Daniel Collin 2015-12-09 15:44:34 +01:00
parent b46868121e
commit d8c2a38f39
2 changed files with 17 additions and 34 deletions

View file

@ -19,7 +19,7 @@ fn main() {
Vsync::No)
.unwrap();
while window.update(&buffer) {
while window.is_open() && !window.is_key_down(Key::Escape) {
for i in buffer.iter_mut() {
noise = seed;
noise >>= 3;
@ -31,13 +31,6 @@ fn main() {
noise &= 0xFF;
*i = (noise << 16) | (noise << 8) | noise;
}
for key in window.get_keys().iter() {
match *key {
Key::A => println!("Pressed A"),
Key::B => println!("Pressed B"),
_ => (),
}
}
window.update(&buffer);
}
}

View file

@ -22,8 +22,6 @@ use self::winapi::winuser::WNDCLASSW;
use self::winapi::wingdi::BITMAPINFOHEADER;
use self::winapi::wingdi::RGBQUAD;
static mut CLOSE_APP: bool = false;
// Wrap this so we can have a proper numbef of bmiColors to write in
#[repr(C)]
struct BitmapInfo {
@ -133,9 +131,10 @@ unsafe extern "system" fn wnd_proc(window: winapi::HWND,
match msg {
winapi::winuser::WM_KEYDOWN => {
update_key_state(wnd, (lparam as u32) >> 16, true);
if (wparam & 0x1ff) == 27 {
CLOSE_APP = true;
}
winapi::winuser::WM_CLOSE => {
wnd.is_open = false;
}
winapi::winuser::WM_KEYUP => {
@ -198,6 +197,7 @@ pub struct Window {
window: Option<HWND>,
keys: [bool; 512],
buffer: Vec<u32>,
is_open : bool,
}
impl Window {
@ -277,30 +277,24 @@ impl Window {
window: Some(handle),
keys: [false; 512],
buffer: Vec::new(),
is_open: true,
};
Ok(window)
}
}
pub fn get_keys(&self) -> Vec<Key> {
let mut index: u16 = 0;
let mut keys: Vec<Key> = Vec::new();
for i in self.keys.iter() {
if *i {
unsafe {
keys.push(mem::transmute(index as u8));
}
#[inline]
pub fn is_key_down(&self, key: Key) -> bool {
return self.keys[key as usize];
}
index += 1;
#[inline]
pub fn is_open(&self) -> bool {
return self.is_open
}
keys
}
pub fn update(&mut self, buffer: &[u32]) -> bool {
pub fn update(&mut self, buffer: &[u32]) {
unsafe {
let mut msg = mem::uninitialized();
let window = self.window.unwrap();
@ -317,10 +311,6 @@ impl Window {
user32::DispatchMessageW(&mut msg);
}
}
unsafe {
return !CLOSE_APP;
}
}
}