Skip to content

Commit

Permalink
bug testing
Browse files Browse the repository at this point in the history
  • Loading branch information
jaynus committed Oct 24, 2019
1 parent e7b632a commit 34e0112
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 17 deletions.
71 changes: 57 additions & 14 deletions amethyst_tiles/src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,20 +336,63 @@ mod tests {
use amethyst_core::math::Point3;
use rayon::prelude::*;

#[derive(Default, Clone)]
struct TestTile;
#[derive(Clone, Debug)]
struct TestTile {
point: Point3<u32>,
}
impl Default for TestTile {
fn default() -> Self {
Self {
point: Point3::new(0, 0, 0),
}
}
}
impl Tile for TestTile {
fn sprite(&self, _: Point3<u32>, _: &World) -> Option<usize> {
None
}
}

use std::cell::RefCell;

pub fn test_single_map<E: CoordinateEncoder>(dimensions: Vector3<u32>) {
let map = TileMap::<TestTile, E>::new(dimensions, Vector3::new(10, 10, 1), None);
struct UnsafeWrapper<E: CoordinateEncoder> {
ptr: *mut TileMap<TestTile, E>,
}
impl<E: CoordinateEncoder> UnsafeWrapper<E> {
pub fn new(map: &mut TileMap<TestTile, E>) -> Self {
Self {
ptr: map as *mut TileMap<TestTile, E>,
}
}
pub fn get(&self) -> &TileMap<TestTile, E> {
unsafe { &*self.ptr }
}
pub fn get_mut(&self) -> &mut TileMap<TestTile, E> {
unsafe { &mut *self.ptr }
}
}
unsafe impl<E: CoordinateEncoder> Send for UnsafeWrapper<E> {}
unsafe impl<E: CoordinateEncoder> Sync for UnsafeWrapper<E> {}

let mut inner = TileMap::<TestTile, E>::new(dimensions, Vector3::new(10, 10, 1), None);
let mut map = UnsafeWrapper::new(&mut inner);

(0..dimensions.x).into_par_iter().for_each(|x| {
(0..dimensions.y).into_par_iter().for_each(|y| {
for z in 0..dimensions.z {
let point = Point3::new(x, y, z);

*map.get_mut().get_mut(&point).unwrap() = TestTile { point };
}
});
});

(0..dimensions.x).into_par_iter().for_each(|x| {
(0..dimensions.y).into_par_iter().for_each(|y| {
for z in 0..dimensions.z {
let _ = map.get(&Point3::new(x, y, z)).unwrap();
let point = Point3::new(x, y, z);
assert_eq!(map.get().get(&Point3::new(x, y, z)).unwrap().point, point);
}
});
});
Expand All @@ -359,21 +402,21 @@ mod tests {
pub fn asymmetric_maps() {
let test_dimensions = [
Vector3::new(10, 58, 54),
Vector3::new(66, 5, 200),
Vector3::new(199, 100, 1),
Vector3::new(5, 423, 6),
Vector3::new(15, 23, 1),
Vector3::new(20, 12, 12),
Vector3::new(48, 48, 12),
Vector3::new(12, 55, 12),
Vector3::new(26, 25, 1),
Vector3::new(1, 2, 5),
Vector3::new(66, 5, 20),
//Vector3::new(199, 100, 1),
Vector3::new(5, 55, 6),
//Vector3::new(15, 23, 1),
//Vector3::new(20, 12, 12),
//Vector3::new(48, 48, 12),
//Vector3::new(12, 55, 12),
//Vector3::new(26, 25, 1),
//Vector3::new(1, 2, 5),
];

test_dimensions.par_iter().for_each(|dimensions| {
test_single_map::<MortonEncoder>(*dimensions);
test_single_map::<MortonEncoder2D>(*dimensions);
test_single_map::<FlatEncoder>(*dimensions);
//test_single_map::<FlatEncoder>(*dimensions);
});
}

Expand Down
1 change: 1 addition & 0 deletions amethyst_tiles/src/morton/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ pub fn morton_decode_lut(morton: u32) -> (u32, u32, u32) {
#[inline]
pub fn morton_encode_intr_3d(x: u32, y: u32, z: u32) -> u32 {
use bitintr::Pdep;

z.pdep(0x2492_4924) | y.pdep(0x1249_2492) | x.pdep(0x0924_9249)
}

Expand Down
6 changes: 3 additions & 3 deletions examples/tiles/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use amethyst::{
types::DefaultBackend,
RenderDebugLines, RenderFlat2D, RenderToWindow, RenderingBundle, Texture,
},
tiles::{RenderTiles2D, Tile, TileMap},
tiles::{MortonEncoder, RenderTiles2D, Tile, TileMap},
utils::application_root_dir,
window::ScreenDimensions,
winit,
Expand Down Expand Up @@ -376,7 +376,7 @@ impl SimpleState for Example {
.with(DebugLinesComponent::with_capacity(1))
.build();

let map = TileMap::<ExampleTile>::new(
let map = TileMap::<ExampleTile, MortonEncoder>::new(
Vector3::new(48, 48, 1),
Vector3::new(20, 20, 1),
Some(map_sprite_sheet_handle),
Expand Down Expand Up @@ -450,7 +450,7 @@ fn main() -> amethyst::Result<()> {
)
.with_plugin(RenderDebugLines::default())
.with_plugin(RenderFlat2D::default())
.with_plugin(RenderTiles2D::<ExampleTile>::default()),
.with_plugin(RenderTiles2D::<ExampleTile, MortonEncoder>::default()),
)?;

let mut game = Application::build(assets_directory, Example)?.build(game_data)?;
Expand Down

0 comments on commit 34e0112

Please sign in to comment.