Documentation for module 'NDK'

Former-commit-id: 63e1cac538c577a1f1aafa71fa7eef69a6d4daab [formerly b2d8769fd02a0e7d9c476d4ad7be1988a1fd6789] [formerly 636b5cb79bcb8da44d9aa45ba1023565bcf29f0d [formerly a2361ec2b8679d4d4ba096e543b5d4b91825dd62]]
Former-commit-id: d402d35477f9db0135c553d55c401939426bf62d [formerly 607336ea0f42731e4604f3a8c2df06f3aecfc401]
Former-commit-id: 69e23cd6c06723486de5e4641ce810012dac66da
This commit is contained in:
Gawaboumga
2016-08-21 13:48:52 +02:00
parent 42abd200be
commit 9eba331f34
75 changed files with 3374 additions and 112 deletions

View File

@@ -0,0 +1,22 @@
#include <NDK/Application.hpp>
#include <Catch/catch.hpp>
SCENARIO("Application", "[NDK][APPLICATION]")
{
GIVEN("An application")
{
Ndk::Application application;
application.AddWorld();
Nz::Window& window = application.AddWindow<Nz::Window>();
WHEN("We close the open window")
{
window.Close();
THEN("Application should close")
{
REQUIRE(!application.Run());
}
}
}
}

View File

@@ -0,0 +1,64 @@
#include <NDK/System.hpp>
#include <NDK/Components/NodeComponent.hpp>
#include <NDK/Components/VelocityComponent.hpp>
#include <NDK/World.hpp>
#include <Catch/catch.hpp>
namespace
{
class TestSystem : public Ndk::System<TestSystem>
{
public:
TestSystem()
{
Requires<Ndk::VelocityComponent>();
Excludes<Ndk::NodeComponent>();
}
~TestSystem() = default;
static Ndk::SystemIndex systemIndex;
private:
void OnUpdate(float elapsedTime) override
{
}
};
Ndk::SystemIndex TestSystem::systemIndex;
}
SCENARIO("BaseSystem", "[NDK][BASESYSTEM]")
{
GIVEN("Our TestSystem")
{
Ndk::World world;
Ndk::BaseSystem& system = world.AddSystem<TestSystem>();
REQUIRE(&system.GetWorld() == &world);
WHEN("We add an entity")
{
const Ndk::EntityHandle& entity = world.CreateEntity();
entity->AddComponent<Ndk::VelocityComponent>();
THEN("System should have it")
{
world.Update(1.f);
REQUIRE(system.HasEntity(entity));
}
}
WHEN("We add an entity with excluded component")
{
const Ndk::EntityHandle& entity = world.CreateEntity();
entity->AddComponent<Ndk::VelocityComponent>();
entity->AddComponent<Ndk::NodeComponent>();
THEN("System should not have it")
{
world.Update(1.f);
REQUIRE(!system.HasEntity(entity));
}
}
}
}

View File

@@ -0,0 +1,31 @@
#include <NDK/Component.hpp>
#include <Catch/catch.hpp>
namespace
{
class TestComponent : public Ndk::Component<TestComponent>
{
public:
static Ndk::ComponentIndex componentIndex;
};
Ndk::ComponentIndex TestComponent::componentIndex;
}
SCENARIO("Component", "[NDK][COMPONENT]")
{
GIVEN("Our TestComponent")
{
TestComponent testComponent;
WHEN("We clone it")
{
std::unique_ptr<Ndk::BaseComponent> clone = testComponent.Clone();
THEN("We should get a copy")
{
REQUIRE(dynamic_cast<TestComponent*>(clone.get()) != nullptr);
}
}
}
}

101
tests/SDK/NDK/Entity.cpp Normal file
View File

@@ -0,0 +1,101 @@
#include <NDK/World.hpp>
#include <NDK/Component.hpp>
#include <Catch/catch.hpp>
namespace
{
class UpdatableComponent : public Ndk::Component<UpdatableComponent>
{
public:
bool IsUpdated()
{
return m_updated;
}
void SetUpdated()
{
m_updated = true;
}
static Ndk::ComponentIndex componentIndex;
private:
bool m_updated = false;
};
Ndk::ComponentIndex UpdatableComponent::componentIndex;
class UpdateSystem : public Ndk::System<UpdateSystem>
{
public:
UpdateSystem()
{
Requires<UpdatableComponent>();
}
~UpdateSystem() = default;
static Ndk::SystemIndex systemIndex;
private:
void OnUpdate(float elapsedTime) override
{
for (const Ndk::EntityHandle& entity : GetEntities())
{
UpdatableComponent& updatable = entity->GetComponent<UpdatableComponent>();
updatable.SetUpdated();
}
}
};
Ndk::SystemIndex UpdateSystem::systemIndex;
}
SCENARIO("Entity", "[NDK][ENTITY]")
{
GIVEN("A world & an entity")
{
Ndk::World world;
Ndk::BaseSystem& system = world.AddSystem<UpdateSystem>();
const Ndk::EntityHandle& entity = world.CreateEntity();
WHEN("We add our UpdateComponent")
{
UpdatableComponent& updatableComponent = entity->AddComponent<UpdatableComponent>();
REQUIRE(!updatableComponent.IsUpdated());
THEN("Update the world should update the entity's component")
{
world.Update(1.f);
UpdatableComponent& updatableComponentGet = entity->GetComponent<UpdatableComponent>();
REQUIRE(updatableComponentGet.IsUpdated());
}
THEN("Update the world should not update the entity's component if it's disabled")
{
entity->Enable(false);
world.Update(1.f);
UpdatableComponent& updatableComponentGet = entity->GetComponent<UpdatableComponent>();
REQUIRE(!updatableComponentGet.IsUpdated());
}
THEN("We can remove its component")
{
entity->RemoveComponent(Ndk::GetComponentIndex<UpdatableComponent>());
world.Update(1.f);
REQUIRE(!entity->HasComponent<UpdatableComponent>());
}
}
WHEN("We kill our entity")
{
entity->Kill();
world.Update(1.f);
THEN("It's no more valid")
{
REQUIRE(!world.IsEntityValid(entity));
}
}
}
}

View File

@@ -0,0 +1,41 @@
#include <NDK/EntityList.hpp>
#include <NDK/World.hpp>
#include <Catch/catch.hpp>
SCENARIO("EntityList", "[NDK][ENTITYLIST]")
{
GIVEN("A world & a set of entities")
{
Ndk::World world;
const Ndk::EntityHandle& entity = world.CreateEntity();
Ndk::EntityList entityList;
entityList.Insert(entity);
WHEN("We ask if entity is in there")
{
THEN("These results are expected")
{
REQUIRE(entityList.Has(entity->GetId()));
const Ndk::EntityHandle& entity = world.CreateEntity();
REQUIRE(!entityList.Has(entity->GetId()));
}
}
WHEN("We remove then insert")
{
entityList.Remove(*entityList.begin());
THEN("Set should be empty")
{
REQUIRE(entityList.empty());
}
entityList.Insert(entity);
THEN("With one element")
{
REQUIRE(!entityList.empty());
}
}
}
}

View File

@@ -0,0 +1,29 @@
#include <NDK/EntityOwner.hpp>
#include <NDK/World.hpp>
#include <Catch/catch.hpp>
SCENARIO("EntityOwner", "[NDK][ENTITYOWNER]")
{
GIVEN("A world & an entity")
{
Ndk::World world;
const Ndk::EntityHandle& entity = world.CreateEntity();
WHEN("We set the ownership of the entity to our owner")
{
Ndk::EntityOwner entityOwner(entity);
THEN("Entity is still valid")
{
REQUIRE(entity.IsValid());
}
THEN("Resetting or getting out of scope is no more valid")
{
entityOwner.Reset();
world.Update(1.f);
REQUIRE(!entity.IsValid());
}
}
}
}

View File

@@ -0,0 +1,48 @@
#include <NDK/StateMachine.hpp>
#include <Catch/catch.hpp>
class TestState : public Ndk::State
{
public:
void Enter(Ndk::StateMachine& fsm) override
{
m_isUpdated = false;
}
bool IsUpdated() const
{
return m_isUpdated;
}
void Leave(Ndk::StateMachine& fsm) override
{
}
bool Update(Ndk::StateMachine& fsm, float elapsedTime) override
{
m_isUpdated = true;
}
private:
bool m_isUpdated;
};
SCENARIO("State & StateMachine", "[NDK][STATE]")
{
GIVEN("A statemachine with our TestState")
{
std::shared_ptr<TestState> testState = std::make_shared<TestState>();
Ndk::StateMachine stateMachine(testState);
REQUIRE(!testState->IsUpdated());
WHEN("We update our machine")
{
stateMachine.Update(1.f);
THEN("Our state has been updated")
{
REQUIRE(testState->IsUpdated());
}
}
}
}

42
tests/SDK/NDK/System.cpp Normal file
View File

@@ -0,0 +1,42 @@
#include <NDK/System.hpp>
#include <Catch/catch.hpp>
namespace
{
class TestSystem : public Ndk::System<TestSystem>
{
public:
TestSystem()
{
}
~TestSystem() = default;
static Ndk::SystemIndex systemIndex;
private:
void OnUpdate(float elapsedTime) override
{
}
};
Ndk::SystemIndex TestSystem::systemIndex;
}
SCENARIO("System", "[NDK][SYSTEM]")
{
GIVEN("Our TestSystem")
{
TestSystem testSystem;
WHEN("We clone it")
{
std::unique_ptr<Ndk::BaseSystem> clone = testSystem.Clone();
THEN("We should get a copy")
{
REQUIRE(dynamic_cast<TestSystem*>(clone.get()) != nullptr);
}
}
}
}

View File

@@ -0,0 +1,43 @@
#include <NDK/Systems/ListenerSystem.hpp>
#include <NDK/World.hpp>
#include <NDK/Components/ListenerComponent.hpp>
#include <NDK/Components/NodeComponent.hpp>
#include <NDK/Components/VelocityComponent.hpp>
#include <Nazara/Audio/Audio.hpp>
#include <Catch/catch.hpp>
SCENARIO("ListenerSystem", "[NDK][LISTENERSYSTEM]")
{
GIVEN("A world and an entity with listener & node components")
{
Ndk::World world;
const Ndk::EntityHandle& entity = world.CreateEntity();
Ndk::ListenerComponent& listenerComponent = entity->AddComponent<Ndk::ListenerComponent>();
Ndk::NodeComponent& nodeComponent = entity->AddComponent<Ndk::NodeComponent>();
WHEN("We move our entity")
{
Nz::Vector3f position = Nz::Vector3f::Unit() * 3.f;
nodeComponent.SetPosition(position);
Nz::Quaternionf rotation = Nz::Quaternionf::RotationBetween(Nz::Vector3f::Forward(), Nz::Vector3f::Up());
nodeComponent.SetRotation(rotation);
world.Update(1.f);
THEN("Our listener should have moved")
{
REQUIRE(Nz::Audio::GetListenerPosition() == position);
REQUIRE(Nz::Audio::GetListenerRotation() == rotation);
}
THEN("With a component of velocity")
{
Ndk::VelocityComponent& velocityComponent = entity->AddComponent<Ndk::VelocityComponent>();
Nz::Vector3f velocity = Nz::Vector3f::Unit() * 2.f;
velocityComponent.linearVelocity = velocity;
world.Update(1.f);
REQUIRE(Nz::Audio::GetListenerVelocity() == velocity);
}
}
}
}

View File

@@ -0,0 +1,34 @@
#include <NDK/Systems/PhysicsSystem.hpp>
#include <NDK/World.hpp>
#include <NDK/Components/CollisionComponent.hpp>
#include <NDK/Components/NodeComponent.hpp>
#include <NDK/Components/PhysicsComponent.hpp>
#include <Catch/catch.hpp>
SCENARIO("PhysicsSystem", "[NDK][PHYSICSSYSTEM]")
{
GIVEN("A world and a static entity & a dynamic entity")
{
Ndk::World world;
const Ndk::EntityHandle& staticEntity = world.CreateEntity();
Ndk::CollisionComponent& collisionComponentStatic = staticEntity->AddComponent<Ndk::CollisionComponent>();
Ndk::NodeComponent& nodeComponentStatic = staticEntity->AddComponent<Ndk::NodeComponent>();
const Ndk::EntityHandle& dynamicEntity = world.CreateEntity();
Ndk::NodeComponent& nodeComponentDynamic = dynamicEntity->AddComponent<Ndk::NodeComponent>();
Ndk::PhysicsComponent& physicsComponentDynamic = dynamicEntity->AddComponent<Ndk::PhysicsComponent>();
WHEN("We make collide these two entities")
{
nodeComponentDynamic.SetPosition(-Nz::Vector3f::UnitZ());
physicsComponentDynamic.AddForce(Nz::Vector3f::UnitZ());
THEN("The dynamic entity should have hit the static one")
{
world.Update(1.f); // On origin
world.Update(1.f); // On origin due to collision
REQUIRE(nodeComponentStatic.GetPosition().SquaredDistance(nodeComponentDynamic.GetPosition()) < 0.2f);
}
}
}
}

View File

@@ -0,0 +1,44 @@
#include <NDK/Systems/RenderSystem.hpp>
#include <NDK/World.hpp>
#include <NDK/Components/CameraComponent.hpp>
#include <NDK/Components/GraphicsComponent.hpp>
#include <NDK/Components/LightComponent.hpp>
#include <NDK/Components/NodeComponent.hpp>
#include <NDK/Components/ParticleGroupComponent.hpp>
#include <Nazara/Graphics/Sprite.hpp>
#include <Catch/catch.hpp>
SCENARIO("RenderSystem", "[NDK][RenderSystem]")
{
GIVEN("A world with a camera, a drawable, a light and some particles")
{
Ndk::World world;
const Ndk::EntityHandle& cameraEntity = world.CreateEntity();
Ndk::CameraComponent& cameraComponentCamera = cameraEntity->AddComponent<Ndk::CameraComponent>();
Ndk::NodeComponent& nodeComponentCamera = cameraEntity->AddComponent<Ndk::NodeComponent>();
const Ndk::EntityHandle& drawableEntity = world.CreateEntity();
Ndk::GraphicsComponent& graphicsComponentDrawable = drawableEntity->AddComponent<Ndk::GraphicsComponent>();
Nz::SpriteRef sprite = Nz::Sprite::New();
graphicsComponentDrawable.Attach(sprite);
Ndk::NodeComponent& nodeComponentDrawable = drawableEntity->AddComponent<Ndk::NodeComponent>();
const Ndk::EntityHandle& lightEntity = world.CreateEntity();
Ndk::LightComponent& lightComponentLight = lightEntity->AddComponent<Ndk::LightComponent>();
Ndk::NodeComponent& nodeComponentLight = lightEntity->AddComponent<Ndk::NodeComponent>();
const Ndk::EntityHandle& particlesEntity = world.CreateEntity();
Ndk::ParticleGroupComponent& particleGroupComponentParticles = particlesEntity->AddComponent<Ndk::ParticleGroupComponent>(1, Nz::ParticleLayout_Sprite);
WHEN("We change the render technique to ForwardRenderTechnique")
{
Ndk::RenderSystem& renderSystem = world.GetSystem<Ndk::RenderSystem>();
renderSystem.ChangeRenderTechnique<Nz::ForwardRenderTechnique>();
THEN("The render system should be ForwardRenderTechnique")
{
REQUIRE(renderSystem.GetRenderTechnique().GetType() == Nz::RenderTechniqueType_BasicForward);
}
}
}
}

View File

@@ -0,0 +1,28 @@
#include <NDK/Systems/VelocitySystem.hpp>
#include <NDK/World.hpp>
#include <NDK/Components/NodeComponent.hpp>
#include <NDK/Components/VelocityComponent.hpp>
#include <Catch/catch.hpp>
SCENARIO("VelocitySystem", "[NDK][VELOCITYSYSTEM]")
{
GIVEN("A world and an entity with velocity & node components")
{
Ndk::World world;
const Ndk::EntityHandle& entity = world.CreateEntity();
Ndk::VelocityComponent& velocityComponent = entity->AddComponent<Ndk::VelocityComponent>();
Ndk::NodeComponent& nodeComponent = entity->AddComponent<Ndk::NodeComponent>();
WHEN("We give a speed to our entity")
{
Nz::Vector3f velocity = Nz::Vector3f::Unit() * 2.f;
velocityComponent.linearVelocity = velocity;
world.Update(1.f);
THEN("Our entity should have moved")
{
REQUIRE(nodeComponent.GetPosition().SquaredDistance(velocity) < 0.2f);
}
}
}
}

103
tests/SDK/NDK/World.cpp Normal file
View File

@@ -0,0 +1,103 @@
#include <NDK/World.hpp>
#include <NDK/Component.hpp>
#include <Catch/catch.hpp>
namespace
{
class UpdatableComponent : public Ndk::Component<UpdatableComponent>
{
public:
bool IsUpdated()
{
return m_updated;
}
void SetUpdated()
{
m_updated = true;
}
static Ndk::ComponentIndex componentIndex;
private:
bool m_updated = false;
};
Ndk::ComponentIndex UpdatableComponent::componentIndex;
class UpdateSystem : public Ndk::System<UpdateSystem>
{
public:
UpdateSystem()
{
Requires<UpdatableComponent>();
}
~UpdateSystem() = default;
static Ndk::SystemIndex systemIndex;
private:
void OnUpdate(float elapsedTime) override
{
for (const Ndk::EntityHandle& entity : GetEntities())
{
UpdatableComponent& updatable = entity->GetComponent<UpdatableComponent>();
updatable.SetUpdated();
}
}
};
Ndk::SystemIndex UpdateSystem::systemIndex;
}
SCENARIO("World", "[NDK][WORLD]")
{
GIVEN("A brave new world and the update system")
{
Ndk::World world;
Ndk::BaseSystem& system = world.AddSystem<UpdateSystem>();
WHEN("We had a new entity with an updatable component and a system")
{
const Ndk::EntityHandle& entity = world.CreateEntity();
UpdatableComponent& component = entity->AddComponent<UpdatableComponent>();
THEN("We can get our entity and our system")
{
const Ndk::EntityHandle& fetchedEntity = world.GetEntity(entity->GetId());
REQUIRE(fetchedEntity->GetWorld() == &world);
}
THEN("We can clone it")
{
const Ndk::EntityHandle& clone = world.CloneEntity(entity->GetId());
REQUIRE(world.IsEntityValid(clone));
}
}
AND_WHEN("We update our world with our entity")
{
REQUIRE(&world.GetSystem(UpdateSystem::systemIndex) == &world.GetSystem<UpdateSystem>());
const Ndk::EntityHandle& entity = world.CreateEntity();
UpdatableComponent& component = entity->AddComponent<UpdatableComponent>();
THEN("Our entity component must be updated")
{
world.Update(1.f);
REQUIRE(component.IsUpdated());
}
THEN("We kill our entity")
{
REQUIRE(entity->IsValid());
world.KillEntity(entity);
world.Update(1.f);
REQUIRE(!world.IsEntityValid(entity));
}
}
}
}