82 lines
2.2 KiB
Rust
82 lines
2.2 KiB
Rust
use crate::shapes::point::{Point, ScreenPoint};
|
|
use crate::window::triangle::triangle;
|
|
use crate::{
|
|
rgb::RGB8,
|
|
shapes::{point::PointVec, triangle},
|
|
};
|
|
use core::time::Duration;
|
|
|
|
pub struct Window {
|
|
pub title: String,
|
|
pub width: usize,
|
|
pub height: usize,
|
|
pub buffer: Vec<u32>,
|
|
pub target_fps: Option<usize>,
|
|
}
|
|
impl Window {
|
|
pub fn new(title: &str, width: usize, height: usize, target_fps: Option<usize>) -> Self {
|
|
let buffer: Vec<u32> = vec![0; width * height];
|
|
Self {
|
|
title: title.to_string(),
|
|
width,
|
|
height,
|
|
buffer,
|
|
target_fps,
|
|
}
|
|
}
|
|
/// timi
|
|
pub fn timing_to_hit_target_fps(&self) -> Option<Duration> {
|
|
if let Some(target_fps) = self.target_fps {
|
|
let target_duration = Duration::from_secs(1) / target_fps as u32;
|
|
Some(target_duration)
|
|
} else {
|
|
None
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Window {
|
|
pub fn clear(&mut self, color: RGB8) {
|
|
for i in self.buffer.iter_mut() {
|
|
*i = color.to_u32();
|
|
}
|
|
}
|
|
|
|
pub fn draw_triangles(&mut self, points: &PointVec) -> Result<(), String> {
|
|
let length = points.len();
|
|
if length % 3 != 0 {
|
|
Err("points must be a multiple of 3".to_string())
|
|
} else {
|
|
for p in points.chunks(3) {
|
|
self.draw_triangle(*p[0], *p[1], *p[2]);
|
|
}
|
|
Ok(())
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Window {
|
|
pub fn draw_triangle(&mut self, p1: Point, p2: Point, p3: Point) {}
|
|
|
|
pub fn draw_line(&mut self, p1: ScreenPoint, p2: ScreenPoint) {
|
|
let x1 = p1.x;
|
|
println!("x1: {}", x1);
|
|
let y1 = p1.y;
|
|
println!("y1: {}", y1);
|
|
let x2 = p2.x;
|
|
println!("x2: {}", x2);
|
|
let y2 = p2.y;
|
|
println!("y2: {}", y2);
|
|
let dx = x2.wrapping_sub(x1);
|
|
println!("dx: {}", dx);
|
|
let dy = y2.wrapping_sub(y1);
|
|
println!("dy: {}", dy);
|
|
for x in x1..x2 {
|
|
let y = y1 + dy * (x - x1) / dx;
|
|
println!("y: {}", y);
|
|
let coordinates = x + 659 * y;
|
|
self.buffer[coordinates as usize] = 0xffff;
|
|
}
|
|
}
|
|
}
|