summaryrefslogtreecommitdiff
path: root/engine/src/lib.rs
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2023-10-16 21:58:06 +0200
committerHampusM <hampus@hampusmat.com>2023-10-16 21:58:06 +0200
commit58b8df3f51ade9d8c689e0421272295038757d06 (patch)
treef529cb841b839a3d4f6a1e800d0f1d897b125a2f /engine/src/lib.rs
parentd343bbd9c14d152ab8523718f5de309842b667b4 (diff)
feat(engine): store objects in engine
Diffstat (limited to 'engine/src/lib.rs')
-rw-r--r--engine/src/lib.rs39
1 files changed, 34 insertions, 5 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()