add core Window resource

This commit is contained in:
Carter Anderson 2020-03-25 21:27:32 -07:00
parent c2545fd161
commit 0316efc909
9 changed files with 48 additions and 28 deletions

View file

@ -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::<Window>().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 {

View file

@ -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;

4
src/core/window.rs Normal file
View file

@ -0,0 +1,4 @@
pub struct Window {
pub width: u32,
pub height: u32,
}

View file

@ -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,

View file

@ -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::<winit::window::Window>().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::<Window>().unwrap();
self.descriptor.size.width = window.width;
self.descriptor.size.height = window.height;
if let Some(old_resource) = renderer
.get_render_resources()

View file

@ -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;

View file

@ -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::<Window>().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::<winit::window::Window>().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,
);
}

View file

@ -70,11 +70,11 @@ impl ShaderDefSuffixProvider for Option<Handle<Texture>> {
}
impl GetBytes for Option<Handle<Texture>> {
fn get_bytes(&self) -> Vec<std::primitive::u8> {
fn get_bytes(&self) -> Vec<u8> {
Vec::new()
}
fn get_bytes_ref(&self) -> Option<&[std::primitive::u8]> {
fn get_bytes_ref(&self) -> Option<&[u8]> {
None
}
}

View file

@ -1,5 +1,4 @@
use crate::prelude::*;
use winit::window::Window;
pub fn build_ui_update_system() -> Box<dyn Schedulable> {
SystemBuilder::new("ui_update_system")
@ -8,8 +7,7 @@ pub fn build_ui_update_system() -> Box<dyn Schedulable> {
.write_component::<Node>()
.read_component::<Children>()
.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(