Skip to content

Commit

Permalink
Merge pull request #14 from jice-nospam/master
Browse files Browse the repository at this point in the history
add method to get screen resolution
  • Loading branch information
jice-nospam authored Dec 4, 2019
2 parents 95b9364 + 4c311dc commit 1b17d2c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "uni-app"
version = "0.1.2"
version = "0.1.3"
authors = ["Edwin Cheng <[email protected]>", "jice <[email protected]>"]
description = "native/wasm compatibility layer for window creation, input and filesystem"
license = "MIT"
Expand Down
33 changes: 23 additions & 10 deletions src/native_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,30 @@ enum WindowContext {
impl WindowContext {
fn hidpi_factor(&self) -> f32 {
match self {
&WindowContext::Normal(ref w) => w.get_hidpi_factor() as f32,
WindowContext::Normal(ref w) => w.get_hidpi_factor() as f32,
_ => 1.0,
}
}

fn window(&self) -> &glutin::GlWindow {
match self {
&WindowContext::Normal(ref w) => w,
WindowContext::Normal(ref w) => w,
_ => unimplemented!(),
}
}

fn context(&self) -> &dyn glutin::GlContext {
match self {
&WindowContext::Normal(ref w) => w,
&WindowContext::Headless(ref w) => w,
WindowContext::Normal(ref w) => w,
WindowContext::Headless(ref w) => w,
}
}

fn swap_buffers(&self) -> Result<(), glutin::ContextError> {
use glutin::GlContext;
match self {
&WindowContext::Normal(ref w) => w.swap_buffers(),
&WindowContext::Headless(_) => Ok(()),
WindowContext::Normal(ref w) => w.swap_buffers(),
WindowContext::Headless(_) => Ok(()),
}
}
}
Expand Down Expand Up @@ -180,14 +180,27 @@ impl App {
}

App {
window: window,
window,
events_loop,
exiting: false,
intercept_close_request: config.intercept_close_request,
events: Rc::new(RefCell::new(Vec::new())),
}
}

/// return the screen resolution in physical pixels
pub fn get_screen_resolution(&self) -> (u32, u32) {
if let WindowContext::Normal(ref glwindow) = self.window {
glwindow
.window()
.get_current_monitor()
.get_dimensions()
.into()
} else {
(0, 0)
}
}

/// return the command line / URL parameters
pub fn get_params() -> Vec<String> {
let mut params: Vec<String> = env::args().collect();
Expand Down Expand Up @@ -218,7 +231,7 @@ impl App {

/// returns the HiDPI factor for current screen
pub fn hidpi_factor(&self) -> f32 {
return self.window.hidpi_factor();
self.window.hidpi_factor()
}

fn get_proc_address(&self, name: &str) -> *const c_void {
Expand Down Expand Up @@ -285,7 +298,7 @@ impl App {
self.events.borrow_mut().clear();
self.window.swap_buffers().unwrap();

return !self.exiting;
!self.exiting
}

/// start the game loop, calling provided callback every frame
Expand Down Expand Up @@ -317,5 +330,5 @@ impl App {
pub fn now() -> f64 {
// precise_time_s() is in second
// https://doc.rust-lang.org/time/time/fn.precise_time_s.html
return time::precise_time_s();
time::precise_time_s()
}
7 changes: 7 additions & 0 deletions src/web_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,13 @@ impl App {

pub fn exit() {}

pub fn get_screen_resolution(&self) -> (u32, u32) {
(
window().inner_width() as u32,
window().inner_height() as u32,
)
}

pub fn get_params() -> Vec<String> {
let params = js! { return window.location.search.substring(1).split("&"); };
params.try_into().unwrap()
Expand Down

0 comments on commit 1b17d2c

Please sign in to comment.