Compare commits
250 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 3678bf6d18 | |||
| 39b9b3c2a7 | |||
| 84818f6fcb | |||
| 349fa9d7fa | |||
| 2a5e0420c8 | |||
| 4a516a4d86 | |||
| 21aea6f31d | |||
| 297a3fb493 | |||
| c47ef61ddc | |||
| e8e93587e9 | |||
| 39dd27726d | |||
| 6adc98eea5 | |||
| ad4e723677 | |||
| 11d547cde8 | |||
| 24b9e951c2 | |||
| 81024b4a1d | |||
| 23e78c4947 | |||
| 3e7f27e1fe | |||
| ee83394678 | |||
| 03456ad731 | |||
| 0bcbcda8b8 | |||
| c6655ce0d3 | |||
| b908a9f9c6 | |||
| dc7ed2e0ba | |||
| f71bfca142 | |||
| dbb44fb4e4 | |||
| 6e6460a000 | |||
| 9f871bbe7c | |||
| 2bd60be741 | |||
| dab4ba2164 | |||
| 25a99a04c5 | |||
| 13ade6d7f2 | |||
| 592776e783 | |||
| ff44dccb33 | |||
| ef9ce7932a | |||
| beb0967c3b | |||
| 0e0a361a38 | |||
| 6ebc4fc784 | |||
| 7fb71f17b2 | |||
| e52c0c9633 | |||
| 578ca13f79 | |||
| 4048ce6bd4 | |||
| c4642cc530 | |||
| 32ebc061b4 | |||
| a1ed79dfbf | |||
| 638c236ceb | |||
| f67f22c82d | |||
| f3ff433bf7 | |||
| 85ff86a2fe | |||
| fa8775a72e | |||
| 49884d7e60 | |||
| e7bb8954bf | |||
| e510f2b88c | |||
| 34cbbb3eb1 | |||
| 0ae6c42df8 | |||
| 3678cceb1c | |||
| de8461e257 | |||
| eb69e03e82 | |||
| d38f58165f | |||
| c133223bac | |||
| 2f6d4fcd0b | |||
| 069a6756b9 | |||
| a550b6077e | |||
| e0cc25dbac | |||
| 00bc55370d | |||
| 330f08b926 | |||
| a0255f1b31 | |||
| ef4cbfece8 | |||
| ab6af30a28 | |||
| a858dab8b6 | |||
| b1ae96f737 | |||
| 7dcd75fccc | |||
| 1e5e24a036 | |||
| 3636ad45af | |||
| 6d649e4d35 | |||
| 5c1a93021b | |||
| 6cf7c12d34 | |||
| ad43cc44c3 | |||
| 002e1d8c5a | |||
| 30db0b10fd | |||
| 97f8be1915 | |||
| 0eb51a0fea | |||
| 13f7fb2561 | |||
| 0b714e593f | |||
| 4a817613e3 | |||
| 8beddca89a | |||
| 5e0fe975f3 | |||
| db5b6ad519 | |||
| a26c8f2a24 | |||
| 076030a252 | |||
| 0f59b863ee | |||
| d0927373a3 | |||
| 46ae2d39af | |||
| 000adb198f | |||
| 51a2da9cc0 | |||
| 0f0a1ceca7 | |||
| 50822d09fc | |||
| 3e2ffdb85b | |||
| 5160a45b44 | |||
| 03d5f0dd67 | |||
| 4bb4e29515 | |||
| a6a08f48bb | |||
| ba831f8a3c | |||
| 4c29e071af | |||
| 6a3c14540d | |||
| 2cb5441b1c | |||
| 97a0d97b0b | |||
| a23d69af9a | |||
| 425f3ca3dd | |||
| 6d1cbd928e | |||
| 6da5fd1263 | |||
| 6eebce05af | |||
| 58a9d38e26 | |||
| 50ccf96268 | |||
| 5602b32507 | |||
| cf0ff9bfe5 | |||
| d10df6ae09 | |||
| 99b76d7ae1 | |||
| a2d55feed1 | |||
| e74b2b6874 | |||
| 3f602c9d63 | |||
| 082b3fd3df | |||
| 04a01104b5 | |||
| 9b348a7d97 | |||
| 889e0bb3d3 | |||
| 1669649186 | |||
| 1431bd1a11 | |||
| ed82a0939a | |||
| e2417be8c7 | |||
| 472a8db81a | |||
| 1e370bac13 | |||
| e940c1d604 | |||
| 478c692315 | |||
| 170610bcb1 | |||
| 454ed0032b | |||
| 1910ab705b | |||
| 0a8a9e5099 | |||
| f9508657f5 | |||
| b698a0d36f | |||
| 1229a99af4 | |||
| 8e431c8edd | |||
| 948f6d5de7 | |||
| ae7d4b9097 | |||
| 7c714d1f98 | |||
| e6fa9df3a5 | |||
| 59095c2ce5 | |||
| 3f77da4a07 | |||
| 7f8e78c8cd | |||
| bac0a1de26 | |||
| bf82bbb5d5 | |||
| e52052b431 | |||
| 8d9ff2b6dc | |||
| 63db236dfa | |||
| 5fce7490a9 | |||
| 8abef7024d | |||
| bf08ffe392 | |||
| 5f5cab1171 | |||
| 8d87841691 | |||
| 11e37ca898 | |||
| 55aad493ff | |||
| 9a7d77f29d | |||
| 36ae451ea6 | |||
| b3bb0e543f | |||
| 81034420a9 | |||
| 15442c8c75 | |||
| 350038057d | |||
| 23c3f0709b | |||
| 01d7388f91 | |||
| 1ceea91f4a | |||
| 4195baa5dc | |||
| 37f9ab2322 | |||
| d8b1f4e514 | |||
| 6a3335315a | |||
| c651face4c | |||
| 641a43b2ec | |||
| 58952725e9 | |||
| de044dabd3 | |||
| ce7b26ccac | |||
| 0d8f7acb7e | |||
| 99543a300d | |||
| f377d9ba70 | |||
| d22e59f28f | |||
| 7804d42fea | |||
| 9a8f5293af | |||
| e535f629bc | |||
| c60e968836 | |||
| bc8a922b77 | |||
| 663aca2641 | |||
| 4de613814b | |||
| e580540143 | |||
| 0807a7d0ae | |||
| ba150a264c | |||
| b650eb95df | |||
| 8949db5456 | |||
| 14bcb7ea0d | |||
| 519178dfba | |||
| 0e6353dd79 | |||
| ed5cd392ab | |||
| bad42fae1c | |||
| 247ff09743 | |||
| 2a35c3055e | |||
| ce41421dbb | |||
| 1d0810db10 | |||
| c85bb2eef7 | |||
| 75402b0f93 | |||
| f83e217638 | |||
| 58db7b755a | |||
| a8c0a1f294 | |||
| 9f54a08536 | |||
| 24dada149f | |||
| a7fb77d998 | |||
| 32d2aa6a93 | |||
| d1880754f5 | |||
| 27b44d2511 | |||
| 94adaf8ae3 | |||
| 6dc5112c26 | |||
| e92119c99e | |||
| c48c9e1f86 | |||
| 7ac2aaef15 | |||
| 06f56d13d1 | |||
| 8f55e90d2d | |||
| b5a010a517 | |||
| b87e543420 | |||
| fa3816e55d | |||
| ee41721a75 | |||
| 648ba502bb | |||
| 2c993e82d6 | |||
| cbbb7deaea | |||
| de5bdef58c | |||
| 000ad6358f | |||
| 04a8eeebe8 | |||
| d8d63f804a | |||
| 955c9ce534 | |||
| e8d23f3b3f | |||
| 89aa8c44b0 | |||
| f0e407bec2 | |||
| 8e21b1fe35 | |||
| 17133b316a | |||
| 28a290600b | |||
| af5b3a9302 | |||
| abb61d2cd6 | |||
| 2f13f5da7a | |||
| 464650c370 | |||
| af294f78b7 | |||
| c840183467 | |||
| 1f99d46a8d | |||
| fd99562c7a | |||
| c698149bdd | |||
| fa0ace8c68 | |||
| 1d19c6901e |
@@ -1,16 +1,17 @@
|
||||
#### Bundled libraries
|
||||
Libraries bundled along with binaries for Windows (MSVC14), available as a submodule in the *deps* directory of the source tree.
|
||||
|
||||
| Project | Version | License |
|
||||
| ------------------------------------------------------------- | ------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| **[GLFW](http://www.glfw.org)** | 3.1.2 | [zlib/libpng License](http://www.glfw.org/license.html) |
|
||||
| **[GLM](http://glm.g-truc.net/0.9.5/index.html)** | 0.9.7.1 | [MIT License](http://glm.g-truc.net/copying.txt) |
|
||||
| **[GLEW](http://glew.sourceforge.net)** | 1.13.0 | [Modified BSD License](http://glew.sourceforge.net/glew.txt), [Mesa 3-D License](http://glew.sourceforge.net/mesa.txt), [Khronos License](http://glew.sourceforge.net/khronos.txt) |
|
||||
| **[Assimp](http://assimp.sourceforge.net)** | 3.1.1 | [BSD 3-Clause License](http://assimp.sourceforge.net/main_license.html) |
|
||||
| **[zlib](http://www.zlib.net)** | 1.28 | [zlib License](http://www.zlib.net/zlib_license.html) |
|
||||
| **[libpng](http://www.libpng.org/pub/png/libpng.html)** | 1.6.19 | [libpng License](http://www.libpng.org/pub/png/src/libpng-LICENSE.txt) |
|
||||
| **[Xerces-C++](https://xerces.apache.org/xerces-c)** | 3.1.2 | [Apache License Version 2.0](https://www.apache.org/licenses/LICENSE-2.0) |
|
||||
| **[ImGui](https://github.com/ocornut/imgui)** | 2015-12-12 | [MIT License](https://github.com/ocornut/imgui/blob/de3a154f3801de22c8e0bd2aeabf663a70c05972/LICENSE) |
|
||||
| Project | Version | License |
|
||||
| ---------------------------------------------------------------- | ------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| **[GLFW](http://www.glfw.org)** | 3.1.2 | [zlib/libpng License](http://www.glfw.org/license.html) |
|
||||
| **[GLM](http://glm.g-truc.net/0.9.5/index.html)** | 0.9.7.1 | [MIT License](http://glm.g-truc.net/copying.txt) |
|
||||
| **[GLEW](http://glew.sourceforge.net)** | 1.13.0 | [Modified BSD License](http://glew.sourceforge.net/glew.txt), [Mesa 3-D License](http://glew.sourceforge.net/mesa.txt), [Khronos License](http://glew.sourceforge.net/khronos.txt) |
|
||||
| **[Assimp](http://assimp.sourceforge.net)** | 3.1.1 | [BSD 3-Clause License](http://assimp.sourceforge.net/main_license.html) |
|
||||
| **[zlib](http://www.zlib.net)** | 1.28 | [zlib License](http://www.zlib.net/zlib_license.html) |
|
||||
| **[libpng](http://www.libpng.org/pub/png/libpng.html)** | 1.6.19 | [libpng License](http://www.libpng.org/pub/png/src/libpng-LICENSE.txt) |
|
||||
| **[Xerces-C++](https://xerces.apache.org/xerces-c)** | 3.1.2 | [Apache License Version 2.0](https://www.apache.org/licenses/LICENSE-2.0) |
|
||||
| **[ImGui](https://github.com/ocornut/imgui)** | 2015-12-12 | [MIT License](https://github.com/ocornut/imgui/blob/de3a154f3801de22c8e0bd2aeabf663a70c05972/LICENSE) |
|
||||
| **[nativefiledialog](https://github.com/mlabbe/nativefiledialog) | 2016-01-08 | https://github.com/mlabbe/nativefiledialog/blob/master/LICENSE |
|
||||
|
||||
#### External libraries
|
||||
Libraries that are too big to be bundled with the project.
|
||||
|
||||
+1
-1
Submodule assets updated: 673d4a4e4c...c8e631f449
+1
-1
Submodule deps updated: 1ae6ba5b12...bf83f099ba
@@ -0,0 +1,59 @@
|
||||
#ifndef Collision_h__
|
||||
#define Collision_h__
|
||||
|
||||
//NOTE: Collision.h needs to be #included before <GLFW/glfw3.h>,
|
||||
//because Collision #includes "RawModel.h", which has "Texture.h", which has "OpenGL.h" which must be #included first
|
||||
//or you will get "fatal error C1189: #error: gl.h included before glew.h"
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "Core/Ray.h"
|
||||
#include "Core/AABB.h"
|
||||
#include "Engine/Rendering/RawModel.h"
|
||||
#include "Core/Entity.h"
|
||||
|
||||
class World;
|
||||
struct ComponentWrapper;
|
||||
|
||||
namespace Collision
|
||||
{
|
||||
//Return true if the ray hits the box.
|
||||
bool RayAABBIntr(const Ray& ray, const AABB& box);
|
||||
bool RayVsAABB(const Ray& ray, const AABB& box);
|
||||
//Return true if the ray hits the box, also outputs distance from ray origin to intersection point in [outDistance].
|
||||
bool RayVsAABB(const Ray& ray, const AABB& box, float& outDistance);
|
||||
|
||||
//Return true if the ray hits any of the triangles in the model. Stops checking when a hit is detected.
|
||||
bool RayVsModel(const Ray& ray,
|
||||
const std::vector<RawModel::Vertex>& modelVertices,
|
||||
const std::vector<unsigned int>& modelIndices);
|
||||
//Return true if the ray hits any of the triangles in the model.
|
||||
//Also returns the position of the intersection point. Will loop through all the whole model indices.
|
||||
bool RayVsModel(const Ray& ray,
|
||||
const std::vector<RawModel::Vertex>& modelVertices,
|
||||
const std::vector<unsigned int>& modelIndices,
|
||||
glm::vec3& outHitPosition);
|
||||
//Return true if the ray hits any of the triangles in the model.
|
||||
//Also returns the distance from the ray origin to the closest
|
||||
//intersection point, and the barycentric u,v-coordinates. Will loop through all the whole model indices.
|
||||
bool RayVsModel(const Ray& ray,
|
||||
const std::vector<RawModel::Vertex>& modelVertices,
|
||||
const std::vector<unsigned int>& modelIndices,
|
||||
float& outDistance,
|
||||
float& outUCoord,
|
||||
float& outVCoord);
|
||||
|
||||
//Return true if the boxes are intersecting.
|
||||
bool AABBVsAABB(const AABB& a, const AABB& b);
|
||||
//Return true if the boxes are intersecting.
|
||||
//Also outputs the minimum translation that box [a] would need in order to resolve collision.
|
||||
bool AABBVsAABB(const AABB& a, const AABB& b, glm::vec3& minimumTranslation);
|
||||
bool IsSameBoxProbably(const AABB& first, const AABB& second, const float epsilon = 0.0001f);
|
||||
|
||||
//Returns true if the entity has a boundingbox. Outputs the aabb in [outBox].
|
||||
bool GetEntityBox(World* world, EntityID entity, AABB& outBox, bool forceBoxFromModel = false);
|
||||
bool GetEntityBox(World* world, ComponentWrapper& AABBComponent, AABB& outBox);
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,31 @@
|
||||
#ifndef CollisionSystem_h__
|
||||
#define CollisionSystem_h__
|
||||
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <glm/common.hpp>
|
||||
|
||||
#include "Common.h"
|
||||
#include "Core/System.h"
|
||||
#include "Core/EventBroker.h"
|
||||
#include "Core/EKeyUp.h"
|
||||
|
||||
class CollisionSystem : public PureSystem
|
||||
{
|
||||
public:
|
||||
CollisionSystem(EventBroker* eventBroker)
|
||||
: PureSystem(eventBroker, "AABB")
|
||||
, zPress(false)
|
||||
{
|
||||
//TODO: Debug stuff, remove later.
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EKeyUp, &CollisionSystem::OnKeyUp);
|
||||
}
|
||||
|
||||
virtual void UpdateComponent(World* world, ComponentWrapper& cAABB, double dt) override;
|
||||
|
||||
private:
|
||||
bool zPress;
|
||||
EventRelay<CollisionSystem, Events::KeyUp> m_EKeyUp;
|
||||
bool OnKeyUp(const Events::KeyUp &event);
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,39 @@
|
||||
#ifndef Events_TriggerEnter_h__
|
||||
#define Events_TriggerEnter_h__
|
||||
|
||||
#include "../Core/EventBroker.h"
|
||||
#include "../Core/Entity.h"
|
||||
|
||||
namespace Events
|
||||
{
|
||||
|
||||
/** Thrown once, when an entity is only touching a trigger. */
|
||||
struct TriggerTouch : Event
|
||||
{
|
||||
/** The id of the entity that touches the trigger. */
|
||||
EntityID Entity;
|
||||
/** The id of the trigger entity. */
|
||||
EntityID Trigger;
|
||||
};
|
||||
|
||||
/** Thrown once, when an entity has completely left a trigger. */
|
||||
struct TriggerLeave : Event
|
||||
{
|
||||
/** The id of the entity that left the trigger. */
|
||||
EntityID Entity;
|
||||
/** The id of the trigger entity. */
|
||||
EntityID Trigger;
|
||||
};
|
||||
|
||||
/** Thrown once, when an entity is completely contained inside a trigger. */
|
||||
struct TriggerEnter : Event
|
||||
{
|
||||
/** The id of the entity that entered the trigger. */
|
||||
EntityID Entity;
|
||||
/** The id of the trigger entity. */
|
||||
EntityID Trigger;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,38 @@
|
||||
#ifndef TriggerSystem_h__
|
||||
#define TriggerSystem_h__
|
||||
|
||||
#include <glm/common.hpp>
|
||||
#include <unordered_set>
|
||||
|
||||
#include "Core/System.h"
|
||||
#include "Core/EventBroker.h"
|
||||
#include "ETrigger.h"
|
||||
|
||||
class AABB;
|
||||
|
||||
class TriggerSystem : public PureSystem
|
||||
{
|
||||
public:
|
||||
TriggerSystem(EventBroker* eventBroker)
|
||||
: PureSystem(eventBroker, "Trigger")
|
||||
{}
|
||||
|
||||
virtual void UpdateComponent(World* world, ComponentWrapper& collision, double dt) override;
|
||||
|
||||
private:
|
||||
std::unordered_map<EntityID, std::unordered_set<EntityID>> m_EntitiesTouchingTrigger;
|
||||
std::unordered_map<EntityID, std::unordered_set<EntityID>> m_EntitiesCompletelyInTrigger;
|
||||
|
||||
//True if leave event was thrown.
|
||||
bool throwLeaveIfWasInTrigger(std::unordered_set<EntityID>& triggerSet, EntityID pId, EntityID tId);
|
||||
template<typename Event>
|
||||
void publish(EntityID pId, EntityID tId)
|
||||
{
|
||||
Event e;
|
||||
e.Trigger = tId;
|
||||
e.Entity = pId;
|
||||
m_EventBroker->Publish(e);
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -4,4 +4,5 @@
|
||||
#include <map>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "Core/Util/Logging.h"
|
||||
#include "Core/Util/Logging.h"
|
||||
#include "Core/Util/IfDebug.h"
|
||||
@@ -0,0 +1,29 @@
|
||||
#ifndef AABB_h__
|
||||
#define AABB_h__
|
||||
|
||||
#include "../GLM.h"
|
||||
|
||||
class AABB
|
||||
{
|
||||
public:
|
||||
AABB() = default;
|
||||
//No checks are made. Values in minPos must be less than values in maxPos, i.e. min.x < max.x, etc.
|
||||
AABB(const glm::vec3& minPos, const glm::vec3& maxPos);
|
||||
AABB(const glm::vec4& minPos, const glm::vec4& maxPos);
|
||||
//No checks are made. Size must consist of non-negative numbers.
|
||||
virtual void CreateFromCenter(const glm::vec3& center, const glm::vec3& size);
|
||||
virtual ~AABB();
|
||||
|
||||
const glm::vec3& MinCorner() const { return m_MinCorner; }
|
||||
const glm::vec3& MaxCorner() const { return m_MaxCorner; }
|
||||
const glm::vec3& Center() const { return m_Center; }
|
||||
const glm::vec3 Size() const { return 2.0f * m_HalfSize; }
|
||||
const glm::vec3& HalfSize() const { return m_HalfSize; }
|
||||
private:
|
||||
glm::vec3 m_MinCorner;
|
||||
glm::vec3 m_MaxCorner;
|
||||
glm::vec3 m_Center;
|
||||
glm::vec3 m_HalfSize;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -12,9 +12,16 @@ struct ComponentInfo
|
||||
unsigned int Stride = 0;
|
||||
};
|
||||
|
||||
struct Field_t
|
||||
{
|
||||
std::string Type;
|
||||
unsigned int Offset;
|
||||
unsigned int Stride;
|
||||
};
|
||||
|
||||
std::string Name;
|
||||
std::unordered_map<std::string, std::string> FieldTypes;
|
||||
std::unordered_map<std::string, unsigned int> FieldOffsets;
|
||||
std::unordered_map<std::string, Field_t> Fields;
|
||||
std::vector<const Field_t*> FieldsInOrder;
|
||||
Meta_t Meta;
|
||||
std::shared_ptr<char> Defaults = nullptr;
|
||||
};
|
||||
|
||||
@@ -20,7 +20,7 @@ public:
|
||||
~ComponentPoolForwardIterator() = default;
|
||||
ComponentPoolForwardIterator& operator=(const ComponentPoolForwardIterator& other) = default;
|
||||
ComponentPoolForwardIterator& operator++();
|
||||
ComponentPoolForwardIterator& operator++(int);
|
||||
ComponentPoolForwardIterator operator++(int);
|
||||
bool operator!=(const ComponentPoolForwardIterator& other) const;
|
||||
bool operator==(const ComponentPoolForwardIterator& other) const;
|
||||
ComponentWrapper operator*() const;
|
||||
|
||||
@@ -21,7 +21,7 @@ struct ComponentWrapper
|
||||
template <typename T>
|
||||
T& Property(std::string name)
|
||||
{
|
||||
unsigned int offset = Info.FieldOffsets.at(name);
|
||||
unsigned int offset = Info.Fields.at(name).Offset;
|
||||
return *reinterpret_cast<T*>(&Data[offset]);
|
||||
}
|
||||
|
||||
@@ -78,8 +78,9 @@ public:
|
||||
void AddProperty(std::string fieldName, T defaultValue)
|
||||
{
|
||||
m_DefaultValues.push_back(defaultValue);
|
||||
m_ComponentInfo.FieldTypes[fieldName] = typeid(T).name();
|
||||
m_ComponentInfo.FieldOffsets[fieldName] = m_ComponentInfo.Meta.Stride;
|
||||
m_ComponentInfo.Fields[fieldName].Name = typeid(T).name();
|
||||
m_ComponentInfo.Fields[fieldName].Offset = m_ComponentInfo.Meta.Stride;
|
||||
m_ComponentInfo.Fields[fieldName].Stride = sizeof(T);
|
||||
m_ComponentInfo.Meta.Stride += sizeof(T);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
#ifndef EPlayerDamage_h__
|
||||
#define EPlayerDamage_h__
|
||||
|
||||
#include "EventBroker.h"
|
||||
#include "../Core/Entity.h"
|
||||
|
||||
namespace Events
|
||||
{
|
||||
|
||||
struct PlayerDamage : Event
|
||||
{
|
||||
double DamageAmount;
|
||||
EntityID PlayerDamagedID;
|
||||
//optional TypeOfDamage
|
||||
std::string TypeOfDamage;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,20 @@
|
||||
#ifndef EPlayerDeath_h__
|
||||
#define EPlayerDeath_h__
|
||||
|
||||
#include "EventBroker.h"
|
||||
#include "../Core/Entity.h"
|
||||
|
||||
namespace Events
|
||||
{
|
||||
|
||||
struct PlayerDeath : Event
|
||||
{
|
||||
//KilledBy,KilledByWhat is optional for now. It might be used later in the playerlog-system
|
||||
EntityID KilledBy;
|
||||
EntityID PlayerID;
|
||||
std::string KilledByWhat;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,18 @@
|
||||
#ifndef EPlayerHealthPickup_h__
|
||||
#define EPlayerHealthPickup_h__
|
||||
|
||||
#include "EventBroker.h"
|
||||
#include "../Core/Entity.h"
|
||||
|
||||
namespace Events
|
||||
{
|
||||
|
||||
struct PlayerHealthPickup : Event
|
||||
{
|
||||
double HealthAmount;
|
||||
EntityID PlayerHealedID;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -2,5 +2,6 @@
|
||||
#define Entity_h__
|
||||
|
||||
typedef unsigned int EntityID;
|
||||
const static unsigned int EntityID_Invalid = -1;
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,303 @@
|
||||
#ifndef EntityFile_h__
|
||||
#define EntityFile_h__
|
||||
|
||||
#include <stack>
|
||||
#include <boost/lexical_cast.hpp>
|
||||
#include <xercesc/util/XercesDefs.hpp>
|
||||
#include <xercesc/util/PlatformUtils.hpp>
|
||||
#include <xercesc/sax2/SAX2XMLReader.hpp>
|
||||
#include <xercesc/sax2/XMLReaderFactory.hpp>
|
||||
#include <xercesc/sax2/DefaultHandler.hpp>
|
||||
#include <xercesc/sax2/Attributes.hpp>
|
||||
#include <xercesc/util/XMLString.hpp>
|
||||
#include <xercesc/util/XMLChar.hpp>
|
||||
#include <xercesc/sax2/DefaultHandler.hpp>
|
||||
#include <xercesc/framework/XMLDocumentHandler.hpp>
|
||||
#include <xercesc/framework/psvi/XSElementDeclaration.hpp>
|
||||
#include <xercesc/framework/psvi/XSComplexTypeDefinition.hpp>
|
||||
#include <xercesc/framework/psvi/XSParticle.hpp>
|
||||
#include <xercesc/framework/psvi/XSModelGroup.hpp>
|
||||
#include <xercesc/framework/psvi/XSAnnotation.hpp>
|
||||
#include <xercesc/framework/psvi/XSValue.hpp>
|
||||
#include <xercesc/framework/XMLGrammarPoolImpl.hpp>
|
||||
#include <xercesc/dom/DOM.hpp>
|
||||
#include <xercesc/dom/DOMLSInput.hpp>
|
||||
#include <xercesc/dom/DOMElement.hpp>
|
||||
|
||||
#include "../GLM.h"
|
||||
#include "Util/XercesString.h"
|
||||
#include "ResourceManager.h"
|
||||
#include "Entity.h"
|
||||
#include "ComponentInfo.h"
|
||||
|
||||
class EntityFileHandler
|
||||
{
|
||||
friend class EntityFileSAXHandler;
|
||||
public:
|
||||
// @param EntityID The entity found
|
||||
// @param EntityID The parent of the entity
|
||||
typedef std::function<void(EntityID, EntityID, const std::string&)> OnStartEntityCallback;
|
||||
void SetStartEntityCallback(OnStartEntityCallback c) { m_OnStartEntityCallback = c; }
|
||||
// @param EntityID The entity the component corresponds to
|
||||
// @param std::string Type name of the component
|
||||
typedef std::function<void(EntityID, const std::string&)> OnStartComponentCallback;
|
||||
void SetStartComponentCallback(OnStartComponentCallback c) { m_OnStartComponentCallback = c; }
|
||||
// @param EntityID Entity
|
||||
// @param std::string Component name
|
||||
// @param std::string Field name
|
||||
// @param std::map<std::string, std::string> Field attribute names and values
|
||||
typedef std::function<void(EntityID, const std::string&, const std::string&, const std::map<std::string, std::string>&)> OnStartFieldCallback;
|
||||
void SetStartFieldCallback(OnStartFieldCallback c) { m_OnStartFieldCallback = c; }
|
||||
// @param EntityID Entity
|
||||
// @param std::string Component name
|
||||
// @param std::string Field name
|
||||
// @param char* Field data
|
||||
typedef std::function<void(EntityID, const std::string&, const std::string&, const char*)> OnStartFieldDataCallback;
|
||||
void SetStartFieldDataCallback(OnStartFieldDataCallback c) { m_OnStartFieldDataCallback = c; }
|
||||
|
||||
private:
|
||||
OnStartEntityCallback m_OnStartEntityCallback = nullptr;
|
||||
OnStartComponentCallback m_OnStartComponentCallback = nullptr;
|
||||
OnStartFieldCallback m_OnStartFieldCallback = nullptr;
|
||||
OnStartFieldDataCallback m_OnStartFieldDataCallback = nullptr;
|
||||
};
|
||||
|
||||
class EntityFileSAXHandler : public xercesc::DefaultHandler
|
||||
{
|
||||
public:
|
||||
enum class State
|
||||
{
|
||||
Unknown,
|
||||
Entity,
|
||||
EntityRef,
|
||||
Component,
|
||||
ComponentField
|
||||
};
|
||||
|
||||
EntityFileSAXHandler(const EntityFileHandler* handler, xercesc::SAX2XMLReader* reader)
|
||||
: m_Handler(handler)
|
||||
, m_Reader(reader)
|
||||
{
|
||||
// Push first parent
|
||||
m_EntityStack.push(EntityID_Invalid);
|
||||
m_StateStack.push(State::Unknown);
|
||||
}
|
||||
|
||||
void startElement(const XMLCh* const _uri, const XMLCh* const _localName, const XMLCh* const _qname, const xercesc::Attributes& attrs) override
|
||||
{
|
||||
std::string name = XS::ToString(_localName);
|
||||
if (m_StateStack.top() == State::Unknown || m_StateStack.top() == State::Entity || m_StateStack.top() == State::EntityRef) {
|
||||
if (name == "Entity") {
|
||||
// Handle entity references
|
||||
const XMLCh* refAttr = attrs.getValue(XS::ToXMLCh("ref"));
|
||||
std::string ref = XS::ToString(refAttr);
|
||||
if (ref.empty()) {
|
||||
m_StateStack.push(State::Entity);
|
||||
onStartEntity(attrs);
|
||||
} else {
|
||||
m_StateStack.push(State::EntityRef);
|
||||
onStartEntityRef(ref, attrs);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
std::string uri = XS::ToString(_uri);
|
||||
if (m_StateStack.top() == State::Entity || m_StateStack.top() == State::EntityRef) {
|
||||
if (uri == "components") {
|
||||
m_StateStack.push(State::Component);
|
||||
onStartComponent(name);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (m_StateStack.top() == State::Component) {
|
||||
m_StateStack.push(State::ComponentField);
|
||||
onStartComponentField(name, attrs);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void characters(const XMLCh* const chars, const XMLSize_t length) override
|
||||
{
|
||||
if (m_StateStack.top() == State::ComponentField) {
|
||||
char* transcoded = xercesc::XMLString::transcode(chars);
|
||||
onFieldData(transcoded);
|
||||
}
|
||||
}
|
||||
|
||||
void endElement(const XMLCh* const _uri, const XMLCh* const _localName, const XMLCh* const _qname) override
|
||||
{
|
||||
std::string name = XS::ToString(_localName);
|
||||
if (m_StateStack.top() == State::Entity || m_StateStack.top() == State::EntityRef) {
|
||||
if (name == "Entity") {
|
||||
if (m_StateStack.top() == State::Entity) {
|
||||
m_StateStack.pop();
|
||||
if (m_StateStack.top() == State::EntityRef) {
|
||||
onEndEntityRef();
|
||||
} else {
|
||||
onEndEntity();
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
std::string uri = XS::ToString(_uri);
|
||||
if (m_StateStack.top() == State::Component) {
|
||||
//if (uri == "components") {
|
||||
m_StateStack.pop();
|
||||
onEndComponent(name);
|
||||
return;
|
||||
//}
|
||||
}
|
||||
|
||||
if (m_StateStack.top() == State::ComponentField) {
|
||||
m_StateStack.pop();
|
||||
onEndComponentField(name);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void fatalError(const xercesc::SAXParseException& e)
|
||||
{
|
||||
XS::ToString s(e.getMessage());
|
||||
LOG_ERROR("SAXParseException: %s", ((std::string)s).c_str());
|
||||
//throw e;
|
||||
}
|
||||
|
||||
private:
|
||||
const EntityFileHandler* m_Handler;
|
||||
xercesc::XMLGrammarPool* m_GrammarPool;
|
||||
xercesc::SAX2XMLReader* m_Reader;
|
||||
//State m_CurrentScope = State::Unknown;
|
||||
std::stack<State> m_StateStack;
|
||||
unsigned int m_NextEntityID = 0;
|
||||
std::stack<EntityID> m_EntityStack;
|
||||
std::string m_CurrentComponent;
|
||||
std::string m_CurrentField;
|
||||
std::map<std::string, std::string> m_CurrentAttributes;
|
||||
|
||||
void onStartEntity(const xercesc::Attributes& attrs)
|
||||
{
|
||||
EntityID parent = m_EntityStack.top();
|
||||
|
||||
if (m_Handler->m_OnStartEntityCallback) {
|
||||
std::string name;
|
||||
auto xName = attrs.getValue(XS::ToXMLCh("name"));
|
||||
if (xName != nullptr) {
|
||||
name = XS::ToString(xName);
|
||||
}
|
||||
m_Handler->m_OnStartEntityCallback(m_NextEntityID, parent, name);
|
||||
}
|
||||
|
||||
m_EntityStack.push(m_NextEntityID);
|
||||
m_NextEntityID++;
|
||||
}
|
||||
void onEndEntity()
|
||||
{
|
||||
m_EntityStack.pop();
|
||||
}
|
||||
void onStartEntityRef(const std::string& ref, const xercesc::Attributes& attrs)
|
||||
{
|
||||
xercesc::SAX2XMLReader* parser = xercesc::XMLReaderFactory::createXMLReader();
|
||||
parser->setContentHandler(this);
|
||||
parser->setErrorHandler(this);
|
||||
parser->parse(ref.c_str());
|
||||
delete parser;
|
||||
//m_StateStack.push(State::Entity);
|
||||
}
|
||||
void onEndEntityRef() { }
|
||||
void onStartComponent(const std::string& name)
|
||||
{
|
||||
//LOG_DEBUG(" Component: %s", name.c_str());
|
||||
m_CurrentComponent = name;
|
||||
if (m_Handler->m_OnStartComponentCallback) {
|
||||
m_Handler->m_OnStartComponentCallback(m_EntityStack.top(), name);
|
||||
}
|
||||
}
|
||||
void onEndComponent(const std::string& name) { }
|
||||
void onStartComponentField(const std::string& field, const xercesc::Attributes& attrs)
|
||||
{
|
||||
//LOG_DEBUG(" Field: %s", field.c_str());
|
||||
m_CurrentField = field;
|
||||
m_CurrentAttributes.clear();
|
||||
for (int i = 0; i < attrs.getLength(); i++) {
|
||||
auto name = attrs.getQName(i);
|
||||
auto value = attrs.getValue(name);
|
||||
//LOG_DEBUG(" %s = %s", (char*)XS::ToString(name), (char*)XS::ToString(value));
|
||||
m_CurrentAttributes[XS::ToString(name).operator std::string()] = XS::ToString(value).operator std::string();
|
||||
}
|
||||
|
||||
if (m_Handler->m_OnStartFieldCallback) {
|
||||
m_Handler->m_OnStartFieldCallback(m_EntityStack.top(), m_CurrentComponent, field, m_CurrentAttributes);
|
||||
}
|
||||
}
|
||||
void onEndComponentField(const std::string& field) { }
|
||||
|
||||
void onFieldData(char* data)
|
||||
{
|
||||
//LOG_DEBUG(" Data: %s", data);
|
||||
|
||||
if (m_Handler->m_OnStartFieldDataCallback) {
|
||||
m_Handler->m_OnStartFieldDataCallback(m_EntityStack.top(), m_CurrentComponent, m_CurrentField, data);
|
||||
}
|
||||
|
||||
xercesc::XMLString::release(&data);
|
||||
}
|
||||
};
|
||||
|
||||
class EntityFileXMLErrorHandler : public xercesc::ErrorHandler
|
||||
{
|
||||
public:
|
||||
void warning(const xercesc::SAXParseException& e) override
|
||||
{
|
||||
reportParseException("Warning", e);
|
||||
}
|
||||
void error(const xercesc::SAXParseException& e) override
|
||||
{
|
||||
reportParseException("Error", e);
|
||||
}
|
||||
void fatalError(const xercesc::SAXParseException& e) override
|
||||
{
|
||||
reportParseException("FATAL ERROR", e);
|
||||
}
|
||||
void resetErrors() override { }
|
||||
|
||||
private:
|
||||
void reportParseException(std::string type, const xercesc::SAXParseException& e)
|
||||
{
|
||||
char* message = xercesc::XMLString::transcode(e.getMessage());
|
||||
char* systemID = xercesc::XMLString::transcode(e.getSystemId());
|
||||
std::cerr << systemID << ":" << e.getLineNumber() << ":" << e.getColumnNumber() << std::endl;
|
||||
std::cerr << type << ": " << message << std::endl;
|
||||
xercesc::XMLString::release(&systemID);
|
||||
xercesc::XMLString::release(&message);
|
||||
}
|
||||
};
|
||||
|
||||
class EntityFile : public Resource
|
||||
{
|
||||
friend class ResourceManager;
|
||||
private:
|
||||
EntityFile(boost::filesystem::path path);
|
||||
~EntityFile();
|
||||
|
||||
public:
|
||||
static std::size_t GetTypeStride(std::string typeName);
|
||||
static void WriteAttributeData(char* outData, const ComponentInfo::Field_t& field, const std::map<std::string, std::string>& attributes);
|
||||
static void WriteValueData(char* outData, const ComponentInfo::Field_t& field, const char* valueData);
|
||||
|
||||
xercesc::XMLGrammarPool* GrammarPool() const { return m_GrammarPool; }
|
||||
|
||||
void Parse(const EntityFileHandler* handler) const;
|
||||
|
||||
private:
|
||||
boost::filesystem::path m_FilePath;
|
||||
xercesc::XMLGrammarPool* m_GrammarPool;
|
||||
xercesc::SAX2XMLReader* m_SAX2XMLReader;
|
||||
//std::map<std::string, ::ComponentInfo> m_ComponentInfo;
|
||||
//std::vector<std::string> m_EntityReferences;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,28 @@
|
||||
#ifndef EntityFileParser_h__
|
||||
#define EntityFileParser_h__
|
||||
|
||||
#include "EntityFile.h"
|
||||
#include "World.h"
|
||||
|
||||
class EntityFileParser
|
||||
{
|
||||
public:
|
||||
EntityFileParser(const EntityFile* entityFile);
|
||||
|
||||
void MergeEntities(World* world, EntityID parent = EntityID_Invalid);
|
||||
|
||||
private:
|
||||
const EntityFile* m_EntityFile;
|
||||
EntityFileHandler m_Handler;
|
||||
World* m_World = nullptr;
|
||||
// Maps EntityIDs local to the file to real IDs in the world after they've been
|
||||
// created in order to resolve parent-child relationships.
|
||||
std::map<EntityID, EntityID> m_EntityIDMapper;
|
||||
|
||||
void onStartEntity(EntityID entity, EntityID parent, const std::string& name);
|
||||
void onStartComponent(EntityID entity, const std::string& component);
|
||||
void onStartComponentField(EntityID entity, const std::string& componentType, const std::string& fieldName, const std::map<std::string, std::string>& attributes);
|
||||
void onFieldData(EntityID entity, const std::string& componentType, const std::string& fieldName, const char* fieldData);
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,36 @@
|
||||
#ifndef EntityFilePreprocessor_h__
|
||||
#define EntityFilePreprocessor_h__
|
||||
|
||||
#include <xercesc/framework/psvi/XSElementDeclaration.hpp>
|
||||
#include <xercesc/framework/psvi/XSComplexTypeDefinition.hpp>
|
||||
#include <xercesc/framework/psvi/XSParticle.hpp>
|
||||
#include <xercesc/framework/psvi/XSModelGroup.hpp>
|
||||
#include <xercesc/framework/psvi/XSAnnotation.hpp>
|
||||
#include <xercesc/framework/psvi/XSValue.hpp>
|
||||
#include <xercesc/framework/MemBufFormatTarget.hpp>
|
||||
#include <xercesc/framework/MemBufInputSource.hpp>
|
||||
#include <xercesc/parsers/XercesDOMParser.hpp>
|
||||
|
||||
#include "Util/XercesString.h"
|
||||
#include "ResourceManager.h"
|
||||
#include "World.h"
|
||||
#include "EntityFile.h"
|
||||
|
||||
class EntityFilePreprocessor
|
||||
{
|
||||
public:
|
||||
EntityFilePreprocessor(const EntityFile* entityFile);
|
||||
|
||||
void RegisterComponents(World* world);
|
||||
|
||||
private:
|
||||
const EntityFile* m_EntityFile;
|
||||
std::map<std::string, unsigned int> m_ComponentCounts;
|
||||
std::map<std::string, ComponentInfo> m_ComponentInfo;
|
||||
|
||||
void onStartComponent(EntityID entity, std::string type);
|
||||
void parseComponentInfo();
|
||||
void parseDefaults();
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,39 @@
|
||||
#ifndef EntityFileWriter_h__
|
||||
#define EntityFileWriter_h__
|
||||
|
||||
#include <boost/filesystem.hpp>
|
||||
#include <xercesc/dom/DOM.hpp>
|
||||
#include <xercesc/util/XMLString.hpp>
|
||||
#include <xercesc/util/PlatformUtils.hpp>
|
||||
#include <xercesc/framework/LocalFileFormatTarget.hpp>
|
||||
|
||||
#include "Util/XercesString.h"
|
||||
#include "EntityFile.h"
|
||||
#include "World.h"
|
||||
|
||||
class EntityFileWriter
|
||||
{
|
||||
public:
|
||||
EntityFileWriter(boost::filesystem::path file)
|
||||
: m_FilePath(file)
|
||||
{
|
||||
using namespace xercesc;
|
||||
m_DOMImplementation = DOMImplementationRegistry::getDOMImplementation(XS::ToXMLCh("LS"));
|
||||
m_DOMLSSerializer = static_cast<DOMImplementationLS*>(m_DOMImplementation)->createLSSerializer();
|
||||
m_DOMLSSerializer->getDomConfig()->setParameter(XMLUni::fgDOMWRTDiscardDefaultContent, true);
|
||||
m_DOMLSSerializer->getDomConfig()->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true);
|
||||
}
|
||||
|
||||
void WriteWorld(World* world);
|
||||
void WriteEntity(World* world, EntityID entity);
|
||||
|
||||
private:
|
||||
boost::filesystem::path m_FilePath;
|
||||
xercesc::DOMImplementation* m_DOMImplementation;
|
||||
xercesc::DOMLSSerializer* m_DOMLSSerializer;
|
||||
|
||||
void appendEntityChildren(xercesc::DOMElement* parentElement, const World* world, EntityID entity);
|
||||
void appentEntityComponents(xercesc::DOMElement* parentElemetn, const World* world, EntityID entity);
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,141 +0,0 @@
|
||||
#ifndef EntityXMLFile_h__
|
||||
#define EntityXMLFile_h__
|
||||
|
||||
#include <sstream>
|
||||
|
||||
#include "../Common.h"
|
||||
#include "../GLM.h"
|
||||
#include <xercesc/dom/DOM.hpp>
|
||||
#include <xercesc/dom/DOMLSInput.hpp>
|
||||
#include <xercesc/dom/DOMElement.hpp>
|
||||
#include <xercesc/framework/Wrapper4InputSource.hpp>
|
||||
#include <xercesc/framework/LocalFileInputSource.hpp>
|
||||
#include <xercesc/framework/LocalFileFormatTarget.hpp>
|
||||
#include <xercesc/framework/MemBufFormatTarget.hpp>
|
||||
#include <xercesc/framework/MemBufInputSource.hpp>
|
||||
#include <xercesc/sax/HandlerBase.hpp>
|
||||
#include <xercesc/util/PlatformUtils.hpp>
|
||||
#include <xercesc/util/XMLFloat.hpp>
|
||||
#include <xercesc/framework/XMLGrammarPoolImpl.hpp>
|
||||
#include <xercesc/parsers/XercesDOMParser.hpp>
|
||||
#include <xercesc/framework/psvi/XSElementDeclaration.hpp>
|
||||
#include <xercesc/framework/psvi/XSComplexTypeDefinition.hpp>
|
||||
#include <xercesc/framework/psvi/XSParticle.hpp>
|
||||
#include <xercesc/framework/psvi/XSModelGroup.hpp>
|
||||
#include <xercesc/framework/psvi/XSAnnotation.hpp>
|
||||
#include <xercesc/framework/psvi/XSValue.hpp>
|
||||
#include <xercesc/framework/XMLValidator.hpp>
|
||||
|
||||
#include "ResourceManager.h"
|
||||
#include "Entity.h"
|
||||
#include "ComponentInfo.h"
|
||||
class World;
|
||||
|
||||
class EntityPreprocessorXMLErrorHandler : public xercesc::DOMErrorHandler
|
||||
{
|
||||
public:
|
||||
bool handleError(const xercesc::DOMError &e) override
|
||||
{
|
||||
char* message = xercesc::XMLString::transcode(e.getMessage());
|
||||
std::cerr << "Preprocessor DOMError: " << message << std::endl;
|
||||
xercesc::XMLString::release(&message);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
class EntityParserXMLErrorHandler : public xercesc::ErrorHandler
|
||||
{
|
||||
public:
|
||||
void warning(const xercesc::SAXParseException& e) override
|
||||
{
|
||||
reportParseException("Warning", e);
|
||||
}
|
||||
void error(const xercesc::SAXParseException& e) override
|
||||
{
|
||||
reportParseException("Error", e);
|
||||
}
|
||||
void fatalError(const xercesc::SAXParseException& e) override
|
||||
{
|
||||
reportParseException("FATAL ERROR", e);
|
||||
}
|
||||
void resetErrors() override { }
|
||||
|
||||
private:
|
||||
void reportParseException(std::string type, const xercesc::SAXParseException& e)
|
||||
{
|
||||
char* message = xercesc::XMLString::transcode(e.getMessage());
|
||||
char* systemID = xercesc::XMLString::transcode(e.getSystemId());
|
||||
std::cerr << systemID << ":" << e.getLineNumber() << ":" << e.getColumnNumber() << std::endl;
|
||||
std::cerr << type << ": " << message << std::endl;
|
||||
xercesc::XMLString::release(&systemID);
|
||||
xercesc::XMLString::release(&message);
|
||||
}
|
||||
};
|
||||
|
||||
class XSTR
|
||||
{
|
||||
public:
|
||||
XSTR(const XMLCh* const xmlString)
|
||||
{
|
||||
m_AsChar = xercesc::XMLString::transcode(xmlString);
|
||||
}
|
||||
|
||||
XSTR(const char* normalString)
|
||||
{
|
||||
m_AsXMLCh = xercesc::XMLString::transcode(normalString);
|
||||
}
|
||||
|
||||
~XSTR()
|
||||
{
|
||||
if (m_AsChar != nullptr) {
|
||||
xercesc::XMLString::release(&m_AsChar);
|
||||
}
|
||||
if (m_AsXMLCh != nullptr) {
|
||||
xercesc::XMLString::release(&m_AsXMLCh);
|
||||
}
|
||||
}
|
||||
|
||||
operator const char*() const { return m_AsChar; }
|
||||
operator const XMLCh*() const { return m_AsXMLCh; }
|
||||
|
||||
private:
|
||||
char* m_AsChar = nullptr;
|
||||
XMLCh* m_AsXMLCh = nullptr;
|
||||
};
|
||||
|
||||
class EntityXMLFile : public Resource
|
||||
{
|
||||
friend class ResourceManager;
|
||||
|
||||
private:
|
||||
EntityXMLFile(std::string path);
|
||||
|
||||
public:
|
||||
~EntityXMLFile();
|
||||
|
||||
void PopulateWorld(World* world);
|
||||
|
||||
private:
|
||||
static unsigned int InstanceCount;
|
||||
|
||||
std::string m_EntityFile;
|
||||
xercesc::XMLGrammarPool* m_GrammarPool = nullptr;
|
||||
EntityParserXMLErrorHandler* m_ErrorHandler = nullptr;
|
||||
xercesc::XercesDOMParser* m_DOMParser = nullptr;
|
||||
xercesc::DOMDocument* m_DOMDocument = nullptr;
|
||||
std::map<std::string, ComponentInfo> m_ComponentInfo;
|
||||
|
||||
// Preprocesses the entity file to insert include-by-copy child entities
|
||||
// TODO: Make this work in memory instead of saving to file
|
||||
void preprocess(std::string inPath, std::string outPath);
|
||||
|
||||
void parseComponentInfo();
|
||||
void parseDefaults();
|
||||
void predictComponentAllocation();
|
||||
void parseEntityGraph(World* world, xercesc::DOMElement* parent, EntityID parentEntity);
|
||||
std::size_t getTypeStride(std::string typeName);
|
||||
float getFloatAttribute(const xercesc::DOMElement* element, const char* attribute) const;
|
||||
void writeData(const xercesc::DOMElement* element, std::string typeName, char* outData);
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -13,6 +13,8 @@
|
||||
relay = decltype(relay)(std::bind(handler, this, std::placeholders::_1)); \
|
||||
m_EventBroker->Subscribe(relay);
|
||||
|
||||
typedef unsigned int EventID;
|
||||
|
||||
class EventBroker;
|
||||
|
||||
class BaseEventRelay
|
||||
@@ -31,6 +33,7 @@ public:
|
||||
virtual bool Receive(const std::shared_ptr<Event> event) = 0;
|
||||
|
||||
protected:
|
||||
EventID m_EventID;
|
||||
std::string m_ContextTypeName;
|
||||
std::string m_EventTypeName;
|
||||
EventBroker* m_Broker;
|
||||
@@ -95,6 +98,7 @@ public:
|
||||
|
||||
private:
|
||||
bool m_IsProcessing = false;
|
||||
EventID m_NextEventID = 0;
|
||||
|
||||
typedef std::string ContextTypeName_t; // typeid(ContextType).name()
|
||||
typedef std::string EventTypeName_t; // typeid(EventType).name()
|
||||
@@ -103,14 +107,14 @@ private:
|
||||
typedef std::unordered_map<ContextTypeName_t, EventRelays_t> ContextRelays_t;
|
||||
ContextRelays_t m_ContextRelays;
|
||||
std::vector<BaseEventRelay*> m_RelaysToSubscribe;
|
||||
std::vector<BaseEventRelay*> m_RelaysToUnsubscribe;
|
||||
std::vector<std::tuple<EventID, ContextTypeName_t, EventTypeName_t>> m_RelaysToUnsubscribe;
|
||||
|
||||
typedef std::list<std::pair<EventTypeName_t, std::shared_ptr<Event>>> EventQueue_t;
|
||||
std::shared_ptr<EventQueue_t> m_EventQueueRead;
|
||||
std::shared_ptr<EventQueue_t> m_EventQueueWrite;
|
||||
|
||||
void subscribeImmediate(BaseEventRelay& relay);
|
||||
void unsubscribeImmediate(BaseEventRelay& relay);
|
||||
void unsubscribeImmediate(std::tuple<EventID, ContextTypeName_t, EventTypeName_t> identifier);
|
||||
};
|
||||
|
||||
template <typename EventType>
|
||||
|
||||
@@ -253,7 +253,7 @@ public:
|
||||
}
|
||||
|
||||
//Postfix increment i.e. iter++. Prefer pre-increment (++iter) for efficiency.
|
||||
MemoryPoolForwardIterator& operator++(int)
|
||||
MemoryPoolForwardIterator operator++(int)
|
||||
{
|
||||
MemoryPoolForwardIterator<T> copyIter(*this);
|
||||
operator++();
|
||||
|
||||
@@ -0,0 +1,104 @@
|
||||
#ifndef OctTree_h__
|
||||
#define OctTree_h__
|
||||
|
||||
#include "Core/AABB.h"
|
||||
|
||||
class Ray;
|
||||
|
||||
class OctTree
|
||||
{
|
||||
public:
|
||||
struct Output
|
||||
{
|
||||
float CollideDistance;
|
||||
};
|
||||
|
||||
OctTree();
|
||||
~OctTree();
|
||||
//For the root OctTree, [octTreeBounds] should be a box containing the entire level.
|
||||
OctTree(const AABB& octTreeBounds, int subDivisions);
|
||||
|
||||
//We cannot copy the OctTree as of now, because of the recursive dynamic allocation.
|
||||
//Define these if the OctTree suddenly needs to be copied, think of the children OctChild* ptrs.
|
||||
OctTree(const OctTree& other) = delete;
|
||||
OctTree(const OctTree&& other) = delete;
|
||||
OctTree& operator= (const OctTree& other) = delete;
|
||||
//Add a dynamic object (one that moves around) into the tree.
|
||||
void AddDynamicObject(const AABB& box);
|
||||
//Add a static object (that does not move) into the tree.
|
||||
void AddStaticObject(const AABB& box);
|
||||
//Get the boxes that are in the same area as the input [box], the boxes are put in [outBoxes].
|
||||
void BoxesInSameRegion(const AABB& box, std::vector<AABB>& outBoxes);
|
||||
//Empty the tree of all objects, static and dynamic.
|
||||
void ClearObjects();
|
||||
//Empty the tree of all dynamic objects. Static objects remain in the tree.
|
||||
void ClearDynamicObjects();
|
||||
|
||||
//Returns true if the ray collides with something in the tree. Result is written to [data].
|
||||
bool RayCollides(const Ray& ray, Output& data);
|
||||
//Returns true if the box collides with something in the tree.
|
||||
//On collision with a box, that box is written to [outBoxIntersected].
|
||||
//Note: More efficient than calling BoxesInSameRegion from outside and testing there.
|
||||
bool BoxCollides(const AABB& boxToTest, AABB& outBoxIntersected);
|
||||
|
||||
private:
|
||||
struct OctChild; //Fwd declaration;
|
||||
struct ContainedObject
|
||||
{
|
||||
ContainedObject()
|
||||
: Box(AABB())
|
||||
, Checked(false)
|
||||
{}
|
||||
ContainedObject(AABB box)
|
||||
: Box(box)
|
||||
, Checked(false)
|
||||
{}
|
||||
AABB Box;
|
||||
bool Checked;
|
||||
};
|
||||
OctChild* m_Root;
|
||||
std::vector<ContainedObject> m_StaticObjects;
|
||||
std::vector<ContainedObject> m_DynamicObjects;
|
||||
|
||||
bool m_UpdatedOnce;
|
||||
unsigned int m_BoxID;
|
||||
glm::vec3 m_PrevPos;
|
||||
glm::quat m_PrevOri;
|
||||
|
||||
void falsifyObjectChecks();
|
||||
|
||||
struct OctChild
|
||||
{
|
||||
~OctChild();
|
||||
OctChild(const AABB& octTreeBounds,
|
||||
int subDivisions,
|
||||
std::vector<OctTree::ContainedObject>& staticObjects,
|
||||
std::vector<OctTree::ContainedObject>& dynamicObjects);
|
||||
OctChild(const OctChild& other) = delete;
|
||||
OctChild(const OctChild&& other) = delete;
|
||||
OctChild& operator= (const OctChild& other) = delete;
|
||||
void AddDynamicObject(const AABB& box);
|
||||
void AddStaticObject(const AABB& box);
|
||||
void BoxesInSameRegion(const AABB& box, std::vector<AABB>& outBoxes) const;
|
||||
void ClearObjects();
|
||||
void ClearDynamicObjects();
|
||||
bool RayCollides(const Ray& ray, Output& data) const;
|
||||
bool BoxCollides(const AABB& boxToTest, AABB& outBoxIntersected) const;
|
||||
|
||||
OctChild* m_Children[8];
|
||||
//Indices into the lists in OctTree.
|
||||
std::vector<int> m_StaticObjIndices;
|
||||
std::vector<int> m_DynamicObjIndices;
|
||||
AABB m_Box;
|
||||
//Reference to the lists in OctTree.
|
||||
std::vector<OctTree::ContainedObject>& m_StaticObjectsRef;
|
||||
std::vector<OctTree::ContainedObject>& m_DynamicObjectsRef;
|
||||
|
||||
inline bool hasChildren() const;
|
||||
int childIndexContainingPoint(const glm::vec3& point) const;
|
||||
std::vector<int> childIndicesContainingBox(const AABB& box) const;
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,31 @@
|
||||
#ifndef Ray_h__
|
||||
#define Ray_h__
|
||||
|
||||
#include "../GLM.h"
|
||||
#include "Common.h"
|
||||
|
||||
class Ray
|
||||
{
|
||||
public:
|
||||
Ray(const glm::vec3& origin, const glm::vec3& dir)
|
||||
: m_Origin(origin)
|
||||
, m_Direction(glm::normalize(dir))
|
||||
{
|
||||
DEBUG_IF(true) {
|
||||
if (glm::any(glm::isnan(m_Direction))) {
|
||||
LOG_WARNING("Ray Direction was set to the zero-vector, expect unknown side effects and/or crashes.");
|
||||
}
|
||||
}
|
||||
}
|
||||
const glm::vec3& Origin() const { return m_Origin; }
|
||||
const glm::vec3& Direction() const { return m_Direction; }
|
||||
//Sets the ray origin at parameter.
|
||||
void SetOrigin(const glm::vec3& origin) { m_Origin = origin; }
|
||||
//Normalizes the parameter and sets direction to it.
|
||||
void SetDirection(const glm::vec3& direction) { m_Direction = glm::normalize(direction); }
|
||||
private:
|
||||
glm::vec3 m_Origin;
|
||||
glm::vec3 m_Direction;
|
||||
};
|
||||
|
||||
#endif // Ray_h__
|
||||
@@ -7,10 +7,13 @@
|
||||
|
||||
class System
|
||||
{
|
||||
friend class SystemPipeline;
|
||||
|
||||
protected:
|
||||
System(EventBroker* eventBroker)
|
||||
: m_EventBroker(eventBroker)
|
||||
{ }
|
||||
virtual ~System() = default;
|
||||
|
||||
EventBroker* m_EventBroker;
|
||||
};
|
||||
@@ -24,6 +27,7 @@ protected:
|
||||
: System(eventBroker)
|
||||
, m_ComponentType(componentType)
|
||||
{ }
|
||||
virtual ~PureSystem() = default;
|
||||
|
||||
const std::string m_ComponentType;
|
||||
|
||||
@@ -38,6 +42,7 @@ protected:
|
||||
ImpureSystem(EventBroker* eventBroker)
|
||||
: System(eventBroker)
|
||||
{ }
|
||||
virtual ~ImpureSystem() = default;
|
||||
|
||||
virtual void Update(World* world, double dt) = 0;
|
||||
};
|
||||
|
||||
@@ -14,23 +14,28 @@ public:
|
||||
{ }
|
||||
~SystemPipeline()
|
||||
{
|
||||
for (auto& pair : m_PureSystems) {
|
||||
for (auto& system : pair.second) {
|
||||
delete system;
|
||||
for (UnorderedSystems& group : m_OrderedSystemGroups) {
|
||||
for (auto& pair : group.Systems) {
|
||||
delete pair.second;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename... Arguments>
|
||||
void AddSystem(Arguments... args)
|
||||
//All systems with orderlevel 0 will be updated first, then 1, 2, etc.
|
||||
void AddSystem(int updateOrderLevel, Arguments... args)
|
||||
{
|
||||
if (updateOrderLevel + 1 > m_OrderedSystemGroups.size()) {
|
||||
m_OrderedSystemGroups.resize(updateOrderLevel + 1);
|
||||
}
|
||||
UnorderedSystems& group = m_OrderedSystemGroups[updateOrderLevel];
|
||||
System* system = new T(m_EventBroker, args...);
|
||||
m_Systems[typeid(T).name()] = system;
|
||||
group.Systems[typeid(T).name()] = system;
|
||||
|
||||
if (std::is_base_of<PureSystem, T>::value) {
|
||||
PureSystem* pureSystem = static_cast<PureSystem*>(system);
|
||||
if (!pureSystem->m_ComponentType.empty()) {
|
||||
m_PureSystems[pureSystem->m_ComponentType].push_back(pureSystem);
|
||||
group.PureSystems[pureSystem->m_ComponentType].push_back(pureSystem);
|
||||
} else {
|
||||
LOG_ERROR("Failed to add pure system \"%s\": Missing component type!", typeid(T).name());
|
||||
}
|
||||
@@ -38,41 +43,47 @@ public:
|
||||
|
||||
if (std::is_base_of<ImpureSystem, T>::value) {
|
||||
ImpureSystem* impureSystem = static_cast<ImpureSystem*>(system);
|
||||
m_ImpureSystems.push_back(impureSystem);
|
||||
group.ImpureSystems.push_back(impureSystem);
|
||||
}
|
||||
}
|
||||
|
||||
void Update(World* world, double dt)
|
||||
{
|
||||
// Process events
|
||||
for (auto& pair : m_Systems) {
|
||||
m_EventBroker->Process(pair.first);
|
||||
}
|
||||
|
||||
// Update
|
||||
for (auto& pair : m_PureSystems) {
|
||||
const std::string& componentName = pair.first;
|
||||
auto& systems = pair.second;
|
||||
const ComponentPool* pool = world->GetComponents(componentName);
|
||||
if (pool == nullptr) {
|
||||
continue;
|
||||
for (UnorderedSystems& group : m_OrderedSystemGroups) {
|
||||
// Process events
|
||||
for (auto& pair : group.Systems) {
|
||||
m_EventBroker->Process(pair.first);
|
||||
}
|
||||
for (auto& component : *pool) {
|
||||
for (auto& system : systems) {
|
||||
system->UpdateComponent(world, component, dt);
|
||||
|
||||
// Update
|
||||
for (auto& pair : group.PureSystems) {
|
||||
const std::string& componentName = pair.first;
|
||||
auto& systems = pair.second;
|
||||
const ComponentPool* pool = world->GetComponents(componentName);
|
||||
if (pool == nullptr) {
|
||||
continue;
|
||||
}
|
||||
for (auto& component : *pool) {
|
||||
for (auto& system : systems) {
|
||||
system->UpdateComponent(world, component, dt);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for (auto& system : m_ImpureSystems) {
|
||||
system->Update(world, dt);
|
||||
for (auto& system : group.ImpureSystems) {
|
||||
system->Update(world, dt);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
EventBroker* m_EventBroker;
|
||||
std::map<std::string, System*> m_Systems;
|
||||
std::map<std::string, std::vector<PureSystem*>> m_PureSystems;
|
||||
std::vector<ImpureSystem*> m_ImpureSystems;
|
||||
struct UnorderedSystems
|
||||
{
|
||||
std::map<std::string, System*> Systems;
|
||||
std::map<std::string, std::vector<PureSystem*>> PureSystems;
|
||||
std::vector<ImpureSystem*> ImpureSystems;
|
||||
};
|
||||
std::vector<UnorderedSystems> m_OrderedSystemGroups;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,12 @@
|
||||
// Example:
|
||||
// DEBUG_IF(condition) {
|
||||
// // This code is executed only in debug mode and if condition is true.
|
||||
// }
|
||||
// NOTE: condition statement is not executed at all in release mode.
|
||||
#ifndef DEBUG_IF
|
||||
#ifndef DEBUG
|
||||
#define DEBUG_IF(c) if(c)
|
||||
#else
|
||||
#define DEBUG_IF(c) if(false)
|
||||
#endif
|
||||
#endif
|
||||
@@ -0,0 +1,46 @@
|
||||
#ifndef Util_XercesString_h__
|
||||
#define Util_XercesString_h__
|
||||
|
||||
#include <string>
|
||||
#include <xercesc/util/XercesDefs.hpp>
|
||||
#include <xercesc/util/XMLString.hpp>
|
||||
|
||||
namespace XS
|
||||
{
|
||||
|
||||
class ToString
|
||||
{
|
||||
public:
|
||||
ToString(const XMLCh* const str) { m_AsChar = xercesc::XMLString::transcode(str); }
|
||||
~ToString()
|
||||
{
|
||||
if (m_AsChar != nullptr) {
|
||||
xercesc::XMLString::release(&m_AsChar);
|
||||
}
|
||||
}
|
||||
|
||||
operator std::string() const { return std::string(m_AsChar); }
|
||||
private:
|
||||
char* m_AsChar = nullptr;
|
||||
};
|
||||
|
||||
class ToXMLCh
|
||||
{
|
||||
public:
|
||||
ToXMLCh(const std::string str) { m_Transcoded = xercesc::XMLString::transcode(str.c_str()); }
|
||||
ToXMLCh(const char* str) { m_Transcoded = xercesc::XMLString::transcode(str); }
|
||||
~ToXMLCh()
|
||||
{
|
||||
if (m_Transcoded != nullptr) {
|
||||
xercesc::XMLString::release(&m_Transcoded);
|
||||
}
|
||||
}
|
||||
|
||||
operator const XMLCh*() const { return m_Transcoded; }
|
||||
private:
|
||||
XMLCh* m_Transcoded = nullptr;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -16,13 +16,14 @@ public:
|
||||
EntityID CreateEntity(EntityID parent = 0);
|
||||
// Delete entity and all components within
|
||||
void DeleteEntity(EntityID entity);
|
||||
|
||||
// Check if an entity exists
|
||||
bool ValidEntity(EntityID entity) const;
|
||||
// Register a component type and allocate space for it
|
||||
void RegisterComponent(ComponentInfo& ci);
|
||||
// Attach a component to an entity and fill it with default values
|
||||
ComponentWrapper AttachComponent(EntityID entity, std::string componentType);
|
||||
// Check if an entity has a component
|
||||
bool HasComponent(EntityID entity, std::string componentType);
|
||||
bool HasComponent(EntityID entity, std::string componentType) const;
|
||||
// Get a component of an entity
|
||||
ComponentWrapper GetComponent(EntityID entity, std::string componentType);
|
||||
// Delete a component off an entity
|
||||
@@ -37,14 +38,19 @@ public:
|
||||
const std::unordered_map<std::string, ComponentPool*>& GetComponentPools() const { return m_ComponentPools; }
|
||||
// Get the entity children map
|
||||
const std::unordered_multimap<EntityID, EntityID>& GetEntityChildren() const { return m_EntityChildren; }
|
||||
// Set the textual name of an entity
|
||||
void SetName(EntityID entity, const std::string& name);
|
||||
// Get the textual name of an entity
|
||||
std::string GetName(EntityID entity) const;
|
||||
|
||||
private:
|
||||
EntityID m_CurrentEntityID = 1;
|
||||
EntityID m_CurrentEntityID = 0;
|
||||
|
||||
std::unordered_map<EntityID, EntityID> m_EntityParents;
|
||||
// TODO: This should be a more effective structure
|
||||
std::unordered_multimap<EntityID, EntityID> m_EntityChildren;
|
||||
std::unordered_map<std::string, ComponentPool*> m_ComponentPools;
|
||||
std::unordered_map<EntityID, std::string> m_EntityNames;
|
||||
|
||||
EntityID generateEntityID();
|
||||
};
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#include <imgui/imgui.h>
|
||||
#include <glm/gtx/common.hpp>
|
||||
#include <boost/filesystem/path.hpp>
|
||||
#include <nativefiledialog/nfd.h>
|
||||
#include "../Core/System.h"
|
||||
#include "../Core/EMousePress.h"
|
||||
#include "../Core/EMouseRelease.h"
|
||||
@@ -11,6 +12,9 @@
|
||||
#include "../Rendering/EPicking.h"
|
||||
#include "../Core/EFileDropped.h"
|
||||
#include "../Rendering/RenderQueueFactory.h"
|
||||
#include "../Core/EntityFilePreprocessor.h"
|
||||
#include "../Core/EntityFileParser.h"
|
||||
#include "../Core/EntityFileWriter.h"
|
||||
|
||||
class EditorSystem : public ImpureSystem
|
||||
{
|
||||
@@ -25,6 +29,9 @@ private:
|
||||
|
||||
bool m_Enabled;
|
||||
bool m_Visible;
|
||||
boost::filesystem::path m_DefaultEntityDir;
|
||||
boost::filesystem::path m_CurrentFile;
|
||||
std::map<boost::filesystem::path, EntityID> m_CurrentFiles;
|
||||
std::vector<glm::vec2> m_PickingQueue;
|
||||
|
||||
enum class WidgetMode
|
||||
@@ -41,22 +48,26 @@ private:
|
||||
Global
|
||||
} m_WidgetSpace = WidgetSpace::Global;
|
||||
|
||||
EntityID m_Widget = 0;
|
||||
EntityID m_WidgetX = 0;
|
||||
EntityID m_WidgetPlaneX = 0;
|
||||
EntityID m_WidgetY = 0;
|
||||
EntityID m_WidgetPlaneY = 0;
|
||||
EntityID m_WidgetZ = 0;
|
||||
EntityID m_WidgetPlaneZ = 0;
|
||||
EntityID m_WidgetOrigin = 0;
|
||||
EntityID m_Widget = EntityID_Invalid;
|
||||
EntityID m_WidgetX = EntityID_Invalid;
|
||||
EntityID m_WidgetPlaneX = EntityID_Invalid;
|
||||
EntityID m_WidgetY = EntityID_Invalid;
|
||||
EntityID m_WidgetPlaneY = EntityID_Invalid;
|
||||
EntityID m_WidgetZ = EntityID_Invalid;
|
||||
EntityID m_WidgetPlaneZ = EntityID_Invalid;
|
||||
EntityID m_WidgetOrigin = EntityID_Invalid;
|
||||
glm::vec3 m_WidgetCurrentAxis;
|
||||
float m_WidgetPickingDepth = 0.f;
|
||||
|
||||
EntityID m_Selection = 0;
|
||||
EntityID m_LastSelection = 0;
|
||||
EntityID m_Selection = EntityID_Invalid;
|
||||
EntityID m_LastSelection = EntityID_Invalid;
|
||||
EntityID m_UIDraggingEntity = EntityID_Invalid;
|
||||
glm::vec3 m_Position;
|
||||
std::string m_LastDroppedFile;
|
||||
|
||||
static boost::filesystem::path openDialog(boost::filesystem::path defaultPath);
|
||||
static boost::filesystem::path saveDialog(boost::filesystem::path defaultPath);
|
||||
|
||||
EventRelay<EditorSystem, Events::InputCommand> m_EInputCommand;
|
||||
bool OnInputCommand(const Events::InputCommand& e);
|
||||
EventRelay<EditorSystem, Events::MouseRelease> m_EMouseRelease;
|
||||
@@ -70,10 +81,15 @@ private:
|
||||
EventRelay<EditorSystem, Events::FileDropped> m_EFileDropped;
|
||||
bool OnFileDropped(const Events::FileDropped& e);
|
||||
|
||||
void createWidget();
|
||||
void updateWidget();
|
||||
void setWidgetMode(WidgetMode newMode);
|
||||
void setWidgetSpace(WidgetSpace space);
|
||||
void drawUI(World* world, double dt);
|
||||
bool createDeleteButton(std::string componentType);
|
||||
bool createEntityNode(World* world, EntityID entity);
|
||||
void changeParent(EntityID entity, EntityID newParent);
|
||||
void fileImport(World* world);
|
||||
void fileSave(World* world);
|
||||
void fileSaveAs(World* world);
|
||||
};
|
||||
@@ -1,14 +1,84 @@
|
||||
#ifndef Client_h__
|
||||
#define Client_h__
|
||||
|
||||
#include <boost\asio.hpp>
|
||||
#include <string>
|
||||
#include <ctime>
|
||||
|
||||
class Client
|
||||
#include <glm/common.hpp>
|
||||
#include <boost/asio.hpp>
|
||||
|
||||
#include "Network/Network.h"
|
||||
#include "Network/MessageType.h"
|
||||
#include "Network/PlayerDefinition.h"
|
||||
#include "Network/SnapshotDefinitions.h"
|
||||
#include "Core/World.h"
|
||||
#include "Core/EventBroker.h"
|
||||
#include "Core/ConfigFile.h"
|
||||
#include "Input/EInputCommand.h"
|
||||
|
||||
class Client : public Network
|
||||
{
|
||||
Client();
|
||||
~Client();
|
||||
public:
|
||||
Client(ConfigFile* config);
|
||||
~Client();
|
||||
void Start(World* world, EventBroker* eventBroker) override;
|
||||
void Update() override;
|
||||
void Close();
|
||||
private:
|
||||
// Assio UDP logic
|
||||
boost::asio::ip::udp::endpoint m_ReceiverEndpoint;
|
||||
boost::asio::io_service m_IOService;
|
||||
boost::asio::ip::udp::socket m_Socket;
|
||||
|
||||
// Sending message to server logic
|
||||
int bytesRead = -1;
|
||||
char readBuf[1024] = { 0 };
|
||||
int snapshotInterval = 33;
|
||||
std::clock_t previousSnapshotMessage = std::clock();
|
||||
|
||||
// Packet loss logic
|
||||
unsigned int m_PacketID = 0;
|
||||
unsigned int m_PreviousPacketID = 0;
|
||||
unsigned int m_SendPacketID = 0;
|
||||
|
||||
// Game logic
|
||||
World* m_World;
|
||||
std::string m_PlayerName;
|
||||
int m_PlayerID = -1;
|
||||
|
||||
// Network logic
|
||||
PlayerDefinition m_PlayerDefinitions[MAXCONNECTIONS];
|
||||
SnapshotDefinitions m_NextSnapshot;
|
||||
bool m_ThreadIsRunning = true;
|
||||
double m_DurationOfPingTime;
|
||||
std::clock_t m_StartPingTime;
|
||||
// Use to check if we should send disconnect message
|
||||
// if game is turned of by closing window.
|
||||
bool m_WasStarted = false;
|
||||
|
||||
// Private member functions
|
||||
void readFromServer();
|
||||
void sendSnapshotToServer();
|
||||
int receive(char* data, size_t length);
|
||||
void send(Packet& packet);
|
||||
void connect();
|
||||
void disconnect();
|
||||
void ping();
|
||||
void moveMessageHead(char*& data, size_t& length, size_t stepSize);
|
||||
void parseMessageType(Packet& packet);
|
||||
void parseEventMessage(Packet& packet);
|
||||
void parseConnect(Packet& packet);
|
||||
void parsePing();
|
||||
void parseServerPing();
|
||||
void parseSnapshot(Packet& packet);
|
||||
void identifyPacketLoss();
|
||||
bool isConnected();
|
||||
EntityID createPlayer();
|
||||
|
||||
// Events
|
||||
EventBroker* m_EventBroker;
|
||||
EventRelay<Client, Events::InputCommand> m_EInputCommand;
|
||||
bool OnInputCommand(const Events::InputCommand &e);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
#ifndef MessageType_h__
|
||||
#define MessageType_h__
|
||||
|
||||
// Message types used by both server and client.
|
||||
// Used to determine what type of message was sent.
|
||||
enum class MessageType
|
||||
{
|
||||
Connect,
|
||||
Disconnect,
|
||||
ClientPing,
|
||||
ServerPing,
|
||||
Message,
|
||||
Snapshot,
|
||||
Event,
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,19 @@
|
||||
#ifndef Network_h__
|
||||
#define Network_h__
|
||||
|
||||
#include "Core/World.h"
|
||||
#include "Core/EventBroker.h"
|
||||
#include "Network/Packet.h"
|
||||
|
||||
#define MAXCONNECTIONS 8
|
||||
#define INPUTSIZE 128
|
||||
|
||||
class Network
|
||||
{
|
||||
public:
|
||||
virtual ~Network() { };
|
||||
virtual void Start(World* m_world, EventBroker *eventBroker) = 0;
|
||||
virtual void Update() = 0;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,61 @@
|
||||
#ifndef Packet_h__
|
||||
#define Packet_h__
|
||||
|
||||
#include <string>
|
||||
#include "Network/MessageType.h"
|
||||
#include "Core/Util/Logging.h"
|
||||
|
||||
// Defines the
|
||||
class Packet
|
||||
{
|
||||
public:
|
||||
// arg1: Type of message (Connect, Disconnect...)
|
||||
// arg2: PacketID for identifying packet loss.
|
||||
Packet(MessageType type, unsigned int& packetID);
|
||||
// Used to create packet from already existing data buffer.
|
||||
Packet(char* data, const int sizeOfPacket);
|
||||
|
||||
~Packet();
|
||||
// Add primitive types like int, float, char...
|
||||
template<typename T>
|
||||
void WritePrimitive(T val)
|
||||
{
|
||||
// Check if we are trying to add more than the package can fit.
|
||||
if (m_MaxPacketSize < m_Offset + sizeof(T)) {
|
||||
LOG_WARNING("Packet AddPrimitive(): You are trying to add more than we have allocated for!");
|
||||
}
|
||||
memcpy(m_Data + m_Offset, &val, sizeof(T));
|
||||
m_Offset += sizeof(T);
|
||||
}
|
||||
// Pops the first element as if it was a primitive.
|
||||
template<typename T>
|
||||
T ReadPrimitive()
|
||||
{
|
||||
if (m_Offset < m_ReturnDataOffset + sizeof(T)) {
|
||||
LOG_WARNING("Packet PopFrontPrimitive(): You are trying to remove more than what exists in this packet!");
|
||||
return -1;
|
||||
}
|
||||
T returnValue;
|
||||
memcpy(&returnValue, m_Data + m_ReturnDataOffset, sizeof(T));
|
||||
m_ReturnDataOffset += sizeof(T);
|
||||
return returnValue;
|
||||
}
|
||||
// Add a string to the message
|
||||
void WriteString(std::string str);
|
||||
// Add data to the message
|
||||
void WriteData(char* data, int sizeOfData);
|
||||
// Pops the first element as if it was a string.
|
||||
std::string ReadString();
|
||||
char* ReadData(int SizeOfData);
|
||||
|
||||
int Size() { return m_Offset; };
|
||||
char* Data() { return m_Data; };
|
||||
|
||||
private:
|
||||
char* m_Data;
|
||||
unsigned int m_ReturnDataOffset = 0;
|
||||
int m_Offset = 0;
|
||||
unsigned int m_MaxPacketSize = 128;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
#ifndef PlayerDefinition_h__
|
||||
#define PlayerDefinition_h__
|
||||
#include <string>
|
||||
|
||||
struct PlayerDefinition {
|
||||
int EntityID = -1;
|
||||
std::string Name = "";
|
||||
boost::asio::ip::udp::endpoint Endpoint;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,12 +1,85 @@
|
||||
#ifndef Server_h__
|
||||
#define Server_h__
|
||||
|
||||
#include <boost\asio.hpp>
|
||||
#include <string>
|
||||
#include <ctime>
|
||||
|
||||
class Server
|
||||
#include <glm/common.hpp>
|
||||
#include <boost/asio/ip/udp.hpp>
|
||||
|
||||
#include "Network/MessageType.h"
|
||||
#include "Network/PlayerDefinition.h"
|
||||
#include "Core/World.h"
|
||||
#include "Core/EventBroker.h"
|
||||
#include "Network/Network.h"
|
||||
|
||||
class Server : public Network
|
||||
{
|
||||
Server();
|
||||
~Server();
|
||||
public:
|
||||
Server();
|
||||
~Server();
|
||||
void Start(World* m_world, EventBroker *eventBroker) override;
|
||||
void Update() override;
|
||||
void Close();
|
||||
|
||||
private:
|
||||
// UDP logic
|
||||
boost::asio::ip::udp::endpoint m_ReceiverEndpoint;
|
||||
boost::asio::io_service m_IOService;
|
||||
boost::asio::ip::udp::socket m_Socket;
|
||||
PlayerDefinition m_PlayerDefinitions[MAXCONNECTIONS];
|
||||
|
||||
// Sending messages to client logic
|
||||
char readBuffer[1024] = { 0 };
|
||||
int bytesRead = 0;
|
||||
// time for previouse message
|
||||
std::clock_t previousePingMessage = std::clock();
|
||||
std::clock_t previousSnapshotMessage = std::clock();
|
||||
std::clock_t timOutTimer = std::clock();
|
||||
// How often we send messages (milliseconds)
|
||||
int intervalMs = 1000;
|
||||
int snapshotInterval = 50;
|
||||
int checkTimeOutInterval = 100;
|
||||
|
||||
//Timers
|
||||
std::clock_t m_StartPingTime;
|
||||
std::clock_t m_StopTimes[8];
|
||||
|
||||
// Game logic
|
||||
World* m_World;
|
||||
EventBroker* m_EventBroker;
|
||||
// vec.size() = ammount of players to create, stores playerID's
|
||||
std::vector<unsigned int> m_PlayersToCreate;
|
||||
|
||||
// Packet loss logic
|
||||
unsigned int m_PacketID;
|
||||
unsigned int m_PreviousPacketID;
|
||||
unsigned int m_SendPacketID;
|
||||
|
||||
// Close logic
|
||||
bool m_ThreadIsRunning = true;
|
||||
|
||||
// Private member functions
|
||||
int receive(char* data, size_t length);
|
||||
void readFromClients();
|
||||
void send(Packet& packet, int playerID);
|
||||
void send(Packet& packet);
|
||||
void moveMessageHead(char*& data, size_t& length, size_t stepSize);
|
||||
void broadcast(std::string message);
|
||||
void broadcast(Packet& packet);
|
||||
void sendSnapshot();
|
||||
void sendPing();
|
||||
void checkForTimeOuts();
|
||||
void disconnect(int i);
|
||||
void parseMessageType(Packet& packet);
|
||||
void parseEvent(Packet& packet);
|
||||
void parseConnect(Packet& packet);
|
||||
void parseDisconnect();
|
||||
void parseClientPing();
|
||||
void parseServerPing();
|
||||
void parseSnapshot(Packet& packet);
|
||||
void identifyPacketLoss();
|
||||
EntityID createPlayer();
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
#ifndef SnapshotDefinitions_h__
|
||||
#define SnapshotDefinitions_h__
|
||||
|
||||
struct SnapshotDefinitions
|
||||
{
|
||||
// "+Forward" is 8 characters * sizeof(char) = 8
|
||||
std::string InputForward;
|
||||
// "+Right" is 6 characters * sizeof(char) = 6
|
||||
std::string InputRight;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -51,7 +51,7 @@ public:
|
||||
if (it != PickingColorsToEntity->end()) {
|
||||
pickData.Entity = it->second;
|
||||
} else {
|
||||
pickData.Entity = 0;
|
||||
pickData.Entity = EntityID_Invalid;
|
||||
}
|
||||
pickData.Position = ScreenCoords::ToWorldPos(screenCoord.x, screenCoord.y, data.Depth, Resolution, ProjectionMatrix, ViewMatrix);
|
||||
|
||||
|
||||
@@ -35,7 +35,7 @@ public:
|
||||
virtual void Draw(RenderQueueCollection& rq) = 0;
|
||||
|
||||
protected:
|
||||
Rectangle m_Resolution = Rectangle(1280, 720);
|
||||
Rectangle m_Resolution = Rectangle::Rectangle(1280, 720);
|
||||
bool m_Fullscreen = false;
|
||||
bool m_VSYNC = false;
|
||||
int m_GLVersion[2];
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
#include "../Core/World.h"
|
||||
#include "PickingPass.h"
|
||||
#include "DrawScenePass.h"
|
||||
#include "DebugCameraInputController.h"
|
||||
|
||||
|
||||
#define TILE_SIZE 16
|
||||
@@ -45,6 +46,8 @@ private:
|
||||
//----------------------Variables----------------------//
|
||||
EventBroker* m_EventBroker;
|
||||
|
||||
std::shared_ptr<DebugCameraInputController<Renderer>> m_DebugCameraInputController;
|
||||
|
||||
Texture* m_ErrorTexture;
|
||||
Texture* m_WhiteTexture;
|
||||
float m_CameraMoveSpeed;
|
||||
|
||||
+19
-1
@@ -13,11 +13,20 @@
|
||||
#include "Input/KeyboardInputHandler.h"
|
||||
#include "Input/MouseInputHandler.h"
|
||||
#include "Core/EKeyDown.h"
|
||||
#include "Core/EntityXMLFile.h"
|
||||
#include "Core/EntityFilePreprocessor.h"
|
||||
#include "Core/SystemPipeline.h"
|
||||
#include "RaptorCopterSystem.h"
|
||||
#include "PlayerSystem.h"
|
||||
#include "Editor/EditorSystem.h"
|
||||
#include "Core/EntityFile.h"
|
||||
#include "Core/EntityFileParser.h"
|
||||
|
||||
// Network
|
||||
#include <boost/thread.hpp>
|
||||
#include "Network/Network.h"
|
||||
#include "Network/Server.h"
|
||||
#include "Network/Client.h"
|
||||
|
||||
|
||||
class Game
|
||||
{
|
||||
@@ -39,12 +48,21 @@ private:
|
||||
World* m_World;
|
||||
SystemPipeline* m_SystemPipeline;
|
||||
RenderQueueFactory* m_RenderQueueFactory;
|
||||
// Network variables
|
||||
boost::thread m_NetworkThread;
|
||||
|
||||
// Network methods
|
||||
void networkFunction();
|
||||
Network* m_ClientOrServer;
|
||||
bool m_IsClientOrServer = false;
|
||||
|
||||
EventRelay<Game, Events::InputCommand> m_EInputCommand;
|
||||
bool debugOnInputCommand(const Events::InputCommand& e);
|
||||
|
||||
void debugInitialize();
|
||||
void debugTick(double dt);
|
||||
EventRelay<Client, Events::KeyDown> m_EKeyDown;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
#ifndef HealthSystem_h__
|
||||
#define HealthSystem_h__
|
||||
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <glm/common.hpp>
|
||||
|
||||
#include "Common.h"
|
||||
#include "Core/System.h"
|
||||
#include "Core\EPlayerDamage.h";
|
||||
#include "Core\EPlayerHealthPickup.h";
|
||||
#include "Core\EPlayerDeath.h";
|
||||
|
||||
#include <tuple>
|
||||
#include <vector>
|
||||
|
||||
class HealthSystem : public PureSystem
|
||||
{
|
||||
public:
|
||||
HealthSystem(EventBroker* eventBroker);
|
||||
|
||||
//updatecomponent
|
||||
virtual void UpdateComponent(World* world, ComponentWrapper& health, double dt) override;
|
||||
|
||||
private:
|
||||
//methods which will take care of specific events
|
||||
EventRelay<HealthSystem, Events::PlayerDamage> m_EPlayerDamage;
|
||||
bool HealthSystem::OnPlayerDamaged(const Events::PlayerDamage& e);
|
||||
EventRelay<HealthSystem, Events::PlayerHealthPickup> m_EPlayerHealthPickup;
|
||||
bool HealthSystem::OnPlayerHealthPickup(const Events::PlayerHealthPickup& e);
|
||||
|
||||
//vector which will keep track of health changes
|
||||
std::vector<std::tuple<EntityID, double>> m_DeltaHealthVector;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
+10
-21
@@ -6,17 +6,7 @@
|
||||
|
||||
#include "Common.h"
|
||||
#include "Core/System.h"
|
||||
#include "Core/EventBroker.h"
|
||||
#include "Core/EKeyDown.h"
|
||||
#include "Core/EKeyUp.h"
|
||||
|
||||
struct KeyInput
|
||||
{
|
||||
bool Forward = false;
|
||||
bool Left = false;
|
||||
bool Back = false;
|
||||
bool Right = false;
|
||||
};
|
||||
#include "Collision/ETrigger.h"
|
||||
|
||||
class PlayerSystem : public PureSystem
|
||||
{
|
||||
@@ -24,21 +14,20 @@ public:
|
||||
PlayerSystem(EventBroker* eventBroker)
|
||||
: PureSystem(eventBroker, "Player")
|
||||
{
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EKeyDown, &PlayerSystem::OnKeyDown);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EKeyUp, &PlayerSystem::OnKeyUp);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_ETouch, &PlayerSystem::OnTouch);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EEnter, &PlayerSystem::OnEnter);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_ELeave, &PlayerSystem::OnLeave);
|
||||
}
|
||||
|
||||
virtual void UpdateComponent(World* world, ComponentWrapper& player, double dt) override;
|
||||
|
||||
private:
|
||||
float m_Speed = 5;
|
||||
glm::vec3 m_Direction;
|
||||
KeyInput input;
|
||||
|
||||
EventRelay<PlayerSystem, Events::KeyDown> m_EKeyDown;
|
||||
bool OnKeyDown(const Events::KeyDown &event);
|
||||
EventRelay<PlayerSystem, Events::KeyUp> m_EKeyUp;
|
||||
bool OnKeyUp(const Events::KeyUp &event);
|
||||
EventRelay<PlayerSystem, Events::TriggerEnter> m_EEnter;
|
||||
bool OnEnter(const Events::TriggerEnter &event);
|
||||
EventRelay<PlayerSystem, Events::TriggerTouch> m_ETouch;
|
||||
bool PlayerSystem::OnTouch(const Events::TriggerTouch &event);
|
||||
EventRelay<PlayerSystem, Events::TriggerLeave> m_ELeave;
|
||||
bool PlayerSystem::OnLeave(const Events::TriggerLeave &event);
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -3,9 +3,17 @@ LogLevel=1
|
||||
LoadMap=
|
||||
EditorEnabled=false
|
||||
|
||||
|
||||
[Video]
|
||||
Fullscreen=false
|
||||
VSYNC=false
|
||||
Width=1280
|
||||
Height=720
|
||||
FOV=45
|
||||
FOV=45
|
||||
|
||||
[Networking]
|
||||
StartNetwork=false
|
||||
IsServer=false
|
||||
Name=Bob
|
||||
Address=127.0.0.1
|
||||
Port=13
|
||||
@@ -18,4 +18,7 @@ F1=ToggleEditor
|
||||
1=EditorToolMove
|
||||
2=EditorToolRotate
|
||||
3=EditorToolScale
|
||||
X=EditorToggleTransformSpace
|
||||
X=EditorToggleTransformSpace
|
||||
C=ConnectToServer
|
||||
N=SwitchToServer
|
||||
M=SwitchToClient
|
||||
@@ -6,4 +6,7 @@
|
||||
<xs:include schemaLocation="Components/Test.xsd"/>
|
||||
<xs:include schemaLocation="Components/RaptorCopter.xsd"/>
|
||||
<xs:include schemaLocation="Components/Player.xsd"/>
|
||||
<xs:include schemaLocation="Components/AABB.xsd"/>
|
||||
<xs:include schemaLocation="Components/Trigger.xsd"/>
|
||||
<xs:include schemaLocation="Components/Health.xsd"/>
|
||||
</xs:schema>
|
||||
@@ -0,0 +1,4 @@
|
||||
<c:AABB>
|
||||
<BoxCenter X="0" Y="0" Z="0"/>
|
||||
<BoxSize X="1" Y="1" Z="1"/>
|
||||
</c:AABB>
|
||||
@@ -0,0 +1,14 @@
|
||||
<?xml version="1.0"?>
|
||||
|
||||
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:t="types">
|
||||
<xs:import schemaLocation="../Types.xsd" namespace="types"/>
|
||||
|
||||
<xs:element name="AABB">
|
||||
<xs:complexType>
|
||||
<xs:all>
|
||||
<xs:element name="BoxCenter" type="t:Vector" minOccurs="0"/>
|
||||
<xs:element name="BoxSize" type="t:Vector" minOccurs="0"/>
|
||||
</xs:all>
|
||||
</xs:complexType>
|
||||
</xs:element>
|
||||
</xs:schema>
|
||||
@@ -0,0 +1,4 @@
|
||||
<c:Health>
|
||||
<Health>100</Health>
|
||||
<MaxHealth>100</MaxHealth>
|
||||
</c:Health>
|
||||
@@ -0,0 +1,14 @@
|
||||
<?xml version="1.0"?>
|
||||
|
||||
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:t="types">
|
||||
<xs:import schemaLocation="../Types.xsd" namespace="types"/>
|
||||
|
||||
<xs:element name="Health">
|
||||
<xs:complexType>
|
||||
<xs:all>
|
||||
<xs:element name="Health" type="t:double" minOccurs="0"/>
|
||||
<xs:element name="MaxHealth" type="t:double" minOccurs="0"/>
|
||||
</xs:all>
|
||||
</xs:complexType>
|
||||
</xs:element>
|
||||
</xs:schema>
|
||||
@@ -1,3 +1,7 @@
|
||||
<c:Player>
|
||||
<Velocity X="0" Y="0" Z="0"/>
|
||||
<Forward>false</Forward>
|
||||
<Left>false</Left>
|
||||
<Back>false</Back>
|
||||
<Right>false</Right>
|
||||
</c:Player>
|
||||
@@ -7,6 +7,10 @@
|
||||
<xs:complexType>
|
||||
<xs:all>
|
||||
<xs:element name="Velocity" type="t:Vector" minOccurs="0"/>
|
||||
<xs:element name="Forward" type="t:bool" minOccurs="0"/>
|
||||
<xs:element name="Left" type="t:bool" minOccurs="0"/>
|
||||
<xs:element name="Back" type="t:bool" minOccurs="0"/>
|
||||
<xs:element name="Right" type="t:bool" minOccurs="0"/>
|
||||
</xs:all>
|
||||
</xs:complexType>
|
||||
</xs:element>
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
<c:Trigger>
|
||||
</c:Trigger>
|
||||
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0"?>
|
||||
|
||||
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:t="types">
|
||||
<xs:import schemaLocation="../Types.xsd" namespace="types"/>
|
||||
|
||||
<xs:element name="Trigger">
|
||||
</xs:element>
|
||||
</xs:schema>
|
||||
@@ -0,0 +1,122 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<Entity xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xi="http://www.w3.org/2001/XInclude" xsi:noNamespaceSchemaLocation="../Types/Entity.xsd" xmlns:c="components">
|
||||
<Components>
|
||||
<c:Transform>
|
||||
<Orientation X="0" Y="0" Z="0"/>
|
||||
</c:Transform>
|
||||
<c:Model>
|
||||
<Resource>Models/DummyScene.obj</Resource>
|
||||
</c:Model>
|
||||
</Components>
|
||||
<Children>
|
||||
<Entity>
|
||||
<Components>
|
||||
<c:Transform>
|
||||
<Position X="-1.5"/>
|
||||
<Scale X="1" Y="1" Z="1"/>
|
||||
</c:Transform>
|
||||
<c:Model>
|
||||
<Resource>Models/ScaleWidget.obj</Resource>
|
||||
</c:Model>
|
||||
</Components>
|
||||
</Entity>
|
||||
<Entity>
|
||||
<Components>
|
||||
<c:Transform>
|
||||
<Position X="1.5"/>
|
||||
<Scale X="2" Y="2" Z="2"/>
|
||||
</c:Transform>
|
||||
<c:Model>
|
||||
<Resource>Models/RotationWidgetX.obj</Resource>
|
||||
</c:Model>
|
||||
<c:Trigger>
|
||||
</c:Trigger>
|
||||
</Components>
|
||||
</Entity>
|
||||
<Entity>
|
||||
<Components>
|
||||
<c:Player>
|
||||
<Velocity X="0" Y="0" Z="0"/>
|
||||
</c:Player>
|
||||
<c:Transform>
|
||||
<Position X="2.5"/>
|
||||
</c:Transform>
|
||||
<c:Model>
|
||||
<Resource>Models/Core/UnitCube.obj</Resource>
|
||||
</c:Model>
|
||||
<c:AABB>
|
||||
</c:AABB>
|
||||
</Components>
|
||||
</Entity>
|
||||
<Entity>
|
||||
<Components>
|
||||
<c:Transform>
|
||||
<Position X="0" Y="-0"/>
|
||||
<Scale X="1" Y="1" Z="1"/>
|
||||
</c:Transform>
|
||||
<!--<c:Move>
|
||||
<Speed>1</Speed>
|
||||
<Direction X="-1"/>
|
||||
<Rotation Y="3.14"/>
|
||||
</c:Move>-->
|
||||
</Components>
|
||||
<Children>
|
||||
<Entity>
|
||||
<Components>
|
||||
<c:Transform>
|
||||
<Position X="0" Y="0"/>
|
||||
<Orientation X="0.0" Y="0" Z="1.0"/>
|
||||
</c:Transform>
|
||||
<c:Model>
|
||||
<Resource>Models/Core/UnitRaptor.obj</Resource>
|
||||
<Color R="1" G="0.4" B="0.8"/>
|
||||
</c:Model>
|
||||
</Components>
|
||||
<Children>
|
||||
<Entity>
|
||||
<Components>
|
||||
<c:Transform>
|
||||
<Position X="-0.01" Y="0.55"/>
|
||||
<Orientation X="0" Y="0" Z="-1"/>
|
||||
</c:Transform>
|
||||
<c:RaptorCopter>
|
||||
<Speed>20</Speed>
|
||||
<Axis Y="1"/>
|
||||
</c:RaptorCopter>
|
||||
</Components>
|
||||
<Children>
|
||||
<Entity>
|
||||
<Components>
|
||||
<c:Transform>
|
||||
<Position X="0" Y="0"/>
|
||||
<Scale X="1.7" Y="0.03" Z="0.1"/>
|
||||
<Orientation X="0" Y="0" Z="0"/>
|
||||
</c:Transform>
|
||||
<c:Model>
|
||||
<Resource>Models/Core/UnitCube.obj</Resource>
|
||||
<Color R="1" G="0.4" B="0.8"/>
|
||||
</c:Model>
|
||||
</Components>
|
||||
</Entity>
|
||||
<Entity>
|
||||
<Components>
|
||||
<c:Transform>
|
||||
<Position X="0" Y="0"/>
|
||||
<Scale X="1.7" Y="0.03" Z="0.1"/>
|
||||
<Orientation X="0" Y="1.57" Z="0"/>
|
||||
</c:Transform>
|
||||
<c:Model>
|
||||
<Resource>Models/Core/UnitCube.obj</Resource>
|
||||
<Color R="1" G="0.4" B="0.8"/>
|
||||
</c:Model>
|
||||
</Components>
|
||||
</Entity>
|
||||
</Children>
|
||||
</Entity>
|
||||
</Children>
|
||||
</Entity>
|
||||
</Children>
|
||||
</Entity>
|
||||
</Children>
|
||||
</Entity>
|
||||
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
|
||||
<Entity xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../Types/Entity.xsd" xmlns:c="components">
|
||||
<Components>
|
||||
<c:Transform/>
|
||||
</Components>
|
||||
</Entity>
|
||||
@@ -0,0 +1,18 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
|
||||
<Entity xmlns:c="components" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../Types/Entity.xsd">
|
||||
|
||||
<Components>
|
||||
<c:Transform/>
|
||||
</Components>
|
||||
|
||||
<Children>
|
||||
<Entity ref="Schema/Entities/RaptorCopter.xml">
|
||||
<Components>
|
||||
<c:Transform>
|
||||
<Position X="1" Y="2" Z="3" />
|
||||
</c:Transform>
|
||||
</Components>
|
||||
</Entity>
|
||||
</Children>
|
||||
|
||||
</Entity>
|
||||
@@ -0,0 +1,68 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<Entity xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xi="http://www.w3.org/2001/XInclude" xsi:noNamespaceSchemaLocation="../Types/Entity.xsd" xmlns:c="components" name="RaptorCopter">
|
||||
<Components>
|
||||
<c:Transform>
|
||||
<Position X="0" Y="0"/>
|
||||
<Orientation X="0.0" Y="0" Z="0.0"/>
|
||||
</c:Transform>
|
||||
<c:Model>
|
||||
<Resource>Models/Core/UnitRaptor.obj</Resource>
|
||||
<Color R="1" G="0.4" B="0.8"/>
|
||||
</c:Model>
|
||||
</Components>
|
||||
<Children>
|
||||
<Entity>
|
||||
<Components>
|
||||
<c:Transform>
|
||||
<Position X="0" Y="0"/>
|
||||
<Orientation X="0" Y="0" Z="0"/>
|
||||
</c:Transform>
|
||||
<c:RaptorCopter>
|
||||
<Speed>20</Speed>
|
||||
<Axis Y="1"/>
|
||||
</c:RaptorCopter>
|
||||
</Components>
|
||||
<Children>
|
||||
<Entity>
|
||||
<Components>
|
||||
<c:Transform>
|
||||
<Position X="0" Y="0.4"/>
|
||||
<Scale X="0.1" Y="0.4" Z="0.1"/>
|
||||
</c:Transform>
|
||||
<c:Model>
|
||||
<Resource>Models/Core/UnitCylinder.obj</Resource>
|
||||
<Color R="1" G="0.4" B="0.8"/>
|
||||
</c:Model>
|
||||
</Components>
|
||||
</Entity>
|
||||
<Entity>
|
||||
<Components>
|
||||
<c:Transform>
|
||||
<Position X="0" Y="0.6"/>
|
||||
<Scale X="1.7" Y="0.03" Z="0.1"/>
|
||||
<Orientation X="0" Y="0" Z="0"/>
|
||||
</c:Transform>
|
||||
<c:Model>
|
||||
<Resource>Models/Core/UnitCube.obj</Resource>
|
||||
<Color R="1" G="0.4" B="0.8"/>
|
||||
</c:Model>
|
||||
</Components>
|
||||
</Entity>
|
||||
<Entity>
|
||||
<Components>
|
||||
<c:Transform>
|
||||
<Position X="0" Y="0.6"/>
|
||||
<Scale X="1.7" Y="0.03" Z="0.1"/>
|
||||
<Orientation X="0" Y="1.57" Z="0"/>
|
||||
</c:Transform>
|
||||
<c:Model>
|
||||
<Resource>Models/Core/UnitCube.obj</Resource>
|
||||
<Color R="1" G="0.4" B="0.8"/>
|
||||
</c:Model>
|
||||
</Components>
|
||||
</Entity>
|
||||
</Children>
|
||||
</Entity>
|
||||
</Children>
|
||||
</Entity>
|
||||
@@ -25,13 +25,16 @@
|
||||
<Components>
|
||||
<c:Transform>
|
||||
<Position X="1.5"/>
|
||||
<Scale X="2" Y="2" Z="2"/>
|
||||
</c:Transform>
|
||||
<c:Model>
|
||||
<Resource>Models/RotationWidget.obj</Resource>
|
||||
</c:Model>
|
||||
<c:Trigger>
|
||||
</c:Trigger>
|
||||
</Components>
|
||||
</Entity>
|
||||
<Entity>
|
||||
<!--<Entity>
|
||||
<Components>
|
||||
<c:Player>
|
||||
<Velocity X="0" Y="0" Z="0"/>
|
||||
@@ -42,8 +45,10 @@
|
||||
<c:Model>
|
||||
<Resource>Models/Core/UnitCube.obj</Resource>
|
||||
</c:Model>
|
||||
<c:AABB>
|
||||
</c:AABB>
|
||||
</Components>
|
||||
</Entity>
|
||||
</Entity>-->
|
||||
<Entity>
|
||||
<Components>
|
||||
<c:Transform>
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
<xs:element ref="c:Test" minOccurs="0"/>
|
||||
<xs:element ref="c:RaptorCopter" minOccurs="0"/>
|
||||
<xs:element ref="c:Player" minOccurs="0"/>
|
||||
<xs:element ref="c:Health" minOccurs="0"/>
|
||||
</xs:all>
|
||||
</xs:complexType>
|
||||
</xs:element>
|
||||
@@ -28,6 +29,8 @@
|
||||
</xs:element>
|
||||
</xs:all>
|
||||
<xs:attribute ref="xml:base"/>
|
||||
<xs:attribute name="name" type="xs:string" minOccurs="0" default=""/>
|
||||
<xs:attribute name="ref" type="xs:string" minOccurs="0" default=""/>
|
||||
</xs:complexType>
|
||||
</xs:element>
|
||||
</xs:schema>
|
||||
@@ -69,6 +69,12 @@ file(GLOB SOURCE_FILES_GUI
|
||||
)
|
||||
source_group(GUI FILES ${SOURCE_FILES_GUI})
|
||||
|
||||
file(GLOB SOURCE_FILES_Collision
|
||||
"${INCLUDE_PATH}/Collision/*.h"
|
||||
"Collision/*.cpp"
|
||||
)
|
||||
source_group(Collision FILES ${SOURCE_FILES_Collision})
|
||||
|
||||
file(GLOB SOURCE_FILES_Editor
|
||||
"${INCLUDE_PATH}/Editor/*.h"
|
||||
"Editor/*.cpp"
|
||||
@@ -83,12 +89,28 @@ set(SOURCE_FILES
|
||||
${SOURCE_FILES_GUI}
|
||||
${SOURCE_FILES_Rendering}
|
||||
${SOURCE_FILES_Rendering_Util}
|
||||
${SOURCE_FILES_Collision}
|
||||
${SOURCE_FILES_Editor}
|
||||
${CMAKE_SOURCE_DIR}/deps/include/imgui/imgui.cpp
|
||||
${CMAKE_SOURCE_DIR}/deps/include/imgui/imgui_draw.cpp
|
||||
${CMAKE_SOURCE_DIR}/deps/include/imgui/imgui_demo.cpp
|
||||
${SOURCE_FILES_Editor}
|
||||
${CMAKE_SOURCE_DIR}/deps/include/nativefiledialog/nfd_common.c
|
||||
)
|
||||
|
||||
# nativefiledialog
|
||||
if(WIN32)
|
||||
set(SOURCE_FILES ${SOURCE_FILES}
|
||||
${CMAKE_SOURCE_DIR}/deps/include/nativefiledialog/nfd_win.cpp
|
||||
)
|
||||
endif()
|
||||
if(UNIX)
|
||||
set(SOURCE_FILES ${SOURCE_FILES}
|
||||
${CMAKE_SOURCE_DIR}/deps/include/nativefiledialog/nfd_gtk.c
|
||||
# TODO: Link with GTK+ here!
|
||||
)
|
||||
endif()
|
||||
|
||||
|
||||
set(LIBRARIES
|
||||
${OPENGL_LIBRARIES}
|
||||
${GLEW_LIBRARIES}
|
||||
|
||||
@@ -0,0 +1,282 @@
|
||||
#include <algorithm>
|
||||
|
||||
#include "Collision/Collision.h"
|
||||
#include "Engine/GLM.h"
|
||||
#include "Core/World.h"
|
||||
#include "Rendering/Model.h"
|
||||
|
||||
namespace Collision
|
||||
{
|
||||
|
||||
//note: this one hasnt been delta adjusted like RayVsAABB has
|
||||
bool RayAABBIntr(const Ray& ray, const AABB& box)
|
||||
{
|
||||
glm::vec3 w = 75.0f * ray.Direction();
|
||||
glm::vec3 v = glm::abs(w);
|
||||
glm::vec3 c = ray.Origin() - box.Center() + w;
|
||||
glm::vec3 half = box.HalfSize();
|
||||
|
||||
if (abs(c.x) > v.x + half.x) {
|
||||
return false;
|
||||
}
|
||||
if (abs(c.y) > v.y + half.y) {
|
||||
return false;
|
||||
}
|
||||
if (abs(c.z) > v.z + half.z) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (abs(c.y*w.z - c.z*w.y) > half.y*v.z + half.z*v.y) {
|
||||
return false;
|
||||
}
|
||||
if (abs(c.x*w.z - c.z*w.x) > half.x*v.z + half.z*v.x) {
|
||||
return false;
|
||||
}
|
||||
return !(abs(c.x*w.y - c.y*w.x) > half.x*v.y + half.y*v.x);
|
||||
}
|
||||
|
||||
bool RayVsAABB(const Ray& ray, const AABB& box)
|
||||
{
|
||||
float dummy;
|
||||
return RayVsAABB(ray, box, dummy);
|
||||
}
|
||||
|
||||
bool RayVsAABB(const Ray& ray, const AABB& box, float& outDistance)
|
||||
{
|
||||
glm::vec3 invdir = 1.0f / ray.Direction();
|
||||
glm::vec3 origin = ray.Origin();
|
||||
|
||||
float t1 = (box.MinCorner().x - origin.x)*invdir.x;
|
||||
float t2 = (box.MaxCorner().x - origin.x)*invdir.x;
|
||||
float t3 = (box.MinCorner().y - origin.y)*invdir.y;
|
||||
float t4 = (box.MaxCorner().y - origin.y)*invdir.y;
|
||||
float t5 = (box.MinCorner().z - origin.z)*invdir.z;
|
||||
float t6 = (box.MaxCorner().z - origin.z)*invdir.z;
|
||||
|
||||
float tmin = std::max(std::max(std::min(t1, t2), std::min(t3, t4)), std::min(t5, t6));
|
||||
float tmax = std::min(std::min(std::max(t1, t2), std::max(t3, t4)), std::max(t5, t6));
|
||||
|
||||
//if (tmax < 0 || tmin > tmax)
|
||||
//if tmin,tmax are almost the same (i.e. hitting exactly in the corner) then tmin might be slightly
|
||||
//greater than tmax becuase of floating-precision problems. fixed by adding a small delta to tmax
|
||||
if (tmax < 0 || tmin>(tmax + 0.0001f))
|
||||
return false;
|
||||
|
||||
outDistance = (tmin > 0) ? tmin : tmax;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool AABBVsAABB(const AABB& a, const AABB& b)
|
||||
{
|
||||
const glm::vec3& aCenter = a.Center();
|
||||
const glm::vec3& bCenter = b.Center();
|
||||
const glm::vec3& aHSize = a.HalfSize();
|
||||
const glm::vec3& bHSize = b.HalfSize();
|
||||
//Test will probably exit because of the X and Z axes more often, so test them first.
|
||||
if (abs(aCenter[0] - bCenter[0]) > (aHSize[0] + bHSize[0])) {
|
||||
return false;
|
||||
}
|
||||
if (abs(aCenter[2] - bCenter[2]) > (aHSize[2] + bHSize[2])) {
|
||||
return false;
|
||||
}
|
||||
return (abs(aCenter[1] - bCenter[1]) <= (aHSize[1] + bHSize[1]));
|
||||
}
|
||||
|
||||
bool AABBVsAABB(const AABB& a, const AABB& b, glm::vec3& minimumTranslation)
|
||||
{
|
||||
minimumTranslation = glm::vec3(0, 0, 0);
|
||||
const glm::vec3& aMax = a.MaxCorner();
|
||||
const glm::vec3& bMax = b.MaxCorner();
|
||||
const glm::vec3& aMin = a.MinCorner();
|
||||
const glm::vec3& bMin = b.MinCorner();
|
||||
const glm::vec3& bSize = b.Size();
|
||||
const glm::vec3& aSize = a.Size();
|
||||
float minOffset = INFINITY;
|
||||
float off;
|
||||
auto axisesIntersecting = glm::tvec3<bool, glm::highp>(false, false, false);
|
||||
for (int i = 0; i < 3; ++i) {
|
||||
off = bMax[i] - aMin[i];
|
||||
if (off > 0 && off < bSize[i] + aSize[i]) {
|
||||
if (off < minOffset) {
|
||||
minimumTranslation = glm::vec3();
|
||||
minimumTranslation[i] = minOffset = off;
|
||||
}
|
||||
axisesIntersecting[i] = true;
|
||||
}
|
||||
off = aMax[i] - bMin[i];
|
||||
if (off > 0 && off < bSize[i] + aSize[i]) {
|
||||
if (off < minOffset) {
|
||||
minOffset = off;
|
||||
minimumTranslation = glm::vec3();
|
||||
minimumTranslation[i] = -off;
|
||||
}
|
||||
axisesIntersecting[i] = true;
|
||||
}
|
||||
}
|
||||
return glm::all(axisesIntersecting);
|
||||
}
|
||||
|
||||
bool RayVsModel(const Ray& ray,
|
||||
const std::vector<RawModel::Vertex>& modelVertices,
|
||||
const std::vector<unsigned int>& modelIndices)
|
||||
{
|
||||
for (int i = 0; i < modelIndices.size(); ++i) {
|
||||
glm::vec3 v0 = modelVertices[modelIndices[i]].Position;
|
||||
glm::vec3 e1 = modelVertices[modelIndices[++i]].Position - v0; //v1 - v0
|
||||
glm::vec3 e2 = modelVertices[modelIndices[++i]].Position - v0; //v2 - v0
|
||||
glm::vec3 m = ray.Origin() - v0;
|
||||
glm::vec3 MxE1 = glm::cross(m, e1);
|
||||
glm::vec3 DxE2 = glm::cross(ray.Direction(), e2);
|
||||
float DetInv = glm::dot(e1, DxE2);
|
||||
if (std::abs(DetInv) < FLT_EPSILON) {
|
||||
continue;
|
||||
}
|
||||
DetInv = 1.0f / DetInv;
|
||||
float u = glm::dot(m, DxE2) * DetInv;
|
||||
float v = glm::dot(ray.Direction(), MxE1) * DetInv;
|
||||
//u,v can be very close to 0 but still negative sometimes. added a deltafactor to compensate for that problem
|
||||
if ((u + 0.001f) < 0 || (v + 0.001f) < 0 || 1 < u + v) {
|
||||
continue;
|
||||
}
|
||||
//Here, u and v are positive, u+v <= 1, and if distance is positive - triangle is hit.
|
||||
if (0 <= glm::dot(e2, MxE1) * DetInv) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool RayVsModel(const Ray& ray,
|
||||
const std::vector<RawModel::Vertex>& modelVertices,
|
||||
const std::vector<unsigned int>& modelIndices,
|
||||
float& outDistance,
|
||||
float& outUCoord,
|
||||
float& outVCoord)
|
||||
{
|
||||
outDistance = INFINITY;
|
||||
bool hit = false;
|
||||
for (int i = 0; i < modelIndices.size(); ++i) {
|
||||
glm::vec3 v0 = modelVertices[modelIndices[i]].Position;
|
||||
glm::vec3 e1 = modelVertices[modelIndices[++i]].Position - v0; //v1 - v0
|
||||
glm::vec3 e2 = modelVertices[modelIndices[++i]].Position - v0; //v2 - v0
|
||||
glm::vec3 m = ray.Origin() - v0;
|
||||
glm::vec3 MxE1 = glm::cross(m, e1);
|
||||
glm::vec3 DxE2 = glm::cross(ray.Direction(), e2);//pVec
|
||||
float DetInv = glm::dot(e1, DxE2);
|
||||
if (std::abs(DetInv) < FLT_EPSILON) {
|
||||
continue;
|
||||
}
|
||||
DetInv = 1.0f / DetInv;
|
||||
float dist = glm::dot(e2, MxE1) * DetInv;
|
||||
if (dist >= outDistance) {
|
||||
continue;
|
||||
}
|
||||
float u = glm::dot(m, DxE2) * DetInv;
|
||||
float v = glm::dot(ray.Direction(), MxE1) * DetInv;
|
||||
|
||||
//u,v can be very close to 0 but still negative sometimes. added a deltafactor to compensate for that problem
|
||||
//If u and v are positive, u+v <= 1, dist is positive, and less than closest.
|
||||
if (0 <= (u + 0.001f) && 0 <= (v + 0.001f) && u + v <= 1 && 0 <= dist) {
|
||||
outDistance = dist;
|
||||
outUCoord = u;
|
||||
outVCoord = v;
|
||||
hit = true;
|
||||
}
|
||||
}
|
||||
return hit;
|
||||
}
|
||||
|
||||
bool RayVsModel(const Ray& ray,
|
||||
const std::vector<RawModel::Vertex>& modelVertices,
|
||||
const std::vector<unsigned int>& modelIndices,
|
||||
glm::vec3& outHitPosition)
|
||||
{
|
||||
float u;
|
||||
float v;
|
||||
float dist;
|
||||
bool hit = RayVsModel(ray, modelVertices, modelIndices, dist, u, v);
|
||||
outHitPosition = ray.Origin() + dist * ray.Direction();
|
||||
return hit;
|
||||
}
|
||||
|
||||
bool IsSameBoxProbably(const AABB& first, const AABB& second, const float epsilon)
|
||||
{
|
||||
const glm::vec3& ma1 = first.MaxCorner();
|
||||
const glm::vec3& ma2 = first.MaxCorner();
|
||||
const glm::vec3& mi1 = second.MinCorner();
|
||||
const glm::vec3& mi2 = second.MinCorner();
|
||||
return (std::abs(ma1.x - ma2.x) < epsilon) &&
|
||||
(std::abs(mi1.x - mi2.x) < epsilon) &&
|
||||
(std::abs(ma1.z - ma2.z) < epsilon) &&
|
||||
(std::abs(mi1.z - mi2.z) < epsilon) &&
|
||||
(std::abs(ma1.y - ma2.y) < epsilon) &&
|
||||
(std::abs(mi1.y - mi2.y) < epsilon);
|
||||
}
|
||||
|
||||
bool attachAABBComponentFromModel(World* world, EntityID id)
|
||||
{
|
||||
if (!world->HasComponent(id, "Model")) {
|
||||
return false;
|
||||
}
|
||||
ComponentWrapper model = world->GetComponent(id, "Model");
|
||||
ComponentWrapper collision = world->AttachComponent(id, "AABB");
|
||||
Model* modelRes = ResourceManager::Load<Model>(model["Resource"]);
|
||||
if (modelRes == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
glm::mat4 modelMatrix = modelRes->m_Matrix;
|
||||
|
||||
glm::vec3 mini = glm::vec3(INFINITY, INFINITY, INFINITY);
|
||||
glm::vec3 maxi = glm::vec3(-INFINITY, -INFINITY, -INFINITY);
|
||||
for (const auto& v : modelRes->m_Vertices) {
|
||||
const auto& wPos = modelMatrix * glm::vec4(v.Position.x, v.Position.y, v.Position.z, 1);
|
||||
maxi.x = std::max(wPos.x, maxi.x);
|
||||
maxi.y = std::max(wPos.y, maxi.y);
|
||||
maxi.z = std::max(wPos.z, maxi.z);
|
||||
mini.x = std::min(wPos.x, mini.x);
|
||||
mini.y = std::min(wPos.y, mini.y);
|
||||
mini.z = std::min(wPos.z, mini.z);
|
||||
}
|
||||
collision["BoxCenter"] = 0.5f * (maxi + mini);
|
||||
collision["BoxSize"] = maxi - mini;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool GetEntityBox(World* world, ComponentWrapper& AABBComponent, AABB& outBox)
|
||||
{
|
||||
ComponentWrapper& cTrans = world->GetComponent(AABBComponent.EntityID, "Transform");
|
||||
ComponentWrapper model = world->GetComponent(AABBComponent.EntityID, "Model");
|
||||
Model* modelRes = ResourceManager::Load<Model>(model["Resource"]);
|
||||
outBox.CreateFromCenter(AABBComponent["BoxCenter"], AABBComponent["BoxSize"]);
|
||||
glm::vec3 mini = outBox.MinCorner();
|
||||
glm::vec3 maxi = outBox.MaxCorner();
|
||||
|
||||
if (modelRes == nullptr) {
|
||||
return false;
|
||||
}
|
||||
glm::mat4 modelMatrix = modelRes->m_Matrix *
|
||||
glm::translate(glm::mat4(), (glm::vec3)cTrans["Position"]) *
|
||||
glm::scale((glm::vec3)cTrans["Scale"]);
|
||||
|
||||
outBox = AABB(modelMatrix * glm::vec4(mini.x, mini.y, mini.z, 1),
|
||||
modelMatrix * glm::vec4(maxi.x, maxi.y, maxi.z, 1));
|
||||
return true;
|
||||
}
|
||||
|
||||
bool GetEntityBox(World* world, EntityID entity, AABB& outBox, bool forceBoxFromModel)
|
||||
{
|
||||
if (!world->HasComponent(entity, "AABB")) {
|
||||
if (forceBoxFromModel) {
|
||||
if (!attachAABBComponentFromModel(world, entity))
|
||||
return false;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
ComponentWrapper& cBox = world->GetComponent(entity, "AABB");
|
||||
return GetEntityBox(world, cBox, outBox);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
#include "Collision/Collision.h"
|
||||
#include "Collision/CollisionSystem.h"
|
||||
#include "Core/AABB.h"
|
||||
|
||||
void CollisionSystem::UpdateComponent(World * world, ComponentWrapper & cAABB, double dt)
|
||||
{
|
||||
//Right now, cAABB is a component attached to any entity that should be collideable.
|
||||
AABB thisBox;
|
||||
if (!Collision::GetEntityBox(world, cAABB, thisBox)) {
|
||||
return;
|
||||
}
|
||||
//Press 'Z' to enable/disable collision.
|
||||
if (zPress) {
|
||||
return;
|
||||
}
|
||||
//Here, mover should be an object that moves, currently only players.
|
||||
for (auto& mover : *world->GetComponents("Player")) {
|
||||
if (cAABB.EntityID == mover.EntityID) {
|
||||
continue;
|
||||
}
|
||||
AABB otherBox;
|
||||
if (!Collision::GetEntityBox(world, mover.EntityID, otherBox)) {
|
||||
continue;
|
||||
}
|
||||
glm::vec3 resolveTranslation;
|
||||
if (Collision::AABBVsAABB(otherBox, thisBox, resolveTranslation)) {
|
||||
ComponentWrapper& trans = world->GetComponent(mover.EntityID, "Transform");
|
||||
//TODO: Special treatment if both are movers.
|
||||
trans["Position"] = (glm::vec3)trans["Position"] + resolveTranslation;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool CollisionSystem::OnKeyUp(const Events::KeyUp & event)
|
||||
{
|
||||
if (event.KeyCode == GLFW_KEY_Z) {
|
||||
zPress = !zPress;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
#include "Collision/TriggerSystem.h"
|
||||
#include "Collision/Collision.h"
|
||||
#include "Core/AABB.h"
|
||||
#include "Rendering/Model.h"
|
||||
|
||||
void TriggerSystem::UpdateComponent(World* world, ComponentWrapper& trigger, double dt)
|
||||
{
|
||||
//Currently only players can trigger things.
|
||||
auto players = world->GetComponents("Player");
|
||||
if (players == nullptr) {
|
||||
return;
|
||||
}
|
||||
EntityID tId = trigger.EntityID;
|
||||
AABB triggerBox;
|
||||
//The trigger *should* have a bounding box, or something, to test against so it can be triggered.
|
||||
if (!Collision::GetEntityBox(world, tId, triggerBox, true)) {
|
||||
return;
|
||||
}
|
||||
for (auto& pc : *players) {
|
||||
EntityID pId = pc.EntityID;
|
||||
AABB playerBox;
|
||||
//The player can't trigger anything without an AABB.
|
||||
if (!Collision::GetEntityBox(world, pId, playerBox, true)) {
|
||||
continue;
|
||||
}
|
||||
if (!Collision::AABBVsAABB(triggerBox, playerBox)) {
|
||||
//Entity is not touching the trigger,
|
||||
//Throw event if it was previously.
|
||||
if (throwLeaveIfWasInTrigger(m_EntitiesTouchingTrigger[tId], pId, tId)) {
|
||||
continue;
|
||||
}
|
||||
//This only occurs if the entity was completely inside the trigger one frame,
|
||||
//then completely outside the trigger, e.g. when dying and respawning.
|
||||
throwLeaveIfWasInTrigger(m_EntitiesCompletelyInTrigger[tId], pId, tId);
|
||||
} else {
|
||||
//Entity is at least touching the trigger.
|
||||
AABB completelyInsideBox;
|
||||
completelyInsideBox.CreateFromCenter(triggerBox.Center(), triggerBox.Size() - 2.0f * playerBox.Size());
|
||||
if (Collision::AABBVsAABB(completelyInsideBox, playerBox) &&
|
||||
glm::all(glm::greaterThan(triggerBox.Size(), playerBox.Size()))) {
|
||||
//Entity is completely inside the trigger.
|
||||
//If it was only touching before, it is erased.
|
||||
m_EntitiesTouchingTrigger[tId].erase(pId);
|
||||
std::unordered_set<EntityID>& completeSet = m_EntitiesCompletelyInTrigger[tId];
|
||||
if (completeSet.count(pId) == 0) {
|
||||
//If it wasn't completely in the trigger, throw Enter and add to the set.
|
||||
completeSet.insert(pId);
|
||||
publish<Events::TriggerEnter>(pId, tId);
|
||||
}
|
||||
} else {
|
||||
//Entity is only touching the trigger.
|
||||
std::unordered_set<EntityID>& touchSet = m_EntitiesTouchingTrigger[tId];
|
||||
std::unordered_set<EntityID>& completeSet = m_EntitiesCompletelyInTrigger[tId];
|
||||
const auto& it = completeSet.find(pId);
|
||||
//If it was completely inside before.
|
||||
if (it != completeSet.end()) {
|
||||
completeSet.erase(it);
|
||||
touchSet.insert(pId);
|
||||
//If it was completely outside before.
|
||||
} else if (touchSet.count(pId) == 0) {
|
||||
publish<Events::TriggerTouch>(pId, tId);
|
||||
touchSet.insert(pId);
|
||||
}
|
||||
//Else, it was touching the trigger last frame too and nothing is done.
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool TriggerSystem::throwLeaveIfWasInTrigger(std::unordered_set<EntityID>& triggerSet, EntityID pId, EntityID tId)
|
||||
{
|
||||
const auto& it = triggerSet.find(pId);
|
||||
if (it != triggerSet.end()) {
|
||||
//If it was in the trigger, but not anymore, throw leaveEvent and erase from the set.
|
||||
triggerSet.erase(it);
|
||||
publish<Events::TriggerLeave>(pId, tId);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
#include "Core/AABB.h"
|
||||
#include "Common.h"
|
||||
|
||||
AABB::AABB(const glm::vec3& minPos, const glm::vec3& maxPos)
|
||||
: m_MinCorner(minPos)
|
||||
, m_MaxCorner(maxPos)
|
||||
, m_Center(0.5f * (maxPos + minPos))
|
||||
, m_HalfSize(0.5f * (maxPos - minPos))
|
||||
{
|
||||
DEBUG_IF(glm::any(glm::lessThan(m_MaxCorner, m_MinCorner))) {
|
||||
LOG_WARNING("AABB maxCorner coordinates are not greater than minCorner");
|
||||
m_MaxCorner.x = glm::max(m_MaxCorner.x, m_MinCorner.x);
|
||||
m_MinCorner.x = glm::min(m_MaxCorner.x, m_MinCorner.x);
|
||||
m_MaxCorner.y = glm::max(m_MaxCorner.y, m_MinCorner.y);
|
||||
m_MinCorner.y = glm::min(m_MaxCorner.y, m_MinCorner.y);
|
||||
m_MaxCorner.z = glm::max(m_MaxCorner.z, m_MinCorner.z);
|
||||
m_MinCorner.z = glm::min(m_MaxCorner.z, m_MinCorner.z);
|
||||
}
|
||||
}
|
||||
|
||||
AABB::AABB(const glm::vec4& minPos, const glm::vec4& maxPos)
|
||||
: AABB(glm::vec3(minPos), glm::vec3(maxPos))
|
||||
{}
|
||||
|
||||
void AABB::CreateFromCenter(const glm::vec3& center, const glm::vec3& size)
|
||||
{
|
||||
m_Center = center;
|
||||
m_HalfSize = 0.5f * size;
|
||||
m_MinCorner = m_Center - m_HalfSize;
|
||||
m_MaxCorner = m_Center + m_HalfSize;
|
||||
}
|
||||
|
||||
AABB::~AABB()
|
||||
{}
|
||||
@@ -19,7 +19,7 @@ bool ComponentPoolForwardIterator::operator!=(const ComponentPoolForwardIterator
|
||||
return m_MemoryPoolIterator != other.m_MemoryPoolIterator;
|
||||
}
|
||||
|
||||
ComponentPoolForwardIterator& ComponentPoolForwardIterator::operator++(int)
|
||||
ComponentPoolForwardIterator ComponentPoolForwardIterator::operator++(int)
|
||||
{
|
||||
ComponentPoolForwardIterator copyIter(*this);
|
||||
operator++();
|
||||
|
||||
@@ -0,0 +1,95 @@
|
||||
#include "Core/EntityFile.h"
|
||||
|
||||
EntityFile::EntityFile(boost::filesystem::path path)
|
||||
: m_FilePath(path)
|
||||
{
|
||||
using namespace xercesc;
|
||||
XMLPlatformUtils::Initialize();
|
||||
m_GrammarPool = new XMLGrammarPoolImpl();
|
||||
m_SAX2XMLReader = XMLReaderFactory::createXMLReader(XMLPlatformUtils::fgMemoryManager, m_GrammarPool);
|
||||
}
|
||||
|
||||
EntityFile::~EntityFile()
|
||||
{
|
||||
delete m_SAX2XMLReader;
|
||||
delete m_GrammarPool;
|
||||
xercesc::XMLPlatformUtils::Terminate();
|
||||
}
|
||||
|
||||
void EntityFile::Parse(const EntityFileHandler* handler) const
|
||||
{
|
||||
using namespace xercesc;
|
||||
|
||||
EntityFileSAXHandler saxHandler(handler, m_SAX2XMLReader);
|
||||
m_SAX2XMLReader->setFeature(XMLUni::fgXercesCacheGrammarFromParse, true);
|
||||
m_SAX2XMLReader->setFeature(XMLUni::fgXercesUseCachedGrammarInParse, true);
|
||||
m_SAX2XMLReader->setContentHandler(&saxHandler);
|
||||
m_SAX2XMLReader->setErrorHandler(&saxHandler);
|
||||
m_SAX2XMLReader->setDeclarationHandler(&saxHandler);
|
||||
m_SAX2XMLReader->parse(m_FilePath.string().c_str());
|
||||
}
|
||||
|
||||
std::size_t EntityFile::GetTypeStride(std::string typeName)
|
||||
{
|
||||
std::map<std::string, size_t> typeStrides{
|
||||
{ "bool", sizeof(bool) },
|
||||
{ "int", sizeof(int) },
|
||||
{ "float", sizeof(float) },
|
||||
{ "double", sizeof(double) },
|
||||
{ "string", sizeof(std::string) },
|
||||
{ "Vector", sizeof(glm::vec3) },
|
||||
{ "Quaternion", sizeof(glm::quat) },
|
||||
{ "Color", sizeof(glm::vec4) }
|
||||
};
|
||||
|
||||
auto it = typeStrides.find(typeName);
|
||||
return (it != typeStrides.end()) ? it->second : 0;
|
||||
}
|
||||
|
||||
void EntityFile::WriteAttributeData(char* outData, const ComponentInfo::Field_t& field, const std::map<std::string, std::string>& attributes)
|
||||
{
|
||||
if (field.Type == "Vector") {
|
||||
glm::vec3 vec;
|
||||
vec.x = boost::lexical_cast<float>(attributes.at("X"));
|
||||
vec.y = boost::lexical_cast<float>(attributes.at("Y"));
|
||||
vec.z = boost::lexical_cast<float>(attributes.at("Z"));
|
||||
memcpy(outData, reinterpret_cast<char*>(&vec), field.Stride);
|
||||
} else if (field.Type == "Color") {
|
||||
glm::vec4 vec;
|
||||
vec.r = boost::lexical_cast<float>(attributes.at("R"));
|
||||
vec.g = boost::lexical_cast<float>(attributes.at("G"));
|
||||
vec.b = boost::lexical_cast<float>(attributes.at("B"));
|
||||
vec.a = boost::lexical_cast<float>(attributes.at("A"));
|
||||
memcpy(outData, reinterpret_cast<char*>(&vec), field.Stride);
|
||||
} else if (field.Type == "Quaternion") {
|
||||
glm::quat q;
|
||||
q.x = boost::lexical_cast<float>(attributes.at("X"));
|
||||
q.y = boost::lexical_cast<float>(attributes.at("Y"));
|
||||
q.z = boost::lexical_cast<float>(attributes.at("Z"));
|
||||
q.w = boost::lexical_cast<float>(attributes.at("W"));
|
||||
memcpy(outData, reinterpret_cast<char*>(&q), field.Stride);
|
||||
} else if (!attributes.empty()) {
|
||||
LOG_WARNING("%i attributes not handled by any type conversion!", attributes.size());
|
||||
}
|
||||
}
|
||||
|
||||
void EntityFile::WriteValueData(char* outData, const ComponentInfo::Field_t& field, const char* valueData)
|
||||
{
|
||||
if (field.Type == "int") {
|
||||
int value = boost::lexical_cast<int>(valueData);
|
||||
memcpy(outData, reinterpret_cast<char*>(&value), field.Stride);
|
||||
} else if (field.Type == "float") {
|
||||
float value = boost::lexical_cast<float>(valueData);
|
||||
memcpy(outData, reinterpret_cast<char*>(&value), field.Stride);
|
||||
} else if (field.Type == "double") {
|
||||
double value = boost::lexical_cast<double>(valueData);
|
||||
memcpy(outData, reinterpret_cast<char*>(&value), field.Stride);
|
||||
} else if (field.Type == "bool") {
|
||||
bool value = (valueData[0] == 't'); // Lazy bool evaluation
|
||||
memcpy(outData, reinterpret_cast<char*>(&value), field.Stride);
|
||||
} else if (field.Type == "string") {
|
||||
new (outData) std::string(valueData);
|
||||
} else {
|
||||
LOG_WARNING("Unknown value data type: %s", field.Type.c_str());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
#include "Core/EntityFileParser.h"
|
||||
|
||||
EntityFileParser::EntityFileParser(const EntityFile* entityFile)
|
||||
: m_EntityFile(entityFile)
|
||||
{
|
||||
m_Handler.SetStartEntityCallback(std::bind(&EntityFileParser::onStartEntity, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
|
||||
m_Handler.SetStartComponentCallback(std::bind(&EntityFileParser::onStartComponent, this, std::placeholders::_1, std::placeholders::_2));
|
||||
m_Handler.SetStartFieldCallback(std::bind(&EntityFileParser::onStartComponentField, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4));
|
||||
m_Handler.SetStartFieldDataCallback(std::bind(&EntityFileParser::onFieldData, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4));
|
||||
}
|
||||
|
||||
void EntityFileParser::MergeEntities(World* world, EntityID parent /*= EntityID_Invalid*/)
|
||||
{
|
||||
m_World = world;
|
||||
m_EntityIDMapper[EntityID_Invalid] = parent;
|
||||
m_EntityFile->Parse(&m_Handler);
|
||||
}
|
||||
|
||||
void EntityFileParser::onStartEntity(EntityID entity, EntityID parent, const std::string& name)
|
||||
{
|
||||
EntityID realParent = m_EntityIDMapper.at(parent);
|
||||
EntityID realEntity = m_World->CreateEntity(realParent);
|
||||
if (!name.empty()) {
|
||||
m_World->SetName(realEntity, name);
|
||||
}
|
||||
m_EntityIDMapper[entity] = realEntity;
|
||||
LOG_DEBUG("Created entity #%i (%i) with parent %i (%i)", entity, realEntity, parent, realParent);
|
||||
}
|
||||
|
||||
void EntityFileParser::onStartComponent(EntityID entity, const std::string& component)
|
||||
{
|
||||
EntityID realEntity = m_EntityIDMapper.at(entity);
|
||||
m_World->AttachComponent(realEntity, component);
|
||||
LOG_DEBUG("Attached component of type \"%s\" to entity #%i (%i)", component.c_str(), entity, realEntity);
|
||||
}
|
||||
|
||||
void EntityFileParser::onStartComponentField(EntityID entity, const std::string& componentType, const std::string& fieldName, const std::map<std::string, std::string>& attributes)
|
||||
{
|
||||
EntityID realEntity = m_EntityIDMapper.at(entity);
|
||||
ComponentWrapper component = m_World->GetComponent(realEntity, componentType);
|
||||
auto& field = component.Info.Fields.at(fieldName);
|
||||
|
||||
LOG_DEBUG("Field \"%s\" type \"%s\"", fieldName.c_str(), field.Type.c_str());
|
||||
LOG_DEBUG("Attributes:");
|
||||
for (auto& kv : attributes) {
|
||||
LOG_DEBUG("\t%s = %s", kv.first.c_str(), kv.second.c_str());
|
||||
}
|
||||
|
||||
char* data = component.Data + field.Offset;
|
||||
EntityFile::WriteAttributeData(data, field, attributes);
|
||||
}
|
||||
|
||||
void EntityFileParser::onFieldData(EntityID entity, const std::string& componentType, const std::string& fieldName, const char* fieldData)
|
||||
{
|
||||
EntityID realEntity = m_EntityIDMapper.at(entity);
|
||||
ComponentWrapper component = m_World->GetComponent(realEntity, componentType);
|
||||
auto& field = component.Info.Fields.at(fieldName);
|
||||
|
||||
char* data = component.Data + field.Offset;
|
||||
EntityFile::WriteValueData(data, field, fieldData);
|
||||
}
|
||||
@@ -0,0 +1,229 @@
|
||||
#include "Core/EntityFilePreprocessor.h"
|
||||
|
||||
EntityFilePreprocessor::EntityFilePreprocessor(const EntityFile* entityFile)
|
||||
: m_EntityFile(entityFile)
|
||||
{
|
||||
EntityFileHandler handler;
|
||||
handler.SetStartComponentCallback(std::bind(&EntityFilePreprocessor::onStartComponent, this, std::placeholders::_1, std::placeholders::_2));
|
||||
m_EntityFile->Parse(&handler);
|
||||
|
||||
LOG_DEBUG("___ COMPONENT DEFINITIONS ___");
|
||||
for (auto& kv : m_ComponentCounts) {
|
||||
LOG_DEBUG("%s: %i", kv.first.c_str(), kv.second);
|
||||
}
|
||||
|
||||
parseComponentInfo();
|
||||
|
||||
for (auto& kv : m_ComponentInfo) {
|
||||
auto& info = kv.second;
|
||||
LOG_DEBUG("Component: %s (%s)", info.Name.c_str(), info.Meta.Annotation.c_str());
|
||||
LOG_DEBUG("Stride: %i", info.Meta.Stride);
|
||||
LOG_DEBUG("Allocation: %i", info.Meta.Allocation);
|
||||
for (auto& kv : info.Fields) {
|
||||
auto& field = kv.second;
|
||||
LOG_DEBUG("\t%i\t%s %s", field.Offset, field.Type, kv.first.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
parseDefaults();
|
||||
}
|
||||
|
||||
void EntityFilePreprocessor::RegisterComponents(World* world)
|
||||
{
|
||||
for (auto& kv : m_ComponentInfo) {
|
||||
world->RegisterComponent(kv.second);
|
||||
}
|
||||
}
|
||||
|
||||
void EntityFilePreprocessor::onStartComponent(EntityID entity, std::string type)
|
||||
{
|
||||
//LOG_DEBUG("Component: %s", type.c_str());
|
||||
m_ComponentCounts[type]++;
|
||||
}
|
||||
|
||||
void EntityFilePreprocessor::parseComponentInfo()
|
||||
{
|
||||
using namespace xercesc;
|
||||
EntityFileXMLErrorHandler errorHandler;
|
||||
auto grammarPool = m_EntityFile->GrammarPool();
|
||||
bool whateverTheFuckThisIs;
|
||||
auto xsModel = grammarPool->getXSModel(whateverTheFuckThisIs);
|
||||
|
||||
// Find component xsd element declarations
|
||||
std::cout << "Enumerating components..." << std::endl;
|
||||
// <xs:element name="ComponentName">
|
||||
auto topLevelElements = xsModel->getComponents(XSConstants::ELEMENT_DECLARATION);
|
||||
for (unsigned int i = 0; i < topLevelElements->getLength(); ++i) {
|
||||
auto element = static_cast<XSElementDeclaration*>(topLevelElements->item(i));
|
||||
|
||||
std::string nameSpace = XS::ToString(element->getNamespace());
|
||||
if (nameSpace != "components") {
|
||||
continue;
|
||||
}
|
||||
|
||||
ComponentInfo compInfo;
|
||||
|
||||
// Name
|
||||
compInfo.Name = XS::ToString(element->getName());
|
||||
// Known allocation
|
||||
compInfo.Meta.Allocation = m_ComponentCounts[compInfo.Name];
|
||||
// Annotation
|
||||
auto componentAnnotation = element->getAnnotation();
|
||||
if (componentAnnotation != nullptr) {
|
||||
// Parse annotation XML
|
||||
char* annotationString = XMLString::transcode(componentAnnotation->getAnnotationString());
|
||||
MemBufInputSource annotationInput(reinterpret_cast<const XMLByte*>(annotationString), strlen(annotationString), "MemBuf: Annotation String");
|
||||
XercesDOMParser parser(nullptr, XMLPlatformUtils::fgMemoryManager, grammarPool);
|
||||
parser.setErrorHandler(&errorHandler);
|
||||
parser.parse(annotationInput);
|
||||
XMLString::release(&annotationString);
|
||||
auto doc = parser.getDocument();
|
||||
|
||||
// TODO: Add allocation estimations from external file on map-to-map basis
|
||||
// Add allocation estimation(s)
|
||||
//auto allocationTags = doc->getElementsByTagName(XSTR("meta:allocation"));
|
||||
//for (int i = 0; i < allocationTags->getLength(); ++i) {
|
||||
// auto allocation = dynamic_cast<DOMElement*>(allocationTags->item(i));
|
||||
// auto child = allocation->getFirstChild();
|
||||
// if (child == nullptr) {
|
||||
// continue;
|
||||
// }
|
||||
|
||||
// XSValue::Status status;
|
||||
// XSValue* val = XSValue::getActualValue(child->getNodeValue(), XSValue::dt_integer, status);
|
||||
// compInfo.Meta.Allocation += val->fData.fValue.f_int;
|
||||
//}
|
||||
|
||||
// Save documentation string
|
||||
auto documentationTags = doc->getElementsByTagName(XS::ToXMLCh("xs:documentation"));
|
||||
if (documentationTags->getLength() != 0) {
|
||||
auto child = documentationTags->item(0)->getFirstChild();
|
||||
if (child != nullptr) {
|
||||
compInfo.Meta.Annotation = XS::ToString(child->getNodeValue());
|
||||
}
|
||||
}
|
||||
} else {
|
||||
LOG_WARNING("Component is missing an annotation!");
|
||||
}
|
||||
|
||||
// <xs:complexType>
|
||||
auto typeDefinition = element->getTypeDefinition();
|
||||
if (typeDefinition->getTypeCategory() != XSTypeDefinition::COMPLEX_TYPE) {
|
||||
LOG_ERROR("Type definition wasn't COMPLEX_TYPE! Skipping.");
|
||||
continue;
|
||||
}
|
||||
auto complexTypeDefinition = dynamic_cast<XSComplexTypeDefinition*>(typeDefinition);
|
||||
|
||||
// <xs:all>
|
||||
auto modelGroupParticle = complexTypeDefinition->getParticle();
|
||||
if (modelGroupParticle->getTermType() != XSParticle::TERM_MODELGROUP) {
|
||||
LOG_ERROR("Model group particle wasn't TERM_MODELGROUP! Skipping.");
|
||||
continue;
|
||||
}
|
||||
auto modelGroup = modelGroupParticle->getModelGroupTerm();
|
||||
|
||||
// <xs:element...
|
||||
// <xs:attribute...
|
||||
unsigned int fieldOffset = 0;
|
||||
auto particles = modelGroup->getParticles();
|
||||
for (unsigned int i = 0; i < particles->size(); ++i) {
|
||||
auto particle = particles->elementAt(i);
|
||||
if (particle->getTermType() != XSParticle::TERM_ELEMENT) {
|
||||
LOG_ERROR("Particle wasn't TERM_ELEMENT! Skipping.");
|
||||
continue;
|
||||
}
|
||||
auto elementDeclaration = particle->getElementTerm();
|
||||
|
||||
std::string name = XS::ToString(elementDeclaration->getName());
|
||||
std::string type = XS::ToString(elementDeclaration->getTypeDefinition()->getName());
|
||||
|
||||
size_t stride = EntityFile::GetTypeStride(type);
|
||||
if (stride == 0) {
|
||||
std::cout << "Warning: Field \"" << name << "\" in component \"" << compInfo.Name << "\" uses unexpected field type \"" << type << "\". Skipping." << std::endl;
|
||||
continue;
|
||||
}
|
||||
|
||||
auto& field = compInfo.Fields[name];
|
||||
field.Type = type;
|
||||
field.Offset = fieldOffset;
|
||||
field.Stride = stride;
|
||||
compInfo.FieldsInOrder.push_back(&field);
|
||||
|
||||
fieldOffset += stride;
|
||||
}
|
||||
|
||||
compInfo.Meta.Stride = fieldOffset;
|
||||
m_ComponentInfo[compInfo.Name] = compInfo;
|
||||
}
|
||||
}
|
||||
|
||||
void EntityFilePreprocessor::parseDefaults()
|
||||
{
|
||||
using namespace xercesc;
|
||||
|
||||
EntityFileXMLErrorHandler errorHandler;
|
||||
|
||||
for (auto& ci : m_ComponentInfo) {
|
||||
// Allocate memory for default values
|
||||
ci.second.Defaults = std::shared_ptr<char>(new char[ci.second.Meta.Stride]);
|
||||
memset(ci.second.Defaults.get(), 0, ci.second.Meta.Stride);
|
||||
|
||||
XercesDOMParser parser(nullptr, XMLPlatformUtils::fgMemoryManager);
|
||||
parser.setErrorHandler(&errorHandler);
|
||||
|
||||
std::string componentName = ci.first;
|
||||
LOG_DEBUG("Parsing defaults for component %s", componentName.c_str());
|
||||
boost::filesystem::path defaultsFile = "Schema/Components/" + componentName + ".xml";
|
||||
|
||||
parser.parse(defaultsFile.string().c_str());
|
||||
auto doc = parser.getDocument();
|
||||
if (doc == nullptr) {
|
||||
LOG_ERROR("%s not found! Skipping.", defaultsFile.string().c_str());
|
||||
continue;
|
||||
}
|
||||
|
||||
// Find the node in the components namespace matching the component name
|
||||
std::string tagName = "c:" + componentName;
|
||||
auto rootNodes = doc->getElementsByTagName(XS::ToXMLCh(tagName));
|
||||
if (rootNodes->getLength() == 0) {
|
||||
LOG_ERROR("Couldn't find defaults for component \"%s\"! Skipping.", componentName.c_str());
|
||||
continue;
|
||||
}
|
||||
auto componentElement = dynamic_cast<DOMElement*>(rootNodes->item(0));
|
||||
|
||||
// Fill the default value buffer with values
|
||||
for (auto& kv : ci.second.Fields) {
|
||||
std::string fieldName = kv.first;
|
||||
auto& field = kv.second;
|
||||
auto fieldNodes = componentElement->getElementsByTagName(XS::ToXMLCh(fieldName));
|
||||
auto fieldNode = fieldNodes->item(0);
|
||||
if (fieldNode == nullptr) {
|
||||
LOG_ERROR("Defaults for component \"%s\" is missing field \"%s\"!", componentName.c_str(), fieldName.c_str());
|
||||
continue;
|
||||
}
|
||||
auto fieldElement = dynamic_cast<DOMElement*>(fieldNode);
|
||||
|
||||
char* data = ci.second.Defaults.get() + field.Offset;
|
||||
|
||||
// Handle potential field attributes
|
||||
if (fieldElement->hasAttributes()) {
|
||||
std::map<std::string, std::string> attributes;
|
||||
auto attributeMap = fieldElement->getAttributes();
|
||||
for (int i = 0; i < attributeMap->getLength(); ++i) {
|
||||
auto attribItem = attributeMap->item(i);
|
||||
attributes[XS::ToString(attribItem->getNodeName())] = XS::ToString(attribItem->getNodeValue());
|
||||
}
|
||||
EntityFile::WriteAttributeData(data, field, attributes);
|
||||
}
|
||||
|
||||
// Handle potential field values
|
||||
auto childNode = fieldElement->getFirstChild();
|
||||
if (childNode != nullptr && childNode->getNodeType() == DOMNode::TEXT_NODE) {
|
||||
char* cstrValue = XMLString::transcode(childNode->getNodeValue());
|
||||
EntityFile::WriteValueData(data, field, cstrValue);
|
||||
XMLString::release(&cstrValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,140 @@
|
||||
#include "Core/EntityFileWriter.h"
|
||||
|
||||
#define X(str) XS::ToXMLCh(str)
|
||||
|
||||
void EntityFileWriter::WriteWorld(World* world)
|
||||
{
|
||||
WriteEntity(world, 0);
|
||||
}
|
||||
|
||||
void EntityFileWriter::WriteEntity(World* world, EntityID entity)
|
||||
{
|
||||
using namespace xercesc;
|
||||
DOMDocument* doc = m_DOMImplementation->createDocument(nullptr, X("Entity"), nullptr);
|
||||
DOMElement* root = doc->getDocumentElement();
|
||||
root->setAttribute(X("xmlns:xsi"), X("http://www.w3.org/2001/XMLSchema-instance"));
|
||||
root->setAttribute(X("xsi:noNamespaceSchemaLocation"), X("../Types/Entity.xsd"));
|
||||
root->setAttribute(X("xmlns:c"), X("components"));
|
||||
const std::string& name = world->GetName(entity);
|
||||
if (!name.empty()) {
|
||||
root->setAttribute(X("name"), X(name));
|
||||
}
|
||||
|
||||
DOMElement* componentsElement = doc->createElement(X("Components"));
|
||||
root->appendChild(componentsElement);
|
||||
appentEntityComponents(componentsElement, world, entity);
|
||||
|
||||
DOMElement* childrenElement = doc->createElement(X("Children"));
|
||||
root->appendChild(childrenElement);
|
||||
appendEntityChildren(childrenElement, world, entity);
|
||||
|
||||
try {
|
||||
LocalFileFormatTarget* target = new LocalFileFormatTarget(X(m_FilePath.string()));
|
||||
DOMLSOutput* output = static_cast<DOMImplementationLS*>(m_DOMImplementation)->createLSOutput();
|
||||
output->setByteStream(target);
|
||||
m_DOMLSSerializer->write(doc, output);
|
||||
delete target;
|
||||
} catch (const std::runtime_error& e) {
|
||||
LOG_ERROR("Failed to save \"%s\": %s", m_FilePath.c_str(), e.what());
|
||||
}
|
||||
|
||||
doc->release();
|
||||
}
|
||||
|
||||
void EntityFileWriter::appendEntityChildren(xercesc::DOMElement* parentElement, const World* world, EntityID entity)
|
||||
{
|
||||
using namespace xercesc;
|
||||
DOMDocument* doc = parentElement->getOwnerDocument();
|
||||
|
||||
auto childrenRange = world->GetEntityChildren().equal_range(entity);
|
||||
for (auto it = childrenRange.first; it != childrenRange.second; ++it) {
|
||||
EntityID childEntity = it->second;
|
||||
|
||||
DOMElement* entityElement = doc->createElement(X("Entity"));
|
||||
const std::string& name = world->GetName(childEntity);
|
||||
if (!name.empty()) {
|
||||
entityElement->setAttribute(X("name"), X(name));
|
||||
}
|
||||
parentElement->appendChild(entityElement);
|
||||
|
||||
DOMElement* componentsElement = doc->createElement(X("Components"));
|
||||
entityElement->appendChild(componentsElement);
|
||||
appentEntityComponents(componentsElement, world, childEntity);
|
||||
|
||||
DOMElement* childrenElement = doc->createElement(X("Children"));
|
||||
entityElement->appendChild(childrenElement);
|
||||
appendEntityChildren(childrenElement, world, childEntity);
|
||||
}
|
||||
}
|
||||
|
||||
void EntityFileWriter::appentEntityComponents(xercesc::DOMElement* parentElement, const World* world, EntityID entity)
|
||||
{
|
||||
using namespace xercesc;
|
||||
DOMDocument* doc = parentElement->getOwnerDocument();
|
||||
auto& componentPools = world->GetComponentPools();
|
||||
|
||||
// Step through all component pools to get an entity's components
|
||||
// HACK: This is sloooow.
|
||||
for (auto& kv : componentPools) {
|
||||
const std::string& componentName = kv.first;
|
||||
if (!world->HasComponent(entity, componentName)) {
|
||||
continue;
|
||||
}
|
||||
std::string qualifiedComponentName = "c:" + componentName;
|
||||
DOMElement* componentElement = doc->createElement(X(qualifiedComponentName));
|
||||
parentElement->appendChild(componentElement);
|
||||
ComponentWrapper c = kv.second->GetByEntity(entity);
|
||||
for (auto& kv : c.Info.Fields) {
|
||||
std::string fieldName = kv.first;
|
||||
auto& field = kv.second;
|
||||
|
||||
// Ignore fields that are equal to the default
|
||||
// HACK: This is probably sloooooow, but it's okay.
|
||||
if (memcmp(c.Data + field.Offset, c.Info.Defaults.get() + field.Offset, field.Stride) == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
DOMElement* fieldElement = doc->createElement(X(fieldName));
|
||||
componentElement->appendChild(fieldElement);
|
||||
|
||||
if (field.Type == "Vector") {
|
||||
const glm::vec3& vec = c[fieldName];
|
||||
fieldElement->setAttribute(X("X"), X(boost::lexical_cast<std::string>(vec.x)));
|
||||
fieldElement->setAttribute(X("Y"), X(boost::lexical_cast<std::string>(vec.y)));
|
||||
fieldElement->setAttribute(X("Z"), X(boost::lexical_cast<std::string>(vec.z)));
|
||||
} else if (field.Type == "Color") {
|
||||
const glm::vec4& vec = c[fieldName];
|
||||
fieldElement->setAttribute(X("R"), X(boost::lexical_cast<std::string>(vec.r)));
|
||||
fieldElement->setAttribute(X("G"), X(boost::lexical_cast<std::string>(vec.g)));
|
||||
fieldElement->setAttribute(X("B"), X(boost::lexical_cast<std::string>(vec.b)));
|
||||
fieldElement->setAttribute(X("A"), X(boost::lexical_cast<std::string>(vec.a)));
|
||||
} else if (field.Type == "Quaternion") {
|
||||
const glm::quat& q = c[fieldName];
|
||||
fieldElement->setAttribute(X("X"), X(boost::lexical_cast<std::string>(q.x)));
|
||||
fieldElement->setAttribute(X("Y"), X(boost::lexical_cast<std::string>(q.y)));
|
||||
fieldElement->setAttribute(X("Z"), X(boost::lexical_cast<std::string>(q.z)));
|
||||
fieldElement->setAttribute(X("W"), X(boost::lexical_cast<std::string>(q.w)));
|
||||
} else if (field.Type == "int") {
|
||||
const int& value = c[fieldName];
|
||||
fieldElement->appendChild(doc->createTextNode(X(boost::lexical_cast<std::string>(value))));
|
||||
} else if (field.Type == "float") {
|
||||
const float& value = c[fieldName];
|
||||
fieldElement->appendChild(doc->createTextNode(X(boost::lexical_cast<std::string>(value))));
|
||||
} else if (field.Type == "double") {
|
||||
const double& value = c[fieldName];
|
||||
fieldElement->appendChild(doc->createTextNode(X(boost::lexical_cast<std::string>(value))));
|
||||
} else if (field.Type == "bool") {
|
||||
const bool& value = c[fieldName];
|
||||
if (value) {
|
||||
fieldElement->appendChild(doc->createTextNode(X("true")));
|
||||
} else {
|
||||
fieldElement->appendChild(doc->createTextNode(X("false")));
|
||||
}
|
||||
} else if (field.Type == "string") {
|
||||
const std::string& value = c[fieldName];
|
||||
fieldElement->appendChild(doc->createTextNode(X(value)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,495 +0,0 @@
|
||||
#include "Core/EntityXMLFile.h"
|
||||
#include "Core/World.h"
|
||||
|
||||
unsigned int EntityXMLFile::InstanceCount = 0;
|
||||
|
||||
EntityXMLFile::EntityXMLFile(std::string path)
|
||||
: m_EntityFile(path)
|
||||
{
|
||||
using namespace xercesc;
|
||||
|
||||
if (InstanceCount == 0) {
|
||||
XMLPlatformUtils::Initialize();
|
||||
}
|
||||
InstanceCount++;
|
||||
|
||||
m_GrammarPool = new XMLGrammarPoolImpl();
|
||||
m_ErrorHandler = new EntityParserXMLErrorHandler();
|
||||
m_DOMParser = new XercesDOMParser(nullptr, XMLPlatformUtils::fgMemoryManager, m_GrammarPool);
|
||||
m_DOMParser->setErrorHandler(m_ErrorHandler);
|
||||
m_DOMParser->setDoNamespaces(true);
|
||||
m_DOMParser->setDoXInclude(true);
|
||||
m_DOMParser->setDoSchema(true);
|
||||
m_DOMParser->setValidationSchemaFullChecking(true);
|
||||
m_DOMParser->setValidationScheme(xercesc::XercesDOMParser::Val_Auto);
|
||||
m_DOMParser->setValidationSchemaFullChecking(true);
|
||||
m_DOMParser->setValidationConstraintFatal(false);
|
||||
m_DOMParser->setIncludeIgnorableWhitespace(false);
|
||||
// Make sure schema grammar is kept after validation
|
||||
m_DOMParser->cacheGrammarFromParse(true);
|
||||
|
||||
// HACK: Use Sax2 parser instead so the entire DOM doesn't have to reside in memory
|
||||
m_DOMParser->parse(m_EntityFile.c_str());
|
||||
m_DOMDocument = m_DOMParser->getDocument();
|
||||
|
||||
// 1. Fill in ComponentInfo name, fields, default values and metadata from PSVI
|
||||
parseComponentInfo();
|
||||
// 2. Parse default value files for those components
|
||||
parseDefaults();
|
||||
// 3. Allocate component structures
|
||||
predictComponentAllocation();
|
||||
}
|
||||
|
||||
EntityXMLFile::~EntityXMLFile()
|
||||
{
|
||||
using namespace xercesc;
|
||||
|
||||
if (m_DOMParser != nullptr) {
|
||||
delete m_DOMParser;
|
||||
}
|
||||
if (m_ErrorHandler != nullptr) {
|
||||
delete m_ErrorHandler;
|
||||
}
|
||||
if (m_GrammarPool != nullptr) {
|
||||
delete m_GrammarPool;
|
||||
}
|
||||
|
||||
InstanceCount--;
|
||||
if (InstanceCount == 0) {
|
||||
XMLPlatformUtils::Terminate();
|
||||
}
|
||||
}
|
||||
|
||||
void EntityXMLFile::PopulateWorld(World* world)
|
||||
{
|
||||
for (auto& pair : m_ComponentInfo) {
|
||||
world->RegisterComponent(pair.second);
|
||||
}
|
||||
|
||||
// 4. Parse entity hierarchy
|
||||
auto root = m_DOMDocument->getDocumentElement();
|
||||
parseEntityGraph(world, root, 0);
|
||||
}
|
||||
|
||||
|
||||
void EntityXMLFile::preprocess(std::string inPath, std::string outPath)
|
||||
{
|
||||
using namespace xercesc;
|
||||
|
||||
static const XMLCh gLS[] = { 'L', 'S', '\0' };
|
||||
DOMImplementationLS* di = static_cast<DOMImplementationLS*>(DOMImplementationRegistry::getDOMImplementation(gLS));
|
||||
|
||||
// Parse the file
|
||||
DOMLSParser* parser = di->createLSParser(DOMImplementationLS::MODE_SYNCHRONOUS, nullptr);
|
||||
DOMConfiguration* config = parser->getDomConfig();
|
||||
config->setParameter(XMLUni::fgDOMNamespaces, true);
|
||||
config->setParameter(XMLUni::fgXercesSchema, true);
|
||||
config->setParameter(XMLUni::fgXercesHandleMultipleImports, true);
|
||||
config->setParameter(XMLUni::fgXercesSchemaFullChecking, true);
|
||||
config->setParameter(XMLUni::fgXercesDoXInclude, true);
|
||||
auto errHandler = new EntityPreprocessorXMLErrorHandler();
|
||||
config->setParameter(XMLUni::fgDOMErrorHandler, errHandler);
|
||||
|
||||
auto source = new LocalFileInputSource(XSTR(inPath.c_str()));
|
||||
Wrapper4InputSource* domSourceWrapper = new Wrapper4InputSource(source);
|
||||
DOMDocument* doc = parser->parse(dynamic_cast<DOMLSInput*>(domSourceWrapper));
|
||||
|
||||
// Serialize and output the new XML
|
||||
DOMLSSerializer* writer = di->createLSSerializer();
|
||||
DOMLSOutput* output = di->createLSOutput();
|
||||
XMLFormatTarget* formatTarget = new LocalFileFormatTarget(outPath.c_str());
|
||||
// TODO: MemBufFormatTarget* formatTarget = new MemBufFormatTarget()
|
||||
output->setByteStream(formatTarget);
|
||||
writer->write(doc, output);
|
||||
|
||||
delete formatTarget;
|
||||
output->release();
|
||||
writer->release();
|
||||
parser->release();
|
||||
}
|
||||
|
||||
void EntityXMLFile::parseComponentInfo()
|
||||
{
|
||||
using namespace xercesc;
|
||||
bool wasChanged;
|
||||
XSModel* xsModel = m_GrammarPool->getXSModel(wasChanged);
|
||||
|
||||
// Find component xsd element declarations
|
||||
std::cout << "Enumerating components..." << std::endl;
|
||||
// <xs:element name="ComponentName">
|
||||
auto topLevelElements = xsModel->getComponents(XSConstants::ELEMENT_DECLARATION);
|
||||
for (unsigned int i = 0; i < topLevelElements->getLength(); ++i) {
|
||||
auto element = static_cast<XSElementDeclaration*>(topLevelElements->item(i));
|
||||
|
||||
std::string nameSpace(XSTR(element->getNamespace()));
|
||||
if (nameSpace != "components") {
|
||||
continue;
|
||||
}
|
||||
|
||||
ComponentInfo compInfo;
|
||||
|
||||
// Name
|
||||
compInfo.Name = XSTR(element->getName());
|
||||
// Annotation
|
||||
auto componentAnnotation = element->getAnnotation();
|
||||
if (componentAnnotation != nullptr) {
|
||||
// Parse annotation XML
|
||||
char* annotationString = XMLString::transcode(componentAnnotation->getAnnotationString());
|
||||
MemBufInputSource annotationInput(reinterpret_cast<const XMLByte*>(annotationString), strlen(annotationString), "MemBuf: Annotation String");
|
||||
XercesDOMParser parser(nullptr, XMLPlatformUtils::fgMemoryManager, m_GrammarPool);
|
||||
parser.setErrorHandler(m_ErrorHandler);
|
||||
parser.parse(annotationInput);
|
||||
XMLString::release(&annotationString);
|
||||
auto doc = parser.getDocument();
|
||||
|
||||
// Add allocation estimation(s)
|
||||
auto allocationTags = doc->getElementsByTagName(XSTR("meta:allocation"));
|
||||
for (int i = 0; i < allocationTags->getLength(); ++i) {
|
||||
auto allocation = dynamic_cast<DOMElement*>(allocationTags->item(i));
|
||||
auto child = allocation->getFirstChild();
|
||||
if (child == nullptr) {
|
||||
continue;
|
||||
}
|
||||
|
||||
XSValue::Status status;
|
||||
XSValue* val = XSValue::getActualValue(child->getNodeValue(), XSValue::dt_integer, status);
|
||||
compInfo.Meta.Allocation += val->fData.fValue.f_int;
|
||||
}
|
||||
|
||||
// Save documentation string
|
||||
auto documentationTags = doc->getElementsByTagName(XSTR("xs:documentation"));
|
||||
if (documentationTags->getLength() != 0) {
|
||||
auto child = documentationTags->item(0)->getFirstChild();
|
||||
if (child != nullptr) {
|
||||
compInfo.Meta.Annotation = XSTR(child->getNodeValue());
|
||||
}
|
||||
}
|
||||
// TODO: Parse annotation string XML
|
||||
// compInfo.Meta.Allocation = ...
|
||||
} else {
|
||||
std::cout << "Warning: Component is missing an annotation!" << std::endl;
|
||||
}
|
||||
|
||||
// <xs:complexType>
|
||||
auto typeDefinition = element->getTypeDefinition();
|
||||
if (typeDefinition->getTypeCategory() != XSTypeDefinition::COMPLEX_TYPE) {
|
||||
std::cerr << "Error: Type definition wasn't COMPLEX_TYPE! Skipping." << std::endl;
|
||||
continue;
|
||||
}
|
||||
auto complexTypeDefinition = dynamic_cast<XSComplexTypeDefinition*>(typeDefinition);
|
||||
|
||||
// <xs:all>
|
||||
auto modelGroupParticle = complexTypeDefinition->getParticle();
|
||||
if (modelGroupParticle->getTermType() != XSParticle::TERM_MODELGROUP) {
|
||||
std::cerr << "Error: Model group particle wasn't TERM_MODELGROUP! Skipping." << std::endl;
|
||||
continue;
|
||||
}
|
||||
auto modelGroup = modelGroupParticle->getModelGroupTerm();
|
||||
|
||||
// <xs:element...
|
||||
// <xs:attribute...
|
||||
unsigned int fieldOffset = 0;
|
||||
auto particles = modelGroup->getParticles();
|
||||
for (unsigned int i = 0; i < particles->size(); ++i) {
|
||||
auto particle = particles->elementAt(i);
|
||||
if (particle->getTermType() != XSParticle::TERM_ELEMENT) {
|
||||
std::cerr << "Error: Particle wasn't TERM_ELEMENT! Skipping." << std::endl;
|
||||
continue;
|
||||
}
|
||||
auto elementDeclaration = particle->getElementTerm();
|
||||
|
||||
std::string name = XSTR(elementDeclaration->getName());
|
||||
std::string type = XSTR(elementDeclaration->getTypeDefinition()->getName());
|
||||
|
||||
size_t stride = getTypeStride(type);
|
||||
if (stride == 0) {
|
||||
std::cout << "Warning: Field \"" << name << "\" in component \"" << compInfo.Name << "\" uses unexpected field type \"" << type << "\". Skipping." << std::endl;
|
||||
continue;
|
||||
}
|
||||
|
||||
compInfo.FieldTypes[name] = type;
|
||||
compInfo.FieldOffsets[name] = fieldOffset;
|
||||
fieldOffset += getTypeStride(type);
|
||||
}
|
||||
|
||||
compInfo.Meta.Stride = fieldOffset;
|
||||
m_ComponentInfo[compInfo.Name] = compInfo;
|
||||
}
|
||||
}
|
||||
|
||||
void EntityXMLFile::parseDefaults()
|
||||
{
|
||||
using namespace xercesc;
|
||||
|
||||
for (auto& ci : m_ComponentInfo) {
|
||||
// Allocate memory for default values
|
||||
ci.second.Defaults = std::shared_ptr<char>(new char[ci.second.Meta.Stride]);
|
||||
memset(ci.second.Defaults.get(), 0, ci.second.Meta.Stride);
|
||||
|
||||
XercesDOMParser parser(nullptr, XMLPlatformUtils::fgMemoryManager);
|
||||
parser.setErrorHandler(m_ErrorHandler);
|
||||
|
||||
std::string componentName = ci.first;
|
||||
LOG_DEBUG("Parsing defaults for component %s", componentName.c_str());
|
||||
boost::filesystem::path defaultsFile = "Schema/Components/" + componentName + ".xml";
|
||||
|
||||
parser.parse(defaultsFile.string().c_str());
|
||||
auto doc = parser.getDocument();
|
||||
if (doc == nullptr) {
|
||||
LOG_ERROR("%s not found! Skipping.", defaultsFile.string().c_str());
|
||||
continue;
|
||||
}
|
||||
|
||||
// Find the node in the components namespace matching the component name
|
||||
std::string tagName = "c:" + componentName;
|
||||
auto rootNodes = doc->getElementsByTagName(XSTR(tagName.c_str()));
|
||||
if (rootNodes->getLength() == 0) {
|
||||
LOG_ERROR("Couldn't find defaults for component \"%s\"! Skipping.", componentName.c_str());
|
||||
continue;
|
||||
}
|
||||
auto componentElement = dynamic_cast<DOMElement*>(rootNodes->item(0));
|
||||
|
||||
// Fill the default value buffer with values
|
||||
for (auto& field : ci.second.FieldOffsets) {
|
||||
std::string fieldName = field.first;
|
||||
auto fieldNodes = componentElement->getElementsByTagName(XSTR(fieldName.c_str()));
|
||||
auto fieldNode = fieldNodes->item(0);
|
||||
if (fieldNode == nullptr) {
|
||||
LOG_ERROR("Defaults for component \"%s\" is missing field \"%s\"!", componentName.c_str(), fieldName.c_str());
|
||||
continue;
|
||||
}
|
||||
auto fieldElement = dynamic_cast<DOMElement*>(fieldNode);
|
||||
|
||||
std::string fieldType = ci.second.FieldTypes.at(fieldName);
|
||||
unsigned int fieldOffset = ci.second.FieldOffsets.at(fieldName);
|
||||
writeData(fieldElement, fieldType, ci.second.Defaults.get() + fieldOffset);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void EntityXMLFile::predictComponentAllocation()
|
||||
{
|
||||
using namespace xercesc;
|
||||
|
||||
auto root = m_DOMDocument->getDocumentElement();
|
||||
|
||||
// Count static instances of components present in entity hierarchy
|
||||
auto components = m_DOMDocument->getElementsByTagNameNS(XSTR("components"), XSTR("*"));
|
||||
for (int i = 0; i < components->getLength(); ++i) {
|
||||
auto component = dynamic_cast<DOMElement*>(components->item(i));
|
||||
|
||||
std::string componentName = XSTR(component->getLocalName());
|
||||
auto& compInfo = m_ComponentInfo.at(componentName);
|
||||
compInfo.Meta.Allocation += 1;
|
||||
}
|
||||
|
||||
std::cout << "COMPONENT INFO" << std::endl;
|
||||
for (auto& pair : m_ComponentInfo) {
|
||||
ComponentInfo& ci = pair.second;
|
||||
std::cout << "Component: " << ci.Name << " (" << ci.Meta.Annotation << ")" << std::endl;
|
||||
std::cout << " Allocation: " << ci.Meta.Allocation << std::endl;
|
||||
std::cout << " Fields:" << std::endl;
|
||||
|
||||
// Calculate component size
|
||||
std::size_t stride = 0;
|
||||
// Add size of fields
|
||||
for (auto& field : ci.FieldTypes) {
|
||||
std::cout << " " << field.second << " " << field.first << " (" << getTypeStride(field.second) << " byte)" << std::endl;
|
||||
stride += getTypeStride(field.second);
|
||||
}
|
||||
std::cout << " Stride: " << ci.Meta.Stride << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void EntityXMLFile::parseEntityGraph(World* world, xercesc::DOMElement* element, EntityID parentEntity)
|
||||
{
|
||||
using namespace xercesc;
|
||||
|
||||
// Create entity
|
||||
EntityID entity = world->CreateEntity(parentEntity);
|
||||
LOG_DEBUG("Created entity %i, parent %i", entity, parentEntity);
|
||||
|
||||
// Add components
|
||||
auto components = m_DOMDocument->evaluate(XSTR("Components/*"), element, nullptr, DOMXPathResult::ORDERED_NODE_SNAPSHOT_TYPE, nullptr);
|
||||
for (int i = 0; i < components->getSnapshotLength(); i++) {
|
||||
components->snapshotItem(i);
|
||||
auto componentElement = dynamic_cast<DOMElement*>(components->getNodeValue());
|
||||
std::string componentName = XSTR(componentElement->getLocalName());
|
||||
auto& ci = m_ComponentInfo.at(componentName);
|
||||
|
||||
// Attach the component
|
||||
auto c = world->AttachComponent(entity, componentName);
|
||||
LOG_DEBUG("Attached %s component", componentName.c_str());
|
||||
|
||||
// Write field data
|
||||
auto fields = componentElement->getChildNodes();
|
||||
for (int j = 0; j < fields->getLength(); ++j) {
|
||||
auto fieldNode = fields->item(j);
|
||||
auto nodeType = fieldNode->getNodeType();
|
||||
if (nodeType != DOMNode::ELEMENT_NODE) {
|
||||
continue;
|
||||
}
|
||||
auto field = dynamic_cast<DOMElement*>(fields->item(j));
|
||||
//const XMLCh* value = fields->item(j)->getTextContent();
|
||||
std::string fieldName(XSTR(field->getLocalName()));
|
||||
if (ci.FieldTypes.find(fieldName) == ci.FieldTypes.end()) {
|
||||
std::cout << "Warning: Component \"" << componentName << "\" contains invalid field \"" << fieldName << "\". Skipping." << std::endl;
|
||||
continue;
|
||||
}
|
||||
|
||||
std::string fieldType = ci.FieldTypes.at(fieldName);
|
||||
unsigned int fieldOffset = ci.FieldOffsets.at(fieldName);
|
||||
std::string fieldValue(XSTR(field->getTextContent()));
|
||||
LOG_DEBUG(" %s %s = %s", fieldType.c_str(), fieldName.c_str(), fieldValue.c_str());
|
||||
writeData(field, fieldType, c.Data + fieldOffset);
|
||||
}
|
||||
}
|
||||
|
||||
// Recurse children
|
||||
auto children = m_DOMDocument->evaluate(XSTR("Children/Entity"), element, nullptr, DOMXPathResult::ORDERED_NODE_SNAPSHOT_TYPE, nullptr);
|
||||
for (int i = 0; i < children->getSnapshotLength(); i++) {
|
||||
children->snapshotItem(i);
|
||||
parseEntityGraph(world, dynamic_cast<DOMElement*>(children->getNodeValue()), entity);
|
||||
}
|
||||
|
||||
//auto components = m_DOMDocument->getElementsByTagNameNS(XSTR("components"), XSTR("*"));
|
||||
//for (int i = 0; i < components->getLength(); ++i) {
|
||||
// auto component = dynamic_cast<DOMElement*>(components->item(i));
|
||||
|
||||
// std::string componentName = XSTR(component->getLocalName());
|
||||
// auto& compStore = m_ComponentStore.at(componentName);
|
||||
// auto& compInfo = compStore.Info;
|
||||
|
||||
// char* data = &compStore.Data[compStore.Size*compStore.Stride];
|
||||
// compStore.Size += 1;
|
||||
|
||||
// auto fields = component->getChildNodes();
|
||||
// for (int j = 0; j < fields->getLength(); ++j) {
|
||||
// auto field = fields->item(j);
|
||||
// auto nodeType = field->getNodeType();
|
||||
// if (nodeType != DOMNode::ELEMENT_NODE) {
|
||||
// continue;
|
||||
// }
|
||||
// //auto field = dynamic_cast<DOMElement*>(fields->item(j));
|
||||
// //const XMLCh* value = fields->item(j)->getTextContent();
|
||||
// std::string fieldName = XSTR(field->getLocalName());
|
||||
// if (compInfo.FieldTypes.find(fieldName) == compInfo.FieldTypes.end()) {
|
||||
// std::cout << "Warning: Component \"" << componentName << "\" contains invalid field \"" << fieldName << "\". Skipping." << std::endl;
|
||||
// continue;
|
||||
// }
|
||||
|
||||
// std::string fieldType = compInfo.FieldTypes.at(fieldName);
|
||||
// unsigned int fieldOffset = compInfo.FieldOffsets.at(fieldName);
|
||||
|
||||
// XSValue::DataType dataType = XSValue::getDataType(XSTR(fieldType.c_str()));
|
||||
// if (dataType == XSValue::DataType::dt_MAXCOUNT) {
|
||||
// // TODO:
|
||||
// continue;
|
||||
// }
|
||||
// if (dataType == XSValue::DataType::dt_string) {
|
||||
// char* str = XMLString::transcode(field->getTextContent());
|
||||
// std::string standardString(str);
|
||||
// XMLString::release(&str);
|
||||
// memcpy(&data[fieldOffset], reinterpret_cast<char*>(&standardString), getTypeStride(fieldType));
|
||||
// } else {
|
||||
// XSValue::Status status;
|
||||
// XSValue* val = XSValue::getActualValue(field->getTextContent(), dataType, status);
|
||||
// memcpy(&data[fieldOffset], reinterpret_cast<char*>(&val->fData.fValue), getTypeStride(fieldType));
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
|
||||
//auto entities = m_DOMDocument->getElementsByTagName(XSTR("Entity"));
|
||||
//for (int i = 0; i < entities->getLength(); ++i) {
|
||||
// auto entity = dynamic_cast<DOMElement*>(entities->item(i));
|
||||
|
||||
|
||||
// //entity->setIdAttribute()
|
||||
|
||||
// std::cout << "ENTITY " << i + 1 << std::endl;
|
||||
//}
|
||||
}
|
||||
|
||||
std::size_t EntityXMLFile::getTypeStride(std::string typeName)
|
||||
{
|
||||
std::map<std::string, size_t> typeStrides{
|
||||
{ "bool", sizeof(bool) },
|
||||
{ "int", sizeof(int) },
|
||||
{ "float", sizeof(float) },
|
||||
{ "double", sizeof(double) },
|
||||
{ "string", sizeof(std::string) },
|
||||
{ "Vector", sizeof(glm::vec3) },
|
||||
{ "Quaternion", sizeof(glm::quat) },
|
||||
{ "Color", sizeof(glm::vec4) }
|
||||
};
|
||||
|
||||
auto it = typeStrides.find(typeName);
|
||||
return (it != typeStrides.end()) ? it->second : 0;
|
||||
}
|
||||
|
||||
float EntityXMLFile::getFloatAttribute(const xercesc::DOMElement* element, const char* attribute) const
|
||||
{
|
||||
using namespace xercesc;
|
||||
XSValue::Status status;
|
||||
XSValue* val = XSValue::getActualValue(element->getAttribute(XSTR(attribute)), xercesc::XSValue::DataType::dt_double, status);
|
||||
if (val == nullptr) {
|
||||
LOG_ERROR("Element \"%s\" doesn't have an \"%s\" attribute!", XSTR(element->getTagName()), attribute);
|
||||
return 0.f;
|
||||
} else {
|
||||
return static_cast<float>(val->fData.fValue.f_double);
|
||||
}
|
||||
}
|
||||
|
||||
void EntityXMLFile::writeData(const xercesc::DOMElement* element, std::string typeName, char* outData)
|
||||
{
|
||||
using namespace xercesc;
|
||||
|
||||
if (typeName == "Vector") {
|
||||
glm::vec3 vec;
|
||||
vec.x = getFloatAttribute(element, "X");
|
||||
vec.y = getFloatAttribute(element, "Y");
|
||||
vec.z = getFloatAttribute(element, "Z");
|
||||
memcpy(outData, reinterpret_cast<char*>(&vec), getTypeStride(typeName));
|
||||
} else if (typeName == "Color") {
|
||||
glm::vec4 vec;
|
||||
vec.r = getFloatAttribute(element, "R");
|
||||
vec.g = getFloatAttribute(element, "G");
|
||||
vec.b = getFloatAttribute(element, "B");
|
||||
vec.a = getFloatAttribute(element, "A");
|
||||
memcpy(outData, reinterpret_cast<char*>(&vec), getTypeStride(typeName));
|
||||
} else if (typeName == "Quaternion") {
|
||||
glm::quat q;
|
||||
q.x = getFloatAttribute(element, "X");
|
||||
q.y = getFloatAttribute(element, "Y");
|
||||
q.z = getFloatAttribute(element, "Z");
|
||||
q.w = getFloatAttribute(element, "W");
|
||||
memcpy(outData, reinterpret_cast<char*>(&q), getTypeStride(typeName));
|
||||
} else if (typeName == "float") {
|
||||
XSValue::Status status;
|
||||
XSValue* val = XSValue::getActualValue(element->getTextContent(), xercesc::XSValue::DataType::dt_float, status);
|
||||
memcpy(outData, reinterpret_cast<char*>(&val->fData.fValue.f_float), getTypeStride(typeName));
|
||||
} else if (typeName == "double") {
|
||||
XSValue::Status status;
|
||||
XSValue* val = XSValue::getActualValue(element->getTextContent(), xercesc::XSValue::DataType::dt_double, status);
|
||||
memcpy(outData, reinterpret_cast<char*>(&val->fData.fValue.f_double), getTypeStride(typeName));
|
||||
} else if (typeName == "bool") {
|
||||
XSValue::Status status;
|
||||
XSValue* val = XSValue::getActualValue(element->getTextContent(), xercesc::XSValue::DataType::dt_boolean, status);
|
||||
memcpy(outData, reinterpret_cast<char*>(&val->fData.fValue.f_bool), getTypeStride(typeName));
|
||||
} else {
|
||||
XSValue::DataType dataType = XSValue::getDataType(XSTR(typeName.c_str()));
|
||||
if (dataType == XSValue::DataType::dt_string) {
|
||||
char* str = XMLString::transcode(element->getTextContent());
|
||||
std::string standardString(str);
|
||||
new (outData) std::string(str);
|
||||
XMLString::release(&str);
|
||||
//memcpy(outData, reinterpret_cast<char*>(&standardString), getTypeStride(typeName));
|
||||
} else {
|
||||
//XSValue::Status status;
|
||||
//XSValue* val = XSValue::getActualValue(element->getTextContent(), dataType, status);
|
||||
//memcpy(outData, reinterpret_cast<char*>(&val->fData.fValue), getTypeStride(typeName));
|
||||
LOG_WARNING("Unknown native data type: %s", typeName.c_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,21 +2,24 @@
|
||||
|
||||
BaseEventRelay::~BaseEventRelay()
|
||||
{
|
||||
if (m_Broker != nullptr) {
|
||||
if (m_Broker != nullptr) {
|
||||
m_Broker->Unsubscribe(*this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void EventBroker::Unsubscribe(BaseEventRelay &relay) // ?
|
||||
void EventBroker::Unsubscribe(BaseEventRelay& relay) // ?
|
||||
{
|
||||
if (m_IsProcessing) {
|
||||
m_RelaysToUnsubscribe.push_back(&relay);
|
||||
} else {
|
||||
unsubscribeImmediate(relay);
|
||||
}
|
||||
auto identifier = std::make_tuple(relay.m_EventID, relay.m_ContextTypeName, relay.m_EventTypeName);
|
||||
|
||||
relay.m_Broker = nullptr;
|
||||
if (m_IsProcessing) {
|
||||
m_RelaysToUnsubscribe.push_back(identifier);
|
||||
} else {
|
||||
unsubscribeImmediate(identifier);
|
||||
}
|
||||
}
|
||||
|
||||
void EventBroker::Subscribe(BaseEventRelay &relay)
|
||||
void EventBroker::Subscribe(BaseEventRelay& relay)
|
||||
{
|
||||
if (m_IsProcessing) {
|
||||
m_RelaysToSubscribe.push_back(&relay);
|
||||
@@ -38,12 +41,11 @@ int EventBroker::Process(std::string contextTypeName)
|
||||
|
||||
int eventsProcessed = 0;
|
||||
for (auto &pair : *m_EventQueueRead) {
|
||||
std::string &eventTypeName = pair.first;
|
||||
std::string& eventTypeName = pair.first;
|
||||
std::shared_ptr<Event> event = pair.second;
|
||||
|
||||
auto itpair = relays.equal_range(eventTypeName);
|
||||
for (auto it2 = itpair.first; it2 != itpair.second; it2++)
|
||||
{
|
||||
for (auto it2 = itpair.first; it2 != itpair.second; it2++) {
|
||||
std::string name = it2->first;
|
||||
BaseEventRelay* relay = it2->second;
|
||||
relay->Receive(event);
|
||||
@@ -60,8 +62,8 @@ int EventBroker::Process(std::string contextTypeName)
|
||||
m_RelaysToSubscribe.clear();
|
||||
|
||||
// Process pending unsubscriptions
|
||||
for (auto& r : m_RelaysToUnsubscribe) {
|
||||
unsubscribeImmediate(*r);
|
||||
for (auto& identifier : m_RelaysToUnsubscribe) {
|
||||
unsubscribeImmediate(identifier);
|
||||
}
|
||||
m_RelaysToUnsubscribe.clear();
|
||||
|
||||
@@ -81,21 +83,26 @@ void EventBroker::Clear()
|
||||
void EventBroker::subscribeImmediate(BaseEventRelay& relay)
|
||||
{
|
||||
relay.m_Broker = this;
|
||||
relay.m_EventID = m_NextEventID++;
|
||||
m_ContextRelays[relay.m_ContextTypeName].insert(std::make_pair(relay.m_EventTypeName, &relay));
|
||||
}
|
||||
|
||||
void EventBroker::unsubscribeImmediate(BaseEventRelay& relay)
|
||||
void EventBroker::unsubscribeImmediate(std::tuple<EventID, ContextTypeName_t, EventTypeName_t> identifier)
|
||||
{
|
||||
auto contextIt = m_ContextRelays.find(relay.m_ContextTypeName);
|
||||
EventID eventID;
|
||||
ContextTypeName_t contextTypeName;
|
||||
EventTypeName_t eventTypeName;
|
||||
std::tie(eventID, contextTypeName, eventTypeName) = identifier;
|
||||
|
||||
auto contextIt = m_ContextRelays.find(contextTypeName);
|
||||
if (contextIt == m_ContextRelays.end()) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto eventRelays = contextIt->second;
|
||||
auto itpair = eventRelays.equal_range(relay.m_EventTypeName);
|
||||
auto itpair = eventRelays.equal_range(eventTypeName);
|
||||
for (auto it = itpair.first; it != itpair.second; ++it) {
|
||||
if (it->second == &relay) {
|
||||
relay.m_Broker = nullptr;
|
||||
if (it->second->m_EventID == eventID) {
|
||||
eventRelays.erase(it);
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,377 @@
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <bitset>
|
||||
|
||||
#include "Core/OctTree.h"
|
||||
#include "Collision/Collision.h"
|
||||
|
||||
namespace
|
||||
{
|
||||
//To be able to sort nodes based on distance to ray origin.
|
||||
struct ChildInfo
|
||||
{
|
||||
int Index;
|
||||
float Distance;
|
||||
};
|
||||
|
||||
bool isFirstLower(const ChildInfo& first, const ChildInfo& second)
|
||||
{
|
||||
return first.Distance < second.Distance;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
OctTree::OctTree()
|
||||
: OctTree(AABB(), 0)
|
||||
{}
|
||||
|
||||
OctTree::OctTree(const AABB& octTreeBounds, int subDivisions)
|
||||
: m_Root(new OctChild(octTreeBounds, subDivisions, m_StaticObjects, m_DynamicObjects))
|
||||
, m_UpdatedOnce(false)
|
||||
{}
|
||||
|
||||
OctTree::~OctTree()
|
||||
{
|
||||
delete m_Root;
|
||||
}
|
||||
|
||||
void OctTree::AddDynamicObject(const AABB& box)
|
||||
{
|
||||
m_Root->AddDynamicObject(box);
|
||||
m_DynamicObjects.push_back(box);
|
||||
}
|
||||
|
||||
void OctTree::AddStaticObject(const AABB& box)
|
||||
{
|
||||
m_Root->AddStaticObject(box);
|
||||
m_StaticObjects.push_back(box);
|
||||
}
|
||||
|
||||
void OctTree::BoxesInSameRegion(const AABB& box, std::vector<AABB>& outBoxes)
|
||||
{
|
||||
falsifyObjectChecks();
|
||||
m_Root->BoxesInSameRegion(box, outBoxes);
|
||||
}
|
||||
|
||||
void OctTree::ClearObjects()
|
||||
{
|
||||
m_StaticObjects.clear();
|
||||
m_DynamicObjects.clear();
|
||||
m_Root->ClearObjects();
|
||||
}
|
||||
|
||||
void OctTree::ClearDynamicObjects()
|
||||
{
|
||||
m_DynamicObjects.clear();
|
||||
m_Root->ClearDynamicObjects();
|
||||
}
|
||||
|
||||
bool OctTree::RayCollides(const Ray& ray, Output& data)
|
||||
{
|
||||
falsifyObjectChecks();
|
||||
data.CollideDistance = -1;
|
||||
return m_Root->RayCollides(ray, data);
|
||||
}
|
||||
|
||||
bool OctTree::BoxCollides(const AABB& boxToTest, AABB& outBoxIntersected)
|
||||
{
|
||||
falsifyObjectChecks();
|
||||
return m_Root->BoxCollides(boxToTest, outBoxIntersected);
|
||||
}
|
||||
|
||||
void OctTree::falsifyObjectChecks()
|
||||
{
|
||||
for (auto& obj : m_StaticObjects) {
|
||||
obj.Checked = false;
|
||||
}
|
||||
for (auto& obj : m_DynamicObjects) {
|
||||
obj.Checked = false;
|
||||
}
|
||||
}
|
||||
|
||||
OctTree::OctChild::OctChild(const AABB& octTreeBounds,
|
||||
int subDivisions,
|
||||
std::vector<ContainedObject>& staticObjects,
|
||||
std::vector<ContainedObject>& dynamicObjects)
|
||||
: m_Box(octTreeBounds)
|
||||
, m_StaticObjectsRef(staticObjects)
|
||||
, m_DynamicObjectsRef(dynamicObjects)
|
||||
{
|
||||
if (subDivisions == 0) {
|
||||
for (OctChild*& c : m_Children) {
|
||||
c = nullptr;
|
||||
}
|
||||
} else {
|
||||
--subDivisions;
|
||||
for (int i = 0; i < 8; ++i) {
|
||||
glm::vec3 minPos, maxPos;
|
||||
const glm::vec3& parentMin = m_Box.MinCorner();
|
||||
const glm::vec3& parentMax = m_Box.MaxCorner();
|
||||
const glm::vec3& parentCenter = m_Box.Center();
|
||||
std::bitset<3> bits(i);
|
||||
//If child is 4,5,6,7.
|
||||
if (bits.test(2)) {
|
||||
minPos.x = parentCenter.x;
|
||||
maxPos.x = parentMax.x;
|
||||
} else {
|
||||
minPos.x = parentMin.x;
|
||||
maxPos.x = parentCenter.x;
|
||||
}
|
||||
|
||||
//If child is 2,3,6,7
|
||||
if (bits.test(1)) {
|
||||
minPos.y = parentCenter.y;
|
||||
maxPos.y = parentMax.y;
|
||||
} else {
|
||||
minPos.y = parentMin.y;
|
||||
maxPos.y = parentCenter.y;
|
||||
}
|
||||
//If child is 1,3,5,7
|
||||
if (bits.test(0)) {
|
||||
minPos.z = parentCenter.z;
|
||||
maxPos.z = parentMax.z;
|
||||
} else {
|
||||
minPos.z = parentMin.z;
|
||||
maxPos.z = parentCenter.z;
|
||||
}
|
||||
m_Children[i] = new OctChild(AABB(minPos, maxPos), subDivisions, m_StaticObjectsRef, m_DynamicObjectsRef);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
OctTree::OctChild::~OctChild()
|
||||
{
|
||||
for (OctChild*& c : m_Children) {
|
||||
if (c != nullptr) {
|
||||
delete c;
|
||||
c = nullptr;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool OctTree::OctChild::BoxCollides(const AABB& boxToTest, AABB& outBoxIntersected) const
|
||||
{
|
||||
if (hasChildren()) {
|
||||
for (int i : childIndicesContainingBox(boxToTest)) {
|
||||
if (m_Children[i]->BoxCollides(boxToTest, outBoxIntersected))
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
for (int i : m_StaticObjIndices) {
|
||||
if (!m_StaticObjectsRef[i].Checked) {
|
||||
const AABB& objBox = m_StaticObjectsRef[i].Box;
|
||||
if (Collision::AABBVsAABB(boxToTest, objBox)) {
|
||||
outBoxIntersected = objBox;
|
||||
return true;
|
||||
}
|
||||
m_StaticObjectsRef[i].Checked = true;
|
||||
}
|
||||
}
|
||||
for (int i : m_DynamicObjIndices) {
|
||||
if (!m_DynamicObjectsRef[i].Checked) {
|
||||
const AABB& objBox = m_DynamicObjectsRef[i].Box;
|
||||
if (!Collision::IsSameBoxProbably(boxToTest, objBox) &&
|
||||
Collision::AABBVsAABB(boxToTest, objBox)) {
|
||||
outBoxIntersected = objBox;
|
||||
return true;
|
||||
}
|
||||
m_DynamicObjectsRef[i].Checked = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool OctTree::OctChild::RayCollides(const Ray& ray, Output& data) const
|
||||
{
|
||||
//If the node AABB is missed, everything it contains is missed.
|
||||
if (Collision::RayAABBIntr(ray, m_Box)) {
|
||||
//If the ray shoots the tree, and it is a parent to 8 children :o
|
||||
if (hasChildren()) {
|
||||
//Sort children according to their distance from the ray origin.
|
||||
std::vector<ChildInfo> childInfos;
|
||||
childInfos.reserve(8);
|
||||
for (int i = 0; i < 8; ++i) {
|
||||
childInfos.push_back({ i, glm::distance(ray.Origin(), m_Children[i]->m_Box.Center()) });
|
||||
}
|
||||
std::sort(childInfos.begin(), childInfos.end(), isFirstLower);
|
||||
//Loop through the children, starting with the one closest to the ray origin. I.e the first to be hit.
|
||||
for (const ChildInfo& info : childInfos) {
|
||||
if (m_Children[info.Index]->RayCollides(ray, data)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
//Check against boxes in the node.
|
||||
float minDist = INFINITY;
|
||||
bool intersected = false;
|
||||
for (int i : m_StaticObjIndices) {
|
||||
float dist;
|
||||
//If we haven't tested against this object before, and the ray hits.
|
||||
if (!m_StaticObjectsRef[i].Checked &&
|
||||
Collision::RayVsAABB(ray, m_StaticObjectsRef[i].Box, dist)) {
|
||||
minDist = std::min(dist, minDist);
|
||||
intersected = true;
|
||||
}
|
||||
m_StaticObjectsRef[i].Checked = true;
|
||||
}
|
||||
for (int i : m_DynamicObjIndices) {
|
||||
float dist;
|
||||
//If we haven't tested against this object before, and the ray hits.
|
||||
if (!m_DynamicObjectsRef[i].Checked &&
|
||||
Collision::RayVsAABB(ray, m_DynamicObjectsRef[i].Box, dist)) {
|
||||
minDist = std::min(dist, minDist);
|
||||
intersected = true;
|
||||
}
|
||||
m_DynamicObjectsRef[i].Checked = true;
|
||||
}
|
||||
|
||||
data.CollideDistance = minDist;
|
||||
return intersected;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
void OctTree::OctChild::AddDynamicObject(const AABB& box)
|
||||
{
|
||||
if (hasChildren()) {
|
||||
for (auto i : childIndicesContainingBox(box)) {
|
||||
m_Children[i]->AddDynamicObject(box);
|
||||
}
|
||||
} else {
|
||||
//Since it hasn't been added yet to the real object list, the index is after the last =size.
|
||||
m_DynamicObjIndices.push_back((int)m_DynamicObjectsRef.size());
|
||||
}
|
||||
}
|
||||
|
||||
void OctTree::OctChild::AddStaticObject(const AABB& box)
|
||||
{
|
||||
if (hasChildren()) {
|
||||
for (auto i : childIndicesContainingBox(box)) {
|
||||
m_Children[i]->AddStaticObject(box);
|
||||
}
|
||||
} else {
|
||||
//Since it hasn't been added yet to the real object list, the index is after the last =size.
|
||||
m_StaticObjIndices.push_back((int)m_StaticObjectsRef.size());
|
||||
}
|
||||
}
|
||||
|
||||
void OctTree::OctChild::BoxesInSameRegion(const AABB& box, std::vector<AABB>& outBoxes) const
|
||||
{
|
||||
if (hasChildren()) {
|
||||
for (auto i : childIndicesContainingBox(box)) {
|
||||
m_Children[i]->BoxesInSameRegion(box, outBoxes);
|
||||
}
|
||||
} else {
|
||||
size_t startIndex = outBoxes.size();
|
||||
int numDuplicates = 0;
|
||||
outBoxes.resize(outBoxes.size() + m_StaticObjIndices.size() + m_DynamicObjIndices.size());
|
||||
for (size_t i = 0; i < m_StaticObjIndices.size(); ++i){
|
||||
ContainedObject& obj = m_StaticObjectsRef[m_StaticObjIndices[i]];
|
||||
if (obj.Checked) {
|
||||
++numDuplicates;
|
||||
} else {
|
||||
obj.Checked = true;
|
||||
outBoxes[startIndex + i - numDuplicates] = obj.Box;
|
||||
}
|
||||
}
|
||||
for (size_t i = 0; i < m_DynamicObjIndices.size(); ++i) {
|
||||
ContainedObject& obj = m_DynamicObjectsRef[m_DynamicObjIndices[i]];
|
||||
if (obj.Checked) {
|
||||
++numDuplicates;
|
||||
} else {
|
||||
obj.Checked = true;
|
||||
outBoxes[startIndex + i - numDuplicates] = obj.Box;
|
||||
}
|
||||
}
|
||||
for (size_t i = 0; i < numDuplicates; ++i) {
|
||||
outBoxes.pop_back();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void OctTree::OctChild::ClearObjects()
|
||||
{
|
||||
if (hasChildren()) {
|
||||
for (OctChild*& c : m_Children) {
|
||||
c->ClearObjects();
|
||||
}
|
||||
} else {
|
||||
m_DynamicObjIndices.clear();
|
||||
m_StaticObjIndices.clear();
|
||||
}
|
||||
}
|
||||
|
||||
void OctTree::OctChild::ClearDynamicObjects()
|
||||
{
|
||||
if (hasChildren()) {
|
||||
for (OctChild*& c : m_Children) {
|
||||
c->ClearObjects();
|
||||
}
|
||||
} else {
|
||||
m_DynamicObjIndices.clear();
|
||||
}
|
||||
}
|
||||
|
||||
//: 3 7
|
||||
//:
|
||||
//: 2 6
|
||||
//: |
|
||||
//: 1 5 \ y
|
||||
//: z
|
||||
//: 0 4 0 x-->
|
||||
//
|
||||
// child: 0 1 2 3 4 5 6 7
|
||||
// x : - - - - + + + +
|
||||
// y : - - + + - - + +
|
||||
// z : - + - + - + - +
|
||||
int OctTree::OctChild::childIndexContainingPoint(const glm::vec3& point) const
|
||||
{
|
||||
const glm::vec3& c = m_Box.Center();
|
||||
return (1 << 2) * (point.x >= c.x) | (1 << 1) * (point.y >= c.y) | (point.z >= c.z);
|
||||
}
|
||||
|
||||
std::vector<int> OctTree::OctChild::childIndicesContainingBox(const AABB& box) const
|
||||
{
|
||||
int minInd = childIndexContainingPoint(box.MinCorner());
|
||||
int maxInd = childIndexContainingPoint(box.MaxCorner());
|
||||
//Because of the predictable ordering of the child indices,
|
||||
//the number of bits set when xor:ing the indices will determine the number of children containing the box.
|
||||
std::bitset<3> bits(minInd ^ maxInd);
|
||||
switch (bits.count()) {
|
||||
//Box contained completely in one child.
|
||||
case 0:
|
||||
return{ minInd };
|
||||
//Two children.
|
||||
case 1:
|
||||
return{ minInd, maxInd };
|
||||
//Four children.
|
||||
case 2:
|
||||
{
|
||||
std::vector<int> ret;
|
||||
//Bit-hax to calculate the correct 4 children containing the box.
|
||||
//This works because of the childrens index determine what part of
|
||||
//the dimensions they are responsible for (which octant).
|
||||
bits.flip();
|
||||
//At this point the bits necessarily have exactly one bit set.
|
||||
for (int c = 0; c < 8; ++c) {
|
||||
//If the child index have the same bit set as the bits, add box to it.
|
||||
if (bits.to_ulong() & c) {
|
||||
ret.push_back(c);
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
case 3: //Eight children.
|
||||
return{ 0,1,2,3,4,5,6,7 };
|
||||
default:
|
||||
return std::vector<int>();
|
||||
}
|
||||
}
|
||||
|
||||
inline bool OctTree::OctChild::hasChildren() const
|
||||
{
|
||||
return m_Children[0] != nullptr;
|
||||
}
|
||||
@@ -10,12 +10,15 @@ World::~World()
|
||||
EntityID World::CreateEntity(EntityID parent /*= 0*/)
|
||||
{
|
||||
EntityID newEntity = generateEntityID();
|
||||
if (newEntity == parent) {
|
||||
LOG_WARNING("Invalid parent #%i of Entity#%i", newEntity, parent);
|
||||
parent = EntityID_Invalid;
|
||||
}
|
||||
m_EntityParents[newEntity] = parent;
|
||||
m_EntityChildren.insert(std::make_pair(parent, newEntity));
|
||||
return newEntity;
|
||||
}
|
||||
|
||||
|
||||
void World::DeleteEntity(EntityID entity)
|
||||
{
|
||||
// Delete components
|
||||
@@ -46,15 +49,26 @@ void World::DeleteEntity(EntityID entity)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Erase potential name
|
||||
m_EntityNames.erase(entity);
|
||||
}
|
||||
|
||||
bool World::ValidEntity(EntityID entity) const
|
||||
{
|
||||
return m_EntityParents.find(entity) != m_EntityParents.end();
|
||||
}
|
||||
|
||||
void World::RegisterComponent(ComponentInfo& ci)
|
||||
{
|
||||
m_ComponentPools[ci.Name] = new ComponentPool(ci);
|
||||
if (m_ComponentPools.find(ci.Name) == m_ComponentPools.end()) {
|
||||
m_ComponentPools[ci.Name] = new ComponentPool(ci);
|
||||
}
|
||||
}
|
||||
|
||||
ComponentWrapper World::AttachComponent(EntityID entity, std::string componentType)
|
||||
{
|
||||
// TODO: Allocate dynamic pool if component isn't registered
|
||||
ComponentPool* pool = m_ComponentPools.at(componentType);
|
||||
const ComponentInfo& ci = pool->ComponentInfo();
|
||||
|
||||
@@ -66,8 +80,7 @@ ComponentWrapper World::AttachComponent(EntityID entity, std::string componentTy
|
||||
return c;
|
||||
}
|
||||
|
||||
|
||||
bool World::HasComponent(EntityID entity, std::string componentType)
|
||||
bool World::HasComponent(EntityID entity, std::string componentType) const
|
||||
{
|
||||
ComponentPool* pool = m_ComponentPools.at(componentType);
|
||||
return pool->KnowsEntity(entity);
|
||||
@@ -79,7 +92,6 @@ ComponentWrapper World::GetComponent(EntityID entity, std::string componentType)
|
||||
return pool->GetByEntity(entity);
|
||||
}
|
||||
|
||||
|
||||
void World::DeleteComponent(EntityID entity, std::string componentType)
|
||||
{
|
||||
ComponentPool* pool = m_ComponentPools.at(componentType);
|
||||
@@ -93,13 +105,11 @@ const ComponentPool* World::GetComponents(std::string componentType)
|
||||
return (it != m_ComponentPools.end()) ? it->second : nullptr;
|
||||
}
|
||||
|
||||
|
||||
EntityID World::GetParent(EntityID entity)
|
||||
{
|
||||
return m_EntityParents.at(entity);
|
||||
}
|
||||
|
||||
|
||||
void World::SetParent(EntityID entity, EntityID parent)
|
||||
{
|
||||
EntityID lastParent = m_EntityParents.at(entity);
|
||||
@@ -115,6 +125,25 @@ void World::SetParent(EntityID entity, EntityID parent)
|
||||
m_EntityChildren.insert(std::make_pair(parent, entity));
|
||||
}
|
||||
|
||||
void World::SetName(EntityID entity, const std::string& name)
|
||||
{
|
||||
m_EntityNames[entity] = name;
|
||||
}
|
||||
|
||||
std::string World::GetName(EntityID entity) const
|
||||
{
|
||||
if (entity == EntityID_Invalid) {
|
||||
return "EntityID_Invalid";
|
||||
}
|
||||
|
||||
auto it = m_EntityNames.find(entity);
|
||||
if (it != m_EntityNames.end()) {
|
||||
return it->second;
|
||||
} else {
|
||||
return std::string();
|
||||
}
|
||||
}
|
||||
|
||||
EntityID World::generateEntityID()
|
||||
{
|
||||
// TODO: Make EntityID generation smarter
|
||||
|
||||
+224
-105
@@ -9,6 +9,7 @@ EditorSystem::EditorSystem(EventBroker* eventBroker, IRenderer* renderer)
|
||||
auto config = ResourceManager::Load<ConfigFile>("Config.ini");
|
||||
m_Enabled = config->Get<bool>("Debug.EditorEnabled", false);
|
||||
m_Visible = m_Enabled;
|
||||
m_DefaultEntityDir = boost::filesystem::path("Schema") / boost::filesystem::path("Entities");
|
||||
|
||||
if (!m_Enabled) {
|
||||
return;
|
||||
@@ -44,6 +45,35 @@ void EditorSystem::Update(World* world, double dt)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
boost::filesystem::path EditorSystem::openDialog(boost::filesystem::path defaultPath)
|
||||
{
|
||||
namespace bfs = boost::filesystem;
|
||||
auto absolutePath = bfs::absolute(defaultPath);
|
||||
nfdchar_t* outPath = nullptr;
|
||||
nfdresult_t result = NFD_OpenDialog(NULL, absolutePath.string().c_str(), &outPath);
|
||||
if (result == NFD_ERROR) {
|
||||
LOG_ERROR("NFD Error: %s", NFD_GetError());
|
||||
return bfs::path();
|
||||
}
|
||||
|
||||
return bfs::absolute(outPath);
|
||||
}
|
||||
|
||||
boost::filesystem::path EditorSystem::saveDialog(boost::filesystem::path defaultPath)
|
||||
{
|
||||
namespace bfs = boost::filesystem;
|
||||
auto absolutePath = bfs::absolute(defaultPath);
|
||||
nfdchar_t* outPath = nullptr;
|
||||
nfdresult_t result = NFD_SaveDialog(NULL, absolutePath.string().c_str(), &outPath);
|
||||
if (result == NFD_ERROR) {
|
||||
LOG_ERROR("NFD Error: %s", NFD_GetError());
|
||||
return bfs::path();
|
||||
}
|
||||
|
||||
return bfs::absolute(outPath);
|
||||
}
|
||||
|
||||
bool EditorSystem::OnInputCommand(const Events::InputCommand& e)
|
||||
{
|
||||
if (e.Command == "ToggleEditor" && e.Value > 0) {
|
||||
@@ -81,17 +111,24 @@ bool EditorSystem::OnMousePress(const Events::MousePress& e)
|
||||
|
||||
bool EditorSystem::OnMouseMove(const Events::MouseMove& e)
|
||||
{
|
||||
if (m_Widget == 0) {
|
||||
if (m_Widget == EntityID_Invalid) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (m_Selection == EntityID_Invalid) {
|
||||
return false;
|
||||
}
|
||||
if (m_Selection == m_Widget) {
|
||||
return false;
|
||||
}
|
||||
// TODO: No widgets for root entity until widgets reside in thier own world,
|
||||
// or the widgets will move relative to the root entity being moved, which is WEEEIRD.
|
||||
if (m_Selection == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
auto widgetTransform = m_World->GetComponent(m_Widget, "Transform");
|
||||
glm::vec3 widgetOrientation = widgetTransform["Orientation"];
|
||||
|
||||
glm::quat totalOrientation = m_Renderer->Camera()->Orientation() * glm::inverse(glm::quat(widgetOrientation));
|
||||
glm::quat totalOrientation = m_Renderer->Camera()->Orientation() * glm::inverse(glm::quat(widgetOrientation));
|
||||
|
||||
int width;
|
||||
int height;
|
||||
@@ -122,9 +159,9 @@ bool EditorSystem::OnMouseMove(const Events::MouseMove& e)
|
||||
if (m_WidgetSpace == WidgetSpace::Global) {
|
||||
EntityID parent = m_World->GetParent(m_Selection);
|
||||
glm::quat inverseParentOrientation;
|
||||
if (parent != 0) {
|
||||
//if (parent != 0) {
|
||||
inverseParentOrientation = glm::inverse(RenderQueueFactory::AbsoluteOrientation(m_World, parent));
|
||||
}
|
||||
//}
|
||||
(glm::vec3&)m_World->GetComponent(m_Selection, "Transform")["Position"] += inverseParentOrientation * movement;
|
||||
} else if (m_WidgetSpace == WidgetSpace::Local) {
|
||||
auto selectionTransform = m_World->GetComponent(m_Selection, "Transform");
|
||||
@@ -138,12 +175,12 @@ bool EditorSystem::OnMouseMove(const Events::MouseMove& e)
|
||||
if (m_WidgetSpace == WidgetSpace::Global) {
|
||||
EntityID parent = m_World->GetParent(m_Selection);
|
||||
glm::quat parentOrientation;
|
||||
if (parent != 0) {
|
||||
parentOrientation = RenderQueueFactory::AbsoluteOrientation(m_World, parent);
|
||||
}
|
||||
//if (parent != 0) {
|
||||
// parentOrientation = RenderQueueFactory::AbsoluteOrientation(m_World, parent);
|
||||
//}
|
||||
glm::vec3& selectionOrientation = m_World->GetComponent(m_Selection, "Transform")["Orientation"];
|
||||
//glm::quat currentOrientation = RenderQueueFactory::AbsoluteOrientation(m_World, m_Selection);
|
||||
glm::quat currentOrientation = parentOrientation * glm::quat(selectionOrientation);
|
||||
glm::quat currentOrientation = RenderQueueFactory::AbsoluteOrientation(m_World, m_Selection);
|
||||
//glm::quat currentOrientation = parentOrientation * glm::quat(selectionOrientation);
|
||||
glm::quat deltaOrientation(finalMovement);
|
||||
selectionOrientation = glm::eulerAngles(glm::inverse(parentOrientation) * (deltaOrientation * currentOrientation));
|
||||
} else if (m_WidgetSpace == WidgetSpace::Local) {
|
||||
@@ -204,9 +241,10 @@ bool EditorSystem::OnPicking(const Events::Picking& e)
|
||||
auto result = e.Pick(pos);
|
||||
EntityID entity = result.Entity;
|
||||
if (glm::length2(m_WidgetCurrentAxis) > 0.f) {
|
||||
// ???
|
||||
} else {
|
||||
LOG_INFO("Selected %i", entity);
|
||||
if (entity != 0) {
|
||||
if (entity != EntityID_Invalid) {
|
||||
EntityID parent = m_World->GetParent(entity);
|
||||
if (parent == m_Widget) {
|
||||
m_WidgetCurrentAxis = glm::vec3(
|
||||
@@ -221,11 +259,12 @@ bool EditorSystem::OnPicking(const Events::Picking& e)
|
||||
//widgetTransform["Position"] = (glm::vec3)selectionTransform["Position"];
|
||||
} else {
|
||||
ImGui::SetActiveID(0, nullptr);
|
||||
m_Selection = entity;
|
||||
if (m_WidgetMode == WidgetMode::None) {
|
||||
m_WidgetMode = WidgetMode::Translate;
|
||||
}
|
||||
setWidgetMode(m_WidgetMode);
|
||||
m_Selection = entity;
|
||||
}
|
||||
} else {
|
||||
m_Selection = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -240,9 +279,9 @@ bool EditorSystem::OnFileDropped(const Events::FileDropped& e)
|
||||
return true;
|
||||
}
|
||||
|
||||
void EditorSystem::updateWidget()
|
||||
void EditorSystem::createWidget()
|
||||
{
|
||||
if (m_Widget == 0) {
|
||||
if (m_Widget == EntityID_Invalid) {
|
||||
m_Widget = m_World->CreateEntity();
|
||||
m_World->AttachComponent(m_Widget, "Transform");
|
||||
m_WidgetX = m_World->CreateEntity(m_Widget);
|
||||
@@ -269,10 +308,20 @@ void EditorSystem::updateWidget()
|
||||
m_WidgetOrigin = m_World->CreateEntity(m_Widget);
|
||||
m_World->AttachComponent(m_WidgetOrigin, "Transform");
|
||||
m_World->AttachComponent(m_WidgetOrigin, "Model");
|
||||
setWidgetMode(WidgetMode::Translate);
|
||||
setWidgetMode(WidgetMode::None);
|
||||
}
|
||||
}
|
||||
|
||||
void EditorSystem::updateWidget()
|
||||
{
|
||||
if (m_Widget == EntityID_Invalid) {
|
||||
return;
|
||||
}
|
||||
if (m_Selection == m_Widget) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (m_Selection != 0) {
|
||||
if (m_Selection != EntityID_Invalid) {
|
||||
auto widgetTransform = m_World->GetComponent(m_Widget, "Transform");
|
||||
glm::vec3 selectionPosition = RenderQueueFactory::AbsolutePosition(m_World, m_Selection);
|
||||
widgetTransform["Position"] = selectionPosition;
|
||||
@@ -284,7 +333,7 @@ void EditorSystem::updateWidget()
|
||||
|
||||
void EditorSystem::setWidgetMode(WidgetMode newMode)
|
||||
{
|
||||
if (m_Widget == 0) {
|
||||
if (m_Widget == EntityID_Invalid) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -309,7 +358,7 @@ void EditorSystem::setWidgetMode(WidgetMode newMode)
|
||||
m_World->GetComponent(m_WidgetPlaneY, "Model")["Visible"] = true;
|
||||
m_World->GetComponent(m_WidgetPlaneZ, "Model")["Visible"] = true;
|
||||
}
|
||||
if (m_Selection != 0) {
|
||||
if (m_Selection != EntityID_Invalid) {
|
||||
if (m_WidgetSpace == WidgetSpace::Local) {
|
||||
auto selectionTransform = m_World->GetComponent(m_Selection, "Transform");
|
||||
widgetTransform["Orientation"] = glm::eulerAngles(RenderQueueFactory::AbsoluteOrientation(m_World, m_Selection));
|
||||
@@ -321,7 +370,7 @@ void EditorSystem::setWidgetMode(WidgetMode newMode)
|
||||
m_World->GetComponent(m_WidgetZ, "Model")["Resource"] = "Models/ScaleWidgetZ.obj";
|
||||
m_World->GetComponent(m_WidgetOrigin, "Model")["Visible"] = true;
|
||||
m_World->GetComponent(m_WidgetOrigin, "Model")["Resource"] = "Models/ScaleWidgetOrigin.obj";
|
||||
if (m_Selection != 0) {
|
||||
if (m_Selection != EntityID_Invalid) {
|
||||
auto selectionTransform = m_World->GetComponent(m_Selection, "Transform");
|
||||
widgetTransform["Orientation"] = glm::eulerAngles(RenderQueueFactory::AbsoluteOrientation(m_World, m_Selection));
|
||||
}
|
||||
@@ -329,7 +378,7 @@ void EditorSystem::setWidgetMode(WidgetMode newMode)
|
||||
m_World->GetComponent(m_WidgetX, "Model")["Resource"] = "Models/RotationWidgetX.obj";
|
||||
m_World->GetComponent(m_WidgetY, "Model")["Resource"] = "Models/RotationWidgetY.obj";
|
||||
m_World->GetComponent(m_WidgetZ, "Model")["Resource"] = "Models/RotationWidgetZ.obj";
|
||||
if (m_Selection != 0) {
|
||||
if (m_Selection != EntityID_Invalid) {
|
||||
auto selectionTransform = m_World->GetComponent(m_Selection, "Transform");
|
||||
if (m_WidgetSpace == WidgetSpace::Local) {
|
||||
widgetTransform["Orientation"] = glm::eulerAngles(RenderQueueFactory::AbsoluteOrientation(m_World, m_Selection));
|
||||
@@ -339,7 +388,6 @@ void EditorSystem::setWidgetMode(WidgetMode newMode)
|
||||
m_WidgetMode = newMode;
|
||||
}
|
||||
|
||||
|
||||
void EditorSystem::setWidgetSpace(WidgetSpace space)
|
||||
{
|
||||
m_WidgetSpace = space;
|
||||
@@ -348,16 +396,23 @@ void EditorSystem::setWidgetSpace(WidgetSpace space)
|
||||
|
||||
void EditorSystem::drawUI(World* world, double dt)
|
||||
{
|
||||
namespace bfs = boost::filesystem;
|
||||
|
||||
ImGui::ShowTestWindow();
|
||||
//ImGui::ShowStyleEditor();
|
||||
|
||||
if (ImGui::BeginMainMenuBar()) {
|
||||
if (ImGui::BeginMenu("File")) {
|
||||
|
||||
if (ImGui::MenuItem("New")) { }
|
||||
if (ImGui::MenuItem("Open", "Ctrl+O")) { }
|
||||
if (ImGui::MenuItem("Save", "Ctrl+S")) { }
|
||||
if (ImGui::MenuItem("Save As...", "Ctrl+Shift+S")) { }
|
||||
//if (ImGui::MenuItem("New")) { }
|
||||
if (ImGui::MenuItem("Import", "Ctrl+O")) {
|
||||
fileImport(world);
|
||||
}
|
||||
if (ImGui::MenuItem("Save", "Ctrl+S")) {
|
||||
fileSave(world);
|
||||
}
|
||||
if (ImGui::MenuItem("Save As...", "Ctrl+Shift+S")) {
|
||||
fileSaveAs(world);
|
||||
}
|
||||
ImGui::Separator();
|
||||
if (ImGui::MenuItem("Close Editor", "F1")) { }
|
||||
|
||||
@@ -392,7 +447,7 @@ void EditorSystem::drawUI(World* world, double dt)
|
||||
|
||||
std::string title = std::string("Components #") + std::to_string(m_Selection) + std::string("###Components");
|
||||
if (ImGui::Begin(title.c_str())) {
|
||||
if (m_Selection != 0) {
|
||||
if (m_Selection != EntityID_Invalid) {
|
||||
auto& pools = world->GetComponentPools();
|
||||
|
||||
std::vector<const char*> componentTypes;
|
||||
@@ -432,16 +487,16 @@ void EditorSystem::drawUI(World* world, double dt)
|
||||
}
|
||||
|
||||
auto& component = world->GetComponent(m_Selection, componentType);
|
||||
for (auto& pair : ci.FieldTypes) {
|
||||
const std::string& field = pair.first;
|
||||
const std::string& type = pair.second;
|
||||
for (auto& kv : ci.Fields) {
|
||||
const std::string& fieldName = kv.first;
|
||||
auto& field = kv.second;
|
||||
|
||||
ImGui::PushID(field.c_str());
|
||||
if (type == "Vector") {
|
||||
auto& val = component.Property<glm::vec3>(field);
|
||||
if (field == "Scale") {
|
||||
ImGui::PushID(fieldName.c_str());
|
||||
if (field.Type == "Vector") {
|
||||
auto& val = component.Property<glm::vec3>(fieldName);
|
||||
if (fieldName == "Scale") {
|
||||
ImGui::DragFloat3("", glm::value_ptr(val), 0.1f, 0.f, std::numeric_limits<float>::max());
|
||||
} else if (field == "Orientation") {
|
||||
} else if (fieldName == "Orientation") {
|
||||
glm::vec3 tempVal = glm::fmod(val, glm::vec3(glm::two_pi<float>()));
|
||||
if (ImGui::SliderFloat3("", glm::value_ptr(tempVal), 0.f, glm::two_pi<float>())) {
|
||||
val = tempVal;
|
||||
@@ -449,16 +504,16 @@ void EditorSystem::drawUI(World* world, double dt)
|
||||
} else {
|
||||
ImGui::DragFloat3("", glm::value_ptr(val), 0.1f, std::numeric_limits<float>::lowest(), std::numeric_limits<float>::max());
|
||||
}
|
||||
} else if (type == "Color") {
|
||||
auto& val = component.Property<glm::vec4>(field);
|
||||
} else if (field.Type == "Color") {
|
||||
auto& val = component.Property<glm::vec4>(fieldName);
|
||||
ImGui::ColorEdit4("", glm::value_ptr(val), true);
|
||||
} else if (type == "string") {
|
||||
std::string& val = component.Property<std::string>(field);
|
||||
} else if (field.Type == "string") {
|
||||
std::string& val = component.Property<std::string>(fieldName);
|
||||
char tempString[1024];
|
||||
memcpy(tempString, val.c_str(), std::min(val.length() + 1, sizeof(tempString)));
|
||||
if (ImGui::InputText("", tempString, sizeof(tempString))) {
|
||||
val = std::string(tempString);
|
||||
LOG_DEBUG("%s::%s changed!", componentType.c_str(), field.c_str());
|
||||
LOG_DEBUG("%s::%s changed!", componentType.c_str(), fieldName.c_str());
|
||||
}
|
||||
// DROP STUFF
|
||||
if (ImGui::IsItemHovered() && !m_LastDroppedFile.empty()) {
|
||||
@@ -466,21 +521,21 @@ void EditorSystem::drawUI(World* world, double dt)
|
||||
m_LastDroppedFile = "";
|
||||
}
|
||||
|
||||
} else if (type == "double") {
|
||||
float tempVal = static_cast<float>(component.Property<double>(field));
|
||||
} else if (field.Type == "double") {
|
||||
float tempVal = static_cast<float>(component.Property<double>(fieldName));
|
||||
if (ImGui::InputFloat("", &tempVal, 0.01f, 1.f)) {
|
||||
component.SetProperty(field, static_cast<double>(tempVal));
|
||||
component.SetProperty(fieldName, static_cast<double>(tempVal));
|
||||
}
|
||||
} else if (type == "bool") {
|
||||
auto& val = component.Property<bool>(field);
|
||||
} else if (field.Type == "bool") {
|
||||
auto& val = component.Property<bool>(fieldName);
|
||||
ImGui::Checkbox("", &val);
|
||||
} else {
|
||||
ImGui::TextDisabled(type.c_str());
|
||||
ImGui::TextDisabled(field.Type.c_str());
|
||||
}
|
||||
ImGui::PopID();
|
||||
|
||||
ImGui::SameLine();
|
||||
ImGui::Text(field.c_str());
|
||||
ImGui::Text(fieldName.c_str());
|
||||
if (ImGui::IsItemHovered()) {
|
||||
ImGui::SetTooltip("field annotation goes here");
|
||||
}
|
||||
@@ -492,74 +547,93 @@ void EditorSystem::drawUI(World* world, double dt)
|
||||
}
|
||||
ImGui::End();
|
||||
|
||||
if (ImGui::Begin("Entitites")) {
|
||||
static EntityID draggingEntity = 0;
|
||||
if (ImGui::Begin("Entities")) {
|
||||
auto entityChildren = world->GetEntityChildren();
|
||||
std::function<void(EntityID)> recurse = [&](EntityID parent) {
|
||||
auto range = entityChildren.equal_range(parent);
|
||||
for (auto it = range.first; it != range.second; it++) {
|
||||
|
||||
ImVec2 pos = ImGui::GetCursorScreenPos();
|
||||
float width = ImGui::GetContentRegionAvailWidth();
|
||||
ImRect bb(pos + ImVec2(20, 0), pos + ImVec2(width, 13));
|
||||
auto window = ImGui::GetCurrentWindow();
|
||||
if (m_Selection == it->second) {
|
||||
const ImU32 col = window->Color(ImGuiCol_HeaderActive);
|
||||
window->DrawList->AddRectFilled(bb.Min, bb.Max, col);
|
||||
}
|
||||
ImGuiID id = window->GetID((std::string("#SelectButton") + std::to_string(it->second)).c_str());
|
||||
bool hovered = false;
|
||||
bool held = false;
|
||||
if (ImGui::ButtonBehavior(bb, id, &hovered, &held)) {
|
||||
m_Selection = it->second;
|
||||
}
|
||||
if (held) {
|
||||
ImVec2 entityDragDelta = ImGui::GetMouseDragDelta(0);
|
||||
if (std::abs(entityDragDelta.x) > 0 && std::abs(entityDragDelta.y) > 0) {
|
||||
if (draggingEntity == 0) {
|
||||
draggingEntity = it->second;
|
||||
LOG_DEBUG("Started drag of entity %i", draggingEntity);
|
||||
}
|
||||
ImGui::SetNextWindowPos(ImGui::GetIO().MousePos + ImVec2(20, 0));
|
||||
ImGui::Begin("Change parent", nullptr, ImVec2(0, 0), 0.3f, ImGuiWindowFlags_NoTitleBar|ImGuiWindowFlags_NoResize|ImGuiWindowFlags_NoMove|ImGuiWindowFlags_NoSavedSettings);
|
||||
ImGui::Text("#%i", draggingEntity);
|
||||
ImGui::End();
|
||||
}
|
||||
}
|
||||
|
||||
ImGui::SetNextTreeNodeOpened(true, ImGuiSetCond_Once);
|
||||
if (ImGui::TreeNode((std::string("#") + std::to_string(it->second)).c_str())) {
|
||||
if (draggingEntity != 0 && ImGui::IsItemHoveredRect() && ImGui::IsMouseReleased(0)) {
|
||||
LOG_DEBUG("Changed parent of %i to %i", draggingEntity, it->second);
|
||||
changeParent(draggingEntity, it->second);
|
||||
draggingEntity = 0;
|
||||
}
|
||||
|
||||
if (ImGui::BeginPopupContextItem("item context menu")) {
|
||||
if (ImGui::Button("Add")) {
|
||||
EntityID entity = world->CreateEntity(it->second);
|
||||
world->AttachComponent(entity, "Transform");
|
||||
}
|
||||
ImGui::SameLine();
|
||||
if (ImGui::Button("Delete")) {
|
||||
world->DeleteEntity(it->second);
|
||||
ImGui::CloseCurrentPopup();
|
||||
if (m_Selection == it->second) {
|
||||
m_Selection = 0;
|
||||
}
|
||||
}
|
||||
ImGui::EndPopup();
|
||||
}
|
||||
if (createEntityNode(world, it->second)) {
|
||||
recurse(it->second);
|
||||
ImGui::TreePop();
|
||||
}
|
||||
}
|
||||
};
|
||||
recurse(0);
|
||||
recurse(EntityID_Invalid);
|
||||
}
|
||||
ImGui::End();
|
||||
}
|
||||
|
||||
bool EditorSystem::createEntityNode(World* world, EntityID entity)
|
||||
{
|
||||
// HACK: Don't show the widget entities in the entity tree
|
||||
if (entity == m_Widget) {
|
||||
return false;
|
||||
}
|
||||
|
||||
ImVec2 pos = ImGui::GetCursorScreenPos();
|
||||
float width = ImGui::GetContentRegionAvailWidth();
|
||||
ImRect bb(pos + ImVec2(20, 0), pos + ImVec2(width, 13));
|
||||
auto window = ImGui::GetCurrentWindow();
|
||||
if (m_Selection == entity) {
|
||||
const ImU32 col = window->Color(ImGuiCol_HeaderActive);
|
||||
window->DrawList->AddRectFilled(bb.Min, bb.Max, col);
|
||||
}
|
||||
ImGuiID id = window->GetID((std::string("#SelectButton") + std::to_string(entity)).c_str());
|
||||
bool hovered = false;
|
||||
bool held = false;
|
||||
if (ImGui::ButtonBehavior(bb, id, &hovered, &held)) {
|
||||
m_Selection = entity;
|
||||
}
|
||||
if (held) {
|
||||
ImVec2 entityDragDelta = ImGui::GetMouseDragDelta(0);
|
||||
if (std::abs(entityDragDelta.x) > 0 && std::abs(entityDragDelta.y) > 0) {
|
||||
if (m_UIDraggingEntity == EntityID_Invalid) {
|
||||
m_UIDraggingEntity = entity;
|
||||
LOG_DEBUG("Started drag of entity %i", m_UIDraggingEntity);
|
||||
}
|
||||
ImGui::SetNextWindowPos(ImGui::GetIO().MousePos + ImVec2(20, 0));
|
||||
ImGui::Begin("Change parent", nullptr, ImVec2(0, 0), 0.3f, ImGuiWindowFlags_NoTitleBar|ImGuiWindowFlags_NoResize|ImGuiWindowFlags_NoMove|ImGuiWindowFlags_NoSavedSettings);
|
||||
ImGui::Text("#%i", m_UIDraggingEntity);
|
||||
ImGui::End();
|
||||
}
|
||||
}
|
||||
|
||||
ImGui::SetNextTreeNodeOpened(true, ImGuiSetCond_Once);
|
||||
std::string nodeTitle;
|
||||
const std::string& entityName = world->GetName(entity);
|
||||
if (!entityName.empty()) {
|
||||
nodeTitle = entityName;
|
||||
} else {
|
||||
nodeTitle = std::string("#") + std::to_string(entity);
|
||||
}
|
||||
if (ImGui::TreeNode(nodeTitle.c_str())) {
|
||||
if (m_UIDraggingEntity != EntityID_Invalid && ImGui::IsItemHoveredRect() && ImGui::IsMouseReleased(0)) {
|
||||
LOG_DEBUG("Changed parent of %i to %i", m_UIDraggingEntity, entity);
|
||||
changeParent(m_UIDraggingEntity, entity);
|
||||
m_UIDraggingEntity = EntityID_Invalid;
|
||||
}
|
||||
|
||||
if (ImGui::BeginPopupContextItem("item context menu")) {
|
||||
if (ImGui::Button("Add")) {
|
||||
EntityID newEntity = world->CreateEntity(entity);
|
||||
world->AttachComponent(newEntity, "Transform");
|
||||
}
|
||||
ImGui::SameLine();
|
||||
if (ImGui::Button("Delete")) {
|
||||
world->DeleteEntity(entity);
|
||||
ImGui::CloseCurrentPopup();
|
||||
if (!world->ValidEntity(m_Selection)) {
|
||||
m_Selection = EntityID_Invalid;
|
||||
}
|
||||
}
|
||||
ImGui::EndPopup();
|
||||
}
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool EditorSystem::createDeleteButton(std::string componentType)
|
||||
{
|
||||
float width = ImGui::GetContentRegionAvailWidth();
|
||||
@@ -594,3 +668,48 @@ void EditorSystem::changeParent(EntityID entity, EntityID newParent)
|
||||
|
||||
m_World->SetParent(entity, newParent);
|
||||
}
|
||||
|
||||
void EditorSystem::fileImport(World* world)
|
||||
{
|
||||
m_CurrentFile = openDialog(m_DefaultEntityDir);
|
||||
auto file = ResourceManager::Load<EntityFile>(m_CurrentFile.string());
|
||||
EntityFilePreprocessor fpp(file);
|
||||
fpp.RegisterComponents(world);
|
||||
EntityFileParser fp(file);
|
||||
fp.MergeEntities(world);
|
||||
createWidget();
|
||||
updateWidget();
|
||||
}
|
||||
|
||||
void EditorSystem::fileSave(World* world)
|
||||
{
|
||||
if (boost::filesystem::exists(m_CurrentFile)) {
|
||||
// HACK: Delete the widgets so they don't appear in the saved file
|
||||
world->DeleteEntity(m_Widget);
|
||||
m_Widget = EntityID_Invalid;
|
||||
|
||||
EntityFileWriter writer(m_CurrentFile.string());
|
||||
writer.WriteWorld(world);
|
||||
|
||||
createWidget();
|
||||
} else {
|
||||
fileSaveAs(world);
|
||||
}
|
||||
}
|
||||
|
||||
void EditorSystem::fileSaveAs(World* world)
|
||||
{
|
||||
auto filePath = saveDialog(m_DefaultEntityDir);
|
||||
if (filePath.empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// HACK: Delete the widgets so they don't appear in the saved file
|
||||
world->DeleteEntity(m_Widget);
|
||||
m_Widget = EntityID_Invalid;
|
||||
|
||||
EntityFileWriter writer(filePath.string());
|
||||
writer.WriteWorld(world);
|
||||
|
||||
createWidget();
|
||||
}
|
||||
|
||||
@@ -62,7 +62,7 @@ void InputProxy::Process()
|
||||
e.Command = command;
|
||||
e.Value = currentValue;
|
||||
m_EventBroker->Publish(e);
|
||||
LOG_DEBUG("Input: Published command %s=%f for player %i", e.Command.c_str(), e.Value, e.PlayerID);
|
||||
//LOG_DEBUG("Input: Published command %s=%f for player %i", e.Command.c_str(), e.Value, e.PlayerID);
|
||||
m_LastCommandValues[command] = currentValue;
|
||||
}
|
||||
}
|
||||
@@ -78,7 +78,7 @@ void InputProxy::Process()
|
||||
}
|
||||
//e.Value = std::max(-1.f, std::min(e.Value, 1.f));
|
||||
m_EventBroker->Publish(e);
|
||||
LOG_DEBUG("Input: Published command %s=%f for player %i", e.Command.c_str(), e.Value, e.PlayerID);
|
||||
//LOG_DEBUG("Input: Published command %s=%f for player %i", e.Command.c_str(), e.Value, e.PlayerID);
|
||||
}
|
||||
m_CommandQueue.clear();
|
||||
}
|
||||
|
||||
@@ -1,11 +1,334 @@
|
||||
#include "Network\Client.h"
|
||||
#include "Network/Client.h"
|
||||
|
||||
Client::Client()
|
||||
using namespace boost::asio::ip;
|
||||
|
||||
|
||||
Client::Client(ConfigFile* config) : m_Socket(m_IOService)
|
||||
{
|
||||
|
||||
// Default is local host
|
||||
std::string address = config->Get<std::string>("Networking.Address", "127.0.0.1");
|
||||
int port = config->Get<int>("Networking.Port", 13);
|
||||
m_ReceiverEndpoint = udp::endpoint(boost::asio::ip::address::from_string(address), port);
|
||||
// Set up network stream
|
||||
m_PlayerName = config->Get<std::string>("Networking.Name", "Raptorcopter");
|
||||
m_NextSnapshot.InputForward = "";
|
||||
m_NextSnapshot.InputRight = "";
|
||||
}
|
||||
|
||||
Client::~Client()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void Client::Start(World* world, EventBroker* eventBroker)
|
||||
{
|
||||
m_WasStarted = true;
|
||||
m_EventBroker = eventBroker;
|
||||
m_World = world;
|
||||
|
||||
// Subscribe to events
|
||||
m_EInputCommand = decltype(m_EInputCommand)(std::bind(&Client::OnInputCommand, this, std::placeholders::_1));
|
||||
m_EventBroker->Subscribe(m_EInputCommand);
|
||||
|
||||
|
||||
//while (m_PlayerName.size() > 7) {
|
||||
// LOG_INFO("Please enter your name (No longer than 7 characters):");
|
||||
// std::cin >> m_PlayerName;
|
||||
//}
|
||||
m_Socket.connect(m_ReceiverEndpoint);
|
||||
LOG_INFO("I am client. BIP BOP");
|
||||
}
|
||||
|
||||
void Client::Update()
|
||||
{
|
||||
readFromServer();
|
||||
}
|
||||
|
||||
void Client::Close()
|
||||
{
|
||||
if (m_WasStarted) {
|
||||
disconnect();
|
||||
m_ThreadIsRunning = false;
|
||||
m_EventBroker->Unsubscribe(m_EInputCommand);
|
||||
}
|
||||
}
|
||||
|
||||
void Client::readFromServer()
|
||||
{
|
||||
if (m_Socket.available()) {
|
||||
bytesRead = receive(readBuf, INPUTSIZE);
|
||||
if (bytesRead > 0) {
|
||||
Packet packet(readBuf, bytesRead);
|
||||
parseMessageType(packet);
|
||||
}
|
||||
}
|
||||
std::clock_t currentTime = std::clock();
|
||||
if (snapshotInterval < (1000 * (currentTime - previousSnapshotMessage) / (double)CLOCKS_PER_SEC)) {
|
||||
if (isConnected()) {
|
||||
sendSnapshotToServer();
|
||||
}
|
||||
previousSnapshotMessage = currentTime;
|
||||
}
|
||||
}
|
||||
|
||||
void Client::sendSnapshotToServer()
|
||||
{
|
||||
// Reset previouse key state in snapshot.
|
||||
m_NextSnapshot.InputForward = "";
|
||||
m_NextSnapshot.InputRight = "";
|
||||
|
||||
auto player = m_World->GetComponent(m_PlayerDefinitions[m_PlayerID].EntityID, "Player");
|
||||
|
||||
|
||||
// See if any movement keys are down
|
||||
// We dont care if it's overwritten by later
|
||||
// if statement. Watcha gonna do, right!
|
||||
if (player["Forward"]) {
|
||||
m_NextSnapshot.InputForward = "+Forward";
|
||||
}
|
||||
if (player["Left"]) {
|
||||
m_NextSnapshot.InputRight = "-Right";
|
||||
}
|
||||
if (player["Back"]) {
|
||||
m_NextSnapshot.InputForward = "-Forward";
|
||||
}
|
||||
if (player["Right"]) {
|
||||
m_NextSnapshot.InputRight = "+Right";
|
||||
}
|
||||
|
||||
if (m_NextSnapshot.InputForward != "") {
|
||||
Packet packet(MessageType::Event, m_SendPacketID);
|
||||
packet.WriteString(m_NextSnapshot.InputForward);
|
||||
send(packet);
|
||||
} else {
|
||||
Packet packet(MessageType::Event, m_SendPacketID);
|
||||
packet.WriteString("0Forward");
|
||||
send(packet);
|
||||
}
|
||||
|
||||
if (m_NextSnapshot.InputRight != "") {
|
||||
Packet packet(MessageType::Event, m_SendPacketID);
|
||||
packet.WriteString(m_NextSnapshot.InputRight);
|
||||
send(packet);
|
||||
} else {
|
||||
Packet packet(MessageType::Event, m_SendPacketID);
|
||||
packet.WriteString("0Right");
|
||||
send(packet);
|
||||
}
|
||||
}
|
||||
|
||||
void Client::parseMessageType(Packet& packet)
|
||||
{
|
||||
int messageType = packet.ReadPrimitive<int>();
|
||||
if (messageType == -1)
|
||||
return;
|
||||
// Read packet ID
|
||||
m_PreviousPacketID = m_PacketID; // Set previous packet id
|
||||
m_PacketID = packet.ReadPrimitive<int>(); //Read new packet id
|
||||
//IdentifyPacketLoss();
|
||||
|
||||
switch (static_cast<MessageType>(messageType)) {
|
||||
case MessageType::Connect:
|
||||
parseConnect(packet);
|
||||
break;
|
||||
case MessageType::ClientPing:
|
||||
parsePing();
|
||||
break;
|
||||
case MessageType::ServerPing:
|
||||
parseServerPing();
|
||||
break;
|
||||
case MessageType::Message:
|
||||
break;
|
||||
case MessageType::Snapshot:
|
||||
parseSnapshot(packet);
|
||||
break;
|
||||
case MessageType::Disconnect:
|
||||
break;
|
||||
case MessageType::Event:
|
||||
parseEventMessage(packet);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void Client::parseConnect(Packet& packet)
|
||||
{
|
||||
m_PlayerID = packet.ReadPrimitive<int>();
|
||||
LOG_INFO("%i: I am player: %i", m_PacketID, m_PlayerID);
|
||||
}
|
||||
|
||||
void Client::parsePing()
|
||||
{
|
||||
m_DurationOfPingTime = 1000 * (std::clock() - m_StartPingTime) / static_cast<double>(CLOCKS_PER_SEC);
|
||||
LOG_INFO("%i: response time with ctime(ms): %f", m_PacketID, m_DurationOfPingTime);
|
||||
}
|
||||
|
||||
void Client::parseServerPing()
|
||||
{
|
||||
Packet packet(MessageType::ServerPing, m_SendPacketID);
|
||||
packet.WriteString("Ping recieved");
|
||||
send(packet);
|
||||
}
|
||||
|
||||
void Client::parseEventMessage(Packet& packet)
|
||||
{
|
||||
int Id = -1;
|
||||
std::string command = packet.ReadString();
|
||||
if (command.find("+Player") != std::string::npos) {
|
||||
Id = packet.ReadPrimitive<int>();
|
||||
// Sett Player name
|
||||
m_PlayerDefinitions[Id].Name = command.erase(0, 7);
|
||||
} else {
|
||||
LOG_INFO("%i: Event message: %s", m_PacketID, command.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
void Client::parseSnapshot(Packet& packet)
|
||||
{
|
||||
std::string tempName;
|
||||
for (size_t i = 0; i < MAXCONNECTIONS; i++) {
|
||||
// We're checking for empty name for now. This might not be the best way,
|
||||
// but it is to avoid sending redundant data.
|
||||
tempName = packet.ReadString();
|
||||
|
||||
|
||||
// Apply the position data read to the player entity
|
||||
// New player connected on the server side
|
||||
if (m_PlayerDefinitions[i].Name == "" && tempName != "") {
|
||||
m_PlayerDefinitions[i].Name = tempName;
|
||||
m_PlayerDefinitions[i].EntityID = createPlayer();
|
||||
} else if (m_PlayerDefinitions[i].Name != "" && tempName == "") {
|
||||
// Someone disconnected
|
||||
// TODO: Insert code here
|
||||
break;
|
||||
} else if (m_PlayerDefinitions[i].Name == "" && tempName == "") {
|
||||
// Not a connected player
|
||||
break;
|
||||
}
|
||||
if (m_PlayerDefinitions[i].EntityID != -1) {
|
||||
|
||||
// Move player to server position
|
||||
int dataSize = m_World->GetComponent(m_PlayerDefinitions[i].EntityID, "Transform").Info.Meta.Stride;
|
||||
memcpy(m_World->GetComponent(m_PlayerDefinitions[i].EntityID, "Transform").Data, packet.ReadData(dataSize), dataSize);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int Client::receive(char* data, size_t length)
|
||||
{
|
||||
boost::system::error_code error;
|
||||
|
||||
int bytesReceived = m_Socket.receive_from(boost
|
||||
::asio::buffer((void*)data, length),
|
||||
m_ReceiverEndpoint,
|
||||
0, error);
|
||||
|
||||
if (error) {
|
||||
//LOG_ERROR("receive: %s", error.message().c_str());
|
||||
}
|
||||
|
||||
return bytesReceived;
|
||||
}
|
||||
|
||||
void Client::send(Packet& packet)
|
||||
{
|
||||
m_Socket.send_to(boost::asio::buffer(
|
||||
packet.Data(),
|
||||
packet.Size()),
|
||||
m_ReceiverEndpoint, 0);
|
||||
}
|
||||
|
||||
void Client::connect()
|
||||
{
|
||||
Packet packet(MessageType::Connect, m_SendPacketID);
|
||||
packet.WriteString(m_PlayerName);
|
||||
m_StartPingTime = std::clock();
|
||||
send(packet);
|
||||
}
|
||||
|
||||
void Client::disconnect()
|
||||
{
|
||||
Packet packet(MessageType::Connect, m_SendPacketID);
|
||||
packet.WriteString("+Disconnect");
|
||||
send(packet);
|
||||
}
|
||||
|
||||
void Client::ping()
|
||||
{
|
||||
Packet packet(MessageType::Connect, m_SendPacketID);
|
||||
packet.WriteString("Ping");
|
||||
m_StartPingTime = std::clock();
|
||||
send(packet);
|
||||
}
|
||||
|
||||
void Client::moveMessageHead(char*& data, size_t& length, size_t stepSize)
|
||||
{
|
||||
data += stepSize;
|
||||
length -= stepSize;
|
||||
}
|
||||
|
||||
bool Client::OnInputCommand(const Events::InputCommand & e)
|
||||
{
|
||||
if (isConnected()) {
|
||||
ComponentWrapper& player = m_World->GetComponent(m_PlayerDefinitions[m_PlayerID].EntityID, "Player");
|
||||
if (e.Command == "Forward") {
|
||||
if (e.Value > 0) {
|
||||
(bool&)player["Forward"] = true;
|
||||
(bool&)player["Back"] = false;
|
||||
} else if (e.Value < 0) {
|
||||
(bool&)player["Back"] = true;
|
||||
(bool&)player["Forward"] = false;
|
||||
} else {
|
||||
(bool&)player["Forward"] = false;
|
||||
(bool&)player["Back"] = false;
|
||||
}
|
||||
}
|
||||
if (e.Command == "Right") {
|
||||
if (e.Value > 0) {
|
||||
(bool&)player["Right"] = true;
|
||||
(bool&)player["Left"] = false;
|
||||
} else if (e.Value < 0) {
|
||||
(bool&)player["Left"] = true;
|
||||
(bool&)player["Right"] = false;
|
||||
} else {
|
||||
(bool&)player["Left"] = false;
|
||||
(bool&)player["Right"] = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (e.Command == "ConnectToServer") { // Connect for now
|
||||
connect();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
void Client::identifyPacketLoss()
|
||||
{
|
||||
// if no packets lost, difference should be equal to 1
|
||||
int difference = m_PacketID - m_PreviousPacketID;
|
||||
if (difference != 1) {
|
||||
LOG_INFO("%i Packet(s) were lost...", difference);
|
||||
}
|
||||
}
|
||||
|
||||
bool Client::isConnected()
|
||||
{
|
||||
if (m_PlayerID != -1) {
|
||||
if (m_PlayerDefinitions[m_PlayerID].EntityID != -1) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
EntityID Client::createPlayer()
|
||||
{
|
||||
EntityID entityID = m_World->CreateEntity();
|
||||
ComponentWrapper transform = m_World->AttachComponent(entityID, "Transform");
|
||||
ComponentWrapper model = m_World->AttachComponent(entityID, "Model");
|
||||
model["Resource"] = "Models/Core/UnitSphere.obj";
|
||||
ComponentWrapper player = m_World->AttachComponent(entityID, "Player");
|
||||
return entityID;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
#include "Network/Packet.h"
|
||||
|
||||
Packet::Packet(MessageType type, unsigned int& packetID)
|
||||
{
|
||||
m_Data = new char[m_MaxPacketSize];
|
||||
// Create message header
|
||||
// Add message type
|
||||
int messageType = static_cast<int>(type);
|
||||
Packet::WritePrimitive<int>(messageType);
|
||||
packetID = packetID % 1000; // Packet id modulos
|
||||
Packet::WritePrimitive<int>(packetID);
|
||||
packetID++;
|
||||
}
|
||||
|
||||
// Create message
|
||||
Packet::Packet(char* data, const int sizeOfPacket)
|
||||
{
|
||||
// Resize message
|
||||
m_MaxPacketSize = sizeOfPacket;
|
||||
// Copy data newly allocated memory
|
||||
m_Data = new char[sizeOfPacket];
|
||||
memcpy(m_Data, data, sizeOfPacket);
|
||||
m_Offset = sizeOfPacket;
|
||||
}
|
||||
|
||||
Packet::~Packet()
|
||||
{
|
||||
delete[] m_Data;
|
||||
}
|
||||
|
||||
void Packet::WriteString(std::string str)
|
||||
{
|
||||
// Message, add one extra byte for null terminator
|
||||
int sizeOfString = str.size() + 1;
|
||||
if (m_Offset + sizeOfString > m_MaxPacketSize) {
|
||||
LOG_WARNING("Package::WriteString(): Data size in packet exceeded maximum package size.\n");
|
||||
}
|
||||
memcpy(m_Data + m_Offset, str.data(), sizeOfString * sizeof(char));
|
||||
m_Offset += sizeOfString * sizeof(char);
|
||||
}
|
||||
|
||||
void Packet::WriteData(char * data, int sizeOfData)
|
||||
{
|
||||
if (m_Offset + sizeOfData > m_MaxPacketSize) {
|
||||
LOG_WARNING("Packet::WriteData(): Data size in packet exceeded maximum packet size.\n");
|
||||
}
|
||||
memcpy(m_Data + m_Offset, data, sizeOfData);
|
||||
m_Offset += sizeOfData;
|
||||
}
|
||||
|
||||
std::string Packet::ReadString()
|
||||
{
|
||||
std::string returnValue(m_Data + m_ReturnDataOffset);
|
||||
if (m_Offset < m_ReturnDataOffset + returnValue.size()) {
|
||||
LOG_WARNING("packet ReadString(): Oh no! You are trying to remove things outside my memory kingdom");
|
||||
return "PopFrontString Failed";
|
||||
}
|
||||
// +1 for null terminator.
|
||||
m_ReturnDataOffset += returnValue.size() + 1;
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
char * Packet::ReadData(int SizeOfData)
|
||||
{
|
||||
if (m_Offset < m_ReturnDataOffset + SizeOfData) {
|
||||
LOG_WARNING("packet ReadData(): Oh no! You are trying to remove things outside my memory kingdom");
|
||||
return nullptr;
|
||||
}
|
||||
unsigned int oldReturnDataOffset = m_ReturnDataOffset;
|
||||
m_ReturnDataOffset += SizeOfData;
|
||||
return (m_Data + oldReturnDataOffset);
|
||||
}
|
||||
@@ -1,11 +1,355 @@
|
||||
#include "Network\Server.h"
|
||||
#include "Network/Server.h"
|
||||
|
||||
Server::Server()
|
||||
{
|
||||
|
||||
}
|
||||
Server::Server() : m_Socket(m_IOService, boost::asio::ip::udp::endpoint(boost::asio::ip::udp::v4(), 13))
|
||||
{ }
|
||||
|
||||
Server::~Server()
|
||||
{
|
||||
{ }
|
||||
|
||||
|
||||
void Server::Start(World* world, EventBroker* eventBroker)
|
||||
{
|
||||
m_World = world;
|
||||
m_EventBroker = eventBroker;
|
||||
for (size_t i = 0; i < MAXCONNECTIONS; i++) {
|
||||
m_StopTimes[i] = std::clock();
|
||||
}
|
||||
LOG_INFO("I am Server. BIP BOP\n");
|
||||
}
|
||||
|
||||
void Server::Update()
|
||||
{
|
||||
readFromClients();
|
||||
}
|
||||
|
||||
void Server::Close()
|
||||
{
|
||||
m_ThreadIsRunning = false;
|
||||
}
|
||||
|
||||
void Server::readFromClients()
|
||||
{
|
||||
// m_ThreadIsRunning might be unnecessary but the
|
||||
// program crashed if it executed m_Socket.available()
|
||||
// when closing the program.
|
||||
|
||||
if (m_Socket.available()) {
|
||||
try {
|
||||
bytesRead = receive(readBuffer, INPUTSIZE);
|
||||
Packet packet(readBuffer, bytesRead);
|
||||
parseMessageType(packet);
|
||||
} catch (const std::exception& err) {
|
||||
//LOG_ERROR("%i: Read from client crashed %s", m_PacketID, err.what());
|
||||
}
|
||||
|
||||
}
|
||||
std::clock_t currentTime = std::clock();
|
||||
// Send snapshot
|
||||
if (snapshotInterval < (1000 * (currentTime - previousSnapshotMessage) / (double)CLOCKS_PER_SEC)) {
|
||||
sendSnapshot();
|
||||
previousSnapshotMessage = currentTime;
|
||||
}
|
||||
|
||||
// Send pings each
|
||||
if (intervalMs < (1000 * (currentTime - previousePingMessage) / (double)CLOCKS_PER_SEC)) {
|
||||
sendPing();
|
||||
previousePingMessage = currentTime;
|
||||
}
|
||||
|
||||
// Time out logic
|
||||
if (checkTimeOutInterval < (1000 * (currentTime - timOutTimer) / (double)CLOCKS_PER_SEC)) {
|
||||
checkForTimeOuts();
|
||||
timOutTimer = currentTime;
|
||||
}
|
||||
}
|
||||
|
||||
void Server::parseMessageType(Packet& packet)
|
||||
{
|
||||
int messageType = packet.ReadPrimitive<int>(); // Read what type off message was sent from server
|
||||
|
||||
// Read packet ID
|
||||
m_PreviousPacketID = m_PacketID; // Set previous packet id
|
||||
m_PacketID = packet.ReadPrimitive<int>(); //Read new packet id
|
||||
//IdentifyPacketLoss();
|
||||
switch (static_cast<MessageType>(messageType)) {
|
||||
case MessageType::Connect:
|
||||
parseConnect(packet);
|
||||
break;
|
||||
case MessageType::ClientPing:
|
||||
//parseClientPing();
|
||||
break;
|
||||
case MessageType::ServerPing:
|
||||
parseServerPing();
|
||||
break;
|
||||
case MessageType::Message:
|
||||
break;
|
||||
case MessageType::Snapshot:
|
||||
parseSnapshot(packet);
|
||||
break;
|
||||
case MessageType::Disconnect:
|
||||
parseDisconnect();
|
||||
break;
|
||||
case MessageType::Event:
|
||||
parseEvent(packet);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int Server::receive(char * data, size_t length)
|
||||
{
|
||||
length = m_Socket.receive_from(
|
||||
boost::asio::buffer((void*)data
|
||||
, length)
|
||||
, m_ReceiverEndpoint, 0);
|
||||
return length;
|
||||
}
|
||||
|
||||
void Server::send(Packet& packet, int playerID)
|
||||
{
|
||||
m_Socket.send_to(
|
||||
boost::asio::buffer(packet.Data(), packet.Size()),
|
||||
m_PlayerDefinitions[playerID].Endpoint,
|
||||
0);
|
||||
}
|
||||
|
||||
void Server::send(Packet & packet)
|
||||
{
|
||||
m_Socket.send_to(
|
||||
boost::asio::buffer(
|
||||
packet.Data(),
|
||||
packet.Size()),
|
||||
m_ReceiverEndpoint,
|
||||
0);
|
||||
}
|
||||
|
||||
void Server::moveMessageHead(char *& data, size_t & length, size_t stepSize)
|
||||
{
|
||||
data += stepSize;
|
||||
length -= stepSize;
|
||||
}
|
||||
|
||||
void Server::broadcast(std::string message)
|
||||
{
|
||||
Packet packet(MessageType::Event, m_SendPacketID);
|
||||
packet.WriteString(message);
|
||||
for (int i = 0; i < MAXCONNECTIONS; i++) {
|
||||
if (m_PlayerDefinitions[i].Endpoint.address() != boost::asio::ip::address()) {
|
||||
send(packet, i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Server::broadcast(Packet& packet)
|
||||
{
|
||||
for (int i = 0; i < MAXCONNECTIONS; ++i) {
|
||||
if (m_PlayerDefinitions[i].Endpoint.address() != boost::asio::ip::address()) {
|
||||
send(packet, i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Server::sendSnapshot()
|
||||
{
|
||||
Packet packet(MessageType::Snapshot, m_SendPacketID);
|
||||
for (size_t i = 0; i < MAXCONNECTIONS; i++) {
|
||||
|
||||
// Send an empty name if there is no player connected on this position.
|
||||
packet.WriteString(m_PlayerDefinitions[i].Name);
|
||||
|
||||
if (m_PlayerDefinitions[i].EntityID == -1) {
|
||||
continue;
|
||||
}
|
||||
// Pack transfrom component into data packet
|
||||
auto transform = m_World->GetComponent(m_PlayerDefinitions[i].EntityID, "Transform");
|
||||
packet.WriteData(transform.Data, transform.Info.Meta.Stride);
|
||||
}
|
||||
broadcast(packet);
|
||||
}
|
||||
|
||||
void Server::sendPing()
|
||||
{
|
||||
// Prints connected players ping
|
||||
for (size_t i = 0; i < MAXCONNECTIONS; i++) {
|
||||
if (m_PlayerDefinitions[i].Endpoint.address() != boost::asio::ip::address()) {
|
||||
int ping = 1000 * (m_StopTimes[i] - m_StartPingTime) / static_cast<double>(CLOCKS_PER_SEC);
|
||||
LOG_INFO("%i: Player %i's ping: %i", m_PacketID, i, ping);
|
||||
}
|
||||
}
|
||||
|
||||
// Create ping message
|
||||
Packet packet(MessageType::ServerPing, m_SendPacketID);
|
||||
packet.WriteString("Ping from server");
|
||||
// Time message
|
||||
m_StartPingTime = std::clock();
|
||||
// Send message
|
||||
broadcast(packet);
|
||||
}
|
||||
|
||||
void Server::checkForTimeOuts()
|
||||
{
|
||||
int timeOutTimeMs = 5000;
|
||||
int startPing = 1000 * m_StartPingTime
|
||||
/ static_cast<double>(CLOCKS_PER_SEC);
|
||||
|
||||
for (size_t i = 0; i < MAXCONNECTIONS; i++) {
|
||||
if (m_PlayerDefinitions[i].Endpoint.address() != boost::asio::ip::address()) {
|
||||
int stopPing = 1000 * m_StopTimes[i]
|
||||
/ static_cast<double>(CLOCKS_PER_SEC);
|
||||
if (startPing > stopPing + timeOutTimeMs) {
|
||||
LOG_INFO("Player %i timed out!", i);
|
||||
disconnect(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Server::disconnect(int i)
|
||||
{
|
||||
broadcast("A player disconnected");
|
||||
LOG_INFO("Player %i disconnected/timed out", i);
|
||||
|
||||
// Remove enteties and stuff
|
||||
m_PlayerDefinitions[i].Endpoint = boost::asio::ip::udp::endpoint();
|
||||
m_PlayerDefinitions[i].EntityID = -1;
|
||||
m_PlayerDefinitions[i].Name = "";
|
||||
}
|
||||
|
||||
void Server::parseEvent(Packet& packet)
|
||||
{
|
||||
size_t i;
|
||||
for (i = 0; i < MAXCONNECTIONS; i++) {
|
||||
if (m_PlayerDefinitions[i].Endpoint.address() == m_ReceiverEndpoint.address()) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
// If no player matches the address return.
|
||||
if (i >= 8)
|
||||
return;
|
||||
|
||||
unsigned int entityId = m_PlayerDefinitions[i].EntityID;
|
||||
std::string eventString = packet.ReadString();
|
||||
if ("+Forward" == eventString) {
|
||||
m_World->GetComponent(entityId, "Player")["Forward"] = true;
|
||||
m_World->GetComponent(entityId, "Player")["Back"] = false;
|
||||
} else if ("-Forward" == eventString) {
|
||||
m_World->GetComponent(entityId, "Player")["Forward"] = false;
|
||||
m_World->GetComponent(entityId, "Player")["Back"] = true;
|
||||
} else if ("0Forward" == eventString) {
|
||||
m_World->GetComponent(entityId, "Player")["Forward"] = false;
|
||||
m_World->GetComponent(entityId, "Player")["Back"] = false;
|
||||
}
|
||||
if ("+Right" == eventString) {
|
||||
m_World->GetComponent(entityId, "Player")["Left"] = false;
|
||||
m_World->GetComponent(entityId, "Player")["Right"] = true;
|
||||
} else if ("-Right" == eventString) {
|
||||
m_World->GetComponent(entityId, "Player")["Right"] = false;
|
||||
m_World->GetComponent(entityId, "Player")["Left"] = true;
|
||||
} else if ("0Right" == eventString) {
|
||||
m_World->GetComponent(entityId, "Player")["Right"] = false;
|
||||
m_World->GetComponent(entityId, "Player")["Left"] = false;
|
||||
}
|
||||
}
|
||||
|
||||
void Server::parseConnect(Packet& packet)
|
||||
{
|
||||
LOG_INFO("Parsing connections");
|
||||
// Check if player is already connected
|
||||
for (int i = 0; i < MAXCONNECTIONS; i++) {
|
||||
if (m_PlayerDefinitions[i].Endpoint.address() == m_ReceiverEndpoint.address()) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < MAXCONNECTIONS; i++) {
|
||||
if (m_PlayerDefinitions[i].Endpoint.address() == boost::asio::ip::address()) {
|
||||
// Create new player
|
||||
m_PlayerDefinitions[i].EntityID = createPlayer();
|
||||
m_PlayerDefinitions[i].Endpoint = m_ReceiverEndpoint;
|
||||
m_PlayerDefinitions[i].Name = packet.ReadString();
|
||||
|
||||
m_StopTimes[i] = std::clock();
|
||||
|
||||
LOG_INFO("Player \"%s\" connected on IP: %s", m_PlayerDefinitions[i].Name, m_PlayerDefinitions[i].Endpoint.address().to_string());
|
||||
|
||||
Packet packet(MessageType::Connect, m_SendPacketID);
|
||||
packet.WritePrimitive<int>(i); // Player ID
|
||||
|
||||
send(packet, i);
|
||||
|
||||
// Send notification that a player has connected
|
||||
std::string str = m_PacketID + "Player " + m_PlayerDefinitions[i].Name + " connected on: "
|
||||
+ m_PlayerDefinitions[i].Endpoint.address().to_string();
|
||||
broadcast(str);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Server::parseDisconnect()
|
||||
{
|
||||
LOG_INFO("%i: Parsing disconnect", m_PacketID);
|
||||
|
||||
for (int i = 0; i < MAXCONNECTIONS; i++) {
|
||||
if (m_PlayerDefinitions[i].Endpoint.address() == m_ReceiverEndpoint.address()) {
|
||||
disconnect(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Server::parseClientPing()
|
||||
{
|
||||
LOG_INFO("%i: Parsing ping", m_PacketID);
|
||||
// Return ping
|
||||
Packet packet(MessageType::ClientPing, m_SendPacketID);
|
||||
packet.WriteString("Ping received");
|
||||
send(packet); // This dosen't work for multiple users
|
||||
}
|
||||
|
||||
void Server::parseServerPing()
|
||||
{
|
||||
for (int i = 0; i < MAXCONNECTIONS; i++) {
|
||||
if (m_PlayerDefinitions[i].Endpoint.address() == m_ReceiverEndpoint.address()) {
|
||||
m_StopTimes[i] = std::clock();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// NOT USED
|
||||
void Server::parseSnapshot(Packet& packet)
|
||||
{
|
||||
// Does no logic. Returns snapshot if client request one
|
||||
// The snapshot is not a real snapshot tho...
|
||||
for (int i = 0; i < MAXCONNECTIONS; i++) {
|
||||
if (m_PlayerDefinitions[i].Endpoint.address() != boost::asio::ip::address()) {
|
||||
m_Socket.send_to(
|
||||
boost::asio::buffer("I'm sending a snapshot to you guys!"),
|
||||
m_PlayerDefinitions[i].Endpoint,
|
||||
0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Server::identifyPacketLoss()
|
||||
{
|
||||
// if no packets lost, difference should be equal to 1
|
||||
int difference = m_PacketID - m_PreviousPacketID;
|
||||
if (difference != 1) {
|
||||
LOG_INFO("%i Packet(s) were lost...", difference);
|
||||
}
|
||||
}
|
||||
|
||||
EntityID Server::createPlayer()
|
||||
{
|
||||
EntityID entityID = m_World->CreateEntity();
|
||||
ComponentWrapper transform = m_World->AttachComponent(entityID, "Transform");
|
||||
transform["Position"] = glm::vec3(-1.5f, 0.f, 0.f);
|
||||
ComponentWrapper model = m_World->AttachComponent(entityID, "Model");
|
||||
model["Resource"] = "Models/Core/UnitSphere.obj";
|
||||
model["Color"] = glm::vec4(rand()%255 / 255.f, rand()%255 / 255.f, rand() %255 / 255.f, 1.f);
|
||||
ComponentWrapper player = m_World->AttachComponent(entityID, "Player");
|
||||
return entityID;
|
||||
}
|
||||
|
||||
@@ -27,16 +27,16 @@ glm::vec3 RenderQueueFactory::AbsolutePosition(World* world, EntityID entity)
|
||||
{
|
||||
glm::vec3 position;
|
||||
|
||||
do {
|
||||
while (entity != EntityID_Invalid) {
|
||||
ComponentWrapper transform = world->GetComponent(entity, "Transform");
|
||||
EntityID parent = world->GetParent(entity);
|
||||
if (parent != 0) {
|
||||
//if (parent != EntityID_Invalid) {
|
||||
position += AbsoluteOrientation(world, parent) * (glm::vec3)transform["Position"];
|
||||
} else {
|
||||
position += (glm::vec3)transform["Position"];
|
||||
}
|
||||
//} else {
|
||||
// position += (glm::vec3)transform["Position"];
|
||||
//}
|
||||
entity = parent;
|
||||
} while (entity != 0);
|
||||
}
|
||||
|
||||
return position;
|
||||
}
|
||||
@@ -45,11 +45,11 @@ glm::quat RenderQueueFactory::AbsoluteOrientation(World* world, EntityID entity)
|
||||
{
|
||||
glm::quat orientation;
|
||||
|
||||
do {
|
||||
while (entity != EntityID_Invalid) {
|
||||
ComponentWrapper transform = world->GetComponent(entity, "Transform");
|
||||
orientation = glm::quat((glm::vec3)transform["Orientation"]) * orientation;
|
||||
entity = world->GetParent(entity);
|
||||
} while (entity != 0);
|
||||
}
|
||||
|
||||
return orientation;
|
||||
}
|
||||
@@ -58,11 +58,11 @@ glm::vec3 RenderQueueFactory::AbsoluteScale(World* world, EntityID entity)
|
||||
{
|
||||
glm::vec3 scale(1.f);
|
||||
|
||||
do {
|
||||
while (entity != EntityID_Invalid) {
|
||||
ComponentWrapper transform = world->GetComponent(entity, "Transform");
|
||||
scale *= (glm::vec3)transform["Scale"];
|
||||
entity = world->GetParent(entity);
|
||||
} while (entity != 0);
|
||||
}
|
||||
|
||||
return scale;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
#include "Rendering/Renderer.h"
|
||||
#include "Rendering/DebugCameraInputController.h"
|
||||
|
||||
void Renderer::Initialize()
|
||||
{
|
||||
@@ -10,6 +9,7 @@ void Renderer::Initialize()
|
||||
if (m_Camera == nullptr) {
|
||||
m_Camera = m_DefaultCamera;
|
||||
}
|
||||
m_DebugCameraInputController = std::make_shared<DebugCameraInputController<Renderer>>(m_EventBroker, -1);
|
||||
TEMPCreateLights();
|
||||
InitializeRenderPasses();
|
||||
|
||||
@@ -89,8 +89,6 @@ void Renderer::InitializeShaders()
|
||||
|
||||
void Renderer::InputUpdate(double dt)
|
||||
{
|
||||
static DebugCameraInputController<Renderer> firstPersonInputController(m_EventBroker, -1);
|
||||
|
||||
glm::vec3 m_Position = m_Camera->Position();
|
||||
if (glfwGetKey(m_Window, GLFW_KEY_O) == GLFW_PRESS)
|
||||
{
|
||||
@@ -120,9 +118,9 @@ void Renderer::InputUpdate(double dt)
|
||||
m_CameraMoveSpeed = 0.5f;
|
||||
}
|
||||
|
||||
firstPersonInputController.Update(dt);
|
||||
m_Camera->SetOrientation(firstPersonInputController.Orientation());
|
||||
m_Camera->SetPosition(firstPersonInputController.Position());
|
||||
m_DebugCameraInputController->Update(dt);
|
||||
m_Camera->SetOrientation(m_DebugCameraInputController->Orientation());
|
||||
m_Camera->SetPosition(m_DebugCameraInputController->Position());
|
||||
}
|
||||
|
||||
void Renderer::Update(double dt)
|
||||
|
||||
@@ -19,6 +19,7 @@ file(GLOB SOURCE_FILES
|
||||
set(SOURCE_FILES
|
||||
${SOURCE_FILES}
|
||||
"Game.cpp"
|
||||
"HealthSystem.cpp"
|
||||
"PlayerSystem.cpp"
|
||||
)
|
||||
|
||||
|
||||
+60
-30
@@ -1,12 +1,16 @@
|
||||
#include "Game.h"
|
||||
#include "Collision/TriggerSystem.h"
|
||||
#include "Collision/CollisionSystem.h"
|
||||
#include "Game/HealthSystem.h"
|
||||
#include "Core/EntityFileWriter.h"
|
||||
|
||||
Game::Game(int argc, char* argv[])
|
||||
{
|
||||
ResourceManager::RegisterType<ConfigFile>("ConfigFile");
|
||||
ResourceManager::RegisterType<Model>("Model");
|
||||
ResourceManager::RegisterType<Texture>("Texture");
|
||||
ResourceManager::RegisterType<EntityXMLFile>("EntityXMLFile");
|
||||
ResourceManager::RegisterType<ShaderProgram>("ShaderProgram");
|
||||
ResourceManager::RegisterType<EntityFile>("EntityFile");
|
||||
|
||||
m_Config = ResourceManager::Load<ConfigFile>("Config.ini");
|
||||
LOG_LEVEL = static_cast<_LOG_LEVEL>(m_Config->Get<int>("Debug.LogLevel", 1));
|
||||
@@ -20,7 +24,7 @@ Game::Game(int argc, char* argv[])
|
||||
m_Renderer = new Renderer(m_EventBroker);
|
||||
m_Renderer->SetFullscreen(m_Config->Get<bool>("Video.Fullscreen", false));
|
||||
m_Renderer->SetVSYNC(m_Config->Get<bool>("Video.VSYNC", false));
|
||||
m_Renderer->SetResolution(Rectangle(
|
||||
m_Renderer->SetResolution(Rectangle::Rectangle(
|
||||
0,
|
||||
0,
|
||||
m_Config->Get<int>("Video.Width", 1280),
|
||||
@@ -45,23 +49,45 @@ Game::Game(int argc, char* argv[])
|
||||
m_World = new World();
|
||||
std::string mapToLoad = m_Config->Get<std::string>("Debug.LoadMap", "");
|
||||
if (!mapToLoad.empty()) {
|
||||
ResourceManager::Load<EntityXMLFile>(mapToLoad)->PopulateWorld(m_World);
|
||||
auto file = ResourceManager::Load<EntityFile>(mapToLoad);
|
||||
EntityFilePreprocessor fpp(file);
|
||||
fpp.RegisterComponents(m_World);
|
||||
EntityFileParser fp(file);
|
||||
fp.MergeEntities(m_World);
|
||||
}
|
||||
|
||||
// Create system pipeline
|
||||
m_SystemPipeline = new SystemPipeline(m_EventBroker);
|
||||
m_SystemPipeline->AddSystem<RaptorCopterSystem>();
|
||||
m_SystemPipeline->AddSystem<PlayerSystem>();
|
||||
m_SystemPipeline->AddSystem<EditorSystem>(m_Renderer);
|
||||
|
||||
//All systems with orderlevel 0 will be updated first.
|
||||
unsigned int updateOrderLevel = 0;
|
||||
m_SystemPipeline->AddSystem<RaptorCopterSystem>(updateOrderLevel);
|
||||
m_SystemPipeline->AddSystem<PlayerSystem>(updateOrderLevel);
|
||||
m_SystemPipeline->AddSystem<EditorSystem>(updateOrderLevel, m_Renderer);
|
||||
m_SystemPipeline->AddSystem<HealthSystem>(updateOrderLevel);
|
||||
|
||||
//Collision and TriggerSystem should update after player.
|
||||
++updateOrderLevel;
|
||||
m_SystemPipeline->AddSystem<CollisionSystem>(updateOrderLevel);
|
||||
m_SystemPipeline->AddSystem<TriggerSystem>(updateOrderLevel);
|
||||
|
||||
// Invoke network
|
||||
if (m_Config->Get<bool>("Networking.StartNetwork", false)) {
|
||||
//boost::thread workerThread(&Game::networkFunction, this);
|
||||
networkFunction();
|
||||
}
|
||||
m_LastTime = glfwGetTime();
|
||||
|
||||
debugInitialize();
|
||||
}
|
||||
|
||||
Game::~Game()
|
||||
{
|
||||
delete m_SystemPipeline;
|
||||
delete m_World;
|
||||
delete m_FrameStack;
|
||||
delete m_InputProxy;
|
||||
delete m_InputManager;
|
||||
delete m_Renderer;
|
||||
delete m_RenderQueueFactory;
|
||||
delete m_EventBroker;
|
||||
}
|
||||
|
||||
@@ -83,10 +109,15 @@ void Game::Tick()
|
||||
m_InputProxy->Process();
|
||||
m_EventBroker->Swap();
|
||||
|
||||
// Update network
|
||||
if (m_IsClientOrServer) {
|
||||
m_ClientOrServer->Update();
|
||||
}
|
||||
|
||||
// Iterate through systems and update world!
|
||||
m_SystemPipeline->Update(m_World, dt);
|
||||
debugTick(dt);
|
||||
m_Renderer->Update(dt);
|
||||
m_EventBroker->Process<Client>();
|
||||
|
||||
m_RenderQueueFactory->Update(m_World);
|
||||
GLERROR("Game::Tick m_RenderQueueFactory->Update");
|
||||
@@ -96,28 +127,27 @@ void Game::Tick()
|
||||
m_EventBroker->Clear();
|
||||
}
|
||||
|
||||
|
||||
bool Game::debugOnInputCommand(const Events::InputCommand& e)
|
||||
{
|
||||
if (e.Command == "DebugReload" && e.Value == 1) {
|
||||
std::string mapToLoad = m_Config->Get<std::string>("Debug.LoadMap", "");
|
||||
if (!mapToLoad.empty()) {
|
||||
delete m_World;
|
||||
m_World = new World();
|
||||
ResourceManager::Release("EntityXMLFile", mapToLoad);
|
||||
ResourceManager::Load<EntityXMLFile>(mapToLoad)->PopulateWorld(m_World);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void Game::debugInitialize()
|
||||
{
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EInputCommand, &Game::debugOnInputCommand);
|
||||
}
|
||||
|
||||
void Game::debugTick(double dt)
|
||||
{
|
||||
m_EventBroker->Process<Game>();
|
||||
}
|
||||
|
||||
void Game::networkFunction()
|
||||
{
|
||||
bool isServer = m_Config->Get<bool>("Networking.IsServer", false);
|
||||
if (!isServer) {
|
||||
m_IsClientOrServer = true;
|
||||
m_ClientOrServer = new Client(m_Config);
|
||||
}
|
||||
if (isServer) {
|
||||
m_IsClientOrServer = true;
|
||||
m_ClientOrServer = new Server();
|
||||
}
|
||||
m_ClientOrServer->Start(m_World, m_EventBroker);
|
||||
// I don't think we are reaching this part of the code right now.
|
||||
// ~Game() is not called if the game is exited by closing console windows
|
||||
// When server or client is done set it to false.
|
||||
//m_IsClientOrServer = false;
|
||||
// Destroy it
|
||||
//delete m_ClientOrServer;
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
#include "HealthSystem.h"
|
||||
#include <algorithm>
|
||||
|
||||
HealthSystem::HealthSystem(EventBroker* eventBroker)
|
||||
: PureSystem(eventBroker, "Health")
|
||||
{
|
||||
//subscribe/listenTo playerdamage,healthpickup events (using the eventBroker)
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EPlayerDamage, &HealthSystem::OnPlayerDamaged);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EPlayerHealthPickup, &HealthSystem::OnPlayerHealthPickup);
|
||||
}
|
||||
|
||||
void HealthSystem::UpdateComponent(World *world, ComponentWrapper &health, double dt)
|
||||
{
|
||||
//if entityID of health is 9 then the players ID is also 9 (player,health are connected to the same entity)
|
||||
ComponentWrapper player = world->GetComponent(health.EntityID, "Player");
|
||||
double maxHealth = (double)health["MaxHealth"];
|
||||
|
||||
//process the DeltaHealthVector and change the entitys health accordingly
|
||||
for (size_t i = m_DeltaHealthVector.size(); i > 0; i--)
|
||||
{
|
||||
auto deltaHP = m_DeltaHealthVector[i - 1];
|
||||
//if we have a healthchange for the current player and health is greater than 0, then apply it
|
||||
if (std::get<0>(deltaHP) == player.EntityID && (double)health["Health"] > 0.0f) {
|
||||
//get the deltaHP value from the tuple and make sure you dont get more than maxHealth
|
||||
double newHealth = std::min((double)health["Health"] + (double)std::get<1>(deltaHP), maxHealth);
|
||||
health["Health"] = newHealth;
|
||||
m_DeltaHealthVector.erase(m_DeltaHealthVector.begin() + i - 1);
|
||||
//check if health is <= 0
|
||||
if ((double)health["Health"] <= 0.0f) {
|
||||
//publish death event
|
||||
Events::PlayerDeath e;
|
||||
e.PlayerID = player.EntityID;
|
||||
m_EventBroker->Publish(e);
|
||||
//clear the remaining hpDeltas for the dead player
|
||||
for (size_t j = m_DeltaHealthVector.size(); j > 0; j--)
|
||||
{
|
||||
if (std::get<0>(m_DeltaHealthVector[j - 1]) == player.EntityID)
|
||||
m_DeltaHealthVector.erase(m_DeltaHealthVector.begin() + j - 1);
|
||||
}
|
||||
//break the loop if the player is dead
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool HealthSystem::OnPlayerDamaged(const Events::PlayerDamage& e)
|
||||
{
|
||||
//save the changed HP to a vector. it will be taken care of in UpdateComponent
|
||||
m_DeltaHealthVector.push_back(std::make_tuple(e.PlayerDamagedID, -e.DamageAmount));
|
||||
return true;
|
||||
}
|
||||
|
||||
bool HealthSystem::OnPlayerHealthPickup(const Events::PlayerHealthPickup& e)
|
||||
{
|
||||
//save the changed HP to a vector. it will be taken care of in UpdateComponent
|
||||
m_DeltaHealthVector.push_back(std::make_tuple(e.PlayerHealedID, e.HealthAmount));
|
||||
return true;
|
||||
}
|
||||
+31
-46
@@ -2,56 +2,41 @@
|
||||
|
||||
void PlayerSystem::UpdateComponent(World * world, ComponentWrapper & player, double dt)
|
||||
{
|
||||
if (input.Forward) {
|
||||
m_Direction.z = -1;
|
||||
} else if (input.Back) {
|
||||
m_Direction.z = 1;
|
||||
} else {
|
||||
m_Direction.z = 0;
|
||||
player["Velocity"] = glm::vec3(0.f, 0.f, 0.f);
|
||||
if ((bool&)player["Forward"] == true) {
|
||||
((glm::vec3&)player["Velocity"]).z = m_Speed * float(dt) * -1;
|
||||
|
||||
}
|
||||
if (input.Left) {
|
||||
m_Direction.x = -1;
|
||||
} else if (input.Right) {
|
||||
m_Direction.x = 1;
|
||||
} else {
|
||||
m_Direction.x = 0;
|
||||
if ((bool&)player["Left"] == true) {
|
||||
((glm::vec3&)player["Velocity"]).x = m_Speed * float(dt) * -1;
|
||||
}
|
||||
if ((bool&)player["Back"] == true) {
|
||||
((glm::vec3&)player["Velocity"]).z = m_Speed * float(dt);
|
||||
}
|
||||
if ((bool&)player["Right"] == true) {
|
||||
((glm::vec3&)player["Velocity"]).x = m_Speed * float(dt);
|
||||
}
|
||||
|
||||
if ((glm::vec3)player["Velocity"] != glm::vec3(0.f)) {
|
||||
ComponentWrapper& transform = world->GetComponent(player.EntityID, "Transform");
|
||||
(glm::vec3&)transform["Position"] += (glm::vec3)player["Velocity"];
|
||||
}
|
||||
m_EventBroker->Process<PlayerSystem>();
|
||||
ComponentWrapper& transform = world->GetComponent(player.EntityID, "Transform");
|
||||
(glm::vec3&)player["Velocity"] = m_Speed * float(dt) * m_Direction;
|
||||
(glm::vec3&)transform["Position"] += (glm::vec3)player["Velocity"];
|
||||
}
|
||||
|
||||
bool PlayerSystem::OnKeyDown(const Events::KeyDown & event)
|
||||
bool PlayerSystem::OnTouch(const Events::TriggerTouch &event)
|
||||
{
|
||||
if (event.KeyCode == GLFW_KEY_W) {
|
||||
input.Forward = true;
|
||||
}
|
||||
if (event.KeyCode == GLFW_KEY_A) {
|
||||
input.Left = true;
|
||||
}
|
||||
if (event.KeyCode == GLFW_KEY_S) {
|
||||
input.Back = true;
|
||||
}
|
||||
if (event.KeyCode == GLFW_KEY_D) {
|
||||
input.Right = true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool PlayerSystem::OnKeyUp(const Events::KeyUp & event)
|
||||
{
|
||||
if (event.KeyCode == GLFW_KEY_W) {
|
||||
input.Forward = false;
|
||||
}
|
||||
if (event.KeyCode == GLFW_KEY_A) {
|
||||
input.Left = false;
|
||||
}
|
||||
if (event.KeyCode == GLFW_KEY_S) {
|
||||
input.Back = false;
|
||||
}
|
||||
if (event.KeyCode == GLFW_KEY_D) {
|
||||
input.Right = false;
|
||||
}
|
||||
LOG_INFO("Player entity %i touched widget (entity %i).", event.Entity, event.Trigger);
|
||||
return false;
|
||||
}
|
||||
|
||||
bool PlayerSystem::OnEnter(const Events::TriggerEnter &event)
|
||||
{
|
||||
LOG_INFO("Player entity %i entered widget (entity %i).", event.Entity, event.Trigger);
|
||||
return false;
|
||||
}
|
||||
|
||||
bool PlayerSystem::OnLeave(const Events::TriggerLeave &event)
|
||||
{
|
||||
LOG_INFO("Player entity %i left widget (entity %i).", event.Entity, event.Trigger);
|
||||
return false;
|
||||
}
|
||||
@@ -12,6 +12,7 @@ include_directories(
|
||||
)
|
||||
|
||||
file(GLOB SOURCE_FILES
|
||||
"*.h"
|
||||
"*.cpp"
|
||||
)
|
||||
|
||||
|
||||
@@ -0,0 +1,220 @@
|
||||
//#define BOOST_TEST_MODULE collTest
|
||||
#include <boost/test/unit_test.hpp>
|
||||
#include <boost/test/execution_monitor.hpp>
|
||||
using boost::unit_test_framework::test_suite;
|
||||
using boost::unit_test_framework::test_case;
|
||||
#include "Engine/Collision/Collision.h"
|
||||
#include "Engine/Core/AABB.h"
|
||||
#include "Engine/Core/Ray.h"
|
||||
#include <stdlib.h>//srand
|
||||
#include "Engine/Core/OctTree.h"
|
||||
//vs model
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
//ray vs model
|
||||
#include "Engine\Core\ResourceManager.h"
|
||||
#include "Engine\Rendering\Model.h"
|
||||
#include "Engine\Core\Ray.h"
|
||||
|
||||
//vs memleaks
|
||||
//#define _CRTDBG_MAP_ALLOC
|
||||
//#include <stdlib.h>
|
||||
//#include <crtdbg.h>
|
||||
//#define DEBUG_CLIENTBLOCK new( _CLIENT_BLOCK, __FILE__, __LINE__)
|
||||
//#define new DEBUG_CLIENTBLOCK
|
||||
|
||||
void RayTest(std::string fileName) {
|
||||
//simple box test
|
||||
Ray ray(glm::vec3(-50, 0, 0), glm::vec3(1, 0, 0));
|
||||
//using a rawmodel here, else we have to init the renderingsystem
|
||||
ResourceManager::RegisterType<RawModel>("RawModel");
|
||||
auto unitBox = ResourceManager::Load<RawModel>(fileName);
|
||||
BOOST_REQUIRE(unitBox != nullptr);
|
||||
bool hit = Collision::RayVsModel(ray, unitBox->m_Vertices, unitBox->m_Indices);
|
||||
BOOST_CHECK(hit);
|
||||
ray.SetDirection(glm::vec3(-1, 0, 0));
|
||||
hit = Collision::RayVsModel(ray, unitBox->m_Vertices, unitBox->m_Indices);
|
||||
BOOST_CHECK(!hit);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE(collisionTests)
|
||||
|
||||
BOOST_AUTO_TEST_CASE(collisionTest)
|
||||
{
|
||||
//memleak
|
||||
int* globalLeak = new int[5];
|
||||
|
||||
//fixed seed
|
||||
srand(2);
|
||||
AABB someAABB;
|
||||
glm::vec3 minPos;
|
||||
glm::vec3 maxPos;
|
||||
bool z;
|
||||
int test = 0;
|
||||
for (size_t i = 0; i < 10; i++)
|
||||
{
|
||||
Ray ray(
|
||||
glm::vec3(rand() % 100, rand() % 100, rand() % 100),
|
||||
glm::vec3(rand() % 100, rand() % 100, rand() % 100)
|
||||
);
|
||||
minPos.x = rand() % 100;
|
||||
minPos.y = rand() % 100;
|
||||
minPos.z = rand() % 100;
|
||||
maxPos.x = rand() % 100;
|
||||
maxPos.y = rand() % 100;
|
||||
maxPos.z = rand() % 100;
|
||||
|
||||
someAABB = AABB(minPos, maxPos);
|
||||
z = Collision::RayVsAABB(ray, someAABB);
|
||||
if (z) ++test;
|
||||
}
|
||||
BOOST_CHECK(test >= 0);
|
||||
|
||||
//_CrtDumpMemoryLeaks();
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(collisionTest2)
|
||||
{
|
||||
//fixed seed
|
||||
srand(2);
|
||||
AABB someAABB;
|
||||
glm::vec3 minPos;
|
||||
glm::vec3 maxPos;
|
||||
bool z;
|
||||
int test = 0;
|
||||
for (size_t i = 0; i < 1000000; i++)
|
||||
{
|
||||
Ray ray(
|
||||
glm::vec3(rand() % 100, rand() % 100, rand() % 100),
|
||||
glm::vec3(rand() % 100, rand() % 100, rand() % 100)
|
||||
);
|
||||
minPos.x = rand() % 100;
|
||||
minPos.y = rand() % 100;
|
||||
minPos.z = rand() % 100;
|
||||
maxPos.x = rand() % 100;
|
||||
maxPos.y = rand() % 100;
|
||||
maxPos.z = rand() % 100;
|
||||
|
||||
someAABB = AABB(minPos, maxPos);
|
||||
z = Collision::RayAABBIntr(ray, someAABB);
|
||||
if (z) ++test;
|
||||
}
|
||||
BOOST_CHECK(test >= 0);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(rayVsModelTest)
|
||||
{
|
||||
//simple box test
|
||||
RayTest("Models/Core/UnitCube.obj");
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(rayVsModelTest2)
|
||||
{
|
||||
//advanced test, this will check so rayVSAABB and rayVsModel(with boxmodel) gives the same result (hit/miss)
|
||||
//testing with different seeds
|
||||
// srand(7676762);
|
||||
// srand(7676462);
|
||||
// srand(7462);
|
||||
srand(72);
|
||||
AABB someAABB;
|
||||
glm::vec3 minPos;
|
||||
glm::vec3 maxPos;
|
||||
bool z;
|
||||
int test = 0;
|
||||
//min/max is the same as the rawmodels boundaries ofcourse
|
||||
minPos = glm::vec3(-0.5f, -0.5f, -0.5f);
|
||||
maxPos = glm::vec3(0.5f, 0.5f, 0.5f);
|
||||
someAABB = AABB(minPos, maxPos);
|
||||
//using a rawmodel here, else we have to init the renderingsystem
|
||||
ResourceManager::RegisterType<RawModel>("RawModel");
|
||||
auto unitBox = ResourceManager::Load<RawModel>("Models/Core/UnitCube.obj");
|
||||
BOOST_CHECK(unitBox != nullptr);
|
||||
|
||||
for (size_t i = 0; i < 1000000; i++)
|
||||
{
|
||||
Ray ray(
|
||||
glm::vec3(-2, 0, 0),
|
||||
glm::vec3(rand() % 100, rand() % 100, rand() % 100)
|
||||
);
|
||||
//if we normalize the ray.direction when its 0,0,0 then we get nan,nan,nan - thus we have this check to prevent that
|
||||
if (glm::any(glm::isnan(ray.Direction())))
|
||||
continue;
|
||||
|
||||
z = Collision::RayVsAABB(ray, someAABB);
|
||||
if (z) {
|
||||
//hit
|
||||
bool hit = Collision::RayVsModel(ray, unitBox->m_Vertices, unitBox->m_Indices);
|
||||
if (!hit) {
|
||||
//if rayvsaabb hit but rayvvmodel didnt hit, we get to here
|
||||
glm::vec3 outtttttttt;
|
||||
hit = Collision::RayVsModel(ray, unitBox->m_Vertices, unitBox->m_Indices, outtttttttt);
|
||||
hit = Collision::RayVsModel(ray, unitBox->m_Vertices, unitBox->m_Indices);
|
||||
}
|
||||
else {
|
||||
hit = hit;
|
||||
}
|
||||
BOOST_CHECK(hit);
|
||||
}
|
||||
////breakpoint test
|
||||
//if (!z) {
|
||||
// z = z;
|
||||
//}
|
||||
//
|
||||
bool hit = Collision::RayVsModel(ray, unitBox->m_Vertices, unitBox->m_Indices);
|
||||
////breakpoint test
|
||||
//if (!hit) {
|
||||
// hit = hit;
|
||||
//}
|
||||
if (hit) {
|
||||
//hit
|
||||
z = Collision::RayVsAABB(ray, someAABB);
|
||||
if (!z) {
|
||||
//if rayvsmodel hit but rayvsaabb didnt hit then we get to here
|
||||
z = Collision::RayVsAABB(ray, someAABB);
|
||||
glm::vec3 outtttttttt;
|
||||
hit = Collision::RayVsModel(ray, unitBox->m_Vertices, unitBox->m_Indices, outtttttttt);
|
||||
hit = Collision::RayVsModel(ray, unitBox->m_Vertices, unitBox->m_Indices);
|
||||
}
|
||||
else {
|
||||
z = z;
|
||||
}
|
||||
BOOST_CHECK(hit);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
BOOST_AUTO_TEST_CASE(rayVsModelTest3)
|
||||
{
|
||||
//simple test
|
||||
RayTest("Models/Core/UnitSphere.obj");
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(rayVsModelTest4)
|
||||
{
|
||||
//simple test
|
||||
RayTest("Models/Core/UnitCylinder.obj");
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(rayVsModelTest5)
|
||||
{
|
||||
//simple test
|
||||
RayTest("Models/Core/UnitRaptor.obj");
|
||||
}
|
||||
BOOST_AUTO_TEST_CASE(octTest)
|
||||
{
|
||||
glm::vec3 mini = glm::vec3(-1, -1, -1);
|
||||
glm::vec3 maxi = glm::vec3(1, 1, 1);
|
||||
OctTree tree(AABB(mini, maxi), 2);
|
||||
tree.AddDynamicObject(AABB(mini, -0.9f*maxi));
|
||||
OctTree::Output data;
|
||||
glm::vec3 origin = 3.0f * mini;
|
||||
bool rayIntersected = tree.RayCollides(Ray(origin , mini - origin), data);
|
||||
BOOST_CHECK(rayIntersected);
|
||||
tree.ClearDynamicObjects();
|
||||
rayIntersected = tree.RayCollides(Ray(origin, mini - origin), data);
|
||||
BOOST_CHECK(!rayIntersected);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
#include <boost/test/unit_test.hpp>
|
||||
#include <boost/test/execution_monitor.hpp>
|
||||
using boost::unit_test_framework::test_suite;
|
||||
using boost::unit_test_framework::test_case;
|
||||
#include <stdlib.h>//srand
|
||||
|
||||
//#define private public
|
||||
#include "Engine\Core\ConfigFile.h"
|
||||
|
||||
#define _CRTDBG_MAP_ALLOC
|
||||
#include <crtdbg.h>
|
||||
#define DEBUG_CLIENTBLOCK new( _CLIENT_BLOCK, __FILE__, __LINE__)
|
||||
#define new DEBUG_CLIENTBLOCK
|
||||
|
||||
BOOST_AUTO_TEST_SUITE(confTest)
|
||||
|
||||
BOOST_AUTO_TEST_CASE(configFileTest)
|
||||
{
|
||||
//note: this ConfigFileclass currently has memleaks!
|
||||
|
||||
ResourceManager::RegisterType<ConfigFile>("ConfigFile");
|
||||
auto m_Config = ResourceManager::Load<ConfigFile>("ConfigTest.ini");
|
||||
|
||||
//bägge måste vara av samma typ, T typen är string
|
||||
//http://www.boost.org/doc/libs/1_42_0/doc/html/boost_propertytree/tutorial.html
|
||||
//"Note that we construct the path to the value by separating the individual keys with dots"
|
||||
|
||||
//get from tree tests
|
||||
auto getSomething = m_Config->Get("Test.Test1", 0);
|
||||
BOOST_CHECK(getSomething == 423);
|
||||
|
||||
auto getSomething2 = m_Config->Get("fsdfdsfd.T", std::string(""));
|
||||
BOOST_CHECK(getSomething2 == "\"gfdjakflsdl!\"");
|
||||
|
||||
//set/get tests
|
||||
m_Config->Set("Test.4321", 123);
|
||||
auto getSomething3 = m_Config->Get("Test.4321", 0);
|
||||
BOOST_CHECK(getSomething3 == 123);
|
||||
|
||||
m_Config->Set("3_2_1_0_5", "t454j54hj5k32");
|
||||
auto getSomething4 = m_Config->Get("3_2_1_0_5", std::string(""));
|
||||
BOOST_CHECK(getSomething4 == "t454j54hj5k32");
|
||||
|
||||
//***check so outputwindow says: EE: Failed to find "DefaultConfigTestNotExists.ini"! Relying on hardcoded default values!
|
||||
auto m_Config2 = ResourceManager::Load<ConfigFile>("ConfigTestNotExists.ini");
|
||||
|
||||
//set value/savetodisk/load/checkvalue...
|
||||
m_Config->SaveToDisk();
|
||||
m_Config->Set("Test.4321", 145);
|
||||
m_Config->SaveToDisk();
|
||||
auto m_Config3 = ResourceManager::Load<ConfigFile>("ConfigTest.ini");
|
||||
auto getSomething5 = m_Config->Get("Test.4321", 0);
|
||||
BOOST_CHECK(getSomething5 == 145);
|
||||
|
||||
//***check so outputwindow says: EE: Failed to parse "DefaultConfigTestFailed.ini"
|
||||
//***check so outputwindow says: EE: Failed to parse "ConfigTestFailed.ini":
|
||||
auto m_Config4 = ResourceManager::Load<ConfigFile>("ConfigTestFailed.ini");
|
||||
|
||||
//reload,onchildreload unimplemented
|
||||
|
||||
//NOTE:still massive amount of memoryleaks from this method
|
||||
_CrtDumpMemoryLeaks();
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
#ifndef EVENTFIXTURE_H
|
||||
#define EVENTFIXTURE_H
|
||||
|
||||
#include <boost/test/unit_test.hpp>
|
||||
#include "Core\EventBroker.h"
|
||||
|
||||
template <typename EventType>
|
||||
struct EventFixture
|
||||
{
|
||||
EventFixture()
|
||||
{
|
||||
this->ventBroker = new EventBroker();
|
||||
m_EEventType = decltype(m_EEventType)(std::bind(&EventFixture::OnEvent, this, std::placeholders::_1));
|
||||
this->ventBroker->Subscribe(m_EEventType);
|
||||
Run();
|
||||
Check();
|
||||
}
|
||||
~EventFixture()
|
||||
{
|
||||
this->ventBroker->Unsubscribe(m_EEventType);
|
||||
delete this->ventBroker;
|
||||
}
|
||||
|
||||
EventBroker* ventBroker = nullptr;
|
||||
EventRelay<EventFixture, EventType> m_EEventType;
|
||||
bool m_EventRecieved = false;
|
||||
EventType Before;
|
||||
EventType After;
|
||||
|
||||
bool OnEvent(const EventType& event)
|
||||
{
|
||||
m_EventRecieved = true;
|
||||
After = event;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void Run()
|
||||
{
|
||||
// Publish the event
|
||||
this->ventBroker->Publish(Before);
|
||||
// Clear to swap buffers
|
||||
this->ventBroker->Swap();
|
||||
// Process the event
|
||||
this->ventBroker->template Process<EventFixture>();
|
||||
}
|
||||
|
||||
void Check()
|
||||
{
|
||||
BOOST_CHECK(m_EventRecieved);
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,19 @@
|
||||
#include <boost/test/unit_test.hpp>
|
||||
#include "EventFixture.h"
|
||||
|
||||
struct ETestEvent : public Event
|
||||
{
|
||||
int Int = 5;
|
||||
float Float = 1.33333f;
|
||||
double Double = 1.33333;
|
||||
std::string String = "Hello World";
|
||||
};
|
||||
|
||||
BOOST_AUTO_TEST_CASE(EventBrokerTest)
|
||||
{
|
||||
EventFixture<ETestEvent> f;
|
||||
BOOST_CHECK(f.Before.Int == f.After.Int);
|
||||
BOOST_CHECK_CLOSE(f.Before.Float, f.After.Float, 0.00001f);
|
||||
BOOST_CHECK_CLOSE(f.Before.Double, f.After.Double, 0.00001f);
|
||||
BOOST_CHECK(f.Before.String == f.After.String);
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
#include <boost/test/unit_test.hpp>
|
||||
using boost::unit_test_framework::test_suite;
|
||||
using boost::unit_test_framework::test_case;
|
||||
|
||||
#include "HealthSystemTest.h"
|
||||
#include "Game/HealthSystem.h"
|
||||
|
||||
BOOST_AUTO_TEST_SUITE(HealthSystemSuite)
|
||||
|
||||
BOOST_AUTO_TEST_CASE(HealthSystemTest)
|
||||
{
|
||||
//this tests 2 healthevents and the healthsystem
|
||||
GameHealthSystemTest game;
|
||||
//100 loops will be more than enough to do the test
|
||||
int loops = 100;
|
||||
bool success = false;
|
||||
while (loops > 0) {
|
||||
game.Tick();
|
||||
if (game.TestSucceeded) {
|
||||
success = true;
|
||||
break;
|
||||
}
|
||||
loops--;
|
||||
}
|
||||
//The system will process the events, hence it will take a while before we can read anything
|
||||
BOOST_TEST(success);
|
||||
}
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
|
||||
GameHealthSystemTest::GameHealthSystemTest()
|
||||
{
|
||||
ResourceManager::RegisterType<ConfigFile>("ConfigFile");
|
||||
ResourceManager::RegisterType<EntityXMLFile>("EntityXMLFile");
|
||||
|
||||
m_Config = ResourceManager::Load<ConfigFile>("Config.ini");
|
||||
LOG_LEVEL = static_cast<_LOG_LEVEL>(m_Config->Get<int>("Debug.LogLevel", 1));
|
||||
|
||||
// Create the core event broker
|
||||
m_EventBroker = new EventBroker();
|
||||
|
||||
// Create a world
|
||||
m_World = new World();
|
||||
std::string mapToLoad = m_Config->Get<std::string>("Debug.LoadMap", "");
|
||||
if (!mapToLoad.empty()) {
|
||||
ResourceManager::Load<EntityXMLFile>(mapToLoad)->PopulateWorld(m_World);
|
||||
}
|
||||
|
||||
// Create system pipeline
|
||||
m_SystemPipeline = new SystemPipeline(m_EventBroker);
|
||||
m_SystemPipeline->AddSystem<PlayerSystem>(0);
|
||||
m_SystemPipeline->AddSystem<HealthSystem>(0);
|
||||
|
||||
//The Test
|
||||
//create entity which has transorm,player,model,health in it. i.e. is a player
|
||||
EntityID playerID = m_World->CreateEntity();
|
||||
ComponentWrapper transform = m_World->AttachComponent(playerID, "Transform");
|
||||
ComponentWrapper model = m_World->AttachComponent(playerID, "Model");
|
||||
model["Resource"] = "Models/Core/UnitSphere.obj";
|
||||
ComponentWrapper player = m_World->AttachComponent(playerID, "Player");
|
||||
ComponentWrapper health = m_World->AttachComponent(playerID, "Health");
|
||||
healthsID = playerID;
|
||||
double currentHealth = (double)m_World->GetComponent(healthsID, "Health")["Health"];
|
||||
|
||||
//heal player with 40
|
||||
Events::PlayerHealthPickup e3;
|
||||
e3.HealthAmount = 40.0f;
|
||||
e3.PlayerHealedID = healthsID;
|
||||
m_EventBroker->Publish(e3);
|
||||
//damage player with 50
|
||||
Events::PlayerDamage e;
|
||||
e.DamageAmount = 50.0f;
|
||||
e.PlayerDamagedID = healthsID;
|
||||
m_EventBroker->Publish(e);
|
||||
//heal some other player with 40
|
||||
Events::PlayerHealthPickup e2;
|
||||
e2.HealthAmount = 40.0f;
|
||||
e2.PlayerHealedID = healthsID+1;
|
||||
m_EventBroker->Publish(e2);
|
||||
|
||||
EntityID playerID2 = m_World->CreateEntity();
|
||||
ComponentWrapper transform2 = m_World->AttachComponent(playerID2, "Transform");
|
||||
ComponentWrapper model2 = m_World->AttachComponent(playerID2, "Model");
|
||||
model2["Resource"] = "Models/Core/UnitSphere.obj";
|
||||
ComponentWrapper player2 = m_World->AttachComponent(playerID2, "Player");
|
||||
ComponentWrapper health2 = m_World->AttachComponent(playerID2, "Health");
|
||||
//END TEST
|
||||
}
|
||||
|
||||
GameHealthSystemTest::~GameHealthSystemTest()
|
||||
{
|
||||
delete m_SystemPipeline;
|
||||
delete m_World;
|
||||
delete m_EventBroker;
|
||||
}
|
||||
|
||||
void GameHealthSystemTest::Tick()
|
||||
{
|
||||
glfwPollEvents();
|
||||
|
||||
double currentTime = glfwGetTime();
|
||||
double dt = currentTime - m_LastTime;
|
||||
m_LastTime = currentTime;
|
||||
|
||||
// Iterate through systems and update world!
|
||||
m_SystemPipeline->Update(m_World, dt);
|
||||
|
||||
m_EventBroker->Swap();
|
||||
m_EventBroker->Clear();
|
||||
|
||||
//if health reaches 90 then we know the test has succeeded (start with 100hp, remove 50hp, add 40hp)
|
||||
double currentHealth = (double)m_World->GetComponent(healthsID, "Health")["Health"];
|
||||
if (currentHealth==90)
|
||||
TestSucceeded = true;
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
#ifndef HealthTest_h__
|
||||
#define HealthTest_h__
|
||||
|
||||
#include "Core/ResourceManager.h"
|
||||
#include "Core/ConfigFile.h"
|
||||
#include "Core/EventBroker.h"
|
||||
#include "Rendering/Renderer.h"
|
||||
#include "Core/InputManager.h"
|
||||
#include "GUI/Frame.h"
|
||||
#include "Core/World.h"
|
||||
#include "Rendering/RenderQueueFactory.h"
|
||||
#include "Input/InputProxy.h"
|
||||
#include "Input/KeyboardInputHandler.h"
|
||||
#include "Input/MouseInputHandler.h"
|
||||
#include "Core/EKeyDown.h"
|
||||
#include "Core/EntityXMLFile.h"
|
||||
#include "Core/SystemPipeline.h"
|
||||
#include "RaptorCopterSystem.h"
|
||||
#include "PlayerSystem.h"
|
||||
#include "Editor/EditorSystem.h"
|
||||
|
||||
class GameHealthSystemTest
|
||||
{
|
||||
public:
|
||||
GameHealthSystemTest();
|
||||
~GameHealthSystemTest();
|
||||
|
||||
void Tick();
|
||||
bool TestSucceeded = false;
|
||||
|
||||
private:
|
||||
double m_LastTime;
|
||||
ConfigFile* m_Config = nullptr;
|
||||
EventBroker* m_EventBroker;
|
||||
World* m_World;
|
||||
SystemPipeline* m_SystemPipeline;
|
||||
int healthsID;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,12 @@
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
#include "Engine\Core\InputManager.h"
|
||||
|
||||
BOOST_AUTO_TEST_SUITE(inputManagerTests)
|
||||
|
||||
BOOST_AUTO_TEST_CASE(inputManagerTest)
|
||||
{
|
||||
//already tested eventbroker so inputManager is indirectly already tested
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
@@ -0,0 +1,476 @@
|
||||
#include <boost/test/unit_test.hpp>
|
||||
using boost::unit_test_framework::test_suite;
|
||||
using boost::unit_test_framework::test_case;
|
||||
#include "Engine/Core/ObjectPool.h"
|
||||
#include <ctime>
|
||||
|
||||
struct S
|
||||
{
|
||||
S() = default;
|
||||
S(int i, float ff) : k(i), f(ff) { }
|
||||
~S() { }
|
||||
int k;
|
||||
float f;
|
||||
};
|
||||
|
||||
//BOOST_GLOBAL_FIXTURE(S);
|
||||
BOOST_AUTO_TEST_SUITE(memProtoTypeTestSuite)
|
||||
|
||||
BOOST_AUTO_TEST_CASE(testPool)
|
||||
{
|
||||
ObjectPool<S> pool(32);//32 true/false values = 32 slots
|
||||
BOOST_CHECK(pool.empty() == true);
|
||||
|
||||
const size_t size = 12;//12 platser i structen addresses, som håller en int, en float vardera
|
||||
S* addresses[size];
|
||||
addresses[0] = pool.New(7, 0.035f);
|
||||
//"Not empty after allocating one element."
|
||||
BOOST_CHECK(!pool.empty());
|
||||
|
||||
//"Element created correctly with k==7"
|
||||
BOOST_CHECK(addresses[0]->k == 7);
|
||||
//"Element created correctly with f==0.035f"
|
||||
BOOST_CHECK_CLOSE_FRACTION(addresses[0]->f, 0.035f, 0.0001f);
|
||||
addresses[0]->k = 5;
|
||||
BOOST_CHECK(addresses[0]->k == 5);
|
||||
|
||||
//"Empty after delete"
|
||||
pool.Delete(addresses[0]);
|
||||
BOOST_CHECK(pool.empty());
|
||||
|
||||
addresses[0] = pool.New(7, 0.035f);
|
||||
addresses[1] = pool.New(5, 0.035f);
|
||||
pool.Delete(addresses[1]);
|
||||
BOOST_CHECK(!pool.empty());
|
||||
pool.Delete(addresses[0]);
|
||||
BOOST_CHECK(pool.empty());
|
||||
|
||||
//INT32_MAX, FLT_MAX test
|
||||
addresses[0] = pool.New(INT32_MAX, FLT_MAX);
|
||||
BOOST_CHECK(!pool.empty());
|
||||
BOOST_CHECK(addresses[0]->k == INT32_MAX);
|
||||
BOOST_CHECK_CLOSE_FRACTION(addresses[0]->f, FLT_MAX, 0.0001f);
|
||||
}
|
||||
/*
|
||||
BOOST_AUTO_TEST_CASE(testPoolArray)
|
||||
{
|
||||
ObjectPool<S> pool(32);
|
||||
S* addresses;
|
||||
|
||||
//Add array size 5 to pool."
|
||||
addresses = pool.NewArray(5);// <-> addresses = new S[5];
|
||||
addresses[0] = S(12, 0.030f);
|
||||
addresses[1] = S(13, 0.031f);
|
||||
addresses[2] = S(14, 0.032f);
|
||||
addresses[3] = S(15, 0.033f);
|
||||
addresses[4] = S(16, 0.034f);
|
||||
|
||||
//"Not empty after allocating
|
||||
BOOST_CHECK(!pool.empty());
|
||||
//"Element created correctly with k==12"
|
||||
BOOST_CHECK(addresses->k == 12);
|
||||
//"Element created correctly with f==0.030f"
|
||||
BOOST_CHECK_CLOSE_FRACTION(addresses->f, 0.030f, 0.0001f);
|
||||
|
||||
//add a few other structs so it becomes bigger than the original size (32),
|
||||
//which means it must push back the rest of the values into a vector
|
||||
S* test2, *test3, *test4, *test5;
|
||||
test2 = pool.NewArray(5);// <-> test2 = new S[5];
|
||||
test3 = pool.NewArray(40);//+40
|
||||
test4 = pool.NewArray(40);//+40
|
||||
test5 = pool.NewArray(40);//+40=120
|
||||
BOOST_CHECK(pool.ExtraSize() == 120);
|
||||
BOOST_CHECK(pool.PoolSize() == 10);
|
||||
BOOST_CHECK(pool.size() == 120 + 10);
|
||||
|
||||
//testar "perfekt delete", dvs bryr mig inte om att testa att deleta bara 38 om storleken egentligen är 40 osv
|
||||
pool.DeleteArray(test2, 5);//callar destructorn på test2 också
|
||||
pool.DeleteArray(test3, 40);
|
||||
|
||||
pool.DeleteArray(addresses, 5);
|
||||
|
||||
//add / del array
|
||||
S* another = pool.NewArray(64);
|
||||
for (int i = 0; i < 64; ++i)
|
||||
another[i] = S(i, 0.1f*i);
|
||||
pool.DeleteArray(another, 64);
|
||||
}
|
||||
*/
|
||||
BOOST_AUTO_TEST_CASE(testIterationNormal)
|
||||
{
|
||||
//extra vector check
|
||||
S* test4, *test5;
|
||||
ObjectPool<S> pool(4);
|
||||
test4 = pool.New();
|
||||
test5 = pool.New();
|
||||
//Check so iterate over pool doesn't throw compile-time errors.
|
||||
for (auto &o : pool)
|
||||
o.k = 14;
|
||||
for (size_t i = 0; i < 1; ++i)
|
||||
BOOST_CHECK(test4[i].k == 14);
|
||||
for (size_t i = 0; i < 1; ++i)
|
||||
BOOST_CHECK(test5[i].k == 14);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(testOutOfScopeDelete)
|
||||
{
|
||||
//extra vector check
|
||||
S* test4, *test5;
|
||||
{
|
||||
ObjectPool<S> pool(4);
|
||||
test4 = pool.New();
|
||||
test5 = pool.New();
|
||||
//Check so iterate over pool doesn't throw compile-time errors.
|
||||
for (auto &o : pool)
|
||||
o.k = 14;
|
||||
for (size_t i = 0; i < 1; ++i)
|
||||
BOOST_CHECK(test4[i].k == 14);
|
||||
for (size_t i = 0; i < 1; ++i)
|
||||
BOOST_CHECK(test5[i].k == 14);
|
||||
}
|
||||
//pool goes out of scope here, and thus the test4 values become undefined (memory is killed at out of scope)
|
||||
BOOST_CHECK(test4[0].k != 14);
|
||||
BOOST_CHECK(test5[0].k != 15);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(testIterationOneExtra)
|
||||
{
|
||||
//extra vector check
|
||||
ObjectPool<S> pool(1);
|
||||
S* test4, *test5;
|
||||
test4 = pool.New();
|
||||
test5 = pool.New();
|
||||
//Check so iterate over pool doesn't throw compile-time errors.
|
||||
for (auto &o : pool)
|
||||
o.k = 14;
|
||||
for (size_t i = 0; i < 1; ++i)
|
||||
BOOST_CHECK(test4[i].k == 14);
|
||||
for (size_t i = 0; i < 1; ++i)
|
||||
BOOST_CHECK(test5[i].k == 14);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(testIterationTwoExtra)
|
||||
{
|
||||
//extra vector check
|
||||
ObjectPool<S> pool(1);
|
||||
S* test4, *test5;
|
||||
test4 = pool.New();
|
||||
test5 = pool.New();
|
||||
//Check so iterate over pool doesn't throw compile-time errors.
|
||||
for (auto &o : pool)
|
||||
o.k = 14;
|
||||
for (size_t i = 0; i < 1; ++i)
|
||||
BOOST_CHECK(test4[i].k == 14);
|
||||
for (size_t i = 0; i < 1; ++i)
|
||||
BOOST_CHECK(test5[i].k == 14);
|
||||
}
|
||||
/*
|
||||
BOOST_AUTO_TEST_CASE(releaseModeTest_RandomAllocateDeallocate)
|
||||
{
|
||||
//run this in releasemode
|
||||
struct I
|
||||
{
|
||||
I() = default;
|
||||
I(size_t i, size_t ff) : k(i), f(ff) { }
|
||||
~I() { }
|
||||
size_t k;
|
||||
size_t f;
|
||||
};
|
||||
srand((unsigned int)time(nullptr));
|
||||
|
||||
const size_t SIZE = 128;
|
||||
ObjectPool<I> pool(SIZE);
|
||||
I* addresses[SIZE];
|
||||
std::vector<bool> allocated(SIZE, false);
|
||||
std::vector<size_t> arrSizes(SIZE, 0);
|
||||
size_t slotsAlloced = 0;
|
||||
size_t superCount = 0;
|
||||
size_t slot;
|
||||
size_t i;
|
||||
|
||||
while (superCount++ < 1000) {
|
||||
if (rand() % 2 == 0) {
|
||||
i = 0;
|
||||
//ta slumpmässig slot som inte är allokerad
|
||||
do {
|
||||
slot = (size_t)((SIZE - 1) * ((float)rand() / RAND_MAX));
|
||||
} while (allocated[slot] && ++i < 512);
|
||||
|
||||
if (i < 512) {
|
||||
arrSizes[slot] = 1 + (size_t)((24 - 1) * ((float)rand() / RAND_MAX));
|
||||
addresses[slot] = pool.NewArray(arrSizes[slot]);
|
||||
for (size_t a = 0; a < arrSizes[slot]; ++a)
|
||||
addresses[slot][a] = I(slot, a);
|
||||
allocated[slot] = true;
|
||||
++slotsAlloced;
|
||||
}
|
||||
}
|
||||
//Deallocate
|
||||
else {
|
||||
i = 0;
|
||||
//ta slumpmässig slot som är allokerad
|
||||
do {
|
||||
slot = (size_t)((SIZE - 1) * ((float)rand() / RAND_MAX));
|
||||
} while (!allocated[slot] && ++i < 512);
|
||||
|
||||
if (i < 512) {
|
||||
pool.DeleteArray(addresses[slot], arrSizes[slot]);
|
||||
arrSizes[slot] = 0;
|
||||
allocated[slot] = false;
|
||||
--slotsAlloced;
|
||||
}
|
||||
}
|
||||
//Check content.
|
||||
for (size_t a = 0; a < SIZE; ++a) {
|
||||
if (allocated[a]) {
|
||||
for (size_t e = 0; e < arrSizes[a]; ++e) {
|
||||
BOOST_CHECK(!(addresses[a][e].k != a || addresses[a][e].f != e));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
BOOST_AUTO_TEST_CASE(testConstructors)
|
||||
{
|
||||
//http://stackoverflow.com/questions/357929/is-it-important-to-unit-test-a-constructor
|
||||
//"If your constructor has, for example, an if (condition), you need to test both flows (true,false).
|
||||
//If your constructor does some kind of job before setting. You should check the job is done"
|
||||
|
||||
//testing the constructors with different T values and a small check so size is initialized to 0
|
||||
MemoryPool<int> memPoolI;
|
||||
BOOST_CHECK(memPoolI.empty());
|
||||
BOOST_CHECK(memPoolI.size() == 0);
|
||||
MemoryPool<float> memPoolF;
|
||||
BOOST_CHECK(memPoolF.empty());
|
||||
BOOST_CHECK(memPoolF.size() == 0);
|
||||
MemoryPool<double> memPoolD;
|
||||
BOOST_CHECK(memPoolD.empty());
|
||||
BOOST_CHECK(memPoolD.size() == 0);
|
||||
MemoryPool<S> memPoolS;
|
||||
BOOST_CHECK(memPoolS.empty());
|
||||
BOOST_CHECK(memPoolS.size() == 0);
|
||||
|
||||
ObjectPool<int> objPoolI(64);
|
||||
BOOST_CHECK(objPoolI.empty());
|
||||
BOOST_CHECK(objPoolI.size() == 0);
|
||||
ObjectPool<float> objPoolF(32);
|
||||
BOOST_CHECK(objPoolF.empty());
|
||||
BOOST_CHECK(objPoolF.size() == 0);
|
||||
ObjectPool<double> objPoolD(16);
|
||||
BOOST_CHECK(objPoolD.empty());
|
||||
BOOST_CHECK(objPoolD.size() == 0);
|
||||
ObjectPool<S> objPoolS(128);
|
||||
BOOST_CHECK(objPoolS.empty());
|
||||
BOOST_CHECK(objPoolS.size() == 0);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(testOperators)
|
||||
{
|
||||
ObjectPool<S> pool(100);
|
||||
// S* s[12] = pool.NewArray(12);
|
||||
S* s[12];
|
||||
s[0] = pool.New();
|
||||
s[11] = pool.New();
|
||||
|
||||
s[0]->k = 2;
|
||||
s[11]->k = 3;
|
||||
//testing operators: ++i,!=
|
||||
auto& iter = pool.begin();
|
||||
for (iter; iter != pool.end(); ++iter) {
|
||||
//testing operators:*,==
|
||||
auto dereferencedIterator = *iter;
|
||||
if (iter == pool.begin()) {
|
||||
BOOST_CHECK(dereferencedIterator.k == 2);
|
||||
}
|
||||
if (iter == pool.end()) {
|
||||
BOOST_CHECK(dereferencedIterator.k == 3);
|
||||
}
|
||||
//testing operators:->
|
||||
iter->k += 2;
|
||||
}
|
||||
BOOST_CHECK(s[0]->k == 4);
|
||||
BOOST_CHECK(s[11]->k == 5);
|
||||
BOOST_CHECK(iter == pool.end());
|
||||
|
||||
//testing operators:i++
|
||||
s[0]->k = 2;
|
||||
s[11]->k = 2;
|
||||
for (auto& iter = pool.begin(); iter != pool.end(); iter++)
|
||||
iter->k += 2;
|
||||
BOOST_CHECK(s[0]->k == 4);
|
||||
BOOST_CHECK(s[11]->k == 4);
|
||||
}
|
||||
/*
|
||||
BOOST_AUTO_TEST_CASE(testBranchFree)
|
||||
{
|
||||
//testing Free , which is the only untested
|
||||
//via delete/deletearray
|
||||
|
||||
//1. no extra memory delete
|
||||
ObjectPool<S> pool(32);//32 true/false values = 32 slots
|
||||
S* addresses[12];
|
||||
addresses[0] = pool.New(7, 0.035f);
|
||||
pool.Delete(addresses[0]);
|
||||
BOOST_CHECK(pool.empty());
|
||||
|
||||
//1b. no extra memory deleteArray
|
||||
ObjectPool<S> pool1b(32);//32 true/false values = 32 slots
|
||||
S* test1b;
|
||||
test1b = pool1b.NewArray(5);// <-> test2 = new S[5];
|
||||
BOOST_CHECK(pool1b.size() == 5);
|
||||
pool1b.DeleteArray(test1b, 5);//callar destructorn på test2 också
|
||||
BOOST_CHECK(pool1b.empty());
|
||||
|
||||
//2. extra memory delete
|
||||
ObjectPool<S> pool2(2);
|
||||
S* addresses2[12];
|
||||
addresses2[0] = pool2.New(7, 0.035f);
|
||||
addresses2[1] = pool2.New(7, 0.035f);
|
||||
addresses2[2] = pool2.New(7, 0.035f);
|
||||
addresses2[3] = pool2.New(7, 0.035f);
|
||||
addresses2[4] = pool2.New(7, 0.035f);
|
||||
BOOST_CHECK(pool2.size() == 5);
|
||||
pool2.Delete(addresses2[0]);
|
||||
BOOST_CHECK(pool2.size() == 4);
|
||||
pool2.Delete(addresses2[1]);
|
||||
BOOST_CHECK(pool2.size() == 3);
|
||||
pool2.Delete(addresses2[2]);
|
||||
BOOST_CHECK(pool2.size() == 2);
|
||||
pool2.Delete(addresses2[3]);
|
||||
BOOST_CHECK(pool2.size() == 1);
|
||||
pool2.Delete(addresses2[4]);
|
||||
BOOST_CHECK(pool2.empty());
|
||||
//reverse delete
|
||||
addresses2[0] = pool2.New(7, 0.035f);
|
||||
addresses2[1] = pool2.New(7, 0.035f);
|
||||
addresses2[2] = pool2.New(7, 0.035f);
|
||||
addresses2[3] = pool2.New(7, 0.035f);
|
||||
addresses2[4] = pool2.New(7, 0.035f);
|
||||
BOOST_CHECK(pool2.size() == 5);
|
||||
pool2.Delete(addresses2[4]);
|
||||
BOOST_CHECK(pool2.size() == 4);
|
||||
pool2.Delete(addresses2[3]);
|
||||
BOOST_CHECK(pool2.size() == 3);
|
||||
pool2.Delete(addresses2[2]);
|
||||
BOOST_CHECK(pool2.size() == 2);
|
||||
pool2.Delete(addresses2[1]);
|
||||
BOOST_CHECK(pool2.size() == 1);
|
||||
pool2.Delete(addresses2[0]);
|
||||
BOOST_CHECK(pool2.empty());
|
||||
|
||||
//2b. extra memory deleteArray
|
||||
ObjectPool<S> pool2b(32);//32 true/false values = 32 slots
|
||||
S* test2b,*test2bb;
|
||||
test2b = pool2b.NewArray(5);// <-> test2 = new S[5];
|
||||
BOOST_CHECK(pool2b.size() == 5);
|
||||
test2bb = pool2b.NewArray(40);// <-> test2 = new S[5];
|
||||
BOOST_CHECK(pool2b.size() == 45);
|
||||
pool2b.DeleteArray(test2b, 5);//callar destructorn på test2 också
|
||||
BOOST_CHECK(pool2b.size() == 40);
|
||||
pool2b.DeleteArray(test2bb, 40);//callar destructorn på test2 också
|
||||
BOOST_CHECK(pool2b.empty());
|
||||
}
|
||||
*/
|
||||
BOOST_AUTO_TEST_CASE(testBranchAllocate)
|
||||
{
|
||||
//1 slot else many slots
|
||||
//see testBranchFree
|
||||
|
||||
//out of mem vs not out of mem allocate
|
||||
//see testBranchFree
|
||||
}
|
||||
BOOST_AUTO_TEST_CASE(testEdgeCase)
|
||||
{
|
||||
//test with a very small pool
|
||||
ObjectPool<S> pool(1);
|
||||
BOOST_CHECK(pool.empty());
|
||||
S* test4;
|
||||
test4 = pool.New(7, 0.035f);
|
||||
BOOST_CHECK(!pool.empty());
|
||||
BOOST_CHECK(test4->k == 7);
|
||||
BOOST_CHECK_CLOSE_FRACTION(test4->f, 0.035f, 0.0001f);
|
||||
|
||||
//test with a very small pool and array, iterating
|
||||
ObjectPool<S> poolA(1);
|
||||
S* test5;
|
||||
test5 = poolA.New();
|
||||
//Check so iterate over pool doesn't throw compile-time errors.
|
||||
for (auto &o : poolA)
|
||||
o.k = 14;
|
||||
for (size_t i = 0; i < 1; ++i)
|
||||
BOOST_CHECK(test5[i].k == 14);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(testBadlyAlignedData)
|
||||
{
|
||||
//small test with non-aligned data 4+1bytes
|
||||
struct S
|
||||
{
|
||||
S() = default;
|
||||
S(float f, char c) : m_f(f), m_c(c) { }
|
||||
~S() { }
|
||||
float m_f;
|
||||
char m_c;
|
||||
};
|
||||
MemoryPool<S> memPoolS;
|
||||
BOOST_CHECK(memPoolS.empty());
|
||||
BOOST_CHECK(memPoolS.size() == 0);
|
||||
ObjectPool<S> objPoolS(64);
|
||||
BOOST_CHECK(objPoolS.empty());
|
||||
BOOST_CHECK(objPoolS.size() == 0);
|
||||
S* test4;
|
||||
test4 = objPoolS.New();
|
||||
//Check so iterate over pool doesn't throw compile-time errors.
|
||||
for (auto &o : objPoolS) {
|
||||
o.m_c = 'v';
|
||||
o.m_f = 0.15534543f;
|
||||
}
|
||||
for (size_t i = 0; i < 1; ++i) {
|
||||
BOOST_CHECK(test4[i].m_c == 'v');
|
||||
BOOST_CHECK_CLOSE_FRACTION(test4[i].m_f, 0.15534543f, 0.0001f);
|
||||
}
|
||||
}
|
||||
BOOST_AUTO_TEST_CASE(testBadlyAlignedData2)
|
||||
{
|
||||
//small test with non-aligned data 1+1+1bytes
|
||||
struct S
|
||||
{
|
||||
S() = default;
|
||||
S(char c, char c2, char c3) : m_c(c), m_c2(c2), m_c3(c3) { }
|
||||
~S() { }
|
||||
char m_c;
|
||||
char m_c2;
|
||||
char m_c3;
|
||||
};
|
||||
MemoryPool<S> memPoolS;
|
||||
BOOST_CHECK(memPoolS.empty());
|
||||
BOOST_CHECK(memPoolS.size() == 0);
|
||||
ObjectPool<S> objPoolS(64);
|
||||
BOOST_CHECK(objPoolS.empty());
|
||||
BOOST_CHECK(objPoolS.size() == 0);
|
||||
S* test4;
|
||||
test4 = objPoolS.New();
|
||||
//Check so iterate over pool doesn't throw compile-time errors.
|
||||
for (auto &o : objPoolS) {
|
||||
o.m_c = 'v';
|
||||
o.m_c2 = 'w';
|
||||
o.m_c3 = 'x';
|
||||
}
|
||||
for (size_t i = 0; i < 1; ++i) {
|
||||
BOOST_CHECK(test4[i].m_c == 'v');
|
||||
BOOST_CHECK(test4[i].m_c2 == 'w');
|
||||
BOOST_CHECK(test4[i].m_c3 == 'x');
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(testWrongData)
|
||||
{
|
||||
}
|
||||
BOOST_AUTO_TEST_CASE(testFillDeleteFillAgain) {
|
||||
//already done in BOOST_AUTO_TEST_CASE(releaseModeTest_RandomAllocateDeallocate)
|
||||
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
@@ -0,0 +1,160 @@
|
||||
#include <boost/test/unit_test.hpp>
|
||||
using boost::unit_test_framework::test_suite;
|
||||
using boost::unit_test_framework::test_case;
|
||||
#include <stdlib.h>//srand
|
||||
|
||||
#include "Engine/Core/OctTree.h"
|
||||
#include "Engine/Core/Ray.h"
|
||||
#include "OldOctTree.h"
|
||||
|
||||
BOOST_AUTO_TEST_SUITE(octTreeTestsW)
|
||||
|
||||
BOOST_AUTO_TEST_CASE(octSameRegionTest)
|
||||
{
|
||||
glm::vec3 mini = glm::vec3(-1, -1, -1);
|
||||
glm::vec3 maxi = glm::vec3(1, 1, 1);
|
||||
OctTree tree(AABB(mini, maxi), 2);
|
||||
AABB firstQuadrant(mini, 0.8f*mini);
|
||||
tree.AddStaticObject(firstQuadrant);
|
||||
AABB testBox(0.9f*mini, 0.8f*mini);
|
||||
std::vector<AABB> region;
|
||||
tree.BoxesInSameRegion(testBox, region);
|
||||
BOOST_REQUIRE(region.size() == 1);
|
||||
AABB& box = region[0];
|
||||
BOOST_CHECK_CLOSE_FRACTION(box.Center().x, firstQuadrant.Center().x, 0.00001f);
|
||||
BOOST_CHECK_CLOSE_FRACTION(box.Center().y, firstQuadrant.Center().y, 0.00001f);
|
||||
BOOST_CHECK_CLOSE_FRACTION(box.Center().z, firstQuadrant.Center().z, 0.00001f);
|
||||
BOOST_CHECK_CLOSE_FRACTION(box.HalfSize().x, firstQuadrant.HalfSize().x, 0.00001f);
|
||||
BOOST_CHECK_CLOSE_FRACTION(box.HalfSize().y, firstQuadrant.HalfSize().y, 0.00001f);
|
||||
BOOST_CHECK_CLOSE_FRACTION(box.HalfSize().z, firstQuadrant.HalfSize().z, 0.00001f);
|
||||
}
|
||||
|
||||
const int LEVEL_BOUNDS = 500;
|
||||
const int MAXSIZE = 50;
|
||||
const int BOXES = 400;
|
||||
const int NUM_DYNAMICS = 0;
|
||||
const int NUM_STATICS = BOXES - NUM_DYNAMICS;
|
||||
const int SEED = 6548;
|
||||
const int TEST_FRAMES = 300;
|
||||
const int NUM_FUNCTION_LOOPS = 25;
|
||||
const int TESTS = 0; //10
|
||||
|
||||
template<typename Tree>
|
||||
void RegionTest(Tree& tree)
|
||||
{
|
||||
AABB aabb;
|
||||
aabb.CreateFromCenter(glm::vec3(rand() % LEVEL_BOUNDS, rand() % LEVEL_BOUNDS, rand() % LEVEL_BOUNDS),
|
||||
glm::vec3(rand() % MAXSIZE, rand() % MAXSIZE, rand() % MAXSIZE));
|
||||
std::vector<AABB> outVec;
|
||||
tree.BoxesInSameRegion(aabb, outVec);
|
||||
}
|
||||
|
||||
template<typename Tree>
|
||||
void RayTest(Tree& tree)
|
||||
{
|
||||
Tree::Output data;
|
||||
glm::vec3 rayStart = glm::vec3(rand() % LEVEL_BOUNDS, rand() % LEVEL_BOUNDS, rand() % LEVEL_BOUNDS);
|
||||
glm::vec3 rayEnd = glm::vec3(rand() % LEVEL_BOUNDS, rand() % LEVEL_BOUNDS, rand() % LEVEL_BOUNDS);
|
||||
tree.RayCollides({ rayStart , glm::normalize(rayEnd - rayStart) }, data);
|
||||
}
|
||||
|
||||
template<typename Tree>
|
||||
void BoxTest(Tree& tree)
|
||||
{
|
||||
AABB outBox;
|
||||
AABB aabb;
|
||||
aabb.CreateFromCenter(glm::vec3(rand() % LEVEL_BOUNDS, rand() % LEVEL_BOUNDS, rand() % LEVEL_BOUNDS),
|
||||
glm::vec3(rand() % MAXSIZE, rand() % MAXSIZE, rand() % MAXSIZE));
|
||||
tree.BoxCollides(aabb, outBox);
|
||||
}
|
||||
|
||||
template<typename Tree>
|
||||
void NopTest(Tree& tree)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
template<typename Tree, typename TestFunction>
|
||||
void TestLoop(TestFunction xTest)
|
||||
{
|
||||
srand(SEED);
|
||||
glm::vec3 mini = glm::vec3(0, 0, 0);
|
||||
glm::vec3 maxi = glm::vec3(LEVEL_BOUNDS, LEVEL_BOUNDS, LEVEL_BOUNDS);
|
||||
Tree tree(AABB(mini, maxi), 3);
|
||||
AABB aabb;
|
||||
glm::vec3 center;
|
||||
glm::vec3 size;
|
||||
for (int t = 0; t < TESTS; ++t) {
|
||||
for (int i = 0; i < NUM_STATICS; ++i) {
|
||||
center = glm::vec3(rand() % LEVEL_BOUNDS, rand() % LEVEL_BOUNDS, rand() % LEVEL_BOUNDS);
|
||||
size = glm::vec3(rand() % MAXSIZE, rand() % MAXSIZE, rand() % MAXSIZE);
|
||||
aabb.CreateFromCenter(center, size);
|
||||
tree.AddStaticObject(aabb);
|
||||
}
|
||||
for (int fr = 0; fr < TEST_FRAMES; ++fr) {
|
||||
for (int i = 0; i < NUM_DYNAMICS; ++i) {
|
||||
center = glm::vec3(rand() % LEVEL_BOUNDS, rand() % LEVEL_BOUNDS, rand() % LEVEL_BOUNDS);
|
||||
size = glm::vec3(rand() % MAXSIZE, rand() % MAXSIZE, rand() % MAXSIZE);
|
||||
aabb.CreateFromCenter(center, size);
|
||||
tree.AddDynamicObject(aabb);
|
||||
}
|
||||
|
||||
for (int fl = 0; fl < NUM_FUNCTION_LOOPS; ++fl) {
|
||||
xTest(tree);
|
||||
}
|
||||
|
||||
tree.ClearDynamicObjects();
|
||||
}
|
||||
tree.ClearObjects();
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(octRegionPerfTestWithDuplicates)
|
||||
{
|
||||
TestLoop<Old::OctTree>(RegionTest<Old::OctTree>);
|
||||
BOOST_CHECK(true);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(octRegionPerfTestNoDuplicates)
|
||||
{
|
||||
TestLoop<OctTree>(RegionTest<OctTree>);
|
||||
BOOST_CHECK(true);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(octBoxPerfTestWithDuplicates)
|
||||
{
|
||||
TestLoop<Old::OctTree>(BoxTest<Old::OctTree>);
|
||||
BOOST_CHECK(true);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(octBoxPerfTestNoDuplicates)
|
||||
{
|
||||
TestLoop<OctTree>(BoxTest<OctTree>);
|
||||
BOOST_CHECK(true);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(octRayPerfTestWithDuplicates)
|
||||
{
|
||||
TestLoop<Old::OctTree>(RayTest<Old::OctTree>);
|
||||
BOOST_CHECK(true);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(octRayPerfTestNoDuplicates)
|
||||
{
|
||||
TestLoop<OctTree>(RayTest<OctTree>);
|
||||
BOOST_CHECK(true);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(octNopPerfTestWithDuplicates)
|
||||
{
|
||||
TestLoop<Old::OctTree>(NopTest<Old::OctTree>);
|
||||
BOOST_CHECK(true);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(octNopPerfTestNoDuplicates)
|
||||
{
|
||||
TestLoop<OctTree>(NopTest<OctTree>);
|
||||
BOOST_CHECK(true);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
@@ -0,0 +1,49 @@
|
||||
#include <boost/test/unit_test.hpp>
|
||||
using boost::unit_test_framework::test_suite;
|
||||
using boost::unit_test_framework::test_case;
|
||||
#include <stdlib.h>//srand
|
||||
|
||||
//#define private public//HACK! Needed for white box testing
|
||||
//#include "Engine/Core/OctTree.h"
|
||||
//#include "OldOctTree.h"
|
||||
//friend class and refactoringIntoNewClass is some extra work and needs to be updated when the original class is updated, and can contain bugs that
|
||||
//isnt in the original class
|
||||
//Reflection-inspection seems to be only available for C#
|
||||
//http://stackoverflow.com/questions/6778496/how-to-do-unit-testing-on-private-members-and-methods-of-c-classes
|
||||
//http://stackoverflow.com/questions/3676664/unit-testing-of-private-methods
|
||||
|
||||
#include "OctTreeTestGameClass.h"
|
||||
|
||||
#define private public//HACK! Needed for white box testing
|
||||
#include <Engine\Core\OctTree.h>
|
||||
//else we would have to "open up" the octTree class more with get/sets, public methods, etc. which is not good encapsulation-wise
|
||||
|
||||
BOOST_AUTO_TEST_SUITE(octTreeTestsA)
|
||||
|
||||
BOOST_AUTO_TEST_CASE(octTreeTest)
|
||||
{
|
||||
//white box testing
|
||||
//http://softwaretestingfundamentals.com/differences-between-black-box-testing-and-white-box-testing/
|
||||
//http://technologyconversations.com/2013/12/11/black-box-vs-white-box-testing/
|
||||
|
||||
//simple AABB constructor check
|
||||
auto minCorner = glm::vec3(0.0f, 0.0f, 0.0f);
|
||||
auto maxCorner = glm::vec3(1.0f, 1.0f, 1.0f);
|
||||
auto someAABB = AABB(minCorner, maxCorner);
|
||||
BOOST_CHECK(someAABB.MinCorner() == minCorner);
|
||||
BOOST_CHECK(someAABB.MaxCorner() == maxCorner);
|
||||
BOOST_CHECK(someAABB.Center() == 0.5f * (minCorner + maxCorner));
|
||||
|
||||
//simple destructor check in the end, just look for memleaks, then it didnt clear the AABB structure
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(octTreeTest2)
|
||||
{
|
||||
//octtree draw etc
|
||||
Game game(0, nullptr);
|
||||
while (game.Running()) {
|
||||
game.Tick();
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
@@ -0,0 +1,193 @@
|
||||
#include "OctTreeTestGameClass.h"
|
||||
|
||||
Game::Game(int argc, char* argv[]) : someOctTree(AABB(-0.5f*worldSize, 0.5f*worldSize), 2)
|
||||
{
|
||||
ResourceManager::RegisterType<ConfigFile>("ConfigFile");
|
||||
ResourceManager::RegisterType<Model>("Model");
|
||||
ResourceManager::RegisterType<Texture>("Texture");
|
||||
ResourceManager::RegisterType<EntityXMLFile>("EntityXMLFile");
|
||||
ResourceManager::RegisterType<ShaderProgram>("ShaderProgram");
|
||||
|
||||
m_Config = ResourceManager::Load<ConfigFile>("Config.ini");
|
||||
LOG_LEVEL = static_cast<_LOG_LEVEL>(m_Config->Get<int>("Debug.LogLevel", 1));
|
||||
|
||||
// Create the core event broker
|
||||
m_EventBroker = new EventBroker();
|
||||
|
||||
m_RenderQueueFactory = new RenderQueueFactory();
|
||||
|
||||
// Create the renderer
|
||||
m_Renderer = new Renderer(m_EventBroker);
|
||||
m_Renderer->SetFullscreen(m_Config->Get<bool>("Video.Fullscreen", false));
|
||||
m_Renderer->SetVSYNC(m_Config->Get<bool>("Video.VSYNC", false));
|
||||
m_Renderer->SetResolution(Rectangle(
|
||||
0,
|
||||
0,
|
||||
m_Config->Get<int>("Video.Width", 1280),
|
||||
m_Config->Get<int>("Video.Height", 720)
|
||||
));
|
||||
m_Renderer->Initialize();
|
||||
m_Renderer->Camera()->SetFOV(glm::radians(m_Config->Get<float>("Video.FOV", 90.f)));
|
||||
|
||||
// Create input manager
|
||||
m_InputManager = new InputManager(m_Renderer->Window(), m_EventBroker);
|
||||
m_InputProxy = new InputProxy(m_EventBroker);
|
||||
m_InputProxy->AddHandler<KeyboardInputHandler>();
|
||||
m_InputProxy->AddHandler<MouseInputHandler>();
|
||||
m_InputProxy->LoadBindings("Input.ini");
|
||||
|
||||
// Create the root level GUI frame
|
||||
m_FrameStack = new GUI::Frame(m_EventBroker);
|
||||
m_FrameStack->Width = m_Renderer->Resolution().Width;
|
||||
m_FrameStack->Height = m_Renderer->Resolution().Height;
|
||||
|
||||
// Create a TEST WORLD
|
||||
m_World = new HardcodedTestWorld();
|
||||
|
||||
m_SystemPipeline = new SystemPipeline(m_EventBroker);
|
||||
m_SystemPipeline->AddSystem<PlayerSystem>(0);
|
||||
|
||||
m_LastTime = glfwGetTime();
|
||||
}
|
||||
|
||||
Game::~Game()
|
||||
{
|
||||
delete m_FrameStack;
|
||||
delete m_EventBroker;
|
||||
}
|
||||
|
||||
void Game::Tick()
|
||||
{
|
||||
double currentTime = glfwGetTime();
|
||||
double dt = currentTime - m_LastTime;
|
||||
m_LastTime = currentTime;
|
||||
|
||||
// Handle input in a weird looking but responsive way
|
||||
m_EventBroker->Process<InputManager>();
|
||||
m_EventBroker->Swap();
|
||||
m_InputManager->Update(dt);
|
||||
m_EventBroker->Swap();
|
||||
m_InputProxy->Update(dt);
|
||||
m_EventBroker->Swap();
|
||||
m_InputProxy->Process();
|
||||
m_EventBroker->Swap();
|
||||
|
||||
#define TEST1
|
||||
//this draws the octTree and you can set the cube inside it and see what boxes in the tree that it belongs to
|
||||
#ifdef TEST1
|
||||
if (!m_UpdatedOnce) {
|
||||
m_UpdatedOnce = true;
|
||||
m_World->createTestEntitiesTest1();
|
||||
}
|
||||
|
||||
//add/move the trigger box
|
||||
auto pos = m_Renderer->Camera()->Forward() + m_Renderer->Camera()->Position();
|
||||
AABB boxi;
|
||||
boxi.CreateFromCenter(pos, maxPos - minPos);
|
||||
frameCounter++;
|
||||
if (frameCounter > 1) {
|
||||
m_World->someOctTree.ClearDynamicObjects();
|
||||
m_World->someOctTree.AddDynamicObject(boxi);
|
||||
frameCounter = 0;
|
||||
}
|
||||
ComponentWrapper transform = m_World->GetComponent(m_World->anotherBoxTransformId, "Transform");
|
||||
transform["Position"] = boxi.Center();
|
||||
|
||||
//check all children again in the tree if they have a box in them or not, and colormark them if they do
|
||||
//contentboxarna får man ut - inte childboxarna!
|
||||
std::vector<int> boxIndex;
|
||||
boxIndex = m_World->someOctTree.m_Root->childIndicesContainingBox(boxi);
|
||||
|
||||
for (auto& oneLinkedObject : m_World->linkOM)
|
||||
{
|
||||
ComponentWrapper model = m_World->GetComponent(oneLinkedObject.entId, "Model");
|
||||
model["Color"] = glm::vec4(1.0f, 1.0f, 1.0f, 1.0f);
|
||||
if (oneLinkedObject.child->m_DynamicObjIndices.size() != 0) {
|
||||
model["Color"] = glm::vec4(0.0f, 0.0f, 0.0f, 1.0f);
|
||||
}
|
||||
|
||||
//next check if the childIndicesContainingBox method returns the correct boxes
|
||||
//REQUIRED: childIndicesContainingBox must be public to test this!
|
||||
for each (auto someBoxIndex in boxIndex)
|
||||
{
|
||||
glm::vec3 pos = m_World->someOctTree.m_Root->m_Children[someBoxIndex]->m_Box.Center();
|
||||
if (abs(pos.x - oneLinkedObject.posxyz.x) < 0.005f &&
|
||||
abs(pos.y - oneLinkedObject.posxyz.y) < 0.005f &&
|
||||
abs(pos.z - oneLinkedObject.posxyz.z) < 0.005f) {
|
||||
model["Color"] = glm::vec4(0.0f, 1.0f, 0.0f, 1.0f);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
m_RenderQueueFactory->Update(m_World);
|
||||
|
||||
//wireframe
|
||||
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
||||
#endif
|
||||
//this tests AABB vs AABB collision and AABB vs OctTree with AABB in it
|
||||
#ifdef TEST2
|
||||
|
||||
//only add 1 for now...
|
||||
//grey box
|
||||
|
||||
const glm::vec4 redCol = glm::vec4(1, 0.2f, 0, 1);
|
||||
const glm::vec4 greenCol = glm::vec4(0.1f, 1.0f, 0.25f, 1);
|
||||
const glm::vec3 boxSize = 0.1f*glm::vec3(1.0f, 1.0f, 1.0f);
|
||||
AABB aabb;
|
||||
aabb.CreateFromCenter(glm::vec3(0, 2.0f, 0.0f), glm::vec3(1.0f, 1.0f, 1.0f));
|
||||
|
||||
if (m_UpdatedOnce) {
|
||||
//auto test = someOctTree.childIndicesContainingBox(aabb);
|
||||
std::vector<AABB> test2;
|
||||
someOctTree.BoxesInSameRegion(aabb, test2);
|
||||
}
|
||||
if (!m_UpdatedOnce) {
|
||||
m_UpdatedOnce = true;
|
||||
someOctTree.AddStaticObject(aabb);
|
||||
//create the "small red box"
|
||||
m_BoxID = m_World->CreateEntity();
|
||||
ComponentWrapper transform = m_World->AttachComponent(m_BoxID, "Transform");
|
||||
transform["Scale"] = boxSize;
|
||||
ComponentWrapper model = m_World->AttachComponent(m_BoxID, "Model");
|
||||
model["Resource"] = "Models/Core/UnitBox.obj";
|
||||
m_World->createTestEntitiesTest2();
|
||||
}
|
||||
|
||||
//red box
|
||||
AABB redBox;
|
||||
auto boxPos = m_Renderer->Camera()->Position() + 1.2f*m_Renderer->Camera()->Forward();
|
||||
redBox.CreateFromCenter(boxPos, boxSize);
|
||||
ComponentWrapper transform = m_World->GetComponent(m_BoxID, "Transform");
|
||||
transform["Position"] = boxPos;
|
||||
ComponentWrapper model = m_World->GetComponent(m_BoxID, "Model");
|
||||
//this checks AABB vs an AABB in the octTree
|
||||
if (someOctTree.BoxCollides(redBox, AABB())) {
|
||||
//this checks AABB vs AABB
|
||||
//if (Collision::AABBVsAABB(redBox, aabb)) {
|
||||
//m_Renderer->Camera()->SetPosition(m_PrevPos);
|
||||
//m_Renderer->Camera()->SetOrientation(m_PrevOri);
|
||||
model["Color"] = greenCol;
|
||||
}
|
||||
else {
|
||||
model["Color"] = redCol;
|
||||
}
|
||||
|
||||
m_PrevPos = m_Renderer->Camera()->Position();
|
||||
m_PrevOri = m_Renderer->Camera()->Orientation();
|
||||
|
||||
m_RenderQueueFactory->Update(m_World);
|
||||
#endif
|
||||
|
||||
// Iterate through systems and update world!
|
||||
m_SystemPipeline->Update(m_World, dt);
|
||||
m_Renderer->Update(dt);
|
||||
|
||||
m_RenderQueueFactory->Update(m_World);
|
||||
GLERROR("Game::Tick m_RenderQueueFactory->Update");
|
||||
m_Renderer->Draw(m_RenderQueueFactory->RenderQueues());
|
||||
GLERROR("Game::Tick m_Renderer->Draw");
|
||||
m_EventBroker->Swap();
|
||||
m_EventBroker->Clear();
|
||||
|
||||
glfwPollEvents();
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
#ifndef Game_h__
|
||||
#define Game_h__
|
||||
|
||||
#include "Core/ResourceManager.h"
|
||||
#include "Core/ConfigFile.h"
|
||||
#include "Core/EventBroker.h"
|
||||
#include "Rendering/Renderer.h"
|
||||
#include "Core/InputManager.h"
|
||||
#include "GUI/Frame.h"
|
||||
#include "Core/World.h"
|
||||
#include "Rendering/RenderQueueFactory.h"
|
||||
#include "Input/InputProxy.h"
|
||||
#include "Input/KeyboardInputHandler.h"
|
||||
#include "Input/MouseInputHandler.h"
|
||||
#include "Core/EKeyDown.h"
|
||||
#include "Core/EntityXMLFile.h"
|
||||
#include "Core/SystemPipeline.h"
|
||||
#include "RaptorCopterSystem.h"
|
||||
#include "PlayerSystem.h"
|
||||
#include "Editor/EditorSystem.h"
|
||||
|
||||
#include "OctTreeTestHardCodedTestWorld.h"
|
||||
#include "Collision/Collision.h"
|
||||
|
||||
class Game
|
||||
{
|
||||
public:
|
||||
Game(int argc, char* argv[]);
|
||||
~Game();
|
||||
|
||||
bool Running() const { return !glfwWindowShouldClose(m_Renderer->Window()); }
|
||||
void Tick();
|
||||
|
||||
private:
|
||||
double m_LastTime;
|
||||
ConfigFile* m_Config = nullptr;
|
||||
EventBroker* m_EventBroker;
|
||||
IRenderer* m_Renderer;
|
||||
InputManager* m_InputManager;
|
||||
GUI::Frame* m_FrameStack;
|
||||
HardcodedTestWorld* m_World;
|
||||
RenderQueueFactory* m_RenderQueueFactory;
|
||||
InputProxy* m_InputProxy;
|
||||
SystemPipeline* m_SystemPipeline;
|
||||
|
||||
//Test1
|
||||
int frameCounter = 0;
|
||||
glm::vec3 minPos = glm::vec3(0.1f, 0.1f, 0.1f);
|
||||
glm::vec3 maxPos = glm::vec3(0.2f, 0.2f, 0.2f);
|
||||
|
||||
//Test2
|
||||
bool m_UpdatedOnce = false;
|
||||
unsigned int m_BoxID;
|
||||
glm::vec3 m_PrevPos;
|
||||
glm::quat m_PrevOri;
|
||||
|
||||
glm::vec3 worldSize = glm::vec3(50, 50, 50);
|
||||
OctTree someOctTree;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user