diff --git a/src/Engine.h b/src/Engine.h index 4093d20..02fb93c 100755 --- a/src/Engine.h +++ b/src/Engine.h @@ -1,6 +1,7 @@ #include #include +#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(); + m_Renderer = std::make_shared(); m_Renderer->Initialize(); - m_World = std::make_shared(m_Renderer); + m_World = std::make_shared(m_EventBroker, m_Renderer); m_World->Initialize(); m_LastTime = glfwGetTime(); @@ -33,6 +36,7 @@ public: } private: + std::shared_ptr m_EventBroker; std::shared_ptr m_Renderer; // TODO: This should ultimately live in GameFrame std::shared_ptr m_World; diff --git a/src/EventBroker.cpp b/src/EventBroker.cpp index f0b085a..68354c9 100644 --- a/src/EventBroker.cpp +++ b/src/EventBroker.cpp @@ -3,7 +3,10 @@ BaseEventRelay::~BaseEventRelay() { - m_Broker->Unsubscribe(*this); + if (m_Broker != nullptr) + { + m_Broker->Unsubscribe(*this); + } } void EventBroker::Unsubscribe(BaseEventRelay &relay) // ? diff --git a/src/EventBroker.h b/src/EventBroker.h index df84fd9..c11fe4a 100644 --- a/src/EventBroker.h +++ b/src/EventBroker.h @@ -20,7 +20,7 @@ friend class EventBroker; protected: BaseEventRelay(std::string typeName) - : m_TypeName(typeName) { } + : m_TypeName(typeName), m_Broker(nullptr) { } ~BaseEventRelay(); public: diff --git a/src/Events/KeyDown.h b/src/Events/KeyDown.h new file mode 100644 index 0000000..f2da381 --- /dev/null +++ b/src/Events/KeyDown.h @@ -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__ diff --git a/src/Events/KeyUp.h b/src/Events/KeyUp.h new file mode 100644 index 0000000..0770767 --- /dev/null +++ b/src/Events/KeyUp.h @@ -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__ \ No newline at end of file diff --git a/src/Events/PlaySound.h b/src/Events/PlaySound.h new file mode 100644 index 0000000..e00ef73 --- /dev/null +++ b/src/Events/PlaySound.h @@ -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__ diff --git a/src/GameWorld.cpp b/src/GameWorld.cpp index d3b9aad..c92f868 100755 --- a/src/GameWorld.cpp +++ b/src/GameWorld.cpp @@ -87,14 +87,14 @@ void GameWorld::Initialize() CommitEntity(ball); } - /*{ + { auto entity = CreateEntity(); AddComponent(entity, "Transform"); auto emitter = AddComponent(entity, "SoundEmitter"); emitter->Path = "Sounds/korvring.wav"; emitter->Loop = true; - GetSystem("SoundSystem")->PlaySound(emitter); - }*/ + //GetSystem("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"); diff --git a/src/GameWorld.h b/src/GameWorld.h index 05ca584..17901c3 100755 --- a/src/GameWorld.h +++ b/src/GameWorld.h @@ -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) - : m_Renderer(renderer), World() { } + GameWorld(std::shared_ptr<::EventBroker> eventBroker, std::shared_ptr renderer) + : World(eventBroker), m_Renderer(renderer) { } void Initialize(); diff --git a/src/System.h b/src/System.h index ff76450..b40bdef 100755 --- a/src/System.h +++ b/src/System.h @@ -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) + : 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; }; class SystemFactory : public Factory { }; diff --git a/src/Systems/DebugSystem.cpp b/src/Systems/DebugSystem.cpp index 964aa9f..f076664 100644 --- a/src/Systems/DebugSystem.cpp +++ b/src/Systems/DebugSystem.cpp @@ -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(e); + + return true; + } + + return false; +} diff --git a/src/Systems/DebugSystem.h b/src/Systems/DebugSystem.h index accaa8c..9db4de6 100644 --- a/src/Systems/DebugSystem.h +++ b/src/Systems/DebugSystem.h @@ -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 m_EKeyDown; + bool OnKeyDown(const Events::KeyDown &event); + //void UpdateEntity(double dt, EntityID entity, EntityID parent) override; }; diff --git a/src/Systems/FreeSteeringSystem.h b/src/Systems/FreeSteeringSystem.h index 11b86ae..49a65d9 100755 --- a/src/Systems/FreeSteeringSystem.h +++ b/src/Systems/FreeSteeringSystem.h @@ -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; diff --git a/src/Systems/InputSystem.cpp b/src/Systems/InputSystem.cpp index b64bc03..11d4b91 100755 --- a/src/Systems/InputSystem.cpp +++ b/src/Systems/InputSystem.cpp @@ -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(e); + } + else + { + Events::KeyUp e; + e.KeyCode = i; + EventBroker->Publish(e); + } + } } // Mouse buttons diff --git a/src/Systems/InputSystem.h b/src/Systems/InputSystem.h index abceb77..6107c2b 100755 --- a/src/Systems/InputSystem.h +++ b/src/Systems/InputSystem.h @@ -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) - : System(world), m_Renderer(renderer) { } + InputSystem(World* world, std::shared_ptr<::EventBroker> eventBroker, std::shared_ptr renderer) + : System(world, eventBroker) + , m_Renderer(renderer) { } + void RegisterComponents(ComponentFactory* cf) override; void Update(double dt) override; diff --git a/src/Systems/PhysicsSystem.cpp b/src/Systems/PhysicsSystem.cpp index 6ec3b45..ff3607c 100644 --- a/src/Systems/PhysicsSystem.cpp +++ b/src/Systems/PhysicsSystem.cpp @@ -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 diff --git a/src/Systems/PhysicsSystem.h b/src/Systems/PhysicsSystem.h index 626bbcd..0544b13 100644 --- a/src/Systems/PhysicsSystem.h +++ b/src/Systems/PhysicsSystem.h @@ -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; diff --git a/src/Systems/RenderSystem.h b/src/Systems/RenderSystem.h index 4df8658..c3cd09a 100755 --- a/src/Systems/RenderSystem.h +++ b/src/Systems/RenderSystem.h @@ -24,8 +24,9 @@ namespace Systems class RenderSystem : public System { public: - RenderSystem(World* world, std::shared_ptr renderer) - : System(world), m_Renderer(renderer) { } + RenderSystem(World* world, std::shared_ptr<::EventBroker> eventBroker, std::shared_ptr renderer) + : System(world, eventBroker) + , m_Renderer(renderer) { } void RegisterComponents(ComponentFactory* cf) override; void RegisterResourceTypes(ResourceManager* rm) override; diff --git a/src/Systems/SoundSystem.cpp b/src/Systems/SoundSystem.cpp index ffb9b06..02a665e 100755 --- a/src/Systems/SoundSystem.cpp +++ b/src/Systems/SoundSystem.cpp @@ -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", event.Resource); + ALuint source = m_Sources.begin()->second; + alSourcei(source, AL_BUFFER, buffer); + alSourcePlay(source); + + return true; +} diff --git a/src/Systems/SoundSystem.h b/src/Systems/SoundSystem.h index 85ee4fa..7fbc5aa 100755 --- a/src/Systems/SoundSystem.h +++ b/src/Systems/SoundSystem.h @@ -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 m_EPlaySound; + bool OnPlaySound(const Events::PlaySound &event); + std::map m_Sources; std::map m_BufferCache; // string = fileName }; diff --git a/src/Systems/TransformSystem.h b/src/Systems/TransformSystem.h index 542000d..f4e6f48 100755 --- a/src/Systems/TransformSystem.h +++ b/src/Systems/TransformSystem.h @@ -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; diff --git a/src/World.cpp b/src/World.cpp index 0b6a245..44bc45e 100755 --- a/src/World.cpp +++ b/src/World.cpp @@ -120,16 +120,6 @@ EntityID World::CreateEntity(EntityID parent /*= 0*/) return newEntity; } -World::~World() -{ - -} - -World::World() -{ - m_LastEntityID = 0; -} - void World::Initialize() { RegisterSystems(); diff --git a/src/World.h b/src/World.h index 255ef1b..fd496cd 100755 --- a/src/World.h +++ b/src/World.h @@ -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* 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; diff --git a/src/main.cpp b/src/main.cpp index 29f3c96..03b63b4 100755 --- a/src/main.cpp +++ b/src/main.cpp @@ -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 testRelay = &OnTestEvent; - broker.Subscribe(testRelay); - - broker.Publish(e); - } - - broker.Publish(e); - - return 0; - Engine engine(argc, argv); while (engine.Running()) engine.Tick(); diff --git a/vs11/Returngeance/Returngeance.vcxproj b/vs11/Returngeance/Returngeance.vcxproj index 52a5283..17327db 100755 --- a/vs11/Returngeance/Returngeance.vcxproj +++ b/vs11/Returngeance/Returngeance.vcxproj @@ -138,6 +138,9 @@ + + + diff --git a/vs11/Returngeance/Returngeance.vcxproj.filters b/vs11/Returngeance/Returngeance.vcxproj.filters index 0527db8..febf3eb 100755 --- a/vs11/Returngeance/Returngeance.vcxproj.filters +++ b/vs11/Returngeance/Returngeance.vcxproj.filters @@ -116,6 +116,12 @@ {6e633434-4aed-453d-b2f9-8c51ca7f78db} + + {85692f3a-5241-4780-a3ca-f4d8a2851392} + + + {ee125b77-b275-4841-abc9-374957a89916} + @@ -225,6 +231,15 @@ Util + + Audio\Events + + + Input\Events + + + Input\Events +