mirror of
https://github.com/italicsjenga/rust_minifb.git
synced 2025-01-26 18:46:34 +11:00
Add support for scaling
This commit is contained in:
parent
946aa5ec14
commit
8e9f8d33a1
4 changed files with 23 additions and 12 deletions
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
28
src/macos.rs
28
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<Window, &str> {
|
||||
pub fn new(name: &str, width: usize, height: usize, scale: Scale) -> Result<Window, &str> {
|
||||
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 {
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue