-
Notifications
You must be signed in to change notification settings - Fork 0
/
vec_grid.rs
59 lines (46 loc) · 1.29 KB
/
vec_grid.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
use simple_logger::SimpleLogger;
use pixelflut_rs::grid::{Grid, Size};
use pixelflut_rs::pixel::{Color, Coordinate, Pixel};
use pixelflut_rs::server::Server;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
SimpleLogger::new().init().unwrap();
let grid = VecGrid::new(Size::new(1024, 768));
let server = Server::new("0.0.0.0".parse()?, 2342, grid);
server.start().await
}
struct VecGrid {
size: Size,
frame: Vec<Vec<Color>>,
}
impl VecGrid {
pub fn new(size: Size) -> VecGrid {
let black = Color::rgb(0x00, 0x00, 0x00);
let frame = vec![vec![black; size.y()]; size.x()];
VecGrid {
size,
frame,
}
}
}
impl Grid for VecGrid {
fn size(&self) -> Size {
self.size.clone()
}
fn draw(&mut self, px: &Pixel) {
let x = px.coordinate().x();
let y = px.coordinate().y();
if x < self.size.x() && y < self.size.y() {
self.frame[x][y] = px.color();
}
}
fn fetch(&self, coord: Coordinate) -> Option<Pixel> {
let x = coord.x();
let y = coord.y();
if x < self.size.x() && y < self.size.y() {
let color = self.frame[x][y];
return Some(Pixel::new(coord, color))
}
None
}
}