summaryrefslogtreecommitdiff
path: root/src/view.rs
diff options
context:
space:
mode:
authorRosyid Haryadi <rosyid_haryadi@protonmail.com>2025-02-26 13:25:47 +0700
committerRosyid Haryadi <rosyid_haryadi@protonmail.com>2025-02-26 13:25:47 +0700
commit4250ef35134b9a090cd14ca85c560b2c966b553e (patch)
treee16c698e8e09afd671ca80bb25c79a2df8cf3019 /src/view.rs
parent29494ca7a8af8589a1f73e813c29365339006810 (diff)
refactor: files separation
Diffstat (limited to 'src/view.rs')
-rw-r--r--src/view.rs42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/view.rs b/src/view.rs
new file mode 100644
index 0000000..e4a09b1
--- /dev/null
+++ b/src/view.rs
@@ -0,0 +1,42 @@
+use std::fs::File;
+use std::io::Write;
+use crate::global::{DisplayBuffer, IMG_HEIGHT, IMG_WIDTH};
+
+pub struct View<'a> {
+ pub data: &'a DisplayBuffer,
+ pub viewer: fn(&DisplayBuffer) -> Result<String, std::io::Error>,
+}
+
+impl<'a> View<'a> {
+ pub fn display(&self) {
+ let result = (self.viewer)(self.data);
+ match result {
+ Ok(success_msg) => {
+ println!("{}", success_msg);
+ },
+ Err(error_msg) => {
+ eprintln!("{}", error_msg);
+ }
+ }
+ }
+}
+
+pub mod render_viewer {
+ use super::*;
+
+ pub fn ppm_exporter(data: &DisplayBuffer) -> Result<String, std::io::Error> {
+ let file_name = "output.ppm";
+ let mut file = File::create(file_name)?;
+ // header
+ let mut txt_data = String::from("P3\n");
+ txt_data.push_str(format!("{} {}\n255\n", IMG_WIDTH, IMG_HEIGHT).as_str());
+ // data point
+ data.iter().for_each(|row| {
+ row.iter().for_each(|&pixel| {
+ txt_data.push_str(&format!("{} {} {}\n", pixel.r, pixel.g, pixel.b));
+ })
+ });
+ file.write_all(txt_data.as_bytes())?;
+ Ok(format!("Output rendered to file {}", file_name))
+ }
+}