extern crate num_traits;
extern crate specs;
extern crate specs_bundler;
extern crate specs_camera;
extern crate specs_transform;
use num_traits::{Float, FromPrimitive};
use specs::{DispatcherBuilder, Join, System, World, WriteStorage};
use specs_bundler::SpecsBundler;
use specs_camera::{
Camera2D, Camera2DBundle, Camera2DSystem, Camera3D, Camera3DBundle, Camera3DSystem,
CameraBundle, CameraSystem,
};
use specs_transform::{
LocalTransform2D, LocalTransform3D, Transform2D, Transform2DBundle, Transform3D,
Transform3DBundle, TransformBundle,
};
use std::marker::PhantomData;
#[derive(Default)]
pub struct Test2DSystem<T>(PhantomData<T>);
impl<'a, T> System<'a> for Test2DSystem<T>
where
T: 'static + Sync + Send + Float + FromPrimitive,
{
type SystemData = (WriteStorage<'a, LocalTransform2D<T>>,);
fn run(&mut self, mut locals_2d: Self::SystemData) {
for local in (&mut locals_2d.0).join() {
local.position[0] = T::from_f32(1.0).unwrap();
local.position[1] = T::from_f32(1.0).unwrap();
}
}
}
#[derive(Default)]
pub struct Test3DSystem<T>(PhantomData<T>);
impl<'a, T> System<'a> for Test3DSystem<T>
where
T: 'static + Sync + Send + Float + FromPrimitive,
{
type SystemData = (WriteStorage<'a, LocalTransform3D<T>>,);
fn run(&mut self, mut locals_3d: Self::SystemData) {
for local in (&mut locals_3d.0).join() {
local.position[0] = T::from_f32(1.0).unwrap();
local.position[1] = T::from_f32(1.0).unwrap();
local.position[2] = T::from_f32(1.0).unwrap();
}
}
}
#[derive(Default)]
pub struct TestSystem<T>(PhantomData<T>);
impl<'a, T> System<'a> for TestSystem<T>
where
T: 'static + Sync + Send + Float + FromPrimitive,
{
type SystemData = (
WriteStorage<'a, LocalTransform2D<T>>,
WriteStorage<'a, LocalTransform3D<T>>,
);
fn run(&mut self, (mut locals_2d, mut locals_3d): Self::SystemData) {
for local in (&mut locals_2d).join() {
local.position[0] = T::from_f32(1.0).unwrap();
local.position[1] = T::from_f32(1.0).unwrap();
}
for local in (&mut locals_3d).join() {
local.position[0] = T::from_f32(1.0).unwrap();
local.position[1] = T::from_f32(1.0).unwrap();
local.position[2] = T::from_f32(1.0).unwrap();
}
}
}
#[test]
fn test_camera_2d() {
let mut world = World::new();
let mut dispatcher = SpecsBundler::new(&mut world, DispatcherBuilder::new())
.bundle(Transform2DBundle::<f32>::new())
.unwrap()
.bundle(Camera2DBundle::<f32>::new(1024_usize, 768_usize))
.unwrap()
.with(
Test2DSystem::<f32>::default(),
"test_system",
&[Camera2DSystem::<f32>::name()],
)
.build();
world
.create_entity()
.with(LocalTransform2D::<f32>::new())
.with(Transform2D::<f32>::new())
.with(Camera2D::<f32>::new())
.build();
dispatcher.dispatch(&mut world.res); dispatcher.dispatch(&mut world.res);
let globals = world.read_storage::<Camera2D<f32>>();
for global in (&globals).join() {
assert_eq!(global.view(), &[1.0, 0.0, 0.0, 1.0, -1.0, -1.0]);
assert_eq!(global.projection(), &[0.375, 0.0, 0.0, 0.5, 0.0, 0.0]);
}
}
#[test]
fn test_camera_3d() {
let mut world = World::new();
let mut dispatcher = SpecsBundler::new(&mut world, DispatcherBuilder::new())
.bundle(Transform3DBundle::<f32>::new())
.unwrap()
.bundle(Camera3DBundle::<f32>::new(1024_usize, 768_usize))
.unwrap()
.with(
Test3DSystem::<f32>::default(),
"test_system",
&[Camera3DSystem::<f32>::name()],
)
.build();
world
.create_entity()
.with(LocalTransform3D::<f32>::new())
.with(Transform3D::<f32>::new())
.with(Camera3D::<f32>::new())
.build();
dispatcher.dispatch(&mut world.res); dispatcher.dispatch(&mut world.res);
let globals = world.read_storage::<Camera3D<f32>>();
for global in (&globals).join() {
assert_eq!(
global.view(),
&[1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, -1.0, -1.0, -1.0, 1.0]
);
assert_eq!(
global.projection(),
&[
-0.16869165,
0.0,
0.0,
0.0,
0.0,
-0.22492221,
0.0,
0.0,
0.0,
0.0,
-1.0,
-1.0,
0.0,
0.0,
-<f32 as Float>::epsilon() * 2.0,
0.0
]
);
}
}
#[test]
fn test_camera_2d_transform_3d() {
let mut world = World::new();
let mut dispatcher = SpecsBundler::new(&mut world, DispatcherBuilder::new())
.bundle(TransformBundle::<f32>::new())
.unwrap()
.bundle(CameraBundle::<f32>::new(1024_usize, 768_usize))
.unwrap()
.with(
TestSystem::<f32>::default(),
"test_system",
&[CameraSystem::<f32>::name()],
)
.build();
world
.create_entity()
.with(LocalTransform3D::<f32>::new())
.with(Transform3D::<f32>::new())
.with(Camera2D::<f32>::new())
.build();
dispatcher.dispatch(&mut world.res); dispatcher.dispatch(&mut world.res);
let globals = world.read_storage::<Camera2D<f32>>();
for global in (&globals).join() {
assert_eq!(global.view(), &[1.0, 0.0, 0.0, 1.0, -1.0, -1.0]);
assert_eq!(global.projection(), &[0.375, 0.0, 0.0, 0.5, 0.0, 0.0]);
}
}
#[test]
fn test_camera_3d_transform_2d() {
let mut world = World::new();
let mut dispatcher = SpecsBundler::new(&mut world, DispatcherBuilder::new())
.bundle(TransformBundle::<f32>::new())
.unwrap()
.bundle(CameraBundle::<f32>::new(1024_usize, 768_usize))
.unwrap()
.with(
TestSystem::<f32>::default(),
"test_system",
&[CameraSystem::<f32>::name()],
)
.build();
world
.create_entity()
.with(LocalTransform2D::<f32>::new())
.with(Transform2D::<f32>::new())
.with(Camera3D::<f32>::new())
.build();
dispatcher.dispatch(&mut world.res); dispatcher.dispatch(&mut world.res);
let globals = world.read_storage::<Camera3D<f32>>();
for global in (&globals).join() {
assert_eq!(
global.view(),
&[1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, -1.0, -1.0, 0.0, 1.0]
);
assert_eq!(
global.projection(),
&[
-0.16869165,
0.0,
0.0,
0.0,
0.0,
-0.22492221,
0.0,
0.0,
0.0,
0.0,
-1.0,
-1.0,
0.0,
0.0,
-<f32 as Float>::epsilon() * 2.0,
0.0
]
);
}
}