okay, code complete enough. Done with it now.

This commit is contained in:
2026-09-18 19:10:43 -05:00
parent 147969056c
commit ae15968417
+36 -19
View File
@@ -245,34 +245,52 @@ fn main() -> AnyResult<()> {
window.update_with_buffer(&buffer, WINDOW_WIDTH, WINDOW_HEIGHT)?; window.update_with_buffer(&buffer, WINDOW_WIDTH, WINDOW_HEIGHT)?;
window.set_target_fps(30); window.set_target_fps(30);
let mut frame = 0.0; let trb = Point3d::new(1.0, 1.0, 1.0);
let trf = Point3d::new(0.0, 1.0, 1.0);
let trb = Point3d::new(5.0, 5.0, 5.0); let tlb = Point3d::new(1.0, 1.0, 0.0);
let trf = Point3d::new(4.0, 5.0, 5.0); let tlf = Point3d::new(0.0, 1.0, 0.0);
let tlb = Point3d::new(5.0, 5.0, 4.0); let blf = Point3d::new(0.0, 0.0, 0.0);
let tlf = Point3d::new(4.0, 5.0, 4.0); let blb = Point3d::new(1.0, 0.0, 0.0);
let blf = Point3d::new(4.0, 4.0, 4.0); let brf = Point3d::new(0.0, 0.0, 1.0);
let blb = Point3d::new(5.0, 4.0, 4.0); let brb = Point3d::new(1.0, 0.0, 1.0);
let brf = Point3d::new(4.0, 4.0, 5.0);
let brb = Point3d::new(5.0, 4.0, 5.0);
let mut cube = Cube::new(trb, trf, tlb, tlf, blf, blb, brf, brb); let mut cube = Cube::new(trb, trf, tlb, tlf, blf, blb, brf, brb);
let mut gif_buffer: Vec<Vec<u8>> = Vec::new(); let mut gif_buffer: Vec<Vec<u8>> = Vec::new();
let mut x_trans = 0.0;
let mut y_trans = 0.0;
let mut z_trans = 0.0;
const X_TRANS_RATE: f64 = 1.0 / 30.0;
const Y_TRANS_RATE: f64 = 0.5 / 30.0;
const Z_TRANS_RATE: f64 = 2.0 / 30.0;
const MAX_TRANS: f64 = 6.0;
while window.is_open() { while window.is_open() {
//Reload the background for each frame //Reload the background for each frame
let mut buffer = background_buffer.clone(); let mut buffer = background_buffer.clone();
frame += 1.0; cube.translate(Point3d::new(X_TRANS_RATE, Y_TRANS_RATE, Z_TRANS_RATE));
cube.translate(Point3d::new(0.01, 0.0, 0.0));
update_buffer_with_pixels(&mut buffer, cube.render(&m)); update_buffer_with_pixels(&mut buffer, cube.render(&m));
if frame > 150.0 { x_trans += X_TRANS_RATE;
frame = 0.0; y_trans += Y_TRANS_RATE;
z_trans += Z_TRANS_RATE;
cube.translate(Point3d::new(-1.5, 0.0, 0.0)); if x_trans > MAX_TRANS {
cube.translate(Point3d::new(-x_trans, 0.0, 0.0));
x_trans = 0.0;
}
if y_trans > MAX_TRANS {
cube.translate(Point3d::new(0.0, -y_trans, 0.0));
y_trans = 0.0;
}
if z_trans > MAX_TRANS {
cube.translate(Point3d::new(0.0, 0.0, -z_trans));
z_trans = 0.0;
} }
//Required to close, and update frame; //Required to close, and update frame;
@@ -288,7 +306,7 @@ fn main() -> AnyResult<()> {
); );
} }
export_gif(WINDOW_WIDTH, WINDOW_HEIGHT, 30, gif_buffer); export_gif(WINDOW_WIDTH, WINDOW_HEIGHT, 30, gif_buffer)?;
Ok(()) Ok(())
} }
@@ -442,7 +460,7 @@ fn export_gif(
height: usize, height: usize,
fps: u32, fps: u32,
frames: Vec<Vec<u8>>, // Each inner Vec is width * height * 4 bytes (RGBA) frames: Vec<Vec<u8>>, // Each inner Vec is width * height * 4 bytes (RGBA)
) -> Result<(), Box<dyn std::error::Error>> { ) -> AnyResult<()> {
let file = File::create("animation.gif")?; let file = File::create("animation.gif")?;
let mut encoder = GifEncoder::new(file); let mut encoder = GifEncoder::new(file);
@@ -451,8 +469,7 @@ fn export_gif(
for raw_buffer in frames { for raw_buffer in frames {
// Construct the image from your raw pixel slice // Construct the image from your raw pixel slice
let img = RgbaImage::from_raw(width as u32, height as u32, raw_buffer) let img = RgbaImage::from_raw(width as u32, height as u32, raw_buffer).unwrap();
.ok_or("Buffer length didn't match width * height * 4")?;
// Calculate frame timing (e.g., 1000 / 30 = 33.3ms) // Calculate frame timing (e.g., 1000 / 30 = 33.3ms)
let delay = Delay::from_numer_denom_ms(1000, fps); let delay = Delay::from_numer_denom_ms(1000, fps);