Antialiasing kind of works on circles drawing each pixel individually

This commit is contained in:
2025-04-28 00:02:23 -07:00
parent 293e03e500
commit e81ff0be66
2 changed files with 42 additions and 10 deletions

View File

@@ -1,6 +1,31 @@
use crate::wayland::shm::ShmPool; use crate::wayland::shm::ShmPool;
fn color_blend(col1: u32, col2: u32, diff: f64) -> u32 {
// TODO: Account for alpha channel
let r1 = (col1 & 0x00ff0000) >> 16;
let g1 = (col1 & 0x0000ff00) >> 8;
let b1 = col1 & 0x000000ff;
let r2 = (col2 & 0x00ff0000) >> 16;
let g2 = (col2 & 0x0000ff00) >> 8;
let b2 = col2 & 0x000000ff;
let r3 = if r1 < r2 {
r1 + ((r2 - r1) as f64 * diff) as u32
} else {
r1 - ((r1 - r2) as f64 * diff) as u32
};
let g3 = if g1 < g2 {
g1 + ((g2 - g1) as f64 * diff) as u32
} else {
g1 - ((g1 - g2) as f64 * diff) as u32
};
let b3 = if b1 < b2 {
b1 + ((b2 - b1) as f64 * diff) as u32
} else {
b1 - ((b1 - b2) as f64 * diff) as u32
};
return 0xff000000 + (r3 << 16) + (g3 << 8) + b3;
}
// l_row means local row and g_row means global row // l_row means local row and g_row means global row
impl ShmPool { impl ShmPool {
@@ -30,14 +55,21 @@ impl ShmPool {
self.write(&vec![color; w], g_row*self.width+x); self.write(&vec![color; w], g_row*self.width+x);
} }
} }
// x and y are center of circle pub fn shitty_circle(&mut self, x: usize, y: usize, radius: usize, color: u32) {
pub fn circle(&mut self, x: usize, y: usize, radius: usize, color: u32) { for g_row in y-radius..y+radius+1 {
for l_row in 0..radius { for g_col in x-radius..x+radius+1 {
let x_diff = ((radius*radius - l_row*l_row) as f64).sqrt(); let l_row = y.abs_diff(g_row);
let mut row: Vec<u32> = vec![color; 2*x_diff as usize]; let l_col = y.abs_diff(g_col);
self.write(&row, (y+l_row)*self.width + x - x_diff as usize); let distance = (((l_row*l_row)+(l_col*l_col)) as f64).sqrt();
self.write(&row, (y-l_row)*self.width + x - x_diff as usize); let offset = g_row*self.width + g_col;
if (distance.floor() as usize) < radius {
self.write_pixel(color, offset as isize);
} else if (distance.ceil() as usize) <= radius+1 {
dbg!(distance);
self.write_pixel(color_blend(color, self.read_pixel(offset), distance.fract()), offset as isize);
}
}
} }
} }
} }

View File

@@ -14,7 +14,7 @@ impl WlClient {
self.shm_pool = Some(shm::ShmPool::new(width, height, self.current_id)?); self.shm_pool = Some(shm::ShmPool::new(width, height, self.current_id)?);
self.shm_pool.as_mut().unwrap().write(&vec![0xffff0000; width * height], 0); self.shm_pool.as_mut().unwrap().write(&vec![0xffff0000; width * height], 0);
self.shm_pool.as_mut().unwrap().rectangle(50, 50, 50, 50, 0xff00ff00); self.shm_pool.as_mut().unwrap().rectangle(50, 50, 50, 50, 0xff00ff00);
self.shm_pool.as_mut().unwrap().circle(200, 200, 50, 0xff0000ff); self.shm_pool.as_mut().unwrap().shitty_circle(200, 200, 20, 0xff0000ff);
self.shm_pool.as_mut().unwrap().rounded_rectangle(400, 200, 200, 100, 14, 0xffffff00); self.shm_pool.as_mut().unwrap().rounded_rectangle(400, 200, 200, 100, 14, 0xffffff00);
let object = self.shm_id.ok_or(UnsetErr("shm_id".to_string()))?; let object = self.shm_id.ok_or(UnsetErr("shm_id".to_string()))?;