From 6cb496b5b8b085ea46549ba7a3593c28a40be7e9 Mon Sep 17 00:00:00 2001 From: HampusM Date: Thu, 2 Nov 2023 18:42:18 +0100 Subject: refactor: replace Object new method with builder --- engine/src/color.rs | 2 +- engine/src/object.rs | 123 ++++++++++++++++++++++++++++++++------------------- engine/src/vertex.rs | 2 +- 3 files changed, 79 insertions(+), 48 deletions(-) (limited to 'engine/src') diff --git a/engine/src/color.rs b/engine/src/color.rs index 339b427..1e87cb9 100644 --- a/engine/src/color.rs +++ b/engine/src/color.rs @@ -1,4 +1,4 @@ -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Default)] #[repr(C)] pub struct Color { diff --git a/engine/src/object.rs b/engine/src/object.rs index dffd240..f2591f2 100644 --- a/engine/src/object.rs +++ b/engine/src/object.rs @@ -19,52 +19,6 @@ pub struct Object impl Object { - /// Returns a new `Object`. - /// - /// # Errors - /// Will return `Err` if shader creation fails or if shader program linking fails. - pub fn new( - id: Id, - vertices: &[Vertex], - indices: Option<&[u32]>, - ) -> Result - { - let vertex_shader = Shader::new(ShaderKind::Vertex); - - vertex_shader - .set_source(include_str!("../vertex.glsl")) - .map_err(Error::CreateVertexShaderFailed)?; - - vertex_shader - .compile() - .map_err(Error::CreateVertexShaderFailed)?; - - let fragment_shader = Shader::new(ShaderKind::Fragment); - - fragment_shader - .set_source(include_str!("../fragment.glsl")) - .map_err(Error::CreateFragmentShaderFailed)?; - - fragment_shader - .compile() - .map_err(Error::CreateFragmentShaderFailed)?; - - let shader_program = ShaderProgram::new(); - - shader_program.attach(&vertex_shader); - shader_program.attach(&fragment_shader); - - shader_program - .link() - .map_err(Error::LinkShaderProgramFailed)?; - - let renderable = Renderable::new(shader_program, vertices, indices); - - let transform = Transform::new(); - - Ok(Self { id, renderable, transform }) - } - /// Returns the object ID. #[must_use] pub fn id(&self) -> Id @@ -99,6 +53,83 @@ impl Object } } +/// Object builder. +#[derive(Debug, Clone, Default)] +pub struct Builder +{ + vertices: Vec, + indices: Option>, +} + +impl Builder +{ + /// Returns a new [`Object`] builder. + #[must_use] + pub fn new() -> Self + { + Self::default() + } + + #[must_use] + pub fn vertices(mut self, vertices: impl IntoIterator) -> Self + { + self.vertices = vertices.into_iter().collect(); + + self + } + + #[must_use] + pub fn indices(mut self, indices: impl IntoIterator) -> Self + { + self.indices = Some(indices.into_iter().collect()); + + self + } + + /// Builds a new [`Object`]. + /// + /// # Errors + /// Will return `Err` if: + /// - Creating shaders fails + /// - Linking the shader program fails + pub fn build(&self, id: Id) -> Result + { + let vertex_shader = + Self::create_shader(ShaderKind::Vertex, include_str!("../vertex.glsl")) + .map_err(Error::CreateVertexShaderFailed)?; + + let fragment_shader = + Self::create_shader(ShaderKind::Fragment, include_str!("../fragment.glsl")) + .map_err(Error::CreateFragmentShaderFailed)?; + + let shader_program = ShaderProgram::new(); + + shader_program.attach(&vertex_shader); + shader_program.attach(&fragment_shader); + + shader_program + .link() + .map_err(Error::LinkShaderProgramFailed)?; + + let renderable = + Renderable::new(shader_program, &self.vertices, self.indices.as_deref()); + + let transform = Transform::new(); + + Ok(Object { id, renderable, transform }) + } + + fn create_shader(kind: ShaderKind, source: &str) -> Result + { + let shader = Shader::new(kind); + + shader.set_source(source)?; + shader.compile()?; + + Ok(shader) + } +} + /// Object error #[derive(Debug, thiserror::Error)] pub enum Error diff --git a/engine/src/vertex.rs b/engine/src/vertex.rs index 592353a..62f629b 100644 --- a/engine/src/vertex.rs +++ b/engine/src/vertex.rs @@ -3,7 +3,7 @@ use std::mem::size_of; use crate::color::Color; use crate::vector::Vec3; -#[derive(Debug)] +#[derive(Debug, Clone, Default)] #[repr(C)] pub struct Vertex { -- cgit v1.2.3-18-g5258