# HG changeset patch # User unc0rr # Date 1540850778 -3600 # Node ID 4b40bdd214dfc810d923cda0d58e2148feb75d90 # Parent 259175ab7e8c9c1d430bcc74519208b84723f3f3 Use next_power_of_two() just like hedgewars engine does, expose original and real dimensions diff -r 259175ab7e8c -r 4b40bdd214df rust/land2d/src/lib.rs --- a/rust/land2d/src/lib.rs Mon Oct 29 23:40:17 2018 +0300 +++ b/rust/land2d/src/lib.rs Mon Oct 29 23:06:18 2018 +0100 @@ -2,25 +2,31 @@ extern crate vec2d; use std::cmp; -use std::ops; use integral_geometry::{ArcPoints, EquidistantPoints, LinePoints, Point}; pub struct Land2D { pixels: vec2d::Vec2D, + play_width: usize, + play_height: usize, width_mask: usize, height_mask: usize, } impl Land2D { - pub fn new(width: usize, height: usize, fill_value: T) -> Self { - assert!(width.is_power_of_two()); - assert!(height.is_power_of_two()); + pub fn new(play_width: usize, play_height: usize, fill_value: T) -> Self { + let real_width = play_width.next_power_of_two(); + let real_height = play_height.next_power_of_two(); + + assert!(real_width > 0); + assert!(real_height > 0); Self { - pixels: vec2d::Vec2D::new(width, height, fill_value), - width_mask: !(width - 1), - height_mask: !(height - 1), + pixels: vec2d::Vec2D::new(real_width, real_height, fill_value), + play_width, + play_height, + width_mask: !(real_width - 1), + height_mask: !(real_height - 1), } } @@ -35,6 +41,16 @@ } #[inline] + pub fn play_width(&self) -> usize { + self.play_width + } + + #[inline] + pub fn play_height(&self) -> usize { + self.play_height + } + + #[inline] pub fn is_valid_x(&self, x: i32) -> bool { (x as usize & self.width_mask) == 0 } @@ -240,7 +256,12 @@ #[test] fn basics() { - let l: Land2D = Land2D::new(32, 64, 0); + let l: Land2D = Land2D::new(30, 50, 0); + + assert_eq!(l.play_width(), 30); + assert_eq!(l.play_height(), 50); + assert_eq!(l.width(), 32); + assert_eq!(l.height(), 64); assert!(l.is_valid_coordinate(0, 0)); assert!(!l.is_valid_coordinate(-1, -1));