Skip to content

Commit

Permalink
fix issues highlighted in review
Browse files Browse the repository at this point in the history
* fix window size on creation
* rename `logical_width` & `logical_height` back to `width` & `height`
* update `Camera` trait to take width/height as `f32`
* update `WindowResized` to report 'f32' sizes instead if `u32`
* update `WindowCommand::SetResolution` to pass physical resolution instead of logical
* put `set_resolution` back, now taking `f32` instead of `u32`
* remove several unneeded `as f32`s now that `width` & `height` returns `f32`
  • Loading branch information
Nathan Jeffords committed Dec 4, 2020
1 parent 2a26163 commit 897387a
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 39 deletions.
5 changes: 1 addition & 4 deletions crates/bevy_render/src/camera/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,7 @@ pub fn camera_system<T: CameraProjection + Component>(
for (entity, mut camera, mut camera_projection) in queries.q0_mut().iter_mut() {
if let Some(window) = windows.get(camera.window) {
if changed_window_ids.contains(&window.id()) || added_cameras.contains(&entity) {
camera_projection.update(
window.logical_width() as usize,
window.logical_height() as usize,
);
camera_projection.update(window.width(), window.height());
camera.projection_matrix = camera_projection.get_projection_matrix();
camera.depth_calculation = camera_projection.depth_calculation();
}
Expand Down
16 changes: 8 additions & 8 deletions crates/bevy_render/src/camera/projection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use serde::{Deserialize, Serialize};

pub trait CameraProjection {
fn get_projection_matrix(&self) -> Mat4;
fn update(&mut self, width: usize, height: usize);
fn update(&mut self, width: f32, height: f32);
fn depth_calculation(&self) -> DepthCalculation;
}

Expand All @@ -23,8 +23,8 @@ impl CameraProjection for PerspectiveProjection {
Mat4::perspective_rh(self.fov, self.aspect_ratio, self.near, self.far)
}

fn update(&mut self, width: usize, height: usize) {
self.aspect_ratio = width as f32 / height as f32;
fn update(&mut self, width: f32, height: f32) {
self.aspect_ratio = width / height;
}

fn depth_calculation(&self) -> DepthCalculation {
Expand Down Expand Up @@ -75,20 +75,20 @@ impl CameraProjection for OrthographicProjection {
)
}

fn update(&mut self, width: usize, height: usize) {
fn update(&mut self, width: f32, height: f32) {
match self.window_origin {
WindowOrigin::Center => {
let half_width = width as f32 / 2.0;
let half_height = height as f32 / 2.0;
let half_width = width / 2.0;
let half_height = height / 2.0;
self.left = -half_width;
self.right = half_width;
self.top = half_height;
self.bottom = -half_height;
}
WindowOrigin::BottomLeft => {
self.left = 0.0;
self.right = width as f32;
self.top = height as f32;
self.right = width;
self.top = height;
self.bottom = 0.0;
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_ui/src/flex/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ impl FlexSurface {
*node,
stretch::style::Style {
size: stretch::geometry::Size {
width: stretch::style::Dimension::Points(window.logical_width() as f32),
height: stretch::style::Dimension::Points(window.logical_height() as f32),
width: stretch::style::Dimension::Points(window.width()),
height: stretch::style::Dimension::Points(window.height()),
},
..Default::default()
},
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_window/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use bevy_math::Vec2;
#[derive(Debug, Clone)]
pub struct WindowResized {
pub id: WindowId,
pub width: usize,
pub height: usize,
pub width: f32,
pub height: f32,
}

/// An event that indicates that a new window should be created.
Expand Down
17 changes: 13 additions & 4 deletions crates/bevy_window/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ pub enum WindowCommand {
title: String,
},
SetResolution {
width: u32,
height: u32,
physical_width: u32,
physical_height: u32,
},
SetVsync {
vsync: bool,
Expand Down Expand Up @@ -123,12 +123,12 @@ impl Window {
}

#[inline]
pub fn logical_width(&self) -> f32 {
pub fn width(&self) -> f32 {
(self.physical_width as f64 / self.scale_factor) as f32
}

#[inline]
pub fn logical_height(&self) -> f32 {
pub fn height(&self) -> f32 {
(self.physical_height as f64 / self.scale_factor) as f32
}

Expand All @@ -142,6 +142,15 @@ impl Window {
self.physical_height
}

pub fn set_resolution(&mut self, width: f32, height: f32) {
self.physical_width = (width as f64 * self.scale_factor) as u32;
self.physical_height = (height as f64 * self.scale_factor) as u32;
self.command_queue.push(WindowCommand::SetResolution {
physical_width: self.physical_width,
physical_height: self.physical_height,
});
}

#[allow(missing_docs)]
#[inline]
pub fn update_physical_size_from_backend(&mut self, width: u32, height: u32) {
Expand Down
18 changes: 12 additions & 6 deletions crates/bevy_winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,15 @@ fn change_window(_: &mut World, resources: &mut Resources) {
let window = winit_windows.get_window(id).unwrap();
window.set_title(&title);
}
bevy_window::WindowCommand::SetResolution { width, height } => {
bevy_window::WindowCommand::SetResolution {
physical_width,
physical_height,
} => {
let window = winit_windows.get_window(id).unwrap();
window.set_inner_size(winit::dpi::PhysicalSize::new(width, height));
window.set_inner_size(winit::dpi::PhysicalSize::new(
physical_width,
physical_height,
));
}
bevy_window::WindowCommand::SetVsync { .. } => (),
bevy_window::WindowCommand::SetResizable { resizable } => {
Expand Down Expand Up @@ -204,8 +210,8 @@ pub fn winit_runner(mut app: App) {
app.resources.get_mut::<Events<WindowResized>>().unwrap();
resize_events.send(WindowResized {
id: window_id,
height: window.logical_height() as usize,
width: window.logical_width() as usize,
height: window.height(),
width: window.width(),
});
}
WindowEvent::CloseRequested => {
Expand Down Expand Up @@ -303,8 +309,8 @@ pub fn winit_runner(mut app: App) {

// FIXME?: On Android window start is top while on PC/Linux/OSX on bottom
if cfg!(target_os = "android") {
let window_height = windows.get_primary().unwrap().logical_height();
location.y = window_height as f32 - location.y;
let window_height = windows.get_primary().unwrap().height();
location.y = window_height - location.y;
}
touch_input_events.send(converters::convert_touch_input(touch, location));
}
Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_winit/src/winit_windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ impl WinitWindows {
}),
)),
_ => winit_window_builder
.with_inner_size(winit::dpi::PhysicalSize::new(
window.physical_width(),
window.physical_height(),
.with_inner_size(winit::dpi::LogicalSize::new(
window.width(),
window.height(),
))
.with_resizable(window.resizable())
.with_decorations(window.decorations()),
Expand Down
8 changes: 4 additions & 4 deletions examples/2d/contributors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,11 +237,11 @@ fn collision_system(

let win = wins.get_primary().unwrap();

let ceiling = (win.logical_height() / 2.) as f32;
let ground = -((win.logical_height() / 2.) as f32);
let ceiling = win.height() / 2.;
let ground = -(win.height() / 2.);

let wall_left = -((win.logical_width() / 2.) as f32);
let wall_right = (win.logical_width() / 2.) as f32;
let wall_left = -(win.width() / 2.);
let wall_right = win.width() / 2.;

for (mut v, mut t) in q.iter_mut() {
let left = t.translation.x - SPRITE_SIZE / 2.0;
Expand Down
12 changes: 6 additions & 6 deletions examples/ecs/parallel_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ fn bounce_system(
mut sprites: Query<(&Transform, &mut Velocity)>,
) {
let window = windows.get_primary().expect("No primary window.");
let width = window.logical_width();
let height = window.logical_height();
let left = width as f32 / -2.0;
let right = width as f32 / 2.0;
let bottom = height as f32 / -2.0;
let top = height as f32 / 2.0;
let width = window.width();
let height = window.height();
let left = width / -2.0;
let right = width / 2.0;
let bottom = height / -2.0;
let top = height / 2.0;
sprites
// Batch size of 32 is chosen to limit the overhead of
// ParallelIterator, since negating a vector is very inexpensive.
Expand Down

0 comments on commit 897387a

Please sign in to comment.