Merge remote-tracking branch 'origin/master' into havok

Conflicts:
	assets
	src/GameWorld.cpp
	vs11/Returngeance/Returngeance.vcxproj.filters
This commit is contained in:
ViktorLjung
2014-05-12 14:03:02 +02:00
43 changed files with 1204 additions and 267 deletions
+1
View File
@@ -29,5 +29,6 @@ ipch/
[Dd]ebug*/
[Rr]elease*/
Ankh.NoLoad
*.orig
!libs/*.lib
+5 -1
View File
@@ -8,8 +8,12 @@ namespace Components
struct Camera : Component
{
Camera() : FOV(glm::radians(45.f)), NearClip(0.1f), FarClip(100.f) { }
Camera()
: FOV(glm::radians(45.f))
, NearClip(0.1f)
, FarClip(100.f) { }
std::string Viewport;
float FOV;
float NearClip;
float FarClip;
+14 -1
View File
@@ -1,7 +1,10 @@
#include <string>
#include <sstream>
#include "EventBroker.h"
#include "Renderer.h"
#include "InputManager.h"
#include "GUI/Frame.h"
#include "GameWorld.h"
class Engine
@@ -9,10 +12,16 @@ class Engine
public:
Engine(int argc, char* argv[])
{
m_EventBroker = std::make_shared<EventBroker>();
m_Renderer = std::make_shared<Renderer>();
m_Renderer->Initialize();
m_World = std::make_shared<GameWorld>(m_Renderer);
m_InputManager = std::make_shared<InputManager>(m_Renderer->GetWindow(), m_EventBroker);
m_UIParent = std::make_shared<GUI::Frame>(m_EventBroker);
m_World = std::make_shared<GameWorld>(m_EventBroker, m_Renderer);
m_World->Initialize();
m_LastTime = glfwGetTime();
@@ -26,6 +35,7 @@ public:
double dt = currentTime - m_LastTime;
m_LastTime = currentTime;
m_InputManager->Update(dt);
m_World->Update(dt);
m_Renderer->Draw(dt);
@@ -33,7 +43,10 @@ public:
}
private:
std::shared_ptr<EventBroker> m_EventBroker;
std::shared_ptr<Renderer> m_Renderer;
std::shared_ptr<InputManager> m_InputManager;
std::shared_ptr<GUI::Frame> m_UIParent;
// TODO: This should ultimately live in GameFrame
std::shared_ptr<GameWorld> m_World;
+29
View File
@@ -0,0 +1,29 @@
#include "PrecompiledHeader.h"
#include "EventBroker.h"
BaseEventRelay::~BaseEventRelay()
{
if (m_Broker != nullptr)
{
m_Broker->Unsubscribe(*this);
}
}
void EventBroker::Unsubscribe(BaseEventRelay &relay) // ?
{
auto itpair = m_Subscribers.equal_range(relay.m_TypeName);
for (auto it = itpair.first; it != itpair.second; ++it)
{
if (it->second == &relay)
{
m_Subscribers.erase(it);
break;
}
}
}
void EventBroker::Subscribe(BaseEventRelay &relay)
{
relay.m_Broker = this;
m_Subscribers.insert(std::make_pair(relay.m_TypeName, &relay));
}
+96
View File
@@ -0,0 +1,96 @@
#ifndef MessageRelay_h__
#define MessageRelay_h__
#include <typeinfo>
#include <functional>
#include <unordered_map>
#include <list>
#define EVENT_SUBSCRIBE_MEMBER(relay, handler) \
relay = decltype(relay)(std::bind(handler, this, std::placeholders::_1)); \
EventBroker->Subscribe(relay);
struct Event
{
protected:
Event() { }
};
class EventBroker;
class BaseEventRelay
{
friend class EventBroker;
protected:
BaseEventRelay(std::string typeName)
: m_TypeName(typeName), m_Broker(nullptr) { }
~BaseEventRelay();
public:
virtual bool Receive(const Event &event) = 0;
protected:
std::string m_TypeName;
EventBroker* m_Broker;
};
template <typename EventType>
class EventRelay : public BaseEventRelay
{
public:
typedef std::function<bool(const EventType&)> CallbackType;
EventRelay()
: m_Callback(nullptr)
, BaseEventRelay(typeid(EventType).name()) { }
EventRelay(CallbackType callback)
: m_Callback(callback)
, BaseEventRelay(typeid(EventType).name()) { }
protected:
bool Receive(const Event &event) override;
private:
CallbackType m_Callback;
};
template <typename EventType>
bool EventRelay<EventType>::Receive(const Event &event)
{
if (m_Callback != nullptr)
{
return m_Callback(static_cast<const EventType&>(event));
}
else
{
return false;
}
}
class EventBroker
{
template <typename EventType> friend class EventRelay;
public:
template <typename EventType>
void Publish(const EventType &event);
void Subscribe(BaseEventRelay &relay);
void Unsubscribe(BaseEventRelay &relay);
private:
std::unordered_multimap<std::string, BaseEventRelay*> m_Subscribers;
};
template <typename EventType>
void EventBroker::Publish(const EventType &event)
{
auto itpair = m_Subscribers.equal_range(typeid(EventType).name());
for (auto it = itpair.first; it != itpair.second; ++it)
{
it->second->Receive(event);
}
}
#endif // MessageRelay_h__
+17
View File
@@ -0,0 +1,17 @@
#ifndef Events_BindKey_h__
#define Events_BindKey_h__
#include "EventBroker.h"
namespace Events
{
struct BindKey : Event
{
int KeyCode;
std::string Command;
};
}
#endif // Events_BindKey_h__
+17
View File
@@ -0,0 +1,17 @@
#ifndef Events_BindMouseButton_h__
#define Events_BindMouseButton_h__
#include "EventBroker.h"
namespace Events
{
struct BindMouseButton : Event
{
int Button;
std::string Command;
};
}
#endif // Events_BindMouseButton_h__
+20
View File
@@ -0,0 +1,20 @@
#ifndef Events_InputCommand_h__
#define Events_InputCommand_h__
#include <boost/any.hpp>
#include "EventBroker.h"
namespace Events
{
struct InputCommand : Event
{
unsigned int PlayerID;
std::string Command;
boost::any Value;
};
}
#endif // Events_InputCommand_h__
+16
View File
@@ -0,0 +1,16 @@
#ifndef Events_KeyDown_h__
#define Events_KeyDown_h__
#include "EventBroker.h"
namespace Events
{
struct KeyDown : Event
{
int KeyCode;
};
}
#endif // Events_KeyDown_h__
+16
View File
@@ -0,0 +1,16 @@
#ifndef Events_KeyUp_h__
#define Events_KeyUp_h__
#include "EventBroker.h"
namespace Events
{
struct KeyUp : Event
{
int KeyCode;
};
}
#endif // Events_KeyUp_h__
+17
View File
@@ -0,0 +1,17 @@
#ifndef Events_MouseMove_h__
#define Events_MouseMove_h__
#include "EventBroker.h"
namespace Events
{
struct MouseMove : Event
{
double X, Y;
double DeltaX, DeltaY;
};
}
#endif // Events_MouseMove_h__
+16
View File
@@ -0,0 +1,16 @@
#ifndef Events_MousePress_h__
#define Events_MousePress_h__
#include "EventBroker.h"
namespace Events
{
struct MousePress : Event
{
int Button;
};
}
#endif // Events_MousePress_h__
+16
View File
@@ -0,0 +1,16 @@
#ifndef Events_MouseRelease_h__
#define Events_MouseRelease_h__
#include "EventBroker.h"
namespace Events
{
struct MouseRelease : Event
{
int Button;
};
}
#endif // Events_MouseRelease_h__
+17
View File
@@ -0,0 +1,17 @@
#ifndef Event_PlaySound_h__
#define Event_PlaySound_h__
#include "EventBroker.h"
namespace Events
{
struct PlaySound : Event
{
EntityID Emitter;
std::string Resource;
};
}
#endif // Event_PlaySound_h__
+49
View File
@@ -0,0 +1,49 @@
#ifndef GUI_Frame_h__
#define GUI_Frame_h__
#include <memory>
#include "Util/Rectangle.h"
#include "EventBroker.h"
namespace GUI
{
class Frame : public Rectangle
{
public:
enum class Anchor
{
Left,
Right,
Top,
Bottom
};
// Set up a base frame with an event broker
Frame(std::shared_ptr<::EventBroker> eventBroker)
: EventBroker(eventBroker)
, Rectangle()
{ Initialize(); }
// Create a frame as a child
Frame(std::shared_ptr<Frame> parent)
: Rectangle(static_cast<Rectangle>(*parent)) // Clone parent rectangle using copy constructor
{ SetParent(parent); Initialize(); }
virtual void Initialize() { }
std::shared_ptr<Frame> Parent() const { return m_Parent; }
void SetParent(std::shared_ptr<Frame> parent)
{
m_Parent = parent;
EventBroker = parent->EventBroker;
}
virtual void Update(double dt) { }
protected:
std::shared_ptr<::EventBroker> EventBroker;
std::shared_ptr<Frame> m_Parent;
};
}
#endif // GUI_Frame_h__
+21
View File
@@ -0,0 +1,21 @@
#ifndef GUI_Viewport_h__
#define GUI_Viewport_h__
#include <memory>
#include "GUI/Frame.h"
namespace GUI
{
class Viewport : public Frame
{
public:
// Create a frame as a child
Viewport(std::shared_ptr<Frame> parent)
: Frame(parent) { }
};
}
#endif // GUI_Viewport_h__
+45 -4
View File
@@ -8,9 +8,31 @@ void GameWorld::Initialize()
m_ResourceManager.Preload("Model", "Models/Placeholders/PhysicsTest/Plane.obj");
m_ResourceManager.Preload("Model", "Models/Placeholders/PhysicsTest/ArrowCube.obj");
BindKey(GLFW_KEY_W, "+forward");
BindKey(GLFW_KEY_S, "+backward");
BindKey(GLFW_KEY_A, "+left");
BindKey(GLFW_KEY_D, "+right");
BindKey(GLFW_KEY_SPACE, "+up");
BindKey(GLFW_KEY_LEFT_CONTROL, "+down");
BindKey(GLFW_KEY_LEFT_ALT, "+slow");
BindKey(GLFW_KEY_LEFT_SHIFT, "+fast");
BindMouseButton(GLFW_MOUSE_BUTTON_1, "+attack");
BindMouseButton(GLFW_MOUSE_BUTTON_2, "+attack2");
BindMouseButton(GLFW_MOUSE_BUTTON_3, "+attack3");
RegisterComponents();
{
auto camera = CreateEntity();
auto transform = AddComponent<Components::Transform>(camera, "Transform");
transform->Position.z = 20.f;
transform->Position.y = 20.f;
//transform->Orientation = glm::quat(glm::vec3(glm::pi<float>() / 8.f, 0.f, 0.f));
auto cameraComp = AddComponent<Components::Camera>(camera, "Camera");
cameraComp->FarClip = 2000.f;
auto freeSteering = AddComponent<Components::FreeSteering>(camera, "FreeSteering");
CommitEntity(camera);
}
{
@@ -513,9 +535,10 @@ void GameWorld::RegisterComponents()
void GameWorld::RegisterSystems()
{
m_SystemFactory.Register("TransformSystem", [this]() { return new Systems::TransformSystem(this); });
m_SystemFactory.Register("TransformSystem", [this]() { return new Systems::TransformSystem(this, m_EventBroker); });
//m_SystemFactory.Register("LevelGenerationSystem", [this]() { return new Systems::LevelGenerationSystem(this); });
m_SystemFactory.Register("InputSystem", [this]() { return new Systems::InputSystem(this, m_Renderer); });
m_SystemFactory.Register("InputSystem", [this]() { return new Systems::InputSystem(this, m_EventBroker); });
m_SystemFactory.Register("DebugSystem", [this]() { return new Systems::DebugSystem(this, m_EventBroker); });
//m_SystemFactory.Register("CollisionSystem", [this]() { return new Systems::CollisionSystem(this); });
////m_SystemFactory.Register("ParticleSystem", [this]() { return new Systems::ParticleSystem(this); });
//m_SystemFactory.Register("PlayerSystem", [this]() { return new Systems::PlayerSystem(this); });
@@ -523,6 +546,7 @@ void GameWorld::RegisterSystems()
m_SystemFactory.Register("SoundSystem", [this]() { return new Systems::SoundSystem(this); });
m_SystemFactory.Register("PhysicsSystem", [this]() { return new Systems::PhysicsSystem(this); });
m_SystemFactory.Register("RenderSystem", [this]() { return new Systems::RenderSystem(this, m_Renderer); });
m_SystemFactory.Register("FreeSteeringSystem", [this]() { return new Systems::FreeSteeringSystem(this, m_EventBroker); });
}
void GameWorld::AddSystems()
@@ -530,6 +554,7 @@ void GameWorld::AddSystems()
AddSystem("TransformSystem");
//AddSystem("LevelGenerationSystem");
AddSystem("InputSystem");
AddSystem("DebugSystem");
//AddSystem("CollisionSystem");
////AddSystem("ParticleSystem");
//AddSystem("PlayerSystem");
@@ -537,4 +562,20 @@ void GameWorld::AddSystems()
AddSystem("SoundSystem");
AddSystem("PhysicsSystem");
AddSystem("RenderSystem");
}
}
void GameWorld::BindKey(int keyCode, std::string command)
{
Events::BindKey e;
e.KeyCode = keyCode;
e.Command = command;
m_EventBroker->Publish(e);
}
void GameWorld::BindMouseButton(int button, std::string command)
{
Events::BindMouseButton e;
e.Button = button;
e.Command = command;
m_EventBroker->Publish(e);
}
+6 -2
View File
@@ -7,6 +7,7 @@
#include "Systems/TransformSystem.h"
//#include "Systems/CollisionSystem.h"
#include "Systems/InputSystem.h"
#include "Systems/DebugSystem.h"
//#include "Systems/LevelGenerationSystem.h"
//#include "Systems/ParticleSystem.h"
//#include "Systems/PlayerSystem.h"
@@ -36,8 +37,8 @@
class GameWorld : public World
{
public:
GameWorld(std::shared_ptr<Renderer> renderer)
: m_Renderer(renderer), World() { }
GameWorld(std::shared_ptr<::EventBroker> eventBroker, std::shared_ptr<Renderer> renderer)
: World(eventBroker), m_Renderer(renderer) { }
void Initialize();
@@ -49,6 +50,9 @@ public:
private:
std::shared_ptr<Renderer> m_Renderer;
void BindKey(int keyCode, std::string command);
void BindMouseButton(int button, std::string command);
};
#endif // GameWorld_h__
+33
View File
@@ -0,0 +1,33 @@
#ifndef InputController_h__
#define InputController_h__
#include <memory>
#include "EventBroker.h"
#include "Events/InputCommand.h"
#include "Events/MouseMove.h"
class InputController
{
public:
InputController(std::shared_ptr<::EventBroker> eventBroker)
: EventBroker(eventBroker) { Initialize(); }
virtual void Initialize()
{
EVENT_SUBSCRIBE_MEMBER(m_EInputCommand, &InputController::OnCommand);
EVENT_SUBSCRIBE_MEMBER(m_EMouseMove, &InputController::OnMouseMove);
}
virtual bool OnCommand(const Events::InputCommand &event) { return false; }
virtual bool OnMouseMove(const Events::MouseMove &event) { return false; }
protected:
std::shared_ptr<::EventBroker> EventBroker;
private:
EventRelay<Events::InputCommand> m_EInputCommand;
EventRelay<Events::MouseMove> m_EMouseMove;
};
#endif // InputController_h__
+86
View File
@@ -0,0 +1,86 @@
#include "PrecompiledHeader.h"
#include "InputManager.h"
void InputManager::Update(double dt)
{
m_LastKeyState = m_CurrentKeyState;
m_LastMouseState = m_CurrentMouseState;
m_LastMouseX = m_CurrentMouseX;
m_LastMouseY = m_CurrentMouseY;
// Keyboard input
for (int i = 0; i <= GLFW_KEY_LAST; ++i)
{
m_CurrentKeyState[i] = glfwGetKey(m_GLFWWindow, i);
if (m_CurrentKeyState[i] != m_LastKeyState[i])
{
// Publish key events
if (m_CurrentKeyState[i])
{
Events::KeyDown e;
e.KeyCode = i;
m_EventBroker->Publish<Events::KeyDown>(e);
}
else
{
Events::KeyUp e;
e.KeyCode = i;
m_EventBroker->Publish<Events::KeyUp>(e);
}
}
}
// Mouse buttons
for (int i = 0; i <= GLFW_MOUSE_BUTTON_LAST; ++i)
{
m_CurrentMouseState[i] = glfwGetMouseButton(m_GLFWWindow, i);
if (m_CurrentMouseState[i] != m_LastMouseState[i])
{
// Publish mouse button events
if (m_CurrentMouseState[i])
{
Events::MousePress e;
e.Button = i;
m_EventBroker->Publish<Events::MousePress>(e);
}
else
{
Events::MouseRelease e;
e.Button = i;
m_EventBroker->Publish<Events::MouseRelease>(e);
}
}
}
// Cursor position
glfwGetCursorPos(m_GLFWWindow, &m_CurrentMouseX, &m_CurrentMouseY);
m_CurrentMouseDeltaX = m_CurrentMouseX - m_LastMouseX;
m_CurrentMouseDeltaY = m_CurrentMouseY - m_LastMouseY;
if (m_CurrentMouseDeltaX != 0 || m_CurrentMouseDeltaY != 0)
{
// Publish mouse move events
Events::MouseMove e;
e.X = m_CurrentMouseX;
e.Y = m_CurrentMouseY;
e.DeltaX = m_CurrentMouseDeltaX;
e.DeltaY = m_CurrentMouseDeltaY;
m_EventBroker->Publish<Events::MouseMove>(e);
}
// // Lock mouse while holding LMB
// if (m_CurrentMouseState[GLFW_MOUSE_BUTTON_LEFT])
// {
// m_LastMouseX = m_Renderer->Width() / 2.f; // xpos;
// m_LastMouseY = m_Renderer->Height() / 2.f; // ypos;
// glfwSetCursorPos(m_GLFWWindow, m_LastMouseX, m_LastMouseY);
// }
// // Hide/show cursor with LMB
// if (m_CurrentMouseState[GLFW_MOUSE_BUTTON_LEFT] && !m_LastMouseState[GLFW_MOUSE_BUTTON_LEFT])
// {
// glfwSetInputMode(m_GLFWWindow, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
// }
// if (!m_CurrentMouseState[GLFW_MOUSE_BUTTON_LEFT] && m_LastMouseState[GLFW_MOUSE_BUTTON_LEFT])
// {
// glfwSetInputMode(m_GLFWWindow, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
// }
}
+42
View File
@@ -0,0 +1,42 @@
#ifndef InputManager_h__
#define InputManager_h__
#include <array>
#include "EventBroker.h"
#include "Events/KeyDown.h"
#include "Events/KeyUp.h"
#include "Events/MousePress.h"
#include "Events/MouseRelease.h"
#include "Events/MouseMove.h"
class InputManager
{
public:
InputManager(GLFWwindow* window, std::shared_ptr<EventBroker> eventBroker)
: m_GLFWWindow(window)
, m_EventBroker(eventBroker)
, m_CurrentKeyState()
, m_LastKeyState()
, m_CurrentMouseState()
, m_LastMouseState()
, m_CurrentMouseX(0), m_CurrentMouseY(0)
, m_LastMouseX(0), m_LastMouseY(0)
, m_CurrentMouseDeltaX(0), m_CurrentMouseDeltaY(0) { }
void Update(double dt);
private:
GLFWwindow* m_GLFWWindow;
std::shared_ptr<EventBroker> m_EventBroker;
std::array<int, GLFW_KEY_LAST+1> m_CurrentKeyState;
std::array<int, GLFW_KEY_LAST+1> m_LastKeyState;
std::array<int, GLFW_MOUSE_BUTTON_LAST+1> m_CurrentMouseState;
std::array<int, GLFW_MOUSE_BUTTON_LAST+1> m_LastMouseState;
double m_CurrentMouseX, m_CurrentMouseY;
double m_LastMouseX, m_LastMouseY;
double m_CurrentMouseDeltaX, m_CurrentMouseDeltaY;
};
#endif // InputManager_h__
+62
View File
@@ -0,0 +1,62 @@
#ifndef RenderQueue_h__
#define RenderQueue_h__
#include <cstdint>
#include <forward_list>
#include "ResourceManager.h"
#include "Texture.h"
#include "Model.h"
class RenderQueue;
struct RenderJob
{
friend class RenderQueue;
unsigned int ViewportID;
unsigned int TextureID;
GLuint DiffuseTexture;
GLuint NormalTexture;
GLuint SpecularTexture;
GLuint VAO;
unsigned int StartIndex;
unsigned int EndIndex;
glm::mat4 ModelMatrix;
protected:
uint64_t Hash;
void CalculateHash()
{
Hash = ViewportID << 58 // 6 bits
| TextureID << 42; // 16 bits
}
bool operator<(const RenderJob& rhs)
{
return this->Hash < rhs.Hash;
}
};
class RenderQueue
{
public:
void Add(RenderJob &job)
{
job.CalculateHash();
m_Jobs.push_front(job);
m_Jobs.sort();
}
void Clear()
{
m_Jobs.clear();
}
private:
std::forward_list<RenderJob> m_Jobs;
};
#endif // RenderQueue_h__
+6 -6
View File
@@ -31,11 +31,11 @@ void Renderer::Initialize()
}
// Create a window
WIDTH = 1280;
HEIGHT = 720;
m_Width = 1280;
m_Height = 720;
// Antialiasing
//glfwWindowHint(GLFW_SAMPLES, 16);
m_Window = glfwCreateWindow(WIDTH, HEIGHT, "OpenGL", nullptr, nullptr);
m_Window = glfwCreateWindow(m_Width, m_Height, "OpenGL", nullptr, nullptr);
if (!m_Window)
{
LOG_ERROR("GLFW: Failed to create window");
@@ -63,7 +63,7 @@ void Renderer::Initialize()
}
// Create Camera
m_Camera = std::make_shared<Camera>(45.f, (float)WIDTH / HEIGHT, 0.01f, 1000.f);
m_Camera = std::make_shared<Camera>(45.f, (float)m_Width / m_Height, 0.01f, 1000.f);
m_Camera->Position(glm::vec3(0.0f, 0.0f, 2.f));
glfwSwapInterval(m_VSync);
@@ -187,7 +187,7 @@ void Renderer::Draw(double dt)
void Renderer::DrawSkybox()
{
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0, 0, WIDTH, HEIGHT);
glViewport(0, 0, m_Width, m_Height);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
m_ShaderProgramSkybox.Bind();
@@ -200,7 +200,7 @@ void Renderer::DrawSkybox()
void Renderer::DrawScene()
{
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0, 0, WIDTH, HEIGHT);
glViewport(0, 0, m_Width, m_Height);
glClear(GL_DEPTH_BUFFER_BIT);
//glClearColor(1.0f, 1.0f, 0.0f, 1.0f);
+4 -3
View File
@@ -20,7 +20,9 @@ public:
glm::mat4 viewMatrix;
glm::mat4 projectionMatrix;
int HEIGHT, WIDTH;
int Width() const { return m_Width; }
int Height() const { return m_Height; }
std::list<std::tuple<Model*, glm::mat4, bool, bool>> ModelsToRender;
int Lights;
@@ -65,9 +67,8 @@ public:
void DrawBounds(bool val) { m_DrawBounds = val; }
void DrawSkybox();
private:
int m_Width, m_Height;
GLFWwindow* m_Window;
GLint m_glVersion[2];
GLchar* m_glVendor;
+5 -1
View File
@@ -4,6 +4,7 @@
#include "Factory.h"
#include "Entity.h"
#include "Component.h"
#include "EventBroker.h"
#include "ResourceManager.h"
class World;
@@ -11,7 +12,9 @@ class World;
class System
{
public:
System(World* world) : m_World(world) { }
System(World* world, std::shared_ptr<EventBroker> eventBroker)
: m_World(world)
, EventBroker(eventBroker) { }
virtual ~System() { }
virtual void RegisterComponents(ComponentFactory* cf) { }
@@ -33,6 +36,7 @@ public:
protected:
World* m_World;
std::shared_ptr<EventBroker> EventBroker;
};
class SystemFactory : public Factory<System*> { };
+27
View File
@@ -2,3 +2,30 @@
#include "DebugSystem.h"
#include "World.h"
void Systems::DebugSystem::Initialize()
{
// Subscribe to events
m_EKeyDown = decltype(m_EKeyDown)(std::bind(&Systems::DebugSystem::OnKeyDown, this, std::placeholders::_1));
EventBroker->Subscribe(m_EKeyDown);
}
void Systems::DebugSystem::Update(double dt)
{
}
bool Systems::DebugSystem::OnKeyDown(const Events::KeyDown &event)
{
if (event.KeyCode == GLFW_KEY_ENTER)
{
Events::PlaySound e;
e.Emitter = 0;
e.Resource = "Sounds/korvring.wav";
EventBroker->Publish<Events::PlaySound>(e);
return true;
}
return false;
}
+10 -2
View File
@@ -3,6 +3,8 @@
#include "System.h"
#include "Components/Transform.h"
#include "Events/KeyDown.h"
#include "Events/PlaySound.h"
namespace Systems
{
@@ -10,10 +12,16 @@ namespace Systems
class DebugSystem : public System
{
public:
DebugSystem(World* world)
: System(world) { }
DebugSystem(World* world, std::shared_ptr<::EventBroker> eventBroker)
: System(world, eventBroker) { }
void Initialize() override;
void Update(double dt) override;
EventRelay<Events::KeyDown> m_EKeyDown;
bool OnKeyDown(const Events::KeyDown &event);
//void UpdateEntity(double dt, EntityID entity, EntityID parent) override;
};
+112 -50
View File
@@ -7,65 +7,127 @@ void Systems::FreeSteeringSystem::RegisterComponents(ComponentFactory* cf)
cf->Register("FreeSteering", []() { return new Components::FreeSteering(); });
}
void Systems::FreeSteeringSystem::Initialize()
{
m_InputController = std::unique_ptr<FreeSteeringInputController>(new FreeSteeringInputController(EventBroker));
}
void Systems::FreeSteeringSystem::Update(double dt)
{
}
void Systems::FreeSteeringSystem::UpdateEntity(double dt, EntityID entity, EntityID parent)
{
auto steering = m_World->GetComponent<Components::FreeSteering>(entity, "FreeSteering");
auto input = m_World->GetComponent<Components::Input>(entity, "Input");
if (steering && input)
if (steering)
{
auto transform = m_World->GetComponent<Components::Transform>(entity, "Transform");
glm::vec3 Camera_Right = glm::vec3(transform->Orientation * glm::vec4(1, 0, 0, 0));
glm::vec3 Camera_Forward = glm::vec3(transform->Orientation * glm::vec4(0, 0, -1, 0));
float speed = steering->Speed;
if (input->KeyState[GLFW_KEY_LEFT_SHIFT])
{
speed *= 4.0f;
}
if (input->KeyState[GLFW_KEY_LEFT_ALT])
{
speed /= 4.0f;
}
if (input->KeyState[GLFW_KEY_A])
{
transform->Position -= Camera_Right * (float)dt * speed;
}
else if (input->KeyState[GLFW_KEY_D])
{
transform->Position += Camera_Right * (float)dt * speed;
}
if (input->KeyState[GLFW_KEY_W])
{
transform->Position += Camera_Forward * (float)dt * speed;
}
if (input->KeyState[GLFW_KEY_S])
{
transform->Position -= Camera_Forward * (float)dt * speed;
}
if (input->KeyState[GLFW_KEY_SPACE])
{
transform->Position += glm::vec3(0, 1, 0) * (float)dt * speed;
}
if (input->KeyState[GLFW_KEY_LEFT_CONTROL])
{
transform->Position -= glm::vec3(0, 1, 0) * (float)dt * speed;
}
if (input->MouseState[GLFW_MOUSE_BUTTON_LEFT])
{
// TOUCHING THIS CODE MIGHT CAUSE THE UNIVERSE TO IMPLODE, ALSO DRAGONS // spelling tobias :3
//---------------------------------------------------------------------
transform->Orientation = glm::angleAxis<float>(input->dX / 300.f, glm::vec3(0, -1, 0)) * transform->Orientation;
transform->Orientation = transform->Orientation * glm::angleAxis<float>(input->dY / 300.f, glm::vec3(-1, 0, 0));
//---------------------------------------------------------------------
// TOUCHING THIS CODE MIGHT CAUSE THE UNIVERSE TO IMPLODE, ALSO DRAGONS
}
glm::vec3 cameraRight = glm::vec3(m_InputController->Orientation * glm::vec4(1, 0, 0, 0));
glm::vec3 cameraForward = glm::vec3(m_InputController->Orientation * glm::vec4(0, 0, -1, 0));
glm::vec3 movement;
movement += cameraRight * m_InputController->Movement.x;
movement.y += m_InputController->Movement.y;
movement += cameraForward * -m_InputController->Movement.z;
transform->Position += movement * steering->Speed * m_InputController->SpeedMultiplier * (float)dt;
transform->Orientation = m_InputController->Orientation;
}
}
bool Systems::FreeSteeringSystem::FreeSteeringInputController::OnCommand(const Events::InputCommand &event)
{
// Movement
if (event.Command == "+forward")
{
Movement.z += -1.f;
}
else if (event.Command == "-forward")
{
Movement.z -= -1.f;
}
else if (event.Command == "+backward")
{
Movement.z += 1.f;
}
else if (event.Command == "-backward")
{
Movement.z -= 1.f;
}
else if (event.Command == "+right")
{
Movement.x += 1.f;
}
else if (event.Command == "-right")
{
Movement.x -= 1.f;
}
else if (event.Command == "+left")
{
Movement.x += -1.f;
}
else if (event.Command == "-left")
{
Movement.x -= -1.f;
}
else if (event.Command == "+up")
{
Movement.y += 1.f;
}
else if (event.Command == "-up")
{
Movement.y -= 1.f;
}
else if (event.Command == "+down")
{
Movement.y += -1.f;
}
else if (event.Command == "-down")
{
Movement.y -= -1.f;
}
// Speed
else if (event.Command == "+fast")
{
SpeedMultiplier *= 4.f;
}
else if (event.Command == "-fast")
{
SpeedMultiplier /= 4.f;
}
else if (event.Command == "+slow")
{
SpeedMultiplier /= 4.f;
}
else if (event.Command == "-slow")
{
SpeedMultiplier *= 4.f;
}
// Mouse click
else if (event.Command == "+attack")
{
OrientationActive = true;
}
else if (event.Command == "-attack")
{
OrientationActive = false;
}
return true;
}
bool Systems::FreeSteeringSystem::FreeSteeringInputController::OnMouseMove(const Events::MouseMove &event)
{
if (OrientationActive)
{
// TOUCHING THIS CODE MIGHT CAUSE THE UNIVERSE TO IMPLODE, ALSO DRAGONS
//---------------------------------------------------------------------
Orientation = glm::angleAxis<float>(event.DeltaX / 300.f, glm::vec3(0, -1, 0)) * Orientation * glm::angleAxis<float>(event.DeltaY / 300.f, glm::vec3(-1, 0, 0));
//---------------------------------------------------------------------
// TOUCHING THIS CODE MIGHT CAUSE THE UNIVERSE TO IMPLODE, ALSO DRAGONS
}
return true;
}
+30 -3
View File
@@ -2,19 +2,46 @@
#include "System.h"
#include "Components/Transform.h"
#include "Components/Input.h"
#include "Components/FreeSteering.h"
#include "InputController.h"
namespace Systems
{
class FreeSteeringSystem : public System
{
public:
FreeSteeringSystem(World* world)
: System(world) { }
FreeSteeringSystem(World* world, std::shared_ptr<::EventBroker> eventBroker)
: System(world, eventBroker) { }
void RegisterComponents(ComponentFactory* cf) override;
void Initialize() override;
void Update(double dt) override;
void UpdateEntity(double dt, EntityID entity, EntityID parent) override;
private:
class FreeSteeringInputController;
std::unique_ptr<FreeSteeringInputController> m_InputController;
};
class FreeSteeringSystem::FreeSteeringInputController : InputController
{
public:
FreeSteeringInputController(std::shared_ptr<::EventBroker> eventBroker)
: InputController(eventBroker)
, SpeedMultiplier(1.f)
, OrientationActive(false) { }
glm::vec3 Movement;
glm::quat Orientation;
float SpeedMultiplier;
bool OrientationActive;
protected:
virtual bool OnCommand(const Events::InputCommand &event);
virtual bool OnMouseMove(const Events::MouseMove &event);
};
}
+112 -69
View File
@@ -7,80 +7,123 @@ void Systems::InputSystem::RegisterComponents(ComponentFactory* cf)
cf->Register("Input", []() { return new Components::Input(); });
}
void Systems::InputSystem::Initialize()
{
// Subscribe to events
EVENT_SUBSCRIBE_MEMBER(m_EKeyDown, &Systems::InputSystem::OnKeyDown)
EVENT_SUBSCRIBE_MEMBER(m_EKeyUp, &Systems::InputSystem::OnKeyUp)
EVENT_SUBSCRIBE_MEMBER(m_EMousePress, &Systems::InputSystem::OnMousePress)
EVENT_SUBSCRIBE_MEMBER(m_EMouseRelease, &Systems::InputSystem::OnMouseRelease)
EVENT_SUBSCRIBE_MEMBER(m_EBindKey, &Systems::InputSystem::OnBindKey)
EVENT_SUBSCRIBE_MEMBER(m_EBindMouseButton, &Systems::InputSystem::OnBindMouseButton)
}
void Systems::InputSystem::Update(double dt)
{
m_LastKeyState = m_CurrentKeyState;
m_LastMouseState = m_CurrentMouseState;
// Keyboard input
for (int i = 0; i <= GLFW_KEY_LAST; ++i)
{
m_CurrentKeyState[i] = glfwGetKey(m_Renderer->GetWindow(), i);
}
// Mouse buttons
for (int i = 0; i <= GLFW_MOUSE_BUTTON_LAST; ++i)
{
m_CurrentMouseState[i] = glfwGetMouseButton(m_Renderer->GetWindow(), i);
}
// Cursor position
double xpos, ypos;
glfwGetCursorPos(m_Renderer->GetWindow(), &xpos, &ypos);
m_CurrentMouseDeltaX = xpos - m_LastMouseX;
m_CurrentMouseDeltaY = ypos - m_LastMouseY;
m_LastMouseX = xpos;
m_LastMouseY = ypos;
// Lock mouse while holding LMB
if (m_CurrentMouseState[GLFW_MOUSE_BUTTON_LEFT])
{
m_LastMouseX = m_Renderer->WIDTH / 2.f; // xpos;
m_LastMouseY = m_Renderer->HEIGHT / 2.f; // ypos;
glfwSetCursorPos(m_Renderer->GetWindow(), m_LastMouseX, m_LastMouseY);
}
// Hide/show cursor with LMB
if (m_CurrentMouseState[GLFW_MOUSE_BUTTON_LEFT] && !m_LastMouseState[GLFW_MOUSE_BUTTON_LEFT])
{
glfwSetInputMode(m_Renderer->GetWindow(), GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
}
if (!m_CurrentMouseState[GLFW_MOUSE_BUTTON_LEFT] && m_LastMouseState[GLFW_MOUSE_BUTTON_LEFT])
{
glfwSetInputMode(m_Renderer->GetWindow(), GLFW_CURSOR, GLFW_CURSOR_NORMAL);
}
#ifdef DEBUG
// Wireframe
if (m_CurrentKeyState[GLFW_KEY_F1] && !m_LastKeyState[GLFW_KEY_F1])
{
m_Renderer->DrawWireframe(!m_Renderer->DrawWireframe());
}
// Normals
if (m_CurrentKeyState[GLFW_KEY_F2] && !m_LastKeyState[GLFW_KEY_F2])
{
m_Renderer->DrawNormals(!m_Renderer->DrawNormals());
}
// Bounds
if (m_CurrentKeyState[GLFW_KEY_F3] && !m_LastKeyState[GLFW_KEY_F3])
{
m_Renderer->DrawBounds(!m_Renderer->DrawBounds());
}
#endif
// #ifdef DEBUG
// // Wireframe
// if (m_CurrentKeyState[GLFW_KEY_F1] && !m_LastKeyState[GLFW_KEY_F1])
// {
// m_Renderer->DrawWireframe(!m_Renderer->DrawWireframe());
// }
// // Normals
// if (m_CurrentKeyState[GLFW_KEY_F2] && !m_LastKeyState[GLFW_KEY_F2])
// {
// m_Renderer->DrawNormals(!m_Renderer->DrawNormals());
// }
// // Bounds
// if (m_CurrentKeyState[GLFW_KEY_F3] && !m_LastKeyState[GLFW_KEY_F3])
// {
// m_Renderer->DrawBounds(!m_Renderer->DrawBounds());
// }
// #endif
}
void Systems::InputSystem::UpdateEntity(double dt, EntityID entity, EntityID parent)
bool Systems::InputSystem::OnKeyDown(const Events::KeyDown &event)
{
auto input = m_World->GetComponent<Components::Input>(entity, "Input");
if (input == nullptr)
return;
auto bindingIt = m_KeyBindings.find(event.KeyCode);
if (bindingIt != m_KeyBindings.end())
{
PublishCommand(0, bindingIt->second, false);
}
input->KeyState = m_CurrentKeyState;
input->LastKeyState = m_LastKeyState;
input->MouseState = m_CurrentMouseState;
input->LastMouseState = m_LastMouseState;
input->dX = m_CurrentMouseDeltaX;
input->dY = m_CurrentMouseDeltaY;
return true;
}
std::array<int, GLFW_KEY_LAST+1> Systems::InputSystem::m_CurrentKeyState;
std::array<int, GLFW_KEY_LAST+1> Systems::InputSystem::m_LastKeyState;
bool Systems::InputSystem::OnKeyUp(const Events::KeyUp &event)
{
auto bindingIt = m_KeyBindings.find(event.KeyCode);
if (bindingIt != m_KeyBindings.end())
{
PublishCommand(0, bindingIt->second, true);
}
return true;
}
bool Systems::InputSystem::OnMousePress(const Events::MousePress &event)
{
auto bindingIt = m_MouseButtonBindings.find(event.Button);
if (bindingIt != m_MouseButtonBindings.end())
{
PublishCommand(0, bindingIt->second, false);
}
return true;
}
bool Systems::InputSystem::OnMouseRelease(const Events::MouseRelease &event)
{
auto bindingIt = m_MouseButtonBindings.find(event.Button);
if (bindingIt != m_MouseButtonBindings.end())
{
PublishCommand(0, bindingIt->second, true);
}
return true;
}
bool Systems::InputSystem::OnBindKey(const Events::BindKey &event)
{
if (event.Command.empty())
{
m_KeyBindings.erase(event.KeyCode);
}
else
{
m_KeyBindings[event.KeyCode] = event.Command;
LOG_DEBUG("Input: Bound key %c to %s", (char)event.KeyCode, event.Command.c_str());
}
return true;
}
bool Systems::InputSystem::OnBindMouseButton(const Events::BindMouseButton &event)
{
if (event.Command.empty())
{
m_MouseButtonBindings.erase(event.Button);
}
else
{
m_MouseButtonBindings[event.Button] = event.Command;
LOG_DEBUG("Input: Bound mouse button %i to %s", event.Button, event.Command.c_str());
}
return true;
}
void Systems::InputSystem::PublishCommand(int playerID, std::string command, bool release /*= false*/)
{
if (release && command.at(0) == '+')
{
command[0] = '-';
}
Events::InputCommand e;
e.PlayerID = playerID;
e.Command = command;
EventBroker->Publish(e);
LOG_DEBUG("Input: Published command %s for player %i", e.Command.c_str(), playerID);
}
+31 -11
View File
@@ -2,10 +2,17 @@
#define InputSystem_h__
#include <array>
#include <unordered_map>
#include "System.h"
#include "Renderer.h"
#include "Components/Input.h"
#include "Events/KeyUp.h"
#include "Events/KeyDown.h"
#include "Events/MousePress.h"
#include "Events/MouseRelease.h"
#include "Events/BindKey.h"
#include "Events/BindMouseButton.h"
#include "Events/InputCommand.h"
namespace Systems
{
@@ -13,22 +20,35 @@ namespace Systems
class InputSystem : public System
{
public:
InputSystem(World* world, std::shared_ptr<Renderer> renderer)
: System(world), m_Renderer(renderer) { }
InputSystem(World* world, std::shared_ptr<::EventBroker> eventBroker)
: System(world, eventBroker) { }
void RegisterComponents(ComponentFactory* cf) override;
void Initialize() override;
void Update(double dt) override;
void UpdateEntity(double dt, EntityID entity, EntityID parent) override;
private:
std::shared_ptr<Renderer> m_Renderer;
static std::array<int, GLFW_KEY_LAST+1> m_CurrentKeyState;
static std::array<int, GLFW_KEY_LAST+1> m_LastKeyState;
std::array<int, GLFW_MOUSE_BUTTON_LAST+1> m_CurrentMouseState;
std::array<int, GLFW_MOUSE_BUTTON_LAST+1> m_LastMouseState;
float m_CurrentMouseDeltaX, m_CurrentMouseDeltaY;
float m_LastMouseX, m_LastMouseY;
// Input binding tables
std::unordered_map<int, std::string> m_KeyBindings; // GLFW_KEY... -> command string
std::unordered_map<int, std::string> m_MouseButtonBindings; // GLFW_MOUSE_BUTTON... -> command string
// Input events
EventRelay<Events::KeyDown> m_EKeyDown;
bool OnKeyDown(const Events::KeyDown &event);
EventRelay<Events::KeyUp> m_EKeyUp;
bool OnKeyUp(const Events::KeyUp &event);
EventRelay<Events::MousePress> m_EMousePress;
bool OnMousePress(const Events::MousePress &event);
EventRelay<Events::MouseRelease> m_EMouseRelease;
bool OnMouseRelease(const Events::MouseRelease &event);
// Input binding events
EventRelay<Events::BindKey> m_EBindKey;
bool OnBindKey(const Events::BindKey &event);
EventRelay<Events::BindMouseButton> m_EBindMouseButton;
bool OnBindMouseButton(const Events::BindMouseButton &event);
void PublishCommand(int playerID, std::string command, bool release = false);
};
}
+1 -1
View File
@@ -25,7 +25,7 @@
#include "PhysicsSystem.h"
#include "World.h"
Systems::PhysicsSystem::PhysicsSystem(World* world) : System(world)
void Systems::PhysicsSystem::Initialize()
{
m_Accumulator = 0;
+4 -1
View File
@@ -63,8 +63,11 @@ namespace Systems
class PhysicsSystem : public System
{
public:
PhysicsSystem(World* world);
PhysicsSystem(World* world, std::shared_ptr<::EventBroker> eventBroker)
: System(world, eventBroker) { }
void RegisterComponents(ComponentFactory* cf) override;
void Initialize() override;
void Update(double dt) override;
void UpdateEntity(double dt, EntityID entity, EntityID parent) override;
-91
View File
@@ -1,91 +0,0 @@
#ifndef PhysicsSystem_h__
#define PhysicsSystem_h__
#include "System.h"
#include "Components/Transform.h"
#include "Components/Physics.h"
#include "Components/Sphere.h"
#include "Components/Box.h"
#include "Components/Vehicle.h"
#include "Components/Input.h"
// Math and base include
#include <Common/Base/hkBase.h>
#include <Common/Base/Memory/System/Util/hkMemoryInitUtil.h>
#include <Common/Base/System/Error/hkDefaultError.h>
#include <Common/Base/Monitor/hkMonitorStream.h>
#include <Common/Base/Config/hkConfigVersion.h>
#include <Common/Base/Memory/System/hkMemorySystem.h>
#include <Common/Base/Memory/Allocator/Malloc/hkMallocAllocator.h>
#include <Common/Base/Container/String/hkStringBuf.h>
// Dynamics includes
#include <Physics2012/Collide/hkpCollide.h>
#include <Physics2012/Collide/Agent/ConvexAgent/SphereBox/hkpSphereBoxAgent.h>
#include <Physics2012/Collide/Shape/Convex/Box/hkpBoxShape.h>
#include <Physics2012/Collide/Shape/Convex/Sphere/hkpSphereShape.h>
#include <Physics2012/Collide/Dispatch/hkpAgentRegisterUtil.h>
#include <Physics2012/Dynamics/World/hkpWorld.h>
#include <Physics2012/Dynamics/Entity/hkpRigidBody.h>
#include <Physics2012/Utilities/Dynamics/Inertia/hkpInertiaTensorComputer.h>
// Visual Debugger includes
#include <Common/Visualize/hkVisualDebugger.h>
#include <Physics2012/Utilities/VisualDebugger/hkpPhysicsContext.h>
#include "Physics/VehicleSetup.h"
#include <unordered_map>
namespace Systems
{
class PhysicsSystem : public System
{
public:
PhysicsSystem(World* world);
void RegisterComponents(ComponentFactory* cf) override;
void Update(double dt) override;
void UpdateEntity(double dt, EntityID entity, EntityID parent) override;
void OnComponentCreated(std::string type, std::shared_ptr<Component> component) override;
void OnComponentRemoved(std::string type, Component* component) override;
void OnEntityCommit(EntityID entity) override;
private:
<<<<<<< HEAD
=======
>>>>>>> havok
double m_Accumulator;
hkpWorld* m_PhysicsWorld;
void SetUpPhysicsState(EntityID entity, EntityID parent);
void TearDownPhysicsState(EntityID entity, EntityID parent);
hkVisualDebugger* m_VisualDebugger;
void SetupVisualDebugger(hkpPhysicsContext* worlds);
void StepVisualDebugger();
static void HK_CALL HavokErrorReport(const char* msg, void*);
void SetupPhysics(hkpWorld* physicsWorld);
std::unordered_map<EntityID, hkpRigidBody*> m_RigidBodies;
std::unordered_map<EntityID, hkpVehicleInstance*> m_Vehicles;
std::vector<EntityID> m_Wheels;
hkpVehicleInstance* Systems::PhysicsSystem::createVehicle(VehicleSetup& vehicleSetup, hkpRigidBody* chassis);
};
}
#endif // PhysicsSystem_h__
+3 -2
View File
@@ -24,8 +24,9 @@ namespace Systems
class RenderSystem : public System
{
public:
RenderSystem(World* world, std::shared_ptr<Renderer> renderer)
: System(world), m_Renderer(renderer) { }
RenderSystem(World* world, std::shared_ptr<::EventBroker> eventBroker, std::shared_ptr<Renderer> renderer)
: System(world, eventBroker)
, m_Renderer(renderer) { }
void RegisterComponents(ComponentFactory* cf) override;
void RegisterResourceTypes(ResourceManager* rm) override;
+17 -2
View File
@@ -2,8 +2,7 @@
#include "SoundSystem.h"
#include "World.h"
Systems::SoundSystem::SoundSystem(World* world)
: System(world)
void Systems::SoundSystem::Initialize()
{
//initialize OpenAL
ALCdevice* Device = alcOpenDevice(NULL);
@@ -22,6 +21,10 @@ Systems::SoundSystem::SoundSystem(World* world)
alSpeedOfSound(340.29f); // Speed of sound
alDistanceModel(AL_INVERSE_DISTANCE_CLAMPED);
// Subscribe to events
m_EPlaySound = decltype(m_EPlaySound)(std::bind(&Systems::SoundSystem::OnPlaySound, this, std::placeholders::_1));
EventBroker->Subscribe(m_EPlaySound);
}
void Systems::SoundSystem::RegisterComponents(ComponentFactory* cf)
@@ -143,3 +146,15 @@ ALuint Systems::SoundSystem::CreateSource()
return source;
}
bool Systems::SoundSystem::OnPlaySound(const Events::PlaySound &event)
{
LOG_DEBUG("Events::PlaySound.Resource = %s", event.Resource.c_str());
ALuint buffer = *m_World->GetResourceManager()->Load<Sound>("Sound", event.Resource);
ALuint source = m_Sources.begin()->second;
alSourcei(source, AL_BUFFER, buffer);
alSourcePlay(source);
return true;
}
+9 -1
View File
@@ -7,6 +7,7 @@
#include "System.h"
#include "Components/Transform.h"
#include "Components/SoundEmitter.h"
#include "Events/PlaySound.h"
#include "Sound.h"
namespace Systems
@@ -15,9 +16,12 @@ namespace Systems
class SoundSystem : public System
{
public:
SoundSystem(World* world);
SoundSystem(World* world, std::shared_ptr<::EventBroker> eventBroker)
: System(world, eventBroker) { }
void RegisterComponents(ComponentFactory* cf) override;
void RegisterResourceTypes(ResourceManager* rm) override;
void Initialize() override;
void Update(double dt) override;
void UpdateEntity(double dt, EntityID entity, EntityID parent) override;
@@ -39,6 +43,10 @@ private:
//short bytesPerSample, bitsPerSample;
//unsigned long dataSize;
// Events
EventRelay<Events::PlaySound> m_EPlaySound;
bool OnPlaySound(const Events::PlaySound &event);
std::map<Component*, ALuint> m_Sources;
std::map<std::string, ALuint> m_BufferCache; // string = fileName
};
+2 -3
View File
@@ -10,9 +10,8 @@ namespace Systems
class TransformSystem : public System
{
public:
TransformSystem(World* world)
: System(world) { }
TransformSystem(World* world, std::shared_ptr<::EventBroker> eventBroker)
: System(world, eventBroker) { }
//void Update(double dt) override;
//void UpdateEntity(double dt, EntityID entity, EntityID parent) override;
+95
View File
@@ -0,0 +1,95 @@
#ifndef Util_Rectangle_h__
#define Util_Rectangle_h__
#include <algorithm>
struct Rectangle
{
Rectangle()
: X(0), Y(0), Width(0), Height(0) { }
Rectangle(int x, int y, int width = 0, int height = 0)
: X(x), Y(y), Width(width), Height(height) { }
/*Rectangle(const Rectangle &rect)
: X(rect.X), Y(rect.Y), Width(rect.Width), Height(rect.Height) { }*/
int X;
int Y;
int Width;
int Height;
const int& GetLeft() const { return X; }
void SetLeft(int left)
{
Width += X - left;
X = left;
}
int GetRight() const { return X + Width; }
void SetRight(int right)
{
Width = right - X;
}
const int& GetTop() const { return Y; }
void SetTop(int top)
{
Height += Y - top;
Y = top;
}
int GetBottom() const { return Y + Height; }
int SetBottom(int bottom)
{
Height = bottom - Y;
}
Rectangle& operator+=(const Rectangle &rhs)
{
SetLeft(std::min(GetLeft(), rhs.GetLeft()));
SetRight(std::max(GetRight(), rhs.GetRight()));
SetTop(std::min(GetTop(), rhs.GetTop()));
SetBottom(std::max(GetBottom(), rhs.GetBottom()));
}
static bool Intersects(const Rectangle &r1, const Rectangle &r2)
{
return !(r2.GetLeft() > r1.GetRight() || r2.GetRight() < r1.GetLeft() || r2.GetTop() > r1.GetBottom() || r2.GetBottom() < r1.GetTop());
}
};
inline bool operator==(const Rectangle &r1, const Rectangle &r2)
{
return (r1.X == r2.X) && (r1.Y == r2.Y) && (r1.Width == r2.Width) && (r1.Height == r2.Height);
}
inline bool operator!=(const Rectangle &lhs, const Rectangle &rhs)
{
return !(lhs == rhs);
}
inline bool operator<(const Rectangle &lhs, const Rectangle &rhs)
{
return (lhs.Width < rhs.Width) && (lhs.Height < rhs.Height);
}
inline bool operator>(const Rectangle &lhs, const Rectangle &rhs)
{
return rhs < lhs;
}
inline bool operator<=(const Rectangle &lhs, const Rectangle &rhs)
{
return !(lhs > rhs);
}
inline bool operator>=(const Rectangle &lhs, const Rectangle &rhs)
{
return !(lhs < rhs);
}
inline Rectangle operator+(Rectangle lhs, const Rectangle &rhs)
{
lhs += rhs;
return lhs;
}
#endif // Util_Rectangle_h__
-10
View File
@@ -117,16 +117,6 @@ EntityID World::CreateEntity(EntityID parent /*= 0*/)
return newEntity;
}
World::~World()
{
}
World::World()
{
m_LastEntityID = 0;
}
void World::Initialize()
{
RegisterSystems();
+17 -3
View File
@@ -14,13 +14,16 @@
#include "Entity.h"
#include "Component.h"
#include "System.h"
#include "EventBroker.h"
#include "ResourceManager.h"
class World
{
public:
World();
~World();
World(std::shared_ptr<::EventBroker> eventBroker)
: m_EventBroker(eventBroker)
, m_LastEntityID(0) { }
~World() { }
virtual void Initialize();
@@ -75,8 +78,10 @@ public:
std::unordered_map<EntityID, EntityID>* GetEntities() { return &m_EntityParents; }
ResourceManager* GetResourceManager() { return &m_ResourceManager; }
std::shared_ptr<::EventBroker> EventBroker() { return m_EventBroker; }
protected:
std::shared_ptr<::EventBroker> m_EventBroker;
SystemFactory m_SystemFactory;
ComponentFactory m_ComponentFactory;
ResourceManager m_ResourceManager;
@@ -138,7 +143,16 @@ std::shared_ptr<T> World::AddComponent(EntityID entity, std::string componentTyp
template <class T>
T* World::GetComponent(EntityID entity, std::string componentType)
{
return (T*)m_EntityComponents[entity][componentType].get();
auto components = m_EntityComponents[entity];
auto it = components.find(componentType);
if (it != components.end())
{
return static_cast<T*>(it->second.get());
}
else
{
return nullptr;
}
}
#endif // World_h__
+18
View File
@@ -96,7 +96,9 @@
<ItemGroup>
<ClCompile Include="..\..\src\Camera.cpp" />
<ClCompile Include="..\..\src\CubemapTexture.cpp" />
<ClCompile Include="..\..\src\EventBroker.cpp" />
<ClCompile Include="..\..\src\GameWorld.cpp" />
<ClCompile Include="..\..\src\InputManager.cpp" />
<ClCompile Include="..\..\src\main.cpp" />
<ClCompile Include="..\..\src\Model.cpp" />
<ClCompile Include="..\..\src\OBJ.cpp" />
@@ -144,13 +146,28 @@
<ClInclude Include="..\..\src\CubemapTexture.h" />
<ClInclude Include="..\..\src\Engine.h" />
<ClInclude Include="..\..\src\Entity.h" />
<ClInclude Include="..\..\src\Events\BindKey.h" />
<ClInclude Include="..\..\src\Events\BindMouseButton.h" />
<ClInclude Include="..\..\src\Events\InputCommand.h" />
<ClInclude Include="..\..\src\Events\KeyDown.h" />
<ClInclude Include="..\..\src\Events\KeyUp.h" />
<ClInclude Include="..\..\src\Events\MouseMove.h" />
<ClInclude Include="..\..\src\Events\MousePress.h" />
<ClInclude Include="..\..\src\Events\MouseRelease.h" />
<ClInclude Include="..\..\src\Events\PlaySound.h" />
<ClInclude Include="..\..\src\Factory.h" />
<ClInclude Include="..\..\src\GameWorld.h" />
<ClInclude Include="..\..\src\GUI\Frame.h" />
<ClInclude Include="..\..\src\EventBroker.h" />
<ClInclude Include="..\..\src\GUI\Viewport.h" />
<ClInclude Include="..\..\src\InputController.h" />
<ClInclude Include="..\..\src\InputManager.h" />
<ClInclude Include="..\..\src\Model.h" />
<ClInclude Include="..\..\src\OBJ.h" />
<ClInclude Include="..\..\src\Physics\VehicleSetup.h" />
<ClInclude Include="..\..\src\PrecompiledHeader.h" />
<ClInclude Include="..\..\src\Renderer.h" />
<ClInclude Include="..\..\src\RenderQueue.h" />
<ClInclude Include="..\..\src\ResourceManager.h" />
<ClInclude Include="..\..\src\ShaderProgram.h" />
<ClInclude Include="..\..\src\Skybox.h" />
@@ -165,6 +182,7 @@
<ClInclude Include="..\..\src\Systems\TransformSystem.h" />
<ClInclude Include="..\..\src\Texture.h" />
<ClInclude Include="..\..\src\Util\GLError.h" />
<ClInclude Include="..\..\src\Util\Rectangle.h" />
<ClInclude Include="..\..\src\Util\Logging.h" />
<ClInclude Include="..\..\src\Util\UnorderedMapPair.h" />
<ClInclude Include="..\..\src\World.h" />
@@ -50,9 +50,13 @@
</ClCompile>
<ClCompile Include="..\..\src\ResourceManager.cpp" />
<ClCompile Include="..\..\src\Sound.cpp" />
<ClCompile Include="..\..\src\EventBroker.cpp" />
<ClCompile Include="..\..\src\Physics\VehicleSetup.cpp">
<Filter>Physics</Filter>
</ClCompile>
<ClCompile Include="..\..\src\InputManager.cpp">
<Filter>Input</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<Filter Include="Util">
@@ -115,6 +119,15 @@
<Filter Include="Particle System\Systems">
<UniqueIdentifier>{9f45f029-46c8-4c0d-b44c-dc9bffd3c6a6}</UniqueIdentifier>
</Filter>
<Filter Include="GUI">
<UniqueIdentifier>{6e633434-4aed-453d-b2f9-8c51ca7f78db}</UniqueIdentifier>
</Filter>
<Filter Include="Audio\Events">
<UniqueIdentifier>{85692f3a-5241-4780-a3ca-f4d8a2851392}</UniqueIdentifier>
</Filter>
<Filter Include="Input\Events">
<UniqueIdentifier>{ee125b77-b275-4841-abc9-374957a89916}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\src\World.h" />
@@ -215,6 +228,7 @@
<ClInclude Include="..\..\src\Sound.h">
<Filter>Audio</Filter>
</ClInclude>
<ClInclude Include="..\..\src\Physics\VehicleSetup.h" />
<ClInclude Include="..\..\src\Components\Vehicle.h">
<Filter>Physics\Components</Filter>
</ClInclude>
@@ -245,6 +259,52 @@
<ClInclude Include="..\..\src\Components\WheelPair.h">
<Filter>Physics\Components</Filter>
</ClInclude>
<ClInclude Include="..\..\src\GUI\Frame.h">
<Filter>GUI</Filter>
</ClInclude>
<ClInclude Include="..\..\src\Util\Rectangle.h">
<Filter>Util</Filter>
</ClInclude>
<ClInclude Include="..\..\src\EventBroker.h" />
<ClInclude Include="..\..\src\Events\PlaySound.h">
<Filter>Audio\Events</Filter>
</ClInclude>
<ClInclude Include="..\..\src\Events\KeyDown.h">
<Filter>Input\Events</Filter>
</ClInclude>
<ClInclude Include="..\..\src\Events\KeyUp.h">
<Filter>Input\Events</Filter>
</ClInclude>
<ClInclude Include="..\..\src\InputManager.h">
<Filter>Input</Filter>
</ClInclude>
<ClInclude Include="..\..\src\Events\MousePress.h">
<Filter>Input\Events</Filter>
</ClInclude>
<ClInclude Include="..\..\src\Events\MouseRelease.h">
<Filter>Input\Events</Filter>
</ClInclude>
<ClInclude Include="..\..\src\Events\MouseMove.h">
<Filter>Input\Events</Filter>
</ClInclude>
<ClInclude Include="..\..\src\Events\BindKey.h">
<Filter>Input\Events</Filter>
</ClInclude>
<ClInclude Include="..\..\src\Events\BindMouseButton.h">
<Filter>Input\Events</Filter>
</ClInclude>
<ClInclude Include="..\..\src\Events\InputCommand.h">
<Filter>Input\Events</Filter>
</ClInclude>
<ClInclude Include="..\..\src\InputController.h">
<Filter>Input</Filter>
</ClInclude>
<ClInclude Include="..\..\src\RenderQueue.h">
<Filter>Rendering</Filter>
</ClInclude>
<ClInclude Include="..\..\src\GUI\Viewport.h">
<Filter>GUI</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="..\..\src\Shaders\AABB.frag.glsl">