diff --git a/.gitignore b/.gitignore index 6db043d..b37e1fe 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .idea /target +image.png \ No newline at end of file diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000..78828c2 --- /dev/null +++ b/src/error.rs @@ -0,0 +1,5 @@ +#[derive(thiserror::Error, Debug)] +pub enum LithiumError { + #[error("There is no camera with the ID {0}")] + CameraNotFound(usize), +} diff --git a/src/main.rs b/src/main.rs index 1385fb4..23c45db 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,46 +1,16 @@ -pub mod output; pub mod render; +pub mod error; -use crate::output::{Metadata, Output}; use crate::render::Renderer; use anyhow::Result; -use image::RgbImage; - -struct TestOutput { - width: u32, - height: u32, - image: RgbImage, -} - -impl output::Output for TestOutput { - fn get_meta(&self) -> Metadata { - Metadata { - width: self.width, - height: self.height, - } - } - - fn render(&mut self, buffer: &RgbImage) -> Result<()> { - self.image = buffer.clone(); - self.image.save("image.png")?; - Ok(()) - } -} - -impl TestOutput { - fn new(width: u32, height: u32) -> Self { - Self { - width, - height, - image: RgbImage::new(width, height), - } - } -} #[tokio::main] async fn main() -> Result<()> { let mut renderer = Renderer::new(); - renderer.add_camera(TestOutput::new(512, 512)); - + let camera = renderer.add_camera(512, 512); + + let image = renderer.render(camera).await?; + image.save("image.png")?; + Ok(()) } diff --git a/src/output.rs b/src/output.rs deleted file mode 100644 index 08b6efa..0000000 --- a/src/output.rs +++ /dev/null @@ -1,12 +0,0 @@ -use anyhow::Result; -use image::RgbImage; - -pub struct Metadata { - pub height: u32, - pub width: u32, -} - -pub trait Output { - fn get_meta(&self) -> Metadata; - fn render(&mut self, buffer: &RgbImage) -> Result<()>; -} diff --git a/src/render/camera.rs b/src/render/camera.rs index bba51b9..799f9db 100644 --- a/src/render/camera.rs +++ b/src/render/camera.rs @@ -1,13 +1,18 @@ -use crate::output::Output; - pub struct Camera { - output: Box, + width: u32, + height: u32, } impl Camera { - pub fn new(output: impl Output + 'static) -> Self { - Self { - output: Box::new(output), - } + pub fn new(width: u32, height: u32) -> Self { + Self { width, height } + } + + pub fn height(&self) -> u32 { + self.height + } + + pub fn width(&self) -> u32 { + self.width } } diff --git a/src/render/mod.rs b/src/render/mod.rs index 2df4884..067a172 100644 --- a/src/render/mod.rs +++ b/src/render/mod.rs @@ -1,8 +1,8 @@ pub mod camera; -use crate::output::Output; -use crate::render::camera::Camera; - +use crate::{error::LithiumError, render::camera::Camera}; +use anyhow::Result; +use image::RgbImage; use lithium_loader::Model; use slab::Slab; @@ -19,7 +19,17 @@ impl Renderer { } } - pub fn add_camera(&mut self, output: impl Output + 'static) -> usize { - self.cameras.insert(Camera::new(output)) + pub fn add_camera(&mut self, width: u32, height: u32) -> usize { + self.cameras.insert(Camera::new(width, height)) + } + + pub async fn render(&self, camera: usize) -> Result { + let camera = self + .cameras + .get(camera) + .ok_or(LithiumError::CameraNotFound(camera))?; + let result = RgbImage::new(camera.height(), camera.width()); + + Ok(result) } }