diff options
author | Rosyid Haryadi <rosyid_haryadi@protonmail.com> | 2025-02-26 13:25:47 +0700 |
---|---|---|
committer | Rosyid Haryadi <rosyid_haryadi@protonmail.com> | 2025-02-26 13:25:47 +0700 |
commit | 4250ef35134b9a090cd14ca85c560b2c966b553e (patch) | |
tree | e16c698e8e09afd671ca80bb25c79a2df8cf3019 | |
parent | 29494ca7a8af8589a1f73e813c29365339006810 (diff) |
refactor: files separation
-rw-r--r-- | src/global.rs | 17 | ||||
-rw-r--r-- | src/main.rs | 65 | ||||
-rw-r--r-- | src/view.rs | 42 |
3 files changed, 67 insertions, 57 deletions
diff --git a/src/global.rs b/src/global.rs new file mode 100644 index 0000000..cb616b1 --- /dev/null +++ b/src/global.rs @@ -0,0 +1,17 @@ +pub const IMG_WIDTH: usize = 256; +pub const IMG_HEIGHT: usize = 256; + +#[derive(Debug, Copy, Clone)] +pub struct Pixel { + pub r: u8, + pub g: u8, + pub b: u8, +} + +impl Default for Pixel { + fn default() -> Self { + Pixel { r:0, g:0, b:0 } + } +} + +pub type DisplayBuffer = [[Pixel; IMG_WIDTH]; IMG_HEIGHT]; diff --git a/src/main.rs b/src/main.rs index f81c5a7..36af9b0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,72 +1,23 @@ -use std::fs::File; -use std::io::Write; - -const IMG_WIDTH: usize = 256; -const IMG_HEIGHT: usize = 256; - -type DisplayBuffer = [[Pixel; IMG_WIDTH]; IMG_HEIGHT]; - -#[derive(Debug, Copy, Clone)] -struct Pixel { - r: u8, - g: u8, - b: u8, -} - -impl Default for Pixel { - fn default() -> Self { - Pixel { r:0, g:0, b:0 } - } -} +mod view; +mod global; -struct View<'a> { - data: &'a DisplayBuffer, - viewer: fn(&DisplayBuffer) -> Result<String, std::io::Error>, -} - -impl<'a> View<'a> { - fn display(&self) { - let result = (self.viewer)(self.data); - match result { - Ok(success_msg) => { - println!("{}", success_msg); - }, - Err(error_msg) => { - eprintln!("{}", error_msg); - } - } - } -} - -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)) -} +use crate::global::*; +use crate::view::{View, render_viewer}; +use std::io::Write; fn main() { let mut data: DisplayBuffer = [[Pixel::default(); IMG_WIDTH]; IMG_HEIGHT]; - render_gradient(&mut data); + test_render(&mut data); let view = View { data: &data, - viewer: ppm_exporter, + viewer: render_viewer::ppm_exporter, }; view.display(); } -fn render_gradient(data: &mut DisplayBuffer) { +fn test_render(data: &mut DisplayBuffer) { (0..IMG_HEIGHT).for_each(|j| { (0..IMG_WIDTH).for_each(|i| { let r = i as f32 / IMG_WIDTH as f32; 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)) + } +} |