diff options
author | HampusM <hampus@hampusmat.com> | 2023-11-02 18:42:18 +0100 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2023-11-02 18:42:18 +0100 |
commit | 6cb496b5b8b085ea46549ba7a3593c28a40be7e9 (patch) | |
tree | cd8cf2df973b2806d65ddd44c383a700ac254c8a /engine/src/object.rs | |
parent | 5b0d859d0fbfb84ec143d894e023dcbd90980ad1 (diff) |
refactor: replace Object new method with builder
Diffstat (limited to 'engine/src/object.rs')
-rw-r--r-- | engine/src/object.rs | 123 |
1 files changed, 77 insertions, 46 deletions
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<Self, Error> - { - 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<Vertex>, + indices: Option<Vec<u32>>, +} + +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<Item = Vertex>) -> Self + { + self.vertices = vertices.into_iter().collect(); + + self + } + + #[must_use] + pub fn indices(mut self, indices: impl IntoIterator<Item = u32>) -> 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<Object, Error> + { + 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<Shader, ShaderError> + { + let shader = Shader::new(kind); + + shader.set_source(source)?; + shader.compile()?; + + Ok(shader) + } +} + /// Object error #[derive(Debug, thiserror::Error)] pub enum Error |