graphics: Unify damage clipping

This commit is contained in:
bjorn3
2024-12-23 16:27:57 +01:00
parent f2156d4654
commit f6cc7d25df
4 changed files with 37 additions and 14 deletions
+17
View File
@@ -1,5 +1,6 @@
#![feature(iter_next_chunk)]
use std::cmp;
use std::fs::File;
use std::io::{Error, Read, Write};
use std::mem::size_of;
@@ -104,3 +105,19 @@ pub struct Damage {
pub width: i32,
pub height: i32,
}
impl Damage {
#[must_use]
pub fn clip(mut self, width: i32, height: i32) -> Self {
// Clip damage
self.x = cmp::min(self.x, width);
if self.x + self.width > width {
self.width -= cmp::min(self.width, width - self.x);
}
self.y = cmp::min(self.y, height);
if self.y + self.height > height {
self.height = cmp::min(self.height, height - self.y);
}
self
}
}