diff options
author | gennyble <gen@nyble.dev> | 2024-12-16 02:45:37 -0600 |
---|---|---|
committer | gennyble <gen@nyble.dev> | 2024-12-16 02:45:37 -0600 |
commit | c4d2c86ea3388db76fd5fb36152d14c34902c47b (patch) | |
tree | 5841501838f966ce51c4959bb443c2adc8e3c8dc /src/image.rs | |
parent | de63314d720a89fbccb03445a207f01812882eeb (diff) | |
download | really-etches-c4d2c86ea3388db76fd5fb36152d14c34902c47b.tar.gz really-etches-c4d2c86ea3388db76fd5fb36152d14c34902c47b.zip |
Diffstat (limited to 'src/image.rs')
-rw-r--r-- | src/image.rs | 11 |
1 files changed, 9 insertions, 2 deletions
diff --git a/src/image.rs b/src/image.rs index 177eff2..5b6cb33 100644 --- a/src/image.rs +++ b/src/image.rs @@ -73,11 +73,18 @@ impl Image { } pub fn rect(&mut self, pos: Vec2<u32>, dim: Vec2<u32>, clr: Color) { + if pos.x >= self.width || pos.y >= self.height { + // if we're fully over in one of these directions, we shouldn't go further. + return; + } + + // .min(..) so we only draw what we can see/what's within bounds let x_start = pos.x as usize; - let x_end = (pos.x + dim.x) as usize; + let x_end = (pos.x + dim.x).min(self.width - 1) as usize; + let y_end = (pos.y + dim.y).min(self.height - 1) as usize; for idx in x_start..x_end { - for idy in pos.y as usize..pos.y as usize + dim.y as usize { + for idy in pos.y as usize..y_end { let data_idx = idx + idy * self.width as usize; self.data[data_idx] = clr.into(); } |