Skip to content

Commit 9c0bb32

Browse files
committed
added hspan & rectangle fills
1 parent ef4c195 commit 9c0bb32

File tree

1 file changed

+36
-8
lines changed

1 file changed

+36
-8
lines changed

src/renderer/render.rs

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![allow(dead_code, unused_variables)]
1+
#![allow(dead_code)]
22

33
/* ik this is jank so i'm gonna figure out how to modularize so
44
every submodule that's a part of src/folder is prefixed by crate::folder */
@@ -49,7 +49,6 @@ impl Renderer {
4949
let x = x as usize;
5050
let y = y as usize;
5151
let w = fb.width() as usize;
52-
let h = fb.height() as usize;
5352

5453
// offset -> 4 u8s, so y * width + x coord will get you u32 pixel #.
5554
// therefore * 4 will get you u8 red # (then you get green blue and alpha immediately after)
@@ -62,14 +61,43 @@ impl Renderer {
6261
data[offset + 3] = color.a;
6362
}
6463

65-
/// plot the entire span of one row
64+
/// plot the span of one row from x0 to x1
6665
pub fn hspan(&mut self, fb: &mut Frame, y: u16, x0: u16, x1: u16, color: Color) {
67-
todo!("[NOT IMPLEMENTED] waiting on implementation.");
66+
// converting to usize again cuz vectors use it
67+
let w = fb.width() as usize;
68+
let h = fb.height() as usize;
69+
let y = y as usize;
70+
if y >= h {
71+
return;
72+
}
73+
74+
// get start and end (exclusive btw)
75+
let start = x0.min(x1) as usize;
76+
let end = x0.max(x1) as usize;
77+
78+
// bounds check (we're gonna eventually just do a precheck before rendering and flag anything that's out of bounds)
79+
// cuz doing this for every hspan is SLOW
80+
let width = end - start;
81+
if width == 0 || start > w || end > w {
82+
return;
83+
}
84+
85+
// offset to first pixel in this row, slice the entire thing
86+
let start = (y * w + start) * 4;
87+
let len = width * 4;
88+
let row_slice = &mut fb.as_bytes_mut()[start .. start + len];
89+
90+
// pack 4 bytes at a time
91+
for chunk in row_slice.chunks_exact_mut(4) {
92+
chunk.copy_from_slice(&[color.r, color.g, color.b, color.a]);
93+
}
6894
}
6995

70-
/// solid rectangle fill
71-
pub fn rect(&mut self, x: u16, y: u16, w: u16, h: u16, color: Color) {
72-
todo!("[NOT IMPLEMENTED] waiting on implementation.");
96+
/// solid rectangle fill. legit just hspan for row in rows
97+
pub fn rect(&mut self, fb: &mut Frame, x: u16, y: u16, width: u16, height: u16, color: Color) {
98+
for _ in y..height {
99+
self.hspan(fb, y, x, width, color);
100+
}
73101
}
74102

75103
/// will be used for the draw queue
@@ -78,7 +106,7 @@ impl Renderer {
78106
}
79107

80108
/// will also be used for the draw queue
81-
pub fn end_frame(&mut self, fb: &mut Frame) {
109+
pub fn end_frame(&mut self, _fb: &mut Frame) {
82110
todo!("[NOT IMPLEMENTED] waiting on implementation.");
83111
}
84112
}

0 commit comments

Comments
 (0)