summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2023-11-02 18:42:18 +0100
committerHampusM <hampus@hampusmat.com>2023-11-02 18:42:18 +0100
commit6cb496b5b8b085ea46549ba7a3593c28a40be7e9 (patch)
treecd8cf2df973b2806d65ddd44c383a700ac254c8a
parent5b0d859d0fbfb84ec143d894e023dcbd90980ad1 (diff)
refactor: replace Object new method with builder
-rw-r--r--engine/src/color.rs2
-rw-r--r--engine/src/object.rs123
-rw-r--r--engine/src/vertex.rs2
3 files changed, 79 insertions, 48 deletions
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<Value>
{
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
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
{