Input system complete (untested)

This commit is contained in:
2014-03-07 02:53:52 +01:00
parent b1f0bc7cce
commit 95a36794e3
6 changed files with 53 additions and 13 deletions
+8 -3
View File
@@ -1,16 +1,21 @@
#ifndef Components_Input_h__ #ifndef Components_Input_h__
#define Components_Input_h__ #define Components_Input_h__
#include "Component.h" #include <array>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include "Component.h"
namespace Components namespace Components
{ {
struct Input : Component struct Input : Component
{ {
int KeyState[GLFW_KEY_LAST]; std::array<int, GLFW_KEY_LAST> KeyState;
int MouseState[GLFW_MOUSE_BUTTON_LAST]; std::array<int, GLFW_KEY_LAST> LastKeyState;
std::array<int, GLFW_MOUSE_BUTTON_LAST> MouseState;
std::array<int, GLFW_MOUSE_BUTTON_LAST> LastMouseState;
float dX, dY; float dX, dY;
}; };
+4 -1
View File
@@ -11,7 +11,10 @@ public:
System(World* world) : m_World(world) { } System(World* world) : m_World(world) { }
virtual ~System() { } virtual ~System() { }
virtual void Update(double dt, EntityID entity, EntityID parent) = 0; // Called once per system every tick
virtual void Update(double dt) { }
// Called once for every entity in the world every tick
virtual void Update(double dt, EntityID entity, EntityID parent) { }
protected: protected:
World* m_World; World* m_World;
+30 -8
View File
@@ -1,18 +1,40 @@
#include "InputSystem.h" #include "InputSystem.h"
#include "World.h" #include "World.h"
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_CurrentKeyState[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;
}
void Systems::InputSystem::Update(double dt, EntityID entity, EntityID parent) void Systems::InputSystem::Update(double dt, EntityID entity, EntityID parent)
{ {
auto input = m_World->GetComponent<Components::Input>(entity, "Input"); auto input = m_World->GetComponent<Components::Input>(entity, "Input");
if (input == nullptr) if (input == nullptr)
return; return;
for (int i = 0; i <= GLFW_KEY_LAST; ++i) { input->KeyState = m_CurrentKeyState;
input->KeyState[i] = glfwGetKey(m_Renderer->GetWindow(), i); input->LastKeyState = m_LastKeyState;
} input->MouseState = m_CurrentMouseState;
input->LastMouseState = m_LastMouseState;
for (int i = 0; i <= GLFW_MOUSE_BUTTON_LAST; ++i) { input->dX = m_CurrentMouseDeltaX;
input->MouseState[i] = glfwGetMouseButton(m_Renderer->GetWindow(), i); input->dY = m_CurrentMouseDeltaY;
}
} }
+9
View File
@@ -1,6 +1,8 @@
#ifndef InputSystem_h__ #ifndef InputSystem_h__
#define InputSystem_h__ #define InputSystem_h__
#include <array>
#include "System.h" #include "System.h"
#include "Renderer.h" #include "Renderer.h"
#include "Components/Input.h" #include "Components/Input.h"
@@ -14,10 +16,17 @@ public:
InputSystem(World* world, Renderer* renderer) InputSystem(World* world, Renderer* renderer)
: System(world), m_Renderer(renderer) { } : System(world), m_Renderer(renderer) { }
void Update(double dt) override;
void Update(double dt, EntityID entity, EntityID parent) override; void Update(double dt, EntityID entity, EntityID parent) override;
private: private:
Renderer* m_Renderer; Renderer* m_Renderer;
std::array<int, GLFW_KEY_LAST> m_CurrentKeyState;
std::array<int, GLFW_KEY_LAST> m_LastKeyState;
std::array<int, GLFW_MOUSE_BUTTON_LAST> m_CurrentMouseState;
std::array<int, GLFW_MOUSE_BUTTON_LAST> m_LastMouseState;
float m_CurrentMouseDeltaX, m_CurrentMouseDeltaY;
float m_LastMouseX, m_LastMouseY; float m_LastMouseX, m_LastMouseY;
}; };
+1
View File
@@ -32,6 +32,7 @@ void World::RecursiveUpdate(std::shared_ptr<System> system, double dt, EntityID
void World::Update(double dt) void World::Update(double dt)
{ {
for (auto system : m_Systems) { for (auto system : m_Systems) {
system->Update(dt);
RecursiveUpdate(system, dt, 0); RecursiveUpdate(system, dt, 0);
} }
} }
+1 -1
View File
@@ -64,8 +64,8 @@ public:
void AddSystem(System* system); void AddSystem(System* system);
// Recursively update through the scene graph
void Update(double dt); void Update(double dt);
// Recursively update through the scene graph
void RecursiveUpdate(std::shared_ptr<System> system, double dt, EntityID parentEntity); void RecursiveUpdate(std::shared_ptr<System> system, double dt, EntityID parentEntity);
private: private: