Skip to content

Commit

Permalink
can change window settings at runtime (bevyengine#644)
Browse files Browse the repository at this point in the history
can change window settings at runtime
  • Loading branch information
mockersf authored and joshuajbouw committed Oct 24, 2020
1 parent e28e7ef commit b0e5f9b
Show file tree
Hide file tree
Showing 13 changed files with 251 additions and 60 deletions.
4 changes: 2 additions & 2 deletions crates/bevy_render/src/camera/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ pub fn camera_system<T: CameraProjection + Component>(
}
for (entity, mut camera, mut camera_projection) in &mut query.iter() {
if let Some(window) = windows.get(camera.window) {
if changed_window_ids.contains(&window.id) || added_cameras.contains(&entity) {
camera_projection.update(window.width as usize, window.height as usize);
if changed_window_ids.contains(&window.id()) || added_cameras.contains(&entity) {
camera_projection.update(window.width() as usize, window.height() as usize);
camera.projection_matrix = camera_projection.get_projection_matrix();
camera.depth_calculation = camera_projection.depth_calculation();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ impl Node for WindowSwapChainNode {
// create window swapchain when window is resized or created
if self
.window_created_event_reader
.find_latest(&window_created_events, |e| e.id == window.id)
.find_latest(&window_created_events, |e| e.id == window.id())
.is_some()
|| self
.window_resized_event_reader
.find_latest(&window_resized_events, |e| e.id == window.id)
.find_latest(&window_resized_events, |e| e.id == window.id())
.is_some()
{
render_resource_context.create_swap_chain(window);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,20 @@ impl Node for WindowTextureNode {

if self
.window_created_event_reader
.find_latest(&window_created_events, |e| e.id == window.id)
.find_latest(&window_created_events, |e| e.id == window.id())
.is_some()
|| self
.window_resized_event_reader
.find_latest(&window_resized_events, |e| e.id == window.id)
.find_latest(&window_resized_events, |e| e.id == window.id())
.is_some()
{
let render_resource_context = render_context.resources_mut();
if let Some(RenderResourceId::Texture(old_texture)) = output.get(WINDOW_TEXTURE) {
render_resource_context.remove_texture(old_texture);
}

self.descriptor.size.width = window.width;
self.descriptor.size.height = window.height;
self.descriptor.size.width = window.width();
self.descriptor.size.height = window.height();
let texture_resource = render_resource_context.create_texture(self.descriptor);
output.set(WINDOW_TEXTURE, RenderResourceId::Texture(texture_resource));
}
Expand Down
8 changes: 4 additions & 4 deletions crates/bevy_ui/src/flex/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl FlexSurface {

pub fn update_window(&mut self, window: &Window) {
let stretch = &mut self.stretch;
let node = self.window_nodes.entry(window.id).or_insert_with(|| {
let node = self.window_nodes.entry(window.id()).or_insert_with(|| {
stretch
.new_node(stretch::style::Style::default(), Vec::new())
.unwrap()
Expand All @@ -116,8 +116,8 @@ impl FlexSurface {
*node,
stretch::style::Style {
size: stretch::geometry::Size {
width: stretch::style::Dimension::Points(window.width as f32),
height: stretch::style::Dimension::Points(window.height as f32),
width: stretch::style::Dimension::Points(window.width() as f32),
height: stretch::style::Dimension::Points(window.height() as f32),
},
..Default::default()
},
Expand Down Expand Up @@ -189,7 +189,7 @@ pub fn flex_node_system(

// update window children (for now assuming all Nodes live in the primary window)
if let Some(primary_window) = windows.get_primary() {
flex_surface.set_window_children(primary_window.id, root_node_query.iter().iter());
flex_surface.set_window_children(primary_window.id(), root_node_query.iter().iter());
}

// update children
Expand Down
13 changes: 8 additions & 5 deletions crates/bevy_wgpu/src/renderer/wgpu_render_resource_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,22 +274,25 @@ impl RenderResourceContext for WgpuRenderResourceContext {

let swap_chain_descriptor: wgpu::SwapChainDescriptor = window.wgpu_into();
let surface = surfaces
.get(&window.id)
.get(&window.id())
.expect("No surface found for window");
let swap_chain = self
.device
.create_swap_chain(surface, &swap_chain_descriptor);

window_swap_chains.insert(window.id, swap_chain);
window_swap_chains.insert(window.id(), swap_chain);
}

fn next_swap_chain_texture(&self, window: &bevy_window::Window) -> TextureId {
if let Some(texture_id) = self.try_next_swap_chain_texture(window.id) {
if let Some(texture_id) = self.try_next_swap_chain_texture(window.id()) {
texture_id
} else {
self.resources.window_swap_chains.write().remove(&window.id);
self.resources
.window_swap_chains
.write()
.remove(&window.id());
self.create_swap_chain(window);
self.try_next_swap_chain_texture(window.id)
self.try_next_swap_chain_texture(window.id())
.expect("Failed to acquire next swap chain texture!")
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_wgpu/src/wgpu_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ impl WgpuRenderer {
#[cfg(feature = "bevy_winit")]
{
let winit_windows = resources.get::<bevy_winit::WinitWindows>().unwrap();
let winit_window = winit_windows.get_window(window.id).unwrap();
let winit_window = winit_windows.get_window(window.id()).unwrap();
let surface = unsafe { self.instance.create_surface(winit_window.deref()) };
render_resource_context.set_window_surface(window.id, surface);
render_resource_context.set_window_surface(window.id(), surface);
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_wgpu/src/wgpu_type_converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -563,9 +563,9 @@ impl WgpuFrom<&Window> for wgpu::SwapChainDescriptor {
wgpu::SwapChainDescriptor {
usage: wgpu::TextureUsage::OUTPUT_ATTACHMENT,
format: wgpu::TextureFormat::Bgra8UnormSrgb,
width: window.width,
height: window.height,
present_mode: if window.vsync {
width: window.width(),
height: window.height(),
present_mode: if window.vsync() {
wgpu::PresentMode::Fifo
} else {
wgpu::PresentMode::Immediate
Expand Down
124 changes: 116 additions & 8 deletions crates/bevy_window/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,41 @@ impl Default for WindowId {

#[derive(Debug)]
pub struct Window {
pub id: WindowId,
pub width: u32,
pub height: u32,
pub title: String,
pub vsync: bool,
pub resizable: bool,
pub decorations: bool,
pub mode: WindowMode,
id: WindowId,
width: u32,
height: u32,
title: String,
vsync: bool,
resizable: bool,
decorations: bool,
mode: WindowMode,
#[cfg(target_arch = "wasm32")]
pub canvas: Option<String>,
command_queue: Vec<WindowCommand>,
}

#[derive(Debug)]
pub enum WindowCommand {
SetWindowMode {
mode: WindowMode,
resolution: (u32, u32),
},
SetTitle {
title: String,
},
SetResolution {
width: u32,
height: u32,
},
SetVsync {
vsync: bool,
},
SetResizable {
resizable: bool,
},
SetDecorations {
decorations: bool,
},
}

/// Defines the way a window is displayed
Expand Down Expand Up @@ -70,8 +95,91 @@ impl Window {
mode: window_descriptor.mode,
#[cfg(target_arch = "wasm32")]
canvas: window_descriptor.canvas.clone(),
command_queue: Vec::new(),
}
}

#[inline]
pub fn id(&self) -> WindowId {
self.id
}

#[inline]
pub fn width(&self) -> u32 {
self.width
}

#[inline]
pub fn height(&self) -> u32 {
self.height
}

pub fn set_resolution(&mut self, width: u32, height: u32) {
self.width = width;
self.height = height;
self.command_queue
.push(WindowCommand::SetResolution { width, height });
}

#[doc(hidden)]
pub fn update_resolution_from_backend(&mut self, width: u32, height: u32) {
self.width = width;
self.height = height;
}

pub fn title(&self) -> &str {
&self.title
}

pub fn set_title(&mut self, title: String) {
self.title = title.to_string();
self.command_queue.push(WindowCommand::SetTitle { title });
}

pub fn vsync(&self) -> bool {
self.vsync
}

pub fn set_vsync(&mut self, vsync: bool) {
self.vsync = vsync;
self.command_queue.push(WindowCommand::SetVsync { vsync });
}

pub fn resizable(&self) -> bool {
self.resizable
}

pub fn set_resizable(&mut self, resizable: bool) {
self.resizable = resizable;
self.command_queue
.push(WindowCommand::SetResizable { resizable });
}

pub fn decorations(&self) -> bool {
self.decorations
}

pub fn set_decorations(&mut self, decorations: bool) {
self.decorations = decorations;
self.command_queue
.push(WindowCommand::SetDecorations { decorations });
}

pub fn mode(&self) -> WindowMode {
self.mode
}

pub fn set_mode(&mut self, mode: WindowMode) {
self.mode = mode;
self.command_queue.push(WindowCommand::SetWindowMode {
mode,
resolution: (self.width, self.height),
});
}

pub fn drain_commands<'a>(&'a mut self) -> impl Iterator<Item = WindowCommand> + 'a {
self.command_queue.drain(..)
}
}

#[derive(Debug, Clone)]
Expand Down
10 changes: 9 additions & 1 deletion crates/bevy_window/src/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub struct Windows {

impl Windows {
pub fn add(&mut self, window: Window) {
self.windows.insert(window.id, window);
self.windows.insert(window.id(), window);
}

pub fn get(&self, id: WindowId) -> Option<&Window> {
Expand All @@ -23,7 +23,15 @@ impl Windows {
self.get(WindowId::primary())
}

pub fn get_primary_mut(&mut self) -> Option<&mut Window> {
self.get_mut(WindowId::primary())
}

pub fn iter(&self) -> impl Iterator<Item = &Window> {
self.windows.values()
}

pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut Window> {
self.windows.values_mut()
}
}
Loading

0 comments on commit b0e5f9b

Please sign in to comment.