diff options
author | Rosyid Haryadi <rosyid_haryadi@protonmail.com> | 2025-02-26 10:49:43 +0700 |
---|---|---|
committer | Rosyid Haryadi <rosyid_haryadi@protonmail.com> | 2025-02-26 10:49:43 +0700 |
commit | 29494ca7a8af8589a1f73e813c29365339006810 (patch) | |
tree | cb68d590609972abf691a7810d679314ac6f592b | |
parent | 7f321d993b7ad9335de0b12e248a477fa333a9c6 (diff) |
upd: type alias
-rw-r--r-- | src/main.rs | 26 |
1 files changed, 16 insertions, 10 deletions
diff --git a/src/main.rs b/src/main.rs index 0c81220..f81c5a7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,8 @@ 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, @@ -18,8 +20,8 @@ impl Default for Pixel { } struct View<'a> { - data: &'a[[Pixel; IMG_WIDTH]; IMG_HEIGHT], - viewer: fn(&[[Pixel; IMG_WIDTH]; IMG_HEIGHT]) -> Result<String, std::io::Error>, + data: &'a DisplayBuffer, + viewer: fn(&DisplayBuffer) -> Result<String, std::io::Error>, } impl<'a> View<'a> { @@ -36,7 +38,7 @@ impl<'a> View<'a> { } } -fn ppm_exporter(data: &[[Pixel; IMG_WIDTH]; IMG_HEIGHT]) -> Result<String, std::io::Error> { +fn ppm_exporter(data: &DisplayBuffer) -> Result<String, std::io::Error> { let file_name = "output.ppm"; let mut file = File::create(file_name)?; // header @@ -53,8 +55,18 @@ fn ppm_exporter(data: &[[Pixel; IMG_WIDTH]; IMG_HEIGHT]) -> Result<String, std:: } fn main() { - let mut data = [[Pixel::default(); IMG_WIDTH]; IMG_HEIGHT]; + let mut data: DisplayBuffer = [[Pixel::default(); IMG_WIDTH]; IMG_HEIGHT]; + + render_gradient(&mut data); + let view = View { + data: &data, + viewer: ppm_exporter, + }; + view.display(); +} + +fn render_gradient(data: &mut DisplayBuffer) { (0..IMG_HEIGHT).for_each(|j| { (0..IMG_WIDTH).for_each(|i| { let r = i as f32 / IMG_WIDTH as f32; @@ -68,10 +80,4 @@ fn main() { data[i][j] = Pixel { r: ir, g: ig, b: ib}; }) }); - - let view = View { - data: &data, - viewer: ppm_exporter, - }; - view.display(); } |