diff --git a/editor/src/lib.rs b/editor/src/lib.rs index 4bd41b22f7..d0b5ef881b 100644 --- a/editor/src/lib.rs +++ b/editor/src/lib.rs @@ -11,6 +11,7 @@ // re-enable this when working on performance optimizations than have it block PRs. #![allow(clippy::large_enum_variant)] +use crate::rect::Rect; use crate::vertex::Vertex; use std::error::Error; use std::io; @@ -21,6 +22,7 @@ use winit::event::{ElementState, ModifiersState, VirtualKeyCode}; use winit::event_loop::ControlFlow; pub mod ast; +mod rect; pub mod text_state; mod vertex; @@ -219,17 +221,39 @@ fn run_event_loop() -> Result<(), Box> { .expect("Failed to acquire next swap chain texture") .output; + // Test Rectangle + let test_rect_1 = Rect { + top: 0.9, + left: -0.8, + width: 0.2, + height: 0.3, + color: [0.0, 1.0, 1.0], + }; + let test_rect_2 = Rect { + top: 0.0, + left: 0.0, + width: 0.5, + height: 0.5, + color: [1.0, 1.0, 0.0], + }; + let mut rectangles = Vec::new(); + rectangles.extend_from_slice(&test_rect_1.as_array()); + rectangles.extend_from_slice(&test_rect_2.as_array()); + + let mut rect_index_buffers = Vec::new(); + rect_index_buffers.extend_from_slice(&Rect::INDEX_BUFFER); + rect_index_buffers.extend_from_slice(&Rect::INDEX_BUFFER); // Vertex Buffer for drawing rectangles let vertex_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor { label: Some("Vertex Buffer"), - contents: bytemuck::cast_slice(&vertex::vertex_buffer_test()), + contents: bytemuck::cast_slice(&rectangles), usage: wgpu::BufferUsage::VERTEX, }); // Index Buffer for drawing rectangles let index_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor { label: Some("Index Buffer"), - contents: bytemuck::cast_slice(&vertex::index_buffer_test()), + contents: bytemuck::cast_slice(&rect_index_buffers), usage: wgpu::BufferUsage::INDEX, }); @@ -262,9 +286,9 @@ fn run_event_loop() -> Result<(), Box> { render_pass.set_index_buffer(index_buffer.slice(..)); render_pass.draw_indexed( - 0..((&vertex::index_buffer_test()).len() as u32), // Draw all of the vertices from our test data. - 0, // Base Vertex - 0..1, // Instances + 0..((&rect_index_buffers).len() as u32), // Draw all of the vertices from our test data. + 0, // Base Vertex + 0..1, // Instances ); } diff --git a/editor/src/rect.rs b/editor/src/rect.rs new file mode 100644 index 0000000000..e3d9253051 --- /dev/null +++ b/editor/src/rect.rs @@ -0,0 +1,35 @@ +use crate::vertex::Vertex; + +pub struct Rect { + pub top: f32, + pub left: f32, + pub width: f32, + pub height: f32, + pub color: [f32; 3], +} + +impl Rect { + pub fn as_array(&self) -> [Vertex; 4] { + [ + Vertex { + position: [self.left, self.top, 0.0], + color: self.color, + }, + Vertex { + position: [self.left + self.width, self.top, 0.0], + color: self.color, + }, + Vertex { + position: [self.left + self.width, self.top - self.height, 0.0], + color: self.color, + }, + Vertex { + position: [self.left, self.top - self.height, 0.0], + color: self.color, + }, + ] + } + + // Currently broken - needs to be offset when additional rectangles are appended + pub const INDEX_BUFFER: [u16; 6] = [0, 1, 3, 1, 2, 3]; +} diff --git a/editor/src/vertex.rs b/editor/src/vertex.rs index c53330da5c..4af9c3a367 100644 --- a/editor/src/vertex.rs +++ b/editor/src/vertex.rs @@ -1,8 +1,8 @@ #[repr(C)] #[derive(Copy, Clone, Debug)] pub struct Vertex { - position: [f32; 3], - color: [f32; 3], + pub position: [f32; 3], + pub color: [f32; 3], } unsafe impl bytemuck::Pod for Vertex {} @@ -31,28 +31,3 @@ impl Vertex { } } } - -pub fn vertex_buffer_test() -> [Vertex; 4] { - [ - Vertex { - position: [-0.5, 0.5, 0.0], - color: [1.0, 0.0, 0.0], - }, - Vertex { - position: [0.5, 0.5, 0.0], - color: [0.0, 1.0, 0.0], - }, - Vertex { - position: [0.5, -0.5, 0.0], - color: [0.0, 0.0, 1.0], - }, - Vertex { - position: [-0.5, -0.5, 0.0], - color: [1.0, 1.0, 1.0], - }, - ] -} - -pub fn index_buffer_test() -> [u16; 6] { - [0, 1, 3, 1, 2, 3] -}