From 8e9f8d33a1bde3cf0fbbbdcd58b55a04cf333508 Mon Sep 17 00:00:00 2001 From: Daniel Collin Date: Sat, 2 Jan 2016 15:49:53 +0100 Subject: [PATCH] Add support for scaling --- examples/noise.rs | 3 +-- src/macos.rs | 28 ++++++++++++++++++-------- src/native/macosx/MacMiniFB.m | 2 +- src/native/macosx/OSXWindowFrameView.m | 2 +- 4 files changed, 23 insertions(+), 12 deletions(-) diff --git a/examples/noise.rs b/examples/noise.rs index cecd3c3..c536b94 100644 --- a/examples/noise.rs +++ b/examples/noise.rs @@ -15,7 +15,7 @@ fn main() { let mut window = Window::new("Noise Test - Press ESC to exit", WIDTH, HEIGHT, - Scale::X1) + Scale::X2) .unwrap(); while window.is_open() && !window.is_key_down(Key::Escape) { @@ -41,7 +41,6 @@ fn main() { } }); - window.update(&buffer); } } diff --git a/src/macos.rs b/src/macos.rs index eb34d18..e2ecf3a 100644 --- a/src/macos.rs +++ b/src/macos.rs @@ -8,7 +8,7 @@ use std::ptr; #[link(name = "Cocoa", kind = "framework")] extern { - fn mfb_open(name: *const c_char, width: u32, height: u32, scale: u32) -> *mut c_void; + fn mfb_open(name: *const c_char, width: u32, height: u32, scale: i32) -> *mut c_void; fn mfb_close(window: *mut c_void); fn mfb_update(window: *mut c_void, buffer: *const c_uchar); } @@ -18,11 +18,7 @@ pub struct Window { } impl Window { - pub fn new(name: &str, - width: usize, - height: usize, - scale: Scale) - -> Result { + pub fn new(name: &str, width: usize, height: usize, scale: Scale) -> Result { let n = match CString::new(name) { Err(_) => { println!("Unable to convert {} to c_string", name); @@ -32,12 +28,12 @@ impl Window { }; unsafe { - let handle = mfb_open(n.as_ptr(), width as u32, height as u32, scale as u32); + let handle = mfb_open(n.as_ptr(), width as u32, height as u32, Self::get_scale_factor(width, height, scale)); if handle == ptr::null_mut() { return Err("Unable to open Window"); } - + Ok(Window { window_handle: handle }) } } @@ -81,6 +77,22 @@ impl Window { pub fn is_open(&self) -> bool { true } + + unsafe fn get_scale_factor(_: usize, _: usize, scale: Scale) -> i32 { + let factor: i32 = match scale { + Scale::X1 => 1, + Scale::X2 => 2, + Scale::X4 => 4, + Scale::X8 => 8, + Scale::X16 => 16, + Scale::X32 => 32, + Scale::FitScreen => { + 1 + } + }; + + return factor; + } } impl Drop for Window { diff --git a/src/native/macosx/MacMiniFB.m b/src/native/macosx/MacMiniFB.m index ae2a60c..8b0ab3f 100644 --- a/src/native/macosx/MacMiniFB.m +++ b/src/native/macosx/MacMiniFB.m @@ -19,7 +19,7 @@ void* mfb_open(const char* name, int width, int height, int scale) unsigned int styles = NSResizableWindowMask | NSClosableWindowMask | NSTitledWindowMask; - NSRect rectangle = NSMakeRect(0, 0, width, height); + NSRect rectangle = NSMakeRect(0, 0, width * scale, height * scale); OSXWindow* window = [[OSXWindow alloc] initWithContentRect:rectangle styleMask:styles backing:NSBackingStoreBuffered defer:NO]; if (!window) diff --git a/src/native/macosx/OSXWindowFrameView.m b/src/native/macosx/OSXWindowFrameView.m index 0f0d67b..09b0b65 100644 --- a/src/native/macosx/OSXWindowFrameView.m +++ b/src/native/macosx/OSXWindowFrameView.m @@ -34,7 +34,7 @@ CGColorSpaceRelease(space); CGDataProviderRelease(provider); - CGContextDrawImage(context, CGRectMake(0, 0, width, height), img); + CGContextDrawImage(context, CGRectMake(0, 0, width * scale, height * scale), img); CGImageRelease(img); }