summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/Nbt.hpp (renamed from include/Nbt.hpp)0
-rw-r--r--src/Utility.hpp (renamed from include/Utility.hpp)0
-rw-r--r--src/Vector.hpp (renamed from include/Vector.hpp)0
-rw-r--r--src/core/AssetManager.cpp2
-rw-r--r--src/core/AssetManager.hpp (renamed from include/AssetManager.hpp)0
-rw-r--r--src/core/Core.cpp4
-rw-r--r--src/core/Core.hpp (renamed from include/Core.hpp)6
-rw-r--r--src/core/Event.cpp76
-rw-r--r--src/core/Event.hpp96
-rw-r--r--src/graphics/Gui.hpp (renamed from include/graphics/Gui.hpp)1
-rw-r--r--src/graphics/RenderSection.cpp18
-rw-r--r--src/graphics/RenderSection.hpp (renamed from include/graphics/RenderSection.hpp)2
-rw-r--r--src/graphics/Shader.hpp (renamed from include/graphics/Shader.hpp)0
-rw-r--r--src/graphics/Texture.hpp (renamed from include/graphics/Texture.hpp)0
-rw-r--r--src/graphics/Widget.hpp (renamed from include/graphics/Widget.hpp)0
-rw-r--r--src/main.cpp5
-rw-r--r--src/network/Network.cpp8
-rw-r--r--src/network/Network.hpp (renamed from include/network/Network.hpp)0
-rw-r--r--src/network/NetworkClient.hpp (renamed from include/network/NetworkClient.hpp)0
-rw-r--r--src/network/Packet.hpp (renamed from include/network/Packet.hpp)0
-rw-r--r--src/network/Socket.hpp (renamed from include/network/Socket.hpp)0
-rw-r--r--src/network/Stream.hpp (renamed from include/network/Stream.hpp)0
-rw-r--r--src/world/Block.hpp (renamed from include/world/Block.hpp)0
-rw-r--r--src/world/Collision.hpp (renamed from include/world/Collision.hpp)0
-rw-r--r--src/world/GameState.cpp2
-rw-r--r--src/world/GameState.hpp (renamed from include/GameState.hpp)0
-rw-r--r--src/world/Section.cpp55
-rw-r--r--src/world/Section.hpp (renamed from include/world/Section.hpp)0
-rw-r--r--src/world/World.hpp (renamed from include/world/World.hpp)0
29 files changed, 228 insertions, 47 deletions
diff --git a/include/Nbt.hpp b/src/Nbt.hpp
index 03f5af0..03f5af0 100644
--- a/include/Nbt.hpp
+++ b/src/Nbt.hpp
diff --git a/include/Utility.hpp b/src/Utility.hpp
index 11b4ff7..11b4ff7 100644
--- a/include/Utility.hpp
+++ b/src/Utility.hpp
diff --git a/include/Vector.hpp b/src/Vector.hpp
index a2d5c6a..a2d5c6a 100644
--- a/include/Vector.hpp
+++ b/src/Vector.hpp
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
diff --git a/include/graphics/Gui.hpp b/src/graphics/Gui.hpp
index 641b941..e22a0a7 100644
--- a/include/graphics/Gui.hpp
+++ b/src/graphics/Gui.hpp
@@ -4,4 +4,5 @@ class Gui {
public:
int WHY=0;
+
};
diff --git a/src/graphics/RenderSection.cpp b/src/graphics/RenderSection.cpp
index 90d4329..ae072d6 100644
--- a/src/graphics/RenderSection.cpp
+++ b/src/graphics/RenderSection.cpp
@@ -138,7 +138,7 @@ RenderSection::~RenderSection() {
refCounterVao[Vao]--;
if (refCounterVbo[VboTextures] <= 0)
glDeleteBuffers(1, &VboTextures);
-
+
if (refCounterVbo[VboModels] <= 0)
glDeleteBuffers(1, &VboTextures);
if (refCounterVbo[VboColors] <= 0)
@@ -297,13 +297,11 @@ void RenderSection::UpdateState(const std::map<BlockTextureId, glm::vec4> &textu
}
}
numOfFaces = textures.size();
- hash = section.GetHash();
+ hash = section.GetHash();
}
void RenderSection::Render(RenderState &state) {
- if (!isEnabled) {
- return;
- }
+ if (!isEnabled) return;
if (!models.empty()) {
glBindBuffer(GL_ARRAY_BUFFER, VboTextures);
glBufferData(GL_ARRAY_BUFFER, textures.size() * sizeof(glm::vec4), textures.data(), GL_DYNAMIC_DRAW);
@@ -339,7 +337,7 @@ RenderSection::RenderSection(const RenderSection &other) {
this->models = other.models;
this->textures = other.textures;
this->colors = other.colors;
- this->hash = other.hash;
+ this->hash = other.hash;
refCounterVbo[VboTextures]++;
refCounterVbo[VboModels]++;
@@ -348,11 +346,11 @@ RenderSection::RenderSection(const RenderSection &other) {
}
void RenderSection::SetEnabled(bool isEnabled) {
- this->isEnabled = isEnabled;
+ this->isEnabled = isEnabled;
}
bool RenderSection::IsNeedUpdate() {
- size_t currentHash = world->GetSection(sectionPosition).GetHash();
- bool isNeedUpdate = currentHash != hash;
- return isNeedUpdate;
+ size_t currentHash = world->GetSection(sectionPosition).GetHash();
+ bool isNeedUpdate = currentHash != hash;
+ return isNeedUpdate;
} \ No newline at end of file
diff --git a/include/graphics/RenderSection.hpp b/src/graphics/RenderSection.hpp
index 58c4acf..5973909 100644
--- a/include/graphics/RenderSection.hpp
+++ b/src/graphics/RenderSection.hpp
@@ -7,7 +7,7 @@
#include <glm/gtx/transform.hpp>
#include <easylogging++.h>
-#include <AssetManager.hpp>
+#include <core/AssetManager.hpp>
#include <world/Section.hpp>
#include <world/World.hpp>
diff --git a/include/graphics/Shader.hpp b/src/graphics/Shader.hpp
index 17a434e..17a434e 100644
--- a/include/graphics/Shader.hpp
+++ b/src/graphics/Shader.hpp
diff --git a/include/graphics/Texture.hpp b/src/graphics/Texture.hpp
index 277806a..277806a 100644
--- a/include/graphics/Texture.hpp
+++ b/src/graphics/Texture.hpp
diff --git a/include/graphics/Widget.hpp b/src/graphics/Widget.hpp
index c4d5dc1..c4d5dc1 100644
--- a/include/graphics/Widget.hpp
+++ b/src/graphics/Widget.hpp
diff --git a/src/main.cpp b/src/main.cpp
index aa30ff8..bcf573b 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -1,4 +1,5 @@
-#include "../include/Core.hpp"
+#include "core/Core.hpp"
+#include <core/Event.hpp>
const char *getTimeSinceProgramStart(void) {
static auto initialTime = std::chrono::steady_clock().now();
@@ -25,6 +26,8 @@ int main() {
el::Loggers::addFlag(el::LoggingFlag::ColoredTerminalOutput);
LOG(INFO) << "Logger is configured";
+ LOG(FATAL)<<"Sizeof data is "<<sizeof(EventData);
+
Core core;
core.Exec();
diff --git a/src/network/Network.cpp b/src/network/Network.cpp
index 91cb481..798756d 100644
--- a/src/network/Network.cpp
+++ b/src/network/Network.cpp
@@ -1,8 +1,12 @@
#include <network/Network.hpp>
Network::Network(std::string address, unsigned short port) {
- socket = new Socket(address, port);
- stream = new StreamSocket(socket);
+ try {
+ socket = new Socket(address, port);
+ stream = new StreamSocket(socket);
+ } catch (std::exception &e) {
+ LOG(FATAL)<<e.what();
+ }
}
Network::~Network() {
diff --git a/include/network/Network.hpp b/src/network/Network.hpp
index 1281289..1281289 100644
--- a/include/network/Network.hpp
+++ b/src/network/Network.hpp
diff --git a/include/network/NetworkClient.hpp b/src/network/NetworkClient.hpp
index 22b1b22..22b1b22 100644
--- a/include/network/NetworkClient.hpp
+++ b/src/network/NetworkClient.hpp
diff --git a/include/network/Packet.hpp b/src/network/Packet.hpp
index 685e3da..685e3da 100644
--- a/include/network/Packet.hpp
+++ b/src/network/Packet.hpp
diff --git a/include/network/Socket.hpp b/src/network/Socket.hpp
index 48bcad9..48bcad9 100644
--- a/include/network/Socket.hpp
+++ b/src/network/Socket.hpp
diff --git a/include/network/Stream.hpp b/src/network/Stream.hpp
index a24dfbe..a24dfbe 100644
--- a/include/network/Stream.hpp
+++ b/src/network/Stream.hpp
diff --git a/include/world/Block.hpp b/src/world/Block.hpp
index 2f823fe..2f823fe 100644
--- a/include/world/Block.hpp
+++ b/src/world/Block.hpp
diff --git a/include/world/Collision.hpp b/src/world/Collision.hpp
index b88fbf7..b88fbf7 100644
--- a/include/world/Collision.hpp
+++ b/src/world/Collision.hpp
diff --git a/src/world/GameState.cpp b/src/world/GameState.cpp
index 79e2f1b..d3a6bd3 100644
--- a/src/world/GameState.cpp
+++ b/src/world/GameState.cpp
@@ -1,4 +1,4 @@
-#include <GameState.hpp>
+#include <world/GameState.hpp>
GameState::GameState(NetworkClient *Net, bool &quit) : nc(Net), isRunning(quit) {
Front = glm::vec3(0.0f, 0.0f, -1.0f);
diff --git a/include/GameState.hpp b/src/world/GameState.hpp
index 6741882..6741882 100644
--- a/include/GameState.hpp
+++ b/src/world/GameState.hpp
diff --git a/src/world/Section.cpp b/src/world/Section.cpp
index 279d2b2..ff2a4fb 100644
--- a/src/world/Section.cpp
+++ b/src/world/Section.cpp
@@ -47,28 +47,28 @@ void Section::Parse() {
endswap(&longArray[i]);
std::vector<unsigned short> blocks;
blocks.reserve(4096);
- {
- auto begin = std::chrono::steady_clock::now();
- int bitPos = 0;
- unsigned short t = 0;
- for (size_t i = 0; i < m_dataBlocksLen; i++) {
- for (int j = 0; j < 8; j++) {
- t |= (m_dataBlocks[i] & 0x01) ? 0x80 : 0x00;
- t >>= 1;
- m_dataBlocks[i] >>= 1;
- bitPos++;
- if (bitPos >= m_bitsPerBlock) {
- bitPos = 0;
- t >>= m_bitsPerBlock - 1;
- blocks.push_back(t);
- t = 0;
- }
- }
- }
- auto end = std::chrono::steady_clock::now();
- std::chrono::duration<double, std::milli> time = end - begin;
- totalParsingTime += time.count();
- }
+ {
+ auto begin = std::chrono::steady_clock::now();
+ int bitPos = 0;
+ unsigned short t = 0;
+ for (size_t i = 0; i < m_dataBlocksLen; i++) {
+ for (int j = 0; j < 8; j++) {
+ t |= (m_dataBlocks[i] & 0x01) ? 0x80 : 0x00;
+ t >>= 1;
+ m_dataBlocks[i] >>= 1;
+ bitPos++;
+ if (bitPos >= m_bitsPerBlock) {
+ bitPos = 0;
+ t >>= m_bitsPerBlock - 1;
+ blocks.push_back(t);
+ t = 0;
+ }
+ }
+ }
+ auto end = std::chrono::steady_clock::now();
+ std::chrono::duration<double, std::milli> time = end - begin;
+ totalParsingTime += time.count();
+ }
std::vector<byte> light;
light.reserve(4096);
for (int i = 0; i < 2048; i++) {
@@ -137,8 +137,11 @@ Vector Section::GetPosition() {
}
size_t Section::GetHash() {
- if (m_blocks.empty())
- return 0;
- std::string str((unsigned char*)m_blocks.data(), (unsigned char*)m_blocks.data() + m_blocks.size() * sizeof(Block));
- return std::hash<std::string>{}(str);
+ if (m_blocks.empty()) return 0;
+
+ unsigned char *from = reinterpret_cast<unsigned char *>(m_blocks.data());
+ size_t length = m_blocks.size() * sizeof(Block);
+
+ std::string str(from, from + length);
+ return std::hash<std::string>{}(str);
} \ No newline at end of file
diff --git a/include/world/Section.hpp b/src/world/Section.hpp
index 2df0cfe..2df0cfe 100644
--- a/include/world/Section.hpp
+++ b/src/world/Section.hpp
diff --git a/include/world/World.hpp b/src/world/World.hpp
index 6b09f1f..6b09f1f 100644
--- a/include/world/World.hpp
+++ b/src/world/World.hpp