From 0316efc909ccc72b4c31242d23fbeec49cc63706 Mon Sep 17 00:00:00 2001 From: Carter Anderson Date: Wed, 25 Mar 2020 21:27:32 -0700 Subject: [PATCH] add core Window resource --- src/app/app.rs | 26 ++++++++++++++----- src/core/mod.rs | 2 ++ src/core/window.rs | 4 +++ src/prelude.rs | 2 +- .../frame_texture_resource_provider.rs | 8 +++--- src/render/renderer/renderer.rs | 2 +- .../renderers/wgpu_renderer/wgpu_renderer.rs | 24 +++++++++-------- src/render/texture/texture.rs | 4 +-- src/ui/ui_update_system.rs | 4 +-- 9 files changed, 48 insertions(+), 28 deletions(-) create mode 100644 src/core/window.rs diff --git a/src/app/app.rs b/src/app/app.rs index 2ddb9117d5..6203ed0f29 100644 --- a/src/app/app.rs +++ b/src/app/app.rs @@ -6,7 +6,7 @@ use winit::{ use legion::prelude::*; -use crate::{app::AppBuilder, core::Time, render::renderer::Renderer}; +use crate::{app::AppBuilder, core::{Window, Time}, render::renderer::Renderer}; pub struct App { pub universe: Universe, @@ -59,11 +59,17 @@ impl App { let event_loop = EventLoop::new(); log::info!("Initializing the window..."); - let window = winit::window::Window::new(&event_loop).unwrap(); - window.set_title("bevy"); - window.set_inner_size(winit::dpi::LogicalSize::new(1280, 720)); + let window = Window { + width: 1280, + height: 720, + }; + + let winit_window = winit::window::Window::new(&event_loop).unwrap(); + winit_window.set_title("bevy"); + winit_window.set_inner_size(winit::dpi::PhysicalSize::new(window.width, window.height)); self.resources.insert(window); + self.resources.insert(winit_window); log::info!("Entering render loop..."); event_loop.run(move |event, _, control_flow| { @@ -74,11 +80,19 @@ impl App { }; match event { event::Event::WindowEvent { - event: WindowEvent::Resized(_size), + event: WindowEvent::Resized(size), .. } => { if let Some(ref mut renderer) = self.renderer { - renderer.resize(&mut self.world, &mut self.resources); + { + let mut window = self.resources.get_mut::().unwrap(); + window.width = size.width; + window.height = size.height; + } + + renderer.resize(&mut self.world, &mut self.resources, size.width, size.height); + } else { + println!("no renderer {} {}", size.width, size.height); } } event::Event::WindowEvent { event, .. } => match event { diff --git a/src/core/mod.rs b/src/core/mod.rs index 608d6378f0..433823a2bb 100644 --- a/src/core/mod.rs +++ b/src/core/mod.rs @@ -1,5 +1,7 @@ pub mod bytes; pub mod time; +pub mod window; pub use bytes::GetBytes; pub use time::Time; +pub use window::Window; diff --git a/src/core/window.rs b/src/core/window.rs new file mode 100644 index 0000000000..c11637282a --- /dev/null +++ b/src/core/window.rs @@ -0,0 +1,4 @@ +pub struct Window { + pub width: u32, + pub height: u32, +} \ No newline at end of file diff --git a/src/prelude.rs b/src/prelude.rs index c59ecb3a5a..08ac1e49a2 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -1,7 +1,7 @@ pub use crate::{ app::{App, AppBuilder}, asset::{Asset, AssetStorage, Handle}, - core::Time, + core::{Time, Window}, ecs, ecs::{ default_archetypes::*, CommandBufferBuilderSource, EntityArchetype, WorldBuilder, diff --git a/src/render/render_resource/resource_providers/frame_texture_resource_provider.rs b/src/render/render_resource/resource_providers/frame_texture_resource_provider.rs index ded63c190b..06d699e413 100644 --- a/src/render/render_resource/resource_providers/frame_texture_resource_provider.rs +++ b/src/render/render_resource/resource_providers/frame_texture_resource_provider.rs @@ -1,4 +1,5 @@ use crate::{ + core::Window, prelude::World, render::{render_resource::ResourceProvider, renderer::Renderer, texture::TextureDescriptor}, }; @@ -18,10 +19,9 @@ impl FrameTextureResourceProvider { } pub fn update(&mut self, renderer: &mut dyn Renderer, _world: &World, resources: &Resources) { - let window = resources.get::().unwrap(); - let window_size = window.inner_size(); - self.descriptor.size.width = window_size.width; - self.descriptor.size.height = window_size.height; + let window = resources.get::().unwrap(); + self.descriptor.size.width = window.width; + self.descriptor.size.height = window.height; if let Some(old_resource) = renderer .get_render_resources() diff --git a/src/render/renderer/renderer.rs b/src/render/renderer/renderer.rs index 2c03ffb2e2..b28642c79c 100644 --- a/src/render/renderer/renderer.rs +++ b/src/render/renderer/renderer.rs @@ -11,7 +11,7 @@ use crate::{ use std::ops::Range; pub trait Renderer { - fn resize(&mut self, world: &mut World, resources: &mut Resources); + fn resize(&mut self, world: &mut World, resources: &mut Resources, width: u32, height: u32); fn update(&mut self, world: &mut World, resources: &mut Resources); fn create_buffer_with_data(&mut self, buffer_info: BufferInfo, data: &[u8]) -> RenderResource; fn create_sampler(&mut self, sampler_descriptor: &SamplerDescriptor) -> RenderResource; diff --git a/src/render/renderer/renderers/wgpu_renderer/wgpu_renderer.rs b/src/render/renderer/renderers/wgpu_renderer/wgpu_renderer.rs index 83f50d2eaf..0b93b63897 100644 --- a/src/render/renderer/renderers/wgpu_renderer/wgpu_renderer.rs +++ b/src/render/renderer/renderers/wgpu_renderer/wgpu_renderer.rs @@ -17,7 +17,7 @@ use crate::{ shader::Shader, texture::{SamplerDescriptor, TextureDescriptor}, update_shader_assignments, - }, + }, core::Window, }; use std::{cell::RefCell, collections::HashMap, ops::Deref, rc::Rc}; @@ -76,7 +76,13 @@ impl WgpuRenderer { self.create_surface(resources); self.initialize_resource_providers(world, resources); - self.resize(world, resources); + + let (width, height) = { + let window = resources.get::().unwrap(); + (window.width, window.height) + }; + + self.resize(world, resources, width, height); self.intialized = true; } @@ -411,18 +417,14 @@ impl WgpuRenderer { } impl Renderer for WgpuRenderer { - fn resize(&mut self, world: &mut World, resources: &mut Resources) { - let window_size = { - let window = resources.get::().unwrap(); - window.inner_size() - }; + fn resize(&mut self, world: &mut World, resources: &mut Resources, width: u32, height: u32) { self.encoder = Some( self.device .borrow() .create_command_encoder(&wgpu::CommandEncoderDescriptor { todo: 0 }), ); - self.swap_chain_descriptor.width = window_size.width; - self.swap_chain_descriptor.height = window_size.height; + self.swap_chain_descriptor.width = width; + self.swap_chain_descriptor.height = height; let swap_chain = self .device .borrow() @@ -436,8 +438,8 @@ impl Renderer for WgpuRenderer { self, world, resources, - window_size.width, - window_size.height, + width, + height, ); } diff --git a/src/render/texture/texture.rs b/src/render/texture/texture.rs index bc876b5dd2..560272888b 100644 --- a/src/render/texture/texture.rs +++ b/src/render/texture/texture.rs @@ -70,11 +70,11 @@ impl ShaderDefSuffixProvider for Option> { } impl GetBytes for Option> { - fn get_bytes(&self) -> Vec { + fn get_bytes(&self) -> Vec { Vec::new() } - fn get_bytes_ref(&self) -> Option<&[std::primitive::u8]> { + fn get_bytes_ref(&self) -> Option<&[u8]> { None } } diff --git a/src/ui/ui_update_system.rs b/src/ui/ui_update_system.rs index d903783c76..747077c1a8 100644 --- a/src/ui/ui_update_system.rs +++ b/src/ui/ui_update_system.rs @@ -1,5 +1,4 @@ use crate::prelude::*; -use winit::window::Window; pub fn build_ui_update_system() -> Box { SystemBuilder::new("ui_update_system") @@ -8,8 +7,7 @@ pub fn build_ui_update_system() -> Box { .write_component::() .read_component::() .build(move |_, world, window, node_query| { - let window_size = window.inner_size(); - let parent_size = math::vec2(window_size.width as f32, window_size.height as f32); + let parent_size = math::vec2(window.width as f32, window.height as f32); let parent_position = math::vec2(0.0, 0.0); for (entity, _) in node_query.iter_entities_mut(world) { ecs::run_on_hierarchy_subworld_mut(