diff options
Diffstat (limited to 'engine')
-rw-r--r-- | engine/src/lib.rs | 39 | ||||
-rw-r--r-- | engine/src/object.rs | 29 |
2 files changed, 61 insertions, 7 deletions
diff --git a/engine/src/lib.rs b/engine/src/lib.rs index dc76b37..9d836c0 100644 --- a/engine/src/lib.rs +++ b/engine/src/lib.rs @@ -1,8 +1,10 @@ #![deny(clippy::all, clippy::pedantic)] +use std::collections::BTreeMap; + use glfw::{Window, WindowBuilder}; -use crate::object::Object; +use crate::object::{Id as ObjectId, Object}; use crate::vector::Vec2; mod opengl; @@ -19,6 +21,7 @@ pub use glfw::WindowSize; pub struct Engine { window: Window, + objects: BTreeMap<ObjectId, Object>, } impl Engine @@ -49,19 +52,45 @@ impl Engine crate::renderer::initialize(&window).map_err(Error::InitializeRendererFailed)?; - Ok(Self { window }) + Ok(Self { + window, + objects: BTreeMap::new(), + }) + } + + pub fn add_object(&mut self, object: Object) + { + self.objects.insert(object.id(), object); + } + + pub fn set_objecs(&mut self, objects: impl IntoIterator<Item = Object>) + { + self.objects + .extend(objects.into_iter().map(|object| (object.id(), object))); + } + + #[must_use] + pub fn get_object_by_id(&self, id: ObjectId) -> Option<&Object> + { + self.objects.get(&id) + } + + #[must_use] + pub fn get_object_by_id_mut(&mut self, id: ObjectId) -> Option<&mut Object> + { + self.objects.get_mut(&id) } /// Starts the engine. /// /// # Errors /// Will return `Err` if updating the window fails. - pub fn start(&self, objects: &[Object], mut func: impl FnMut()) -> Result<(), Error> + pub fn start(&mut self, mut func: impl FnMut(&mut Self)) -> Result<(), Error> { while !self.window.should_close() { - func(); + func(self); - crate::renderer::render(objects.iter().map(Object::renderable)); + crate::renderer::render(self.objects.values().map(Object::renderable)); self.window .swap_buffers() diff --git a/engine/src/object.rs b/engine/src/object.rs index a650abc..11ba7e6 100644 --- a/engine/src/object.rs +++ b/engine/src/object.rs @@ -10,6 +10,7 @@ use crate::vertex::Vertex; #[derive(Debug)] pub struct Object { + id: Id, renderable: Renderable, } @@ -19,7 +20,7 @@ impl Object /// /// # Errors /// Will return `Err` if shader creation fails or if shader program linking fails. - pub fn new(vertices: &[Vertex]) -> Result<Self, Error> + pub fn new(id: Id, vertices: &[Vertex]) -> Result<Self, Error> { let vertex_shader = Shader::new(ShaderKind::Vertex); @@ -52,7 +53,14 @@ impl Object let renderable = Renderable::new(shader_program, vertices); - Ok(Self { renderable }) + Ok(Self { id, renderable }) + } + + /// Returns the object ID. + #[must_use] + pub fn id(&self) -> Id + { + self.id } pub(crate) fn renderable(&self) -> &Renderable @@ -74,3 +82,20 @@ pub enum Error #[error("Failed to link shader program")] LinkShaderProgramFailed(#[source] ShaderError), } + +/// Object ID. +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub struct Id +{ + id: u16, +} + +impl Id +{ + /// Returns a new object ID. + #[must_use] + pub fn new(id: u16) -> Self + { + Self { id } + } +} |