Replace line_drawing with clipline (#381)

* Replaced `line_drawing` with `clipline`

* Update `clipline` to 0.1.1
This commit is contained in:
Nurzhan Sakén 2023-10-31 04:36:32 +04:00 committed by GitHub
parent 763a7a9b41
commit 5461133a63
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 26 additions and 34 deletions

19
Cargo.lock generated
View file

@ -384,6 +384,12 @@ version = "1.0.10"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "75476fe966a8af7c0ceae2a3e514afa87d4451741fcdfab8bfaa07ad301842ec" checksum = "75476fe966a8af7c0ceae2a3e514afa87d4451741fcdfab8bfaa07ad301842ec"
[[package]]
name = "clipline"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "261dc8d360e39175d787ffc8ee5885da0ec7fe167171d574f6857772a348c344"
[[package]] [[package]]
name = "cmake" name = "cmake"
version = "0.1.50" version = "0.1.50"
@ -480,10 +486,10 @@ name = "conway"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"byteorder", "byteorder",
"clipline",
"env_logger", "env_logger",
"error-iter", "error-iter",
"getrandom", "getrandom",
"line_drawing",
"log", "log",
"pixels", "pixels",
"randomize", "randomize",
@ -1751,15 +1757,6 @@ dependencies = [
"pkg-config", "pkg-config",
] ]
[[package]]
name = "line_drawing"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d1478a313008a3e6c8149995e90a99ee9094034b5c5c3da1eeb81183cb61d1d"
dependencies = [
"num-traits",
]
[[package]] [[package]]
name = "linux-raw-sys" name = "linux-raw-sys"
version = "0.4.10" version = "0.4.10"
@ -2745,8 +2742,8 @@ name = "simple-invaders"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"byteorder", "byteorder",
"clipline",
"getrandom", "getrandom",
"line_drawing",
"pcx", "pcx",
"randomize", "randomize",
] ]

View file

@ -14,7 +14,7 @@ byteorder = "1"
env_logger = "0.10" env_logger = "0.10"
error-iter = "0.4" error-iter = "0.4"
getrandom = "0.2" getrandom = "0.2"
line_drawing = "1" clipline = "0.1.1"
log = "0.4" log = "0.4"
pixels = { path = "../.." } pixels = { path = "../.." }
randomize = "3" randomize = "3"

View file

@ -318,19 +318,16 @@ impl ConwayGrid {
} }
} }
fn set_line(&mut self, x0: isize, y0: isize, x1: isize, y1: isize, alive: bool) { fn set_line(&mut self, x0: isize, y0: isize, x1: isize, y1: isize, alive: bool) -> Option<()> {
// probably should do sutherland-hodgeman if this were more serious. // possible to optimize by matching on Clipline and iterating over its arms
// instead just clamp the start pos, and draw until moving towards the for (x, y) in clipline::Clipline::new(
// end pos takes us out of bounds. ((x0, y0), (x1, y1)),
let x0 = x0.clamp(0, self.width as isize); ((0, 0), (self.width as isize - 1, self.height as isize - 1)),
let y0 = y0.clamp(0, self.height as isize); )? {
for (x, y) in line_drawing::Bresenham::new((x0, y0), (x1, y1)) { let (x, y) = (x as usize, y as usize);
if let Some(i) = self.grid_idx(x, y) { self.cells[x + y * self.width].set_alive(alive);
self.cells[i].set_alive(alive);
} else {
break;
}
} }
Some(())
} }
fn grid_idx<I: std::convert::TryInto<usize>>(&self, x: I, y: I) -> Option<usize> { fn grid_idx<I: std::convert::TryInto<usize>>(&self, x: I, y: I) -> Option<usize> {

View file

@ -5,7 +5,7 @@ authors = ["Jay Oster <jay@kodewerx.org>"]
edition = "2018" edition = "2018"
[dependencies] [dependencies]
line_drawing = "1.0" clipline = "0.1.1"
pcx = "0.2" pcx = "0.2"
randomize = "3.0" randomize = "3.0"

View file

@ -3,9 +3,7 @@ use crate::TIME_STEP;
use crate::{Point, HEIGHT, WIDTH}; use crate::{Point, HEIGHT, WIDTH};
use alloc::rc::Rc; use alloc::rc::Rc;
use alloc::vec::Vec; use alloc::vec::Vec;
use core::cmp::min;
use core::time::Duration; use core::time::Duration;
use line_drawing::Bresenham;
// This is the type stored in the `Assets` hash map // This is the type stored in the `Assets` hash map
pub(crate) type CachedSprite = (usize, usize, Rc<[u8]>); pub(crate) type CachedSprite = (usize, usize, Rc<[u8]>);
@ -215,17 +213,17 @@ where
} }
/// Draw a line to the pixel buffer using Bresenham's algorithm. /// Draw a line to the pixel buffer using Bresenham's algorithm.
pub(crate) fn line(screen: &mut [u8], p1: &Point, p2: &Point, color: [u8; 4]) { pub(crate) fn line(screen: &mut [u8], p1: &Point, p2: &Point, color: [u8; 4]) -> Option<()> {
let p1 = (p1.x as i64, p1.y as i64); let p1 = (p1.x as isize, p1.y as isize);
let p2 = (p2.x as i64, p2.y as i64); let p2 = (p2.x as isize, p2.y as isize);
let clip_max = (WIDTH as isize - 1, HEIGHT as isize - 1);
for (x, y) in Bresenham::new(p1, p2) { for (x, y) in clipline::Clipline::new((p1, p2), ((0, 0), clip_max))? {
let x = min(x as usize, WIDTH - 1); let (x, y) = (x as usize, y as usize);
let y = min(y as usize, HEIGHT - 1);
let i = x * 4 + y * WIDTH * 4; let i = x * 4 + y * WIDTH * 4;
screen[i..i + 4].copy_from_slice(&color); screen[i..i + 4].copy_from_slice(&color);
} }
Some(())
} }
/// Draw a rectangle to the pixel buffer using two points in opposite corners. /// Draw a rectangle to the pixel buffer using two points in opposite corners.