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

View file

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