diff --git a/Cargo.toml b/Cargo.toml index 549cc2a..108fc04 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,11 +14,6 @@ gcc = "0.3.19" libc = "0.2" time = "0.1.34" -[target.x86_64-apple-darwin.dependencies] -cocoa = "0.2" -core-foundation = "0" -core-graphics = "0" - [target.x86_64-pc-windows-msvc.dependencies] user32-sys = "0.1.2" winapi = "0.2.4" diff --git a/examples/noise.rs b/examples/noise.rs index 4279f15..ab39f11 100644 --- a/examples/noise.rs +++ b/examples/noise.rs @@ -33,9 +33,9 @@ fn main() { *i = (noise << 16) | (noise << 8) | noise; } - window.get_keys_pressed(KeyRepeat::No).map(|keys| { - for t in keys.iter() { - match *t { + window.get_keys().map(|keys| { + for t in keys { + match t { Key::W => println!("holding w!"), Key::T => println!("holding t!"), _ => (), diff --git a/src/lib.rs b/src/lib.rs index eb2ef5f..31dc551 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -159,15 +159,171 @@ pub enum Key { extern crate libc; -pub mod key_handler; - -#[cfg(target_os = "windows")] -pub mod windows; -#[cfg(target_os = "windows")] -pub use windows::*; +pub mod os; +mod key_handler; #[cfg(target_os = "macos")] -pub mod macos; -#[cfg(target_os = "macos")] -pub use macos::*; +use self::os::macos as imp; +#[cfg(target_os = "windows")] +use self::os::windows as imp; +/// Window used for displaying a 32-bit RGB buffer +pub struct Window(imp::Window); + +impl Window { + /// + /// Opens up a new window + /// + /// ```ignore + /// let mut window = match Window::new("Test", 640, 400, Scale::X1) { + /// Ok(win) => win, + /// Err(err) => { + /// println!("Unable to create window {}", err); + /// return; + /// } + ///}; + /// ``` + pub fn new(name: &str, width: usize, height: usize, scale: Scale) -> Result { + imp::Window::new(name, width, height, scale) + } + + /// + /// 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 + /// + /// ```ignore + /// let mut buffer: [u32; 640 * 400] = [0; 640 * 400]; + /// + /// let mut window = match Window::new("Test", 640, 400, Scale::X1).unwrap(); + /// + /// window.update(&buffer); + /// ``` + pub fn update(&mut self, buffer: &[u32]) { + self.0.update(buffer) + } + + /// + /// 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 + /// + /// ```ignore + /// while window.is_open() { + /// window.update(...) + /// } + /// ``` + #[inline] + pub fn is_open(&self) -> bool { + self.0.is_open() + } + + /// + /// Get the current keys that are down. + /// + /// # Examples + /// + /// ```ignore + /// window.get_keys().map(|keys| { + /// for t in keys { + /// match t { + /// Key::W => println!("holding w"), + /// Key::T => println!("holding t"), + /// _ => (), + /// } + /// } + /// }); + /// ``` + #[inline] + pub fn get_keys(&self) -> Option> { + self.0.get_keys() + } + + /// + /// Get the current pressed keys. Repeat can be used to control if keys should + /// be repeated if down or not. + /// + /// # Examples + /// + /// ```ignore + /// window.get_keys_pressed(KeyRepeat::No).map(|keys| { + /// for t in keys { + /// match t { + /// Key::W => println!("pressed w"), + /// Key::T => println!("pressed t"), + /// _ => (), + /// } + /// } + /// }); + /// ``` + #[inline] + pub fn get_keys_pressed(&self, repeat: KeyRepeat) -> Option> { + self.0.get_keys_pressed(repeat) + } + + /// + /// Check if a single key is down. + /// + /// # Examples + /// + /// ```ignore + /// if window.is_key_down(Key::A) { + /// println!("Key A is down"); + /// } + /// ``` + /// + #[inline] + pub fn is_key_down(&self, key: Key) -> bool { + self.0.is_key_down(key) + } + + /// + /// Check if a single key is pressed. KeyRepeat will control if the key should be repeated or + /// not while being pressed. + /// + /// # Examples + /// + /// ```ignore + /// if window.is_key_pressed(KeyRepeat::No) { + /// println!("Key A is down"); + /// } + /// ``` + /// + #[inline] + pub fn is_key_pressed(&self, key: Key, repeat: KeyRepeat) -> bool { + self.0.is_key_pressed(key, repeat) + } + + /// + /// Sets the delay for when a key is being held before it starts being repeated the default + /// value is 0.25 sec + /// + /// # Examples + /// + /// ```ignore + /// window.set_key_repeat_delay(0.5) // 0.5 sec before repeat starts + /// ``` + /// + #[inline] + pub fn set_key_repeat_delay(&mut self, delay: f32) { + self.0.set_key_repeat_delay(delay) + } + + /// + /// Sets the rate in between when the keys has passed the intital repeat_delay. The default + /// value is 0.05 sec + /// + /// # Examples + /// + /// ```ignore + /// window.set_key_repeat_rate(0.01) // 0.01 sec between keys + /// ``` + /// + #[inline] + pub fn set_key_repeat_rate(&mut self, rate: f32) { + self.0.set_key_repeat_rate(rate) + } +} diff --git a/src/native/macosx/MacMiniFB.m b/src/native/macosx/MacMiniFB.m index 4117534..c761c42 100644 --- a/src/native/macosx/MacMiniFB.m +++ b/src/native/macosx/MacMiniFB.m @@ -26,12 +26,6 @@ void* mfb_open(const char* name, int width, int height, int scale) if (!window) return 0; - NSRect e = [[NSScreen mainScreen] frame]; - int H = (int)e.size.height; - int W = (int)e.size.width; - - printf("H %d W %d\n", W, H); - window->draw_buffer = malloc(width * height * 4); if (!window->draw_buffer) diff --git a/src/native/macosx/OSXWindow.m b/src/native/macosx/OSXWindow.m index c406f55..fb5f196 100644 --- a/src/native/macosx/OSXWindow.m +++ b/src/native/macosx/OSXWindow.m @@ -149,8 +149,6 @@ NSRect bounds = [self frame]; bounds.origin = NSZeroPoint; - printf("view size\n"); - OSXWindowFrameView* frameView = [super contentView]; if (!frameView) { @@ -219,8 +217,6 @@ frameView->draw_buffer = draw_buffer; frameView->scale = scale; } - - printf("UpdateSize %d %d - %d\n", width, height, scale); } @end diff --git a/src/macos.rs b/src/os/macos/mod.rs similarity index 100% rename from src/macos.rs rename to src/os/macos/mod.rs diff --git a/src/os/mod.rs b/src/os/mod.rs new file mode 100644 index 0000000..0669925 --- /dev/null +++ b/src/os/mod.rs @@ -0,0 +1,4 @@ +#[cfg(target_os = "macos")] +pub mod macos; +#[cfg(target_os = "windows")] +pub mod windows; diff --git a/src/windows.rs b/src/os/windows/mod.rs similarity index 100% rename from src/windows.rs rename to src/os/windows/mod.rs