diff options
Diffstat (limited to '')
-rw-r--r-- | src/core/AssetManager.cpp | 2 | ||||
-rw-r--r-- | src/core/AssetManager.hpp (renamed from include/AssetManager.hpp) | 0 | ||||
-rw-r--r-- | src/core/Core.cpp | 4 | ||||
-rw-r--r-- | src/core/Core.hpp (renamed from include/Core.hpp) | 6 | ||||
-rw-r--r-- | src/core/Event.cpp | 76 | ||||
-rw-r--r-- | src/core/Event.hpp | 96 |
6 files changed, 178 insertions, 6 deletions
diff --git a/src/core/AssetManager.cpp b/src/core/AssetManager.cpp index d263c4a..14ea677 100644 --- a/src/core/AssetManager.cpp +++ b/src/core/AssetManager.cpp @@ -1,4 +1,4 @@ -#include <AssetManager.hpp> +#include <core/AssetManager.hpp> namespace fs = std::experimental::filesystem; diff --git a/include/AssetManager.hpp b/src/core/AssetManager.hpp index 26c7eca..26c7eca 100644 --- a/include/AssetManager.hpp +++ b/src/core/AssetManager.hpp diff --git a/src/core/Core.cpp b/src/core/Core.cpp index 44e2648..e98d204 100644 --- a/src/core/Core.cpp +++ b/src/core/Core.cpp @@ -44,7 +44,7 @@ void Core::Exec() { UpdateChunksToRender(); } - /*std::ostringstream toWindow; + std::ostringstream toWindow; auto camPos = gameState->Position(); auto velPos = glm::vec3(gameState->g_PlayerVelocityX, gameState->g_PlayerVelocityY, gameState->g_PlayerVelocityZ); @@ -57,7 +57,7 @@ void Core::Exec() { toWindow << " (" << deltaTime * 1000 << "ms); "; toWindow << "Tickrate: " << tickRate << " (" << (1.0 / tickRate * 1000) << "ms); "; toWindow << "Sections: " << sectionRate << " (" << (1.0 / sectionRate * 1000) << "ms); "; - window->setTitle(toWindow.str());*/ + window->setTitle(toWindow.str()); HandleEvents(); if (isMouseCaptured) HandleMouseCapture(); diff --git a/include/Core.hpp b/src/core/Core.hpp index 8d60ff6..fdbb377 100644 --- a/include/Core.hpp +++ b/src/core/Core.hpp @@ -8,8 +8,8 @@ #include <GL/glew.h> #include <glm/gtc/type_ptr.hpp> -#include <GameState.hpp> -#include <AssetManager.hpp> +#include <world/GameState.hpp> +#include <core/AssetManager.hpp> #include <graphics/Shader.hpp> #include <graphics/Gui.hpp> #include <graphics/RenderSection.hpp> @@ -79,7 +79,7 @@ class Core { std::map<Vector, RenderSection> availableChunks; std::mutex availableChunksMutex; - int ChunkDistance = 1; + int ChunkDistance = 3; RenderState renderState; diff --git a/src/core/Event.cpp b/src/core/Event.cpp new file mode 100644 index 0000000..10b2eaa --- /dev/null +++ b/src/core/Event.cpp @@ -0,0 +1,76 @@ +#include <core/Event.hpp> +#include <easylogging++.h> + +std::queue <Event> EventAgregator::eventsToHandle; +std::mutex EventAgregator::queueMutex; +bool EventAgregator::isStarted = false; +std::vector<EventListener*> EventAgregator::listeners; +std::mutex EventAgregator::listenersMutex; + +void EventAgregator::EventHandlingLoop() { + while (true) { + queueMutex.lock(); + if (!eventsToHandle.empty()) { + auto queue = eventsToHandle; + while (!eventsToHandle.empty()) + eventsToHandle.pop(); + queueMutex.unlock(); + + while (!queue.empty()) { + auto event = queue.front(); + listenersMutex.lock(); + for (auto& listener : listeners) { + LOG(INFO)<<"Listener notified about event"; + listener->PushEvent(event); + } + listenersMutex.unlock(); + queue.pop(); + } + + queueMutex.lock(); + } + queueMutex.unlock(); + } +} + +void EventAgregator::RegisterListener(EventListener &listener) { + listenersMutex.lock(); + LOG(INFO)<<"Registered handler "<<&listener; + listeners.push_back(&listener); + listenersMutex.unlock(); +} + +void EventAgregator::UnregisterListener(EventListener &listener) { + listenersMutex.lock(); + LOG(INFO)<<"Unregistered handler "<<&listener; + listeners.erase(std::find(listeners.begin(), listeners.end(), &listener)); + listenersMutex.unlock(); +} + + + +EventListener::EventListener() { + EventAgregator::RegisterListener(*this); +} + +EventListener::~EventListener() { + EventAgregator::UnregisterListener(*this); +} + +void EventListener::PushEvent(Event event) { + eventsMutex.lock(); + LOG(INFO)<<"Pushed event to queue"; + events.push(event); + eventsMutex.unlock(); +} + +/*void EventListener::RegisterHandler(EventType type, std::function<void(void*)> handler) { + handlers[type] = handler; +}*/ + +bool EventListener::IsEventsQueueIsNotEmpty() { + eventsMutex.lock(); + bool value = !events.empty(); + eventsMutex.unlock(); + return value; +}
\ No newline at end of file diff --git a/src/core/Event.hpp b/src/core/Event.hpp new file mode 100644 index 0000000..cfa990a --- /dev/null +++ b/src/core/Event.hpp @@ -0,0 +1,96 @@ +#pragma once + +#include <queue> +#include <map> +#include <thread> +#include <mutex> +#include <condition_variable> +#include <chrono> +#include <variant> +#include <functional> + +#include <Vector.hpp> + +enum class EventType { + Echo, + ChunkChanged, +}; + +struct EchoData { + std::chrono::time_point<std::chrono::high_resolution_clock> time; +}; + +struct ChunkChangedData { + Vector chunkPosition; +}; + +using EventData = std::variant<EchoData, ChunkChangedData>; + +struct Event { + EventType type; + EventData data; +}; + +class EventListener { + friend class EventAgregator; + + using HandlerFunc = std::function<void(EventData)>; + + std::map<EventType, HandlerFunc> handlers; //TODO: There must be more elegant solution than std::variant of all data + + std::mutex eventsMutex; + + std::queue<Event> events; + + void PushEvent(Event event); + +public: + EventListener(); + ~EventListener(); + bool IsEventsQueueIsNotEmpty(); + + void RegisterHandler(EventType type, HandlerFunc handler) { + handlers[type] = handler; + } + + void HandleEvent() { + eventsMutex.lock(); + if (events.empty()) { + eventsMutex.unlock(); + return; + } + Event event = events.front(); + events.pop(); + eventsMutex.unlock(); + auto function = handlers[event.type]; + function(event.data); + } +}; + +class EventAgregator { + friend EventListener; + + EventAgregator() = default; + static std::queue<Event> eventsToHandle; + static std::mutex queueMutex; + static bool isStarted; + static std::vector<EventListener *> listeners; + static std::mutex listenersMutex; + + static void EventHandlingLoop(); + + static void RegisterListener(EventListener &listener); + static void UnregisterListener(EventListener &listener); + +public: + static void PushEvent(EventType type, EventData data) { + if (!isStarted) { + isStarted = true; + std::thread(&EventAgregator::EventHandlingLoop).detach(); + } + Event event; + event.type = type; + event.data = data; + eventsToHandle.push(event); + } +};
\ No newline at end of file |