Started implementation of events for InputSystem and SoundSystem

This commit is contained in:
2014-04-28 19:55:31 +02:00
parent 5870a4d7bf
commit 57d29d0db7
25 changed files with 201 additions and 71 deletions
+5 -1
View File
@@ -1,6 +1,7 @@
#include <string>
#include <sstream>
#include "EventBroker.h"
#include "Renderer.h"
#include "GameWorld.h"
@@ -9,10 +10,12 @@ 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_World = std::make_shared<GameWorld>(m_EventBroker, m_Renderer);
m_World->Initialize();
m_LastTime = glfwGetTime();
@@ -33,6 +36,7 @@ public:
}
private:
std::shared_ptr<EventBroker> m_EventBroker;
std::shared_ptr<Renderer> m_Renderer;
// TODO: This should ultimately live in GameFrame
std::shared_ptr<GameWorld> m_World;
+4 -1
View File
@@ -3,7 +3,10 @@
BaseEventRelay::~BaseEventRelay()
{
m_Broker->Unsubscribe(*this);
if (m_Broker != nullptr)
{
m_Broker->Unsubscribe(*this);
}
}
void EventBroker::Unsubscribe(BaseEventRelay &relay) // ?
+1 -1
View File
@@ -20,7 +20,7 @@ friend class EventBroker;
protected:
BaseEventRelay(std::string typeName)
: m_TypeName(typeName) { }
: m_TypeName(typeName), m_Broker(nullptr) { }
~BaseEventRelay();
public:
+16
View File
@@ -0,0 +1,16 @@
#ifndef Event_KeyDown_h__
#define Event_KeyDown_h__
#include "EventBroker.h"
namespace Events
{
struct KeyDown : Event
{
int KeyCode;
};
}
#endif // Event_KeyDown_h__
+16
View File
@@ -0,0 +1,16 @@
#ifndef Event_KeyUp_h__
#define Event_KeyUp_h__
#include "EventBroker.h"
namespace Events
{
struct KeyUp : Event
{
int KeyCode;
};
}
#endif // Event_KeyUp_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__
+11 -9
View File
@@ -87,14 +87,14 @@ void GameWorld::Initialize()
CommitEntity(ball);
}
/*{
{
auto entity = CreateEntity();
AddComponent(entity, "Transform");
auto emitter = AddComponent<Components::SoundEmitter>(entity, "SoundEmitter");
emitter->Path = "Sounds/korvring.wav";
emitter->Loop = true;
GetSystem<Systems::SoundSystem>("SoundSystem")->PlaySound(emitter);
}*/
//GetSystem<Systems::SoundSystem>("SoundSystem")->PlaySound(emitter);
}
}
void GameWorld::Update(double dt)
@@ -112,16 +112,17 @@ 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_Renderer); });
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); });
m_SystemFactory.Register("FreeSteeringSystem", [this]() { return new Systems::FreeSteeringSystem(this); });
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); });
m_SystemFactory.Register("SoundSystem", [this]() { return new Systems::SoundSystem(this, m_EventBroker); });
m_SystemFactory.Register("PhysicsSystem", [this]() { return new Systems::PhysicsSystem(this, m_EventBroker); });
m_SystemFactory.Register("RenderSystem", [this]() { return new Systems::RenderSystem(this, m_EventBroker, m_Renderer); });
}
void GameWorld::AddSystems()
@@ -129,6 +130,7 @@ void GameWorld::AddSystems()
AddSystem("TransformSystem");
//AddSystem("LevelGenerationSystem");
AddSystem("InputSystem");
AddSystem("DebugSystem");
//AddSystem("CollisionSystem");
////AddSystem("ParticleSystem");
//AddSystem("PlayerSystem");
+3 -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"
@@ -33,8 +34,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();
+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;
};
+3 -2
View File
@@ -10,8 +10,9 @@ 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 Update(double dt) override;
+16
View File
@@ -16,6 +16,22 @@ void Systems::InputSystem::Update(double dt)
for (int i = 0; i <= GLFW_KEY_LAST; ++i)
{
m_CurrentKeyState[i] = glfwGetKey(m_Renderer->GetWindow(), i);
if (m_CurrentKeyState[i] != m_LastKeyState[i])
{
// Publish key events
if (m_CurrentKeyState[i])
{
Events::KeyDown e;
e.KeyCode = i;
EventBroker->Publish<Events::KeyDown>(e);
}
else
{
Events::KeyUp e;
e.KeyCode = i;
EventBroker->Publish<Events::KeyUp>(e);
}
}
}
// Mouse buttons
+6 -2
View File
@@ -6,6 +6,8 @@
#include "System.h"
#include "Renderer.h"
#include "Components/Input.h"
#include "Events/KeyUp.h"
#include "Events/KeyDown.h"
namespace Systems
{
@@ -13,8 +15,10 @@ 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, std::shared_ptr<Renderer> renderer)
: System(world, eventBroker)
, m_Renderer(renderer) { }
void RegisterComponents(ComponentFactory* cf) override;
void Update(double dt) override;
+1 -1
View File
@@ -27,7 +27,7 @@
Systems::PhysicsSystem::PhysicsSystem(World* world) : System(world)
void Systems::PhysicsSystem::Initialize()
{
{
hkMemorySystem::FrameInfo finfo(500 * 1024); // Allocate 500KB of Physics solver buffer
+4 -1
View File
@@ -46,8 +46,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;
+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;
-10
View File
@@ -120,16 +120,6 @@ EntityID World::CreateEntity(EntityID parent /*= 0*/)
return newEntity;
}
World::~World()
{
}
World::World()
{
m_LastEntityID = 0;
}
void World::Initialize()
{
RegisterSystems();
+7 -2
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;
-28
View File
@@ -1,36 +1,8 @@
#include "PrecompiledHeader.h"
#include "Engine.h"
#include "EventBroker.h"
struct TestEvent : Event
{
std::string Hello;
};
bool OnTestEvent(const TestEvent &event)
{
std::cout << "TestEvent.Hello = " << event.Hello << std::endl;
return true;
}
int main(int argc, char* argv[])
{
EventBroker broker;
TestEvent e;
e.Hello = "Hello world";
{
EventRelay<TestEvent> testRelay = &OnTestEvent;
broker.Subscribe(testRelay);
broker.Publish<TestEvent>(e);
}
broker.Publish<TestEvent>(e);
return 0;
Engine engine(argc, argv);
while (engine.Running())
engine.Tick();