author | unC0Rr |
Wed, 01 Feb 2023 11:19:45 +0100 | |
branch | transitional_engine |
changeset 15914 | c571d4b8879c |
parent 15913 | c5684cc62de8 |
permissions | -rw-r--r-- |
15901
f39f0f614dbf
Use LandPixels array allocate in hwengine-future library
unC0Rr
parents:
15893
diff
changeset
|
1 |
use std::{cmp, ops::Index, ops::IndexMut}; |
15904 | 2 |
use vec2d::Vec2D; |
15828 | 3 |
use integral_geometry::{ArcPoints, EquidistantPoints, Line, Point, PotSize, Rect, Size, SizeMask}; |
13938
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13936
diff
changeset
|
4 |
|
15912 | 5 |
#[derive(Debug)] |
13917 | 6 |
pub struct Land2D<T> { |
7 |
pixels: vec2d::Vec2D<T>, |
|
14137
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14135
diff
changeset
|
8 |
play_box: Rect, |
14078 | 9 |
mask: SizeMask, |
13917 | 10 |
} |
11 |
||
15912 | 12 |
impl<T: Copy + PartialEq + Default> Land2D<T> { |
15905
022ec6b916b7
Split generation and painting phases, paint by old engine, use template filters
unC0Rr
parents:
15904
diff
changeset
|
13 |
pub fn new(play_size: &Size, fill_value: T) -> Self { |
14052 | 14 |
let real_size = play_size.next_power_of_two(); |
14078 | 15 |
let top_left = Point::new( |
15828 | 16 |
((real_size.width() - play_size.width) / 2) as i32, |
17 |
(real_size.height() - play_size.height) as i32, |
|
14078 | 18 |
); |
15905
022ec6b916b7
Split generation and painting phases, paint by old engine, use template filters
unC0Rr
parents:
15904
diff
changeset
|
19 |
let play_box = Rect::from_size(top_left, *play_size); |
13917 | 20 |
Self { |
14078 | 21 |
play_box, |
15913 | 22 |
pixels: vec2d::Vec2D::new(&real_size.size(), fill_value), |
14078 | 23 |
mask: real_size.to_mask(), |
13917 | 24 |
} |
25 |
} |
|
26 |
||
14121 | 27 |
pub fn raw_pixels(&self) -> &[T] { |
14160 | 28 |
&self.pixels.as_slice() |
14121 | 29 |
} |
30 |
||
14702 | 31 |
pub fn raw_pixel_bytes(&self) -> &[u8] { |
15828 | 32 |
unsafe { self.pixels.as_bytes() } |
14702 | 33 |
} |
34 |
||
13917 | 35 |
#[inline] |
13924 | 36 |
pub fn width(&self) -> usize { |
37 |
self.pixels.width() |
|
38 |
} |
|
39 |
||
40 |
#[inline] |
|
41 |
pub fn height(&self) -> usize { |
|
42 |
self.pixels.height() |
|
43 |
} |
|
44 |
||
45 |
#[inline] |
|
15828 | 46 |
pub fn size(&self) -> PotSize { |
47 |
self.mask.to_size() |
|
14052 | 48 |
} |
49 |
||
50 |
#[inline] |
|
14050
4b40bdd214df
Use next_power_of_two() just like hedgewars engine does, expose original and real dimensions
unc0rr
parents:
13951
diff
changeset
|
51 |
pub fn play_width(&self) -> usize { |
14135
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14121
diff
changeset
|
52 |
self.play_box.width() |
14050
4b40bdd214df
Use next_power_of_two() just like hedgewars engine does, expose original and real dimensions
unc0rr
parents:
13951
diff
changeset
|
53 |
} |
4b40bdd214df
Use next_power_of_two() just like hedgewars engine does, expose original and real dimensions
unc0rr
parents:
13951
diff
changeset
|
54 |
|
4b40bdd214df
Use next_power_of_two() just like hedgewars engine does, expose original and real dimensions
unc0rr
parents:
13951
diff
changeset
|
55 |
#[inline] |
4b40bdd214df
Use next_power_of_two() just like hedgewars engine does, expose original and real dimensions
unc0rr
parents:
13951
diff
changeset
|
56 |
pub fn play_height(&self) -> usize { |
14135
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14121
diff
changeset
|
57 |
self.play_box.height() |
14052 | 58 |
} |
59 |
||
60 |
#[inline] |
|
61 |
pub fn play_size(&self) -> Size { |
|
14135
7f5a591e1c43
separate rectangle types based on right/bottom edge inclusivity
alfadur
parents:
14121
diff
changeset
|
62 |
self.play_box.size() |
14050
4b40bdd214df
Use next_power_of_two() just like hedgewars engine does, expose original and real dimensions
unc0rr
parents:
13951
diff
changeset
|
63 |
} |
4b40bdd214df
Use next_power_of_two() just like hedgewars engine does, expose original and real dimensions
unc0rr
parents:
13951
diff
changeset
|
64 |
|
4b40bdd214df
Use next_power_of_two() just like hedgewars engine does, expose original and real dimensions
unc0rr
parents:
13951
diff
changeset
|
65 |
#[inline] |
14137
3119d665d3c6
collapse rectangle types back together with consistent usage of size
alfadur
parents:
14135
diff
changeset
|
66 |
pub fn play_box(&self) -> Rect { |
14078 | 67 |
self.play_box |
68 |
} |
|
69 |
||
70 |
#[inline] |
|
13931 | 71 |
pub fn is_valid_x(&self, x: i32) -> bool { |
14032 | 72 |
self.mask.contains_x(x as usize) |
13931 | 73 |
} |
74 |
||
75 |
#[inline] |
|
76 |
pub fn is_valid_y(&self, y: i32) -> bool { |
|
14032 | 77 |
self.mask.contains_y(y as usize) |
13931 | 78 |
} |
79 |
||
80 |
#[inline] |
|
13924 | 81 |
pub fn is_valid_coordinate(&self, x: i32, y: i32) -> bool { |
13931 | 82 |
self.is_valid_x(x) && self.is_valid_y(y) |
13917 | 83 |
} |
84 |
||
85 |
#[inline] |
|
14170 | 86 |
pub fn rows(&self) -> impl DoubleEndedIterator<Item = &[T]> { |
14030 | 87 |
self.pixels.rows() |
88 |
} |
|
89 |
||
90 |
#[inline] |
|
13940
1c30793b1cea
put back land2d.map accidentally replaced by testing code
alfadur
parents:
13938
diff
changeset
|
91 |
pub fn map<U: Default, F: FnOnce(&mut T) -> U>(&mut self, y: i32, x: i32, f: F) -> U { |
13917 | 92 |
if self.is_valid_coordinate(x, y) { |
13934
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13931
diff
changeset
|
93 |
unsafe { |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13931
diff
changeset
|
94 |
// hey, I just checked that coordinates are valid! |
13940
1c30793b1cea
put back land2d.map accidentally replaced by testing code
alfadur
parents:
13938
diff
changeset
|
95 |
f(self.pixels.get_unchecked_mut(y as usize, x as usize)) |
13931 | 96 |
} |
13934
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13931
diff
changeset
|
97 |
} else { |
13940
1c30793b1cea
put back land2d.map accidentally replaced by testing code
alfadur
parents:
13938
diff
changeset
|
98 |
U::default() |
13917 | 99 |
} |
100 |
} |
|
101 |
||
13944
4162ea9ae333
Use integral-geometry iterators to implement Land2D::draw_thick_line, remove no longer unused functions from Land2D
unc0rr
parents:
13943
diff
changeset
|
102 |
#[inline] |
15912 | 103 |
pub fn get(&self, y: i32, x: i32) -> T { |
104 |
if self.is_valid_coordinate(x, y) { |
|
105 |
unsafe { |
|
106 |
// hey, I just checked that coordinates are valid! |
|
107 |
*self.pixels.get_unchecked(y as usize, x as usize) |
|
108 |
} |
|
109 |
} else { |
|
110 |
T::default() |
|
111 |
} |
|
112 |
} |
|
113 |
||
114 |
#[inline] |
|
13944
4162ea9ae333
Use integral-geometry iterators to implement Land2D::draw_thick_line, remove no longer unused functions from Land2D
unc0rr
parents:
13943
diff
changeset
|
115 |
pub fn map_point<U: Default, F: FnOnce(&mut T) -> U>(&mut self, point: Point, f: F) -> U { |
4162ea9ae333
Use integral-geometry iterators to implement Land2D::draw_thick_line, remove no longer unused functions from Land2D
unc0rr
parents:
13943
diff
changeset
|
116 |
self.map(point.y, point.x, f) |
13934
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13931
diff
changeset
|
117 |
} |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13931
diff
changeset
|
118 |
|
13938
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13936
diff
changeset
|
119 |
pub fn fill_from_iter<I>(&mut self, i: I, value: T) -> usize |
13943
a325ed57ebfe
Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents:
13940
diff
changeset
|
120 |
where |
a325ed57ebfe
Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents:
13940
diff
changeset
|
121 |
I: std::iter::Iterator<Item = Point>, |
13938
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13936
diff
changeset
|
122 |
{ |
13943
a325ed57ebfe
Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents:
13940
diff
changeset
|
123 |
i.map(|p| { |
a325ed57ebfe
Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents:
13940
diff
changeset
|
124 |
self.map(p.y, p.x, |v| { |
a325ed57ebfe
Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents:
13940
diff
changeset
|
125 |
*v = value; |
a325ed57ebfe
Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents:
13940
diff
changeset
|
126 |
1 |
a325ed57ebfe
Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents:
13940
diff
changeset
|
127 |
}) |
15828 | 128 |
}) |
129 |
.count() |
|
13938
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13936
diff
changeset
|
130 |
} |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13936
diff
changeset
|
131 |
|
14076
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14052
diff
changeset
|
132 |
pub fn draw_line(&mut self, line: Line, value: T) -> usize { |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14052
diff
changeset
|
133 |
self.fill_from_iter(line.into_iter(), value) |
13917 | 134 |
} |
13924 | 135 |
|
13948 | 136 |
pub fn fill(&mut self, start_point: Point, border_value: T, fill_value: T) { |
14150 | 137 |
assert!(self.is_valid_coordinate(start_point.x - 1, start_point.y)); |
138 |
assert!(self.is_valid_coordinate(start_point.x, start_point.y)); |
|
13924 | 139 |
|
14148 | 140 |
let mask = self.mask; |
141 |
let width = self.width(); |
|
142 |
||
13924 | 143 |
let mut stack: Vec<(usize, usize, usize, isize)> = Vec::new(); |
14148 | 144 |
fn push( |
145 |
mask: SizeMask, |
|
13924 | 146 |
stack: &mut Vec<(usize, usize, usize, isize)>, |
147 |
xl: usize, |
|
148 |
xr: usize, |
|
149 |
y: usize, |
|
150 |
dir: isize, |
|
151 |
) { |
|
152 |
let yd = y as isize + dir; |
|
14148 | 153 |
if mask.contains_y(yd as usize) { |
13924 | 154 |
stack.push((xl, xr, yd as usize, dir)); |
155 |
} |
|
15893
5b3beb90e1a6
Implement generation of c header from rust interface, adapt qmlfrontend
unC0Rr
parents:
15828
diff
changeset
|
156 |
} |
13924 | 157 |
|
13948 | 158 |
let start_x_l = (start_point.x - 1) as usize; |
159 |
let start_x_r = start_point.x as usize; |
|
14148 | 160 |
for dir in [-1, 1].iter().cloned() { |
15828 | 161 |
push( |
162 |
mask, |
|
163 |
&mut stack, |
|
164 |
start_x_l, |
|
165 |
start_x_r, |
|
166 |
start_point.y as usize, |
|
167 |
dir, |
|
168 |
); |
|
14148 | 169 |
} |
13924 | 170 |
|
14148 | 171 |
while let Some((mut xl, mut xr, y, dir)) = stack.pop() { |
172 |
let row = &mut self.pixels[y][..]; |
|
15828 | 173 |
while xl > 0 && row[xl] != border_value && row[xl] != fill_value { |
13951 | 174 |
xl -= 1; |
175 |
} |
|
13924 | 176 |
|
15828 | 177 |
while xr < width - 1 && row[xr] != border_value && row[xr] != fill_value { |
13951 | 178 |
xr += 1; |
179 |
} |
|
13924 | 180 |
|
13951 | 181 |
while xl < xr { |
15828 | 182 |
while xl <= xr && (row[xl] == border_value || row[xl] == fill_value) { |
13951 | 183 |
xl += 1; |
184 |
} |
|
13924 | 185 |
|
14148 | 186 |
let x = xl; |
13924 | 187 |
|
15828 | 188 |
while xl <= xr && row[xl] != border_value && row[xl] != fill_value { |
14148 | 189 |
row[xl] = fill_value; |
13951 | 190 |
xl += 1; |
191 |
} |
|
13924 | 192 |
|
13951 | 193 |
if x < xl { |
14148 | 194 |
push(mask, &mut stack, x, xl - 1, y, dir); |
195 |
push(mask, &mut stack, x, xl - 1, y, -dir); |
|
13924 | 196 |
} |
197 |
} |
|
198 |
} |
|
199 |
} |
|
13931 | 200 |
|
201 |
#[inline] |
|
202 |
fn fill_circle_line<F: Fn(&mut T) -> usize>( |
|
203 |
&mut self, |
|
204 |
y: i32, |
|
205 |
x_from: i32, |
|
206 |
x_to: i32, |
|
207 |
f: &F, |
|
208 |
) -> usize { |
|
209 |
let mut result = 0; |
|
210 |
||
211 |
if self.is_valid_y(y) { |
|
212 |
for i in cmp::min(x_from, 0) as usize..cmp::max(x_to as usize, self.width() - 1) { |
|
13934
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13931
diff
changeset
|
213 |
unsafe { |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13931
diff
changeset
|
214 |
// coordinates are valid at this point |
13931 | 215 |
result += f(self.pixels.get_unchecked_mut(y as usize, i)); |
216 |
} |
|
217 |
} |
|
218 |
} |
|
219 |
||
220 |
result |
|
221 |
} |
|
222 |
||
223 |
#[inline] |
|
224 |
fn fill_circle_lines<F: Fn(&mut T) -> usize>( |
|
225 |
&mut self, |
|
226 |
x: i32, |
|
227 |
y: i32, |
|
228 |
dx: i32, |
|
229 |
dy: i32, |
|
230 |
f: &F, |
|
231 |
) -> usize { |
|
232 |
self.fill_circle_line(y + dy, x - dx, x + dx, f) |
|
233 |
+ self.fill_circle_line(y - dy, x - dx, x + dx, f) |
|
234 |
+ self.fill_circle_line(y + dx, x - dy, x + dy, f) |
|
235 |
+ self.fill_circle_line(y - dx, x - dy, x + dy, f) |
|
236 |
} |
|
237 |
||
238 |
pub fn change_round<F: Fn(&mut T) -> usize>( |
|
239 |
&mut self, |
|
240 |
x: i32, |
|
241 |
y: i32, |
|
242 |
radius: i32, |
|
243 |
f: F, |
|
244 |
) -> usize { |
|
13943
a325ed57ebfe
Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents:
13940
diff
changeset
|
245 |
ArcPoints::new(radius) |
a325ed57ebfe
Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents:
13940
diff
changeset
|
246 |
.map(&mut |p: Point| self.fill_circle_lines(x, y, p.x, p.y, &f)) |
a325ed57ebfe
Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents:
13940
diff
changeset
|
247 |
.sum() |
13934
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13931
diff
changeset
|
248 |
} |
13931 | 249 |
|
14031 | 250 |
fn fill_row(&mut self, center: Point, offset: Point, value: T) -> usize { |
251 |
let row_index = center.y + offset.y; |
|
252 |
if self.is_valid_y(row_index) { |
|
253 |
let from_x = cmp::max(0, center.x - offset.x) as usize; |
|
254 |
let to_x = cmp::min(self.width() - 1, (center.x + offset.x) as usize); |
|
255 |
self.pixels[row_index as usize][from_x..=to_x] |
|
14078 | 256 |
.iter_mut() |
257 |
.for_each(|v| *v = value); |
|
14031 | 258 |
to_x - from_x + 1 |
259 |
} else { |
|
260 |
0 |
|
261 |
} |
|
262 |
} |
|
263 |
||
264 |
pub fn fill_circle(&mut self, center: Point, radius: i32, value: T) -> usize { |
|
14078 | 265 |
let transforms = [[0, 1, 1, 0], [0, 1, -1, 0], [1, 0, 0, 1], [1, 0, 0, -1]]; |
266 |
ArcPoints::new(radius) |
|
267 |
.map(|vector| { |
|
268 |
transforms |
|
269 |
.iter() |
|
270 |
.map(|m| self.fill_row(center, vector.transform(m), value)) |
|
271 |
.sum::<usize>() |
|
15828 | 272 |
}) |
273 |
.sum() |
|
14031 | 274 |
} |
275 |
||
14076
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14052
diff
changeset
|
276 |
pub fn draw_thick_line(&mut self, line: Line, radius: i32, value: T) -> usize { |
13944
4162ea9ae333
Use integral-geometry iterators to implement Land2D::draw_thick_line, remove no longer unused functions from Land2D
unc0rr
parents:
13943
diff
changeset
|
277 |
let mut result = 0; |
13931 | 278 |
|
13949
a1895019bb94
change draw_thick_line iteration order to benchmark winner
alfadur
parents:
13948
diff
changeset
|
279 |
for vector in ArcPoints::new(radius) { |
a1895019bb94
change draw_thick_line iteration order to benchmark winner
alfadur
parents:
13948
diff
changeset
|
280 |
for delta in EquidistantPoints::new(vector) { |
14076
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14052
diff
changeset
|
281 |
for point in line.into_iter() { |
13946 | 282 |
self.map_point(point + delta, |p| { |
283 |
if *p != value { |
|
284 |
*p = value; |
|
285 |
result += 1; |
|
286 |
} |
|
287 |
}) |
|
13944
4162ea9ae333
Use integral-geometry iterators to implement Land2D::draw_thick_line, remove no longer unused functions from Land2D
unc0rr
parents:
13943
diff
changeset
|
288 |
} |
13943
a325ed57ebfe
Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents:
13940
diff
changeset
|
289 |
} |
a325ed57ebfe
Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents:
13940
diff
changeset
|
290 |
} |
a325ed57ebfe
Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents:
13940
diff
changeset
|
291 |
|
13944
4162ea9ae333
Use integral-geometry iterators to implement Land2D::draw_thick_line, remove no longer unused functions from Land2D
unc0rr
parents:
13943
diff
changeset
|
292 |
result |
13934
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13931
diff
changeset
|
293 |
} |
13917 | 294 |
} |
295 |
||
14144 | 296 |
impl<T> Index<usize> for Land2D<T> { |
297 |
type Output = [T]; |
|
298 |
#[inline] |
|
299 |
fn index(&self, row: usize) -> &[T] { |
|
300 |
&self.pixels[row] |
|
301 |
} |
|
302 |
} |
|
303 |
||
15901
f39f0f614dbf
Use LandPixels array allocate in hwengine-future library
unC0Rr
parents:
15893
diff
changeset
|
304 |
impl<T> IndexMut<usize> for Land2D<T> { |
f39f0f614dbf
Use LandPixels array allocate in hwengine-future library
unC0Rr
parents:
15893
diff
changeset
|
305 |
#[inline] |
f39f0f614dbf
Use LandPixels array allocate in hwengine-future library
unC0Rr
parents:
15893
diff
changeset
|
306 |
fn index_mut(&mut self, row: usize) -> &mut [T] { |
f39f0f614dbf
Use LandPixels array allocate in hwengine-future library
unC0Rr
parents:
15893
diff
changeset
|
307 |
&mut self.pixels[row] |
f39f0f614dbf
Use LandPixels array allocate in hwengine-future library
unC0Rr
parents:
15893
diff
changeset
|
308 |
} |
f39f0f614dbf
Use LandPixels array allocate in hwengine-future library
unC0Rr
parents:
15893
diff
changeset
|
309 |
} |
f39f0f614dbf
Use LandPixels array allocate in hwengine-future library
unC0Rr
parents:
15893
diff
changeset
|
310 |
|
15904 | 311 |
impl<T> From<Vec2D<T>> for Land2D<T> { |
312 |
fn from(vec: Vec2D<T>) -> Self { |
|
313 |
let actual_size = vec.size(); |
|
314 |
let pot_size = actual_size.next_power_of_two(); |
|
315 |
||
316 |
assert_eq!(actual_size, pot_size.size()); |
|
317 |
||
318 |
let top_left = Point::new(0, 0); |
|
319 |
let play_box = Rect::from_size(top_left, actual_size); |
|
320 |
Self { |
|
321 |
play_box, |
|
322 |
pixels: vec, |
|
323 |
mask: pot_size.to_mask(), |
|
324 |
} |
|
325 |
} |
|
326 |
} |
|
327 |
||
13917 | 328 |
#[cfg(test)] |
329 |
mod tests { |
|
330 |
use super::*; |
|
331 |
||
332 |
#[test] |
|
333 |
fn basics() { |
|
14052 | 334 |
let l: Land2D<u8> = Land2D::new(Size::new(30, 50), 0); |
14050
4b40bdd214df
Use next_power_of_two() just like hedgewars engine does, expose original and real dimensions
unc0rr
parents:
13951
diff
changeset
|
335 |
|
4b40bdd214df
Use next_power_of_two() just like hedgewars engine does, expose original and real dimensions
unc0rr
parents:
13951
diff
changeset
|
336 |
assert_eq!(l.play_width(), 30); |
4b40bdd214df
Use next_power_of_two() just like hedgewars engine does, expose original and real dimensions
unc0rr
parents:
13951
diff
changeset
|
337 |
assert_eq!(l.play_height(), 50); |
4b40bdd214df
Use next_power_of_two() just like hedgewars engine does, expose original and real dimensions
unc0rr
parents:
13951
diff
changeset
|
338 |
assert_eq!(l.width(), 32); |
4b40bdd214df
Use next_power_of_two() just like hedgewars engine does, expose original and real dimensions
unc0rr
parents:
13951
diff
changeset
|
339 |
assert_eq!(l.height(), 64); |
13917 | 340 |
|
341 |
assert!(l.is_valid_coordinate(0, 0)); |
|
342 |
assert!(!l.is_valid_coordinate(-1, -1)); |
|
343 |
||
344 |
assert!(l.is_valid_coordinate(31, 63)); |
|
345 |
assert!(!l.is_valid_coordinate(32, 63)); |
|
346 |
assert!(!l.is_valid_coordinate(31, 64)); |
|
347 |
} |
|
348 |
||
13924 | 349 |
#[test] |
350 |
fn fill() { |
|
14032 | 351 |
let mut l: Land2D<u8> = Land2D::new(Size::square(128), 0); |
13924 | 352 |
|
14076
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14052
diff
changeset
|
353 |
l.draw_line(Line::new(Point::new(0, 0), Point::new(32, 96)), 1); |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14052
diff
changeset
|
354 |
l.draw_line(Line::new(Point::new(32, 96), Point::new(64, 32)), 1); |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14052
diff
changeset
|
355 |
l.draw_line(Line::new(Point::new(64, 32), Point::new(96, 80)), 1); |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14052
diff
changeset
|
356 |
l.draw_line(Line::new(Point::new(96, 80), Point::new(128, 0)), 1); |
13924 | 357 |
|
14076
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14052
diff
changeset
|
358 |
l.draw_line(Line::new(Point::new(0, 128), Point::new(64, 96)), 1); |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14052
diff
changeset
|
359 |
l.draw_line(Line::new(Point::new(128, 128), Point::new(64, 96)), 1); |
13924 | 360 |
|
13948 | 361 |
l.fill(Point::new(32, 32), 1, 2); |
362 |
l.fill(Point::new(16, 96), 1, 3); |
|
363 |
l.fill(Point::new(60, 100), 1, 4); |
|
13924 | 364 |
|
365 |
assert_eq!(l.pixels[0][0], 1); |
|
366 |
assert_eq!(l.pixels[96][64], 1); |
|
367 |
||
368 |
assert_eq!(l.pixels[40][32], 2); |
|
369 |
assert_eq!(l.pixels[40][96], 2); |
|
370 |
assert_eq!(l.pixels[5][0], 3); |
|
371 |
assert_eq!(l.pixels[120][0], 3); |
|
372 |
assert_eq!(l.pixels[5][127], 3); |
|
373 |
assert_eq!(l.pixels[120][127], 3); |
|
374 |
assert_eq!(l.pixels[35][64], 3); |
|
375 |
assert_eq!(l.pixels[120][20], 4); |
|
376 |
assert_eq!(l.pixels[120][100], 4); |
|
377 |
assert_eq!(l.pixels[100][64], 4); |
|
378 |
} |
|
13917 | 379 |
} |