From 6f47463a2e27c4332d4acec3b0dd59c2569d1129 Mon Sep 17 00:00:00 2001 From: Daniel Collin Date: Fri, 11 Dec 2015 10:33:20 +0100 Subject: [PATCH] Implemented scaling BestFit isn't implemented yet though --- src/lib.rs | 1 + src/windows.rs | 31 ++++++++++++++++++++++++++----- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index cab0b79..a6548e1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,6 +12,7 @@ extern crate core_foundation; /// Scale will scale the frame buffer and the window that is being sent in when calling the update /// function. This is useful if you for example want to display a 320 x 256 window on a screen with /// much higher resolution which would result in that the window is very small. +#[derive(Clone, Copy)] pub enum Scale { /// This mode checks your current screen resolution and will caluclate the largest window size /// that can be used within that limit and resize it. Useful if you have a small buffer to diff --git a/src/windows.rs b/src/windows.rs index 3b16d72..7ffe293 100644 --- a/src/windows.rs +++ b/src/windows.rs @@ -162,8 +162,8 @@ unsafe extern "system" fn wnd_proc(window: winapi::HWND, gdi32::StretchDIBits(wnd.dc.unwrap(), 0, 0, - width, - height, + width * wnd.scale_factor, + height * wnd.scale_factor, 0, 0, width, @@ -198,10 +198,11 @@ pub struct Window { keys: [bool; 512], buffer: Vec, is_open : bool, + scale_factor: i32, } impl Window { - fn open_window(name: &str, width: usize, height: usize, _: Scale, _: Vsync) -> Option { + fn open_window(name: &str, width: usize, height: usize, scale: Scale, _: Vsync) -> Option { unsafe { let class_name = to_wstring("minifb_window"); let class = WNDCLASSW { @@ -222,11 +223,15 @@ impl Window { return None; } + let scale_factor = Self::get_scale_factor(scale); + let new_width = width * scale_factor as usize; + let new_height = height * scale_factor as usize; + let mut rect = winapi::RECT { left: 0, - right: width as winapi::LONG, + right: new_width as winapi::LONG, top: 0, - bottom: height as winapi::LONG, + bottom: new_height as winapi::LONG, }; user32::AdjustWindowRect(&mut rect, @@ -282,6 +287,7 @@ impl Window { keys: [false; 512], buffer: Vec::new(), is_open: true, + scale_factor: Self::get_scale_factor(scale), }; Ok(window) @@ -316,6 +322,21 @@ impl Window { } } } + + fn get_scale_factor(scale: Scale) -> i32 { + // TODO: Implement best fit + let factor: i32 = match scale { + Scale::X1 => 1, + Scale::X2 => 2, + Scale::X4 => 4, + Scale::X8 => 8, + Scale::X16 => 16, + Scale::X32 => 32, + _ => 1, + }; + + return factor; + } } impl Drop for Window {