diff --git a/crates/comet_app/Cargo.toml b/crates/comet_app/Cargo.toml index db42dd4..1c920cc 100644 --- a/crates/comet_app/Cargo.toml +++ b/crates/comet_app/Cargo.toml @@ -10,6 +10,7 @@ comet_resources = { path = "../comet_resources" } comet_colors = { path = "../comet_colors" } comet_log = { path = "../comet_log" } comet_input = { path = "../comet_input" } +comet_structs = { path = "../comet_structs" } winit = { version = "0.29", features = ["rwh_05"] } env_logger = "0.10" diff --git a/crates/comet_app/src/app.rs b/crates/comet_app/src/app.rs index 932fa53..2a544b1 100644 --- a/crates/comet_app/src/app.rs +++ b/crates/comet_app/src/app.rs @@ -24,6 +24,7 @@ use winit_input_helper::WinitInputHelper; use comet_input::input_handler::InputHandler; use comet_input::keyboard::Key; use comet_renderer::renderer::Renderer; +use comet_structs::ComponentSet; use crate::GameState; pub enum ApplicationType { @@ -187,6 +188,10 @@ impl App { self.world.get_component_mut::(entity_id) } + pub fn get_entities_with(&self, components: ComponentSet) -> Vec { + self.world.get_entities_with(components) + } + pub fn quit(&mut self) { self.should_quit = true; } diff --git a/crates/comet_ecs/src/world.rs b/crates/comet_ecs/src/world.rs index 4e46218..10d147f 100644 --- a/crates/comet_ecs/src/world.rs +++ b/crates/comet_ecs/src/world.rs @@ -204,9 +204,9 @@ impl World { } /// Returns a list of entities that have the given components. - pub fn get_entities_with(&self, components: ComponentSet) -> Vec { + pub fn get_entities_with(&self, components: ComponentSet) -> Vec { if self.archetypes.contains_archetype(&components) { - return self.archetypes.get_archetype(&components).unwrap().clone(); + return self.archetypes.get_archetype(&components).unwrap().clone().iter().map(|x| *x as usize).collect(); } error!("The given components {:?} are not registered in the world!", components); Vec::new() diff --git a/crates/comet_renderer/src/renderer2d.rs b/crates/comet_renderer/src/renderer2d.rs index c4a2c75..f232049 100644 --- a/crates/comet_renderer/src/renderer2d.rs +++ b/crates/comet_renderer/src/renderer2d.rs @@ -739,12 +739,126 @@ impl<'a> Renderer2D<'a> { return; } - let entities = world.get_entities_with(ComponentSet::from_ids(vec![Transform2D::type_id(), Render2D::type_id()])); + let camera_component = world.get_component::(cameras[0]).unwrap(); + let camera_position = world.get_component::(cameras[0]).unwrap().position(); - for camera in cameras { - let camera_position = world.get_component::(camera as usize).unwrap().position(); - let camera_component = world.get_component::(camera as usize).unwrap(); - let camera = OldCam::new(camera_component.zoom(), camera_component.dimensions(), Vec3::new(camera_position.as_vec().x(), camera_position.as_vec().y(), 0.0)); + let camera = OldCam::new( + camera_component.zoom(), + camera_component.dimensions(), + Vec3::new(camera_position.as_vec().x(), + camera_position.as_vec().y(), + 0.0)); + let mut camera_uniform = CameraUniform::new(); + camera_uniform.update_view_proj(&camera); + + let camera_buffer = self.device.create_buffer_init(&wgpu::util::BufferInitDescriptor { + label: Some("Camera Buffer"), + contents: bytemuck::cast_slice(&[camera_uniform]), + usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST, + }); + + let camera_bind_group_layout = + self.device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { + entries: &[wgpu::BindGroupLayoutEntry { + binding: 0, + visibility: wgpu::ShaderStages::VERTEX, + ty: wgpu::BindingType::Buffer { + ty: wgpu::BufferBindingType::Uniform, + has_dynamic_offset: false, + min_binding_size: None, + }, + count: None, + }], + label: Some("camera_bind_group_layout"), + }); + + let camera_bind_group = self.device.create_bind_group(&wgpu::BindGroupDescriptor { + layout: &camera_bind_group_layout, + entries: &[wgpu::BindGroupEntry { + binding: 0, + resource: camera_buffer.as_entire_binding(), + }], + label: Some("camera_bind_group"), + }); + + self.camera = camera; + self.camera_buffer = camera_buffer; + self.camera_uniform = camera_uniform; + self.camera_bind_group = camera_bind_group; + + let mut visible_entities: Vec = vec![]; + + for entity in world.get_entities_with(ComponentSet::from_ids(vec![Transform2D::type_id(), Render2D::type_id()])) { + let entity_id = entity as usize; + + /*if !camera_component + .in_view_frustum(*camera_position, *world.get_component::(entity_id).unwrap().position()) + { + continue; + }*/ + + match world.get_component::(entity_id) { + Some(render) => { + if !render.is_visible() { + continue; + } + if let Some(cam) = world.get_component::(entity_id) { + continue; + } + visible_entities.push(entity_id); + } + None => { + continue; + } + } + } + + let mut vertex_buffer: Vec = Vec::new(); + let mut index_buffer: Vec = Vec::new(); + + for entity in visible_entities { + let renderer_component = world.get_component::(entity); + let transform_component = world.get_component::(entity); + + if renderer_component.unwrap().is_visible() { + //self.draw_texture_at(renderer_component.get_texture(), Point3::new(transform_component.position().x(), transform_component.position().y(), 0.0)); + let mut position = transform_component.unwrap().position().clone(); + position.set_x(position.x() / self.config().width as f32); + position.set_y(position.y() / self.config().height as f32); + let region = self.get_texture_region(renderer_component.unwrap().get_texture().to_string()); + let (dim_x, dim_y) = region.dimensions(); + + let (bound_x, bound_y) = + ((dim_x as f32 / self.config().width as f32) * 0.5, (dim_y as f32 / self.config().height as f32) * 0.5); + + let buffer_size = vertex_buffer.len() as u16; + + vertex_buffer.append(&mut vec![ + Vertex::new([-bound_x + position.x(), bound_y + position.y(), 0.0], [region.x0(), region.y0()], [0.0, 0.0, 0.0, 0.0]), + Vertex::new([-bound_x + position.x(), -bound_y + position.y(), 0.0], [region.x0(), region.y1()], [0.0, 0.0, 0.0, 0.0]), + Vertex::new([bound_x + position.x(), -bound_y + position.y(), 0.0], [region.x1(), region.y1()], [0.0, 0.0, 0.0, 0.0]), + Vertex::new([bound_x + position.x(), bound_y + position.y(), 0.0], [region.x1(), region.y0()], [0.0, 0.0, 0.0, 0.0]) + ]); + + index_buffer.append(&mut vec![ + 0 + buffer_size, 1 + buffer_size, 3 + buffer_size, + 1 + buffer_size, 2 + buffer_size, 3 + buffer_size + ]); + } + } + + //debug!("Visible entities: {:?}", vertex_buffer); + self.set_buffers(vertex_buffer, index_buffer); + + /*for camera in cameras { + let camera_position = world.get_component::(camera).unwrap().position(); + let camera_component = world.get_component::(camera).unwrap(); + let camera = OldCam::new( + camera_component.zoom(), + camera_component.dimensions(), + Vec3::new(camera_position.as_vec().x(), + camera_position.as_vec().y(), + 0.0)); let mut camera_uniform = CameraUniform::new(); camera_uniform.update_view_proj(&camera); @@ -783,57 +897,70 @@ impl<'a> Renderer2D<'a> { self.camera_uniform = camera_uniform; self.camera_bind_group = camera_bind_group; - let mut visible_entities: Vec = vec![]; - for entity in world.entities() { - if camera_component.in_view_frustum(*camera_position, *world.get_component::(*entity.clone().unwrap().id() as usize).unwrap().position()) { - if let Some(render) = world.get_component::(*entity.clone().unwrap().id() as usize) { - if render.is_visible() { - visible_entities.push(*entity.clone().unwrap().id()); + let mut visible_entities: Vec = vec![]; + + for entity in world.get_entities_with(ComponentSet::from_ids(vec![Transform2D::type_id(), Render2D::type_id()])) { + let entity_id = entity as usize; + + if !camera_component + .in_view_frustum(*camera_position, *world.get_component::(entity_id).unwrap().position()) + { + continue; + } + + match world.get_component::(entity_id) { + Some(render) => { + if !render.is_visible() { + continue; } + if let Some(cam) = world.get_component::(entity_id) { + continue; + } + visible_entities.push(entity_id); + } + None => { + continue; } } } - //println!("{:?}", visible_entities); - } + let mut vertex_buffer: Vec = Vec::new(); + let mut index_buffer: Vec = Vec::new(); + for entity in visible_entities { + let renderer_component = world.get_component::(entity); + let transform_component = world.get_component::(entity); - /*let entities = world.get_entities_with(ComponentSet::from_ids(vec![Transform2D::type_id(), Render2D::type_id()])); - let mut vertex_buffer: Vec = Vec::new(); - let mut index_buffer: Vec = Vec::new(); + if renderer_component.unwrap().is_visible() { + //self.draw_texture_at(renderer_component.get_texture(), Point3::new(transform_component.position().x(), transform_component.position().y(), 0.0)); + let mut position = transform_component.unwrap().position().clone(); + position.set_x(position.x() / self.config().width as f32); + position.set_y(position.y() / self.config().height as f32); + let region = self.get_texture_region(renderer_component.unwrap().get_texture().to_string()); + let (dim_x, dim_y) = region.dimensions(); - for entity in entities { - let renderer_component = world.get_component::(entity as usize); - let transform_component = world.get_component::(entity as usize); + let (bound_x, bound_y) = + ((dim_x as f32 / self.config().width as f32) * 0.5, (dim_y as f32 / self.config().height as f32) * 0.5); - if renderer_component.unwrap().is_visible() { - //renderer.draw_texture_at(renderer_component.get_texture(), Point3::new(transform_component.position().x(), transform_component.position().y(), 0.0)); - let mut position = transform_component.unwrap().position().clone(); - position.set_x(position.x() / self.config().width as f32); - position.set_y(position.y() / self.config().height as f32); - let region = self.get_texture_region(renderer_component.unwrap().get_texture().to_string()); - let (dim_x, dim_y) = region.dimensions(); + let buffer_size = vertex_buffer.len() as u16; - let (bound_x, bound_y) = - ((dim_x as f32/ self.config().width as f32) * 0.5, (dim_y as f32/ self.config().height as f32) * 0.5); + vertex_buffer.append(&mut vec![ + Vertex::new([-bound_x + position.x(), bound_y + position.y(), 0.0], [region.x0(), region.y0()], [0.0, 0.0, 0.0, 0.0]), + Vertex::new([-bound_x + position.x(), -bound_y + position.y(), 0.0], [region.x0(), region.y1()], [0.0, 0.0, 0.0, 0.0]), + Vertex::new([bound_x + position.x(), -bound_y + position.y(), 0.0], [region.x1(), region.y1()], [0.0, 0.0, 0.0, 0.0]), + Vertex::new([bound_x + position.x(), bound_y + position.y(), 0.0], [region.x1(), region.y0()], [0.0, 0.0, 0.0, 0.0]) + ]); - let buffer_size = vertex_buffer.len() as u16; - - vertex_buffer.append(&mut vec![ - Vertex :: new ( [-bound_x + position.x(), bound_y + position.y(), 0.0], [region.x0(), region.y0()], [0.0, 0.0, 0.0, 0.0] ), - Vertex :: new ( [-bound_x + position.x(), -bound_y + position.y(), 0.0], [region.x0(), region.y1()], [0.0, 0.0, 0.0, 0.0] ), - Vertex :: new ( [ bound_x + position.x(), -bound_y + position.y(), 0.0], [region.x1(), region.y1()], [0.0, 0.0, 0.0, 0.0] ) , - Vertex :: new ( [ bound_x + position.x(), bound_y + position.y(), 0.0], [region.x1(), region.y0()], [0.0, 0.0, 0.0, 0.0] ) - ]); - - index_buffer.append(&mut vec![ - 0 + buffer_size, 1 + buffer_size, 3 + buffer_size, - 1 + buffer_size, 2 + buffer_size, 3 + buffer_size - ]); + index_buffer.append(&mut vec![ + 0 + buffer_size, 1 + buffer_size, 3 + buffer_size, + 1 + buffer_size, 2 + buffer_size, 3 + buffer_size + ]); + } } - } - self.set_buffers(vertex_buffer, index_buffer);*/ + debug!("Visible entities: {:?}", vertex_buffer); + self.set_buffers(vertex_buffer, index_buffer); + }*/ } pub fn update(&mut self) -> f32 {