diff options
Diffstat (limited to '')
-rw-r--r-- | src/Event.cpp | 4 | ||||
-rw-r--r-- | src/Event.hpp | 4 | ||||
-rw-r--r-- | src/GameState.cpp | 4 | ||||
-rw-r--r-- | src/GameState.hpp | 6 | ||||
-rw-r--r-- | src/Render.cpp | 17 | ||||
-rw-r--r-- | src/Render.hpp | 1 | ||||
-rw-r--r-- | src/RendererSection.cpp | 8 | ||||
-rw-r--r-- | src/RendererWorld.cpp | 257 | ||||
-rw-r--r-- | src/RendererWorld.hpp | 19 | ||||
-rw-r--r-- | src/Texture.cpp | 5 | ||||
-rw-r--r-- | src/ThreadGame.cpp | 9 | ||||
-rw-r--r-- | src/ThreadGame.hpp | 2 | ||||
-rw-r--r-- | src/ThreadNetwork.cpp | 7 | ||||
-rw-r--r-- | src/ThreadNetwork.hpp | 2 | ||||
-rw-r--r-- | src/Utility.cpp | 6 | ||||
-rw-r--r-- | src/Utility.hpp | 2 | ||||
-rw-r--r-- | src/World.cpp | 62 |
17 files changed, 142 insertions, 273 deletions
diff --git a/src/Event.cpp b/src/Event.cpp index 94eedba..1a0816f 100644 --- a/src/Event.cpp +++ b/src/Event.cpp @@ -65,14 +65,14 @@ void EventListener::HandleEvent() { void EventAgregator::RegisterListener(EventListener &listener) { listenersMutex.lock(); - LOG(WARNING) << "Registered handler " << &listener; + //LOG(WARNING) << "Registered handler " << &listener; listeners.push_back(&listener); listenersMutex.unlock(); } void EventAgregator::UnregisterListener(EventListener &listener) { listenersMutex.lock(); - LOG(WARNING) << "Unregistered handler " << &listener; + //LOG(WARNING) << "Unregistered handler " << &listener; listeners.erase(std::find(listeners.begin(), listeners.end(), &listener)); listenersMutex.unlock(); } diff --git a/src/Event.hpp b/src/Event.hpp index 5467f85..e7ebe66 100644 --- a/src/Event.hpp +++ b/src/Event.hpp @@ -60,7 +60,7 @@ struct ConnectToServerData { class NetworkClient; struct ConnectionSuccessfullData { - NetworkClient *ptr; + std::shared_ptr<NetworkClient> ptr; }; struct DisconnectData { @@ -86,7 +86,7 @@ struct RegisterNetworkClientData { class GameState; struct PlayerConnectedData { - GameState *ptr; + std::shared_ptr<GameState> ptr; }; struct RemoveLoadingScreenData { diff --git a/src/GameState.cpp b/src/GameState.cpp index 77b6394..c49b332 100644 --- a/src/GameState.cpp +++ b/src/GameState.cpp @@ -2,7 +2,7 @@ #include "Event.hpp" #include <iomanip> -GameState::GameState(NetworkClient *networkClient) : nc(networkClient) { +GameState::GameState(std::shared_ptr<NetworkClient> networkClient) : nc(networkClient) { Front = glm::vec3(0.0f, 0.0f, -1.0f); this->SetPosition(glm::vec3(0.0f, 0.0f, 3.0f)); this->WorldUp = glm::vec3(0.0f, 1.0f, 0.0f); @@ -195,7 +195,7 @@ void GameState::UpdatePacket() g_ReducedDebugInfo = packet->ReducedDebugInfo; LOG(INFO) << "Gamemode is " << g_Gamemode << ", Difficulty is " << (int)g_Difficulty << ", Level Type is " << g_LevelType; - EventAgregator::PushEvent(EventType::PlayerConnected, PlayerConnectedData{ this }); + EventAgregator::PushEvent(EventType::PlayerConnected, PlayerConnectedData{ gs }); break; } case Map: diff --git a/src/GameState.hpp b/src/GameState.hpp index d104e54..f09e5ab 100644 --- a/src/GameState.hpp +++ b/src/GameState.hpp @@ -10,9 +10,9 @@ #include "Event.hpp" class GameState { - NetworkClient *nc; + std::shared_ptr<NetworkClient> nc; public: - GameState(NetworkClient *networkClient); + GameState(std::shared_ptr<NetworkClient> networkClient); void Update(float deltaTime); @@ -68,4 +68,6 @@ public: double g_PlayerVelocityX = 0; double g_PlayerVelocityY = 0; double g_PlayerVelocityZ = 0; + + std::shared_ptr<GameState> gs; }; diff --git a/src/Render.cpp b/src/Render.cpp index 4d0f4b7..a175278 100644 --- a/src/Render.cpp +++ b/src/Render.cpp @@ -5,7 +5,7 @@ #include "AssetManager.hpp" #include "Event.hpp" -Render::Render(unsigned int windowWidth, unsigned int windowHeight, std::string windowTitle) { +Render::Render(unsigned int windowWidth, unsigned int windowHeight, std::string windowTitle) : timer(std::chrono::milliseconds(16)) { InitSfml(windowWidth, windowHeight, windowTitle); glCheckError(); InitGlew(); @@ -56,17 +56,19 @@ void Render::PrepareToRendering() { //TextureAtlas texture glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, AssetManager::Instance().GetTextureAtlas()); + AssetManager::Instance().GetTextureAtlasIndexes(); } void Render::RenderFrame() { glClearColor(0.2f, 0.3f, 0.3f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - if (world) { - world->Update(); - } if (renderWorld) - world->Render(renderState); + world->Render(renderState); + + if (world) { + world->Update(timer.RemainTimeMs()); + } window->display(); } @@ -95,7 +97,7 @@ void Render::HandleEvents() { SetMouseCapture(!isMouseCaptured); break; case sf::Keyboard::U: - EventAgregator::PushEvent(EventType::ConnectToServer, ConnectToServerData{ "127.0.0.1", 25565 }); + EventAgregator::PushEvent(EventType::ConnectToServer, ConnectToServerData{ "10.1.1.2", 25565 }); break; case sf::Keyboard::I: EventAgregator::PushEvent(EventType::Disconnect, DisconnectData{ "Manual disconnect" }); @@ -211,8 +213,7 @@ void Render::ExecuteRenderLoop() { listener.RegisterHandler(EventType::Connecting, [this](EventData eventData) { window->setTitle("Connecting"); }); - - LoopExecutionTimeController timer(std::chrono::milliseconds(16)); + while (isRunning) { HandleEvents(); if (isMouseCaptured) HandleMouseCapture(); diff --git a/src/Render.hpp b/src/Render.hpp index 83b3d1a..6b4b5a4 100644 --- a/src/Render.hpp +++ b/src/Render.hpp @@ -13,6 +13,7 @@ class Render { std::unique_ptr<RendererWorld> world; bool renderWorld = false; RenderState renderState; + LoopExecutionTimeController timer; void SetMouseCapture(bool IsCaptured); diff --git a/src/RendererSection.cpp b/src/RendererSection.cpp index 16514b2..609333c 100644 --- a/src/RendererSection.cpp +++ b/src/RendererSection.cpp @@ -33,15 +33,15 @@ RendererSection::~RendererSection() { refCounterVbo[VboModels]--; refCounterVbo[VboColors]--; refCounterVao[Vao]--; - if (refCounterVbo[VboTextures] == 0) + if (refCounterVbo[VboTextures] <= 0) glDeleteBuffers(1, &VboTextures); - if (refCounterVbo[VboModels] == 0) + if (refCounterVbo[VboModels] <= 0) glDeleteBuffers(1, &VboTextures); - if (refCounterVbo[VboColors] == 0) + if (refCounterVbo[VboColors] <= 0) glDeleteBuffers(1, &VboColors); - if (refCounterVao[Vao] == 0) + if (refCounterVao[Vao] <= 0) glDeleteVertexArrays(1, &Vao); } diff --git a/src/RendererWorld.cpp b/src/RendererWorld.cpp index 61c2afa..194ddd8 100644 --- a/src/RendererWorld.cpp +++ b/src/RendererWorld.cpp @@ -1,97 +1,6 @@ #include "RendererWorld.hpp" -void RendererWorld::LoadedSectionController() { - el::Helpers::setThreadName("RenderParser"); - std::function<void(Vector)> updateAllSections = [this](Vector playerPos) { - Vector playerChunk(std::floor(gs->g_PlayerX / 16), 0, std::floor(gs->g_PlayerZ / 16)); - - std::vector<Vector> suitableChunks; - auto chunks = gs->world.GetSectionsList(); - for (auto& it : chunks) { - double distance = (Vector(it.x, 0, it.z) - playerChunk).GetLength(); - if (distance > MaxRenderingDistance) - continue; - suitableChunks.push_back(it); - } - - std::vector<Vector> toRemove; - - sectionsMutex.lock(); - for (auto& it : sections) { - if (std::find(suitableChunks.begin(), suitableChunks.end(), it.first) == suitableChunks.end()) - toRemove.push_back(it.first); - } - sectionsMutex.unlock(); - - for (auto& it : toRemove) { - EventAgregator::PushEvent(EventType::DeleteSectionRender, DeleteSectionRenderData{ it }); - } - std::sort(suitableChunks.begin(), suitableChunks.end(), [playerChunk](Vector lhs, Vector rhs) { - return (playerChunk - lhs).GetLength() < (playerChunk - rhs).GetLength(); - }); - for (auto& it : suitableChunks) { - EventAgregator::PushEvent(EventType::ChunkChanged, ChunkChangedData{ it }); - } - }; - - - EventListener contentListener; - contentListener.RegisterHandler(EventType::ChunkChanged, [this](EventData eventData) { - auto vec = std::get<ChunkChangedData>(eventData).chunkPosition; - Vector playerChunk(std::floor(gs->g_PlayerX / 16), 0, std::floor(gs->g_PlayerZ / 16)); - - if ((Vector(vec.x, 0, vec.z) - playerChunk).GetLength() > MaxRenderingDistance) - return; - - sectionsMutex.lock(); - auto& result = sections.find(vec); - if (result != sections.end()) { - if (result->second.GetHash() != gs->world.GetSection(result->first).GetHash()) { - sectionsMutex.unlock(); - RendererSectionData data(&gs->world, vec); - renderDataMutex.lock(); - renderData.push(data); - renderDataMutex.unlock(); - EventAgregator::PushEvent(EventType::NewRenderDataAvailable, NewRenderDataAvailableData{}); - sectionsMutex.lock(); - } - } - else { - sectionsMutex.unlock(); - RendererSectionData data(&gs->world, vec); - renderDataMutex.lock(); - renderData.push(data); - renderDataMutex.unlock(); - EventAgregator::PushEvent(EventType::NewRenderDataAvailable, NewRenderDataAvailableData{}); - sectionsMutex.lock(); - } - sectionsMutex.unlock(); - }); - - contentListener.RegisterHandler(EventType::PlayerPosChanged, [this,&updateAllSections](EventData eventData) { - auto pos = std::get<PlayerPosChangedData>(eventData).newPos; - updateAllSections(Vector(pos.x,pos.y,pos.z)); - }); - - contentListener.RegisterHandler(EventType::UpdateSectionsRender, [this,&updateAllSections](EventData eventData) { - updateAllSections(Vector(gs->g_PlayerX, gs->g_PlayerY, gs->g_PlayerZ)); - }); - - - LoopExecutionTimeController timer(std::chrono::milliseconds(32)); - auto timeSincePreviousUpdate = std::chrono::steady_clock::now(); - while (isRunning) { - while (contentListener.IsEventsQueueIsNotEmpty()) - contentListener.HandleEvent(); - if (std::chrono::steady_clock::now() - timeSincePreviousUpdate > std::chrono::seconds(15)) { - EventAgregator::PushEvent(EventType::UpdateSectionsRender, UpdateSectionsRenderData{}); - timeSincePreviousUpdate = std::chrono::steady_clock::now(); - } - timer.Update(); - } -} - -/*void RendererWorld::WorkerFunction(size_t workerId) { +void RendererWorld::WorkerFunction(size_t workerId) { EventListener tasksListener; tasksListener.RegisterHandler(EventType::RendererWorkerTask, [&](EventData eventData) { @@ -112,6 +21,11 @@ void RendererWorld::LoadedSectionController() { EventAgregator::PushEvent(EventType::NewRenderDataAvailable, NewRenderDataAvailableData{}); sectionsMutex.lock(); } + else { + isParsingMutex.lock(); + isParsing[vec] = false; + isParsingMutex.unlock(); + } } else { sectionsMutex.unlock(); @@ -123,52 +37,55 @@ void RendererWorld::LoadedSectionController() { sectionsMutex.lock(); } sectionsMutex.unlock(); - LOG(INFO) << "Task " << vec << " done in " << workerId; - }); LoopExecutionTimeController timer(std::chrono::milliseconds(50)); while (isRunning) { - while (tasksListener.IsEventsQueueIsNotEmpty()) + while (tasksListener.IsEventsQueueIsNotEmpty() && isRunning) tasksListener.HandleEvent(); timer.Update(); } -}*/ +} -void RendererWorld::RenderBlocks(RenderState& renderState) +void RendererWorld::UpdateAllSections(VectorF playerPos) { - renderState.SetActiveShader(blockShader->Program); - glCheckError(); + Vector playerChunk(std::floor(gs->g_PlayerX / 16), 0, std::floor(gs->g_PlayerZ / 16)); - GLint projectionLoc = glGetUniformLocation(blockShader->Program, "projection"); - GLint viewLoc = glGetUniformLocation(blockShader->Program, "view"); - GLint windowSizeLoc = glGetUniformLocation(blockShader->Program, "windowSize"); - glm::mat4 projection = glm::perspective(45.0f, (float)renderState.WindowWidth / (float)renderState.WindowHeight, 0.1f, 10000000.0f); - glm::mat4 view = gs->GetViewMatrix(); - glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection)); - glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view)); - glUniform2f(windowSizeLoc, renderState.WindowWidth, renderState.WindowHeight); + std::vector<Vector> suitableChunks; + auto chunks = gs->world.GetSectionsList(); + for (auto& it : chunks) { + double distance = (Vector(it.x, 0, it.z) - playerChunk).GetLength(); + if (distance > MaxRenderingDistance) + continue; + suitableChunks.push_back(it); + } - glCheckError(); + std::vector<Vector> toRemove; sectionsMutex.lock(); for (auto& it : sections) { - - it.second.Render(renderState); + if (std::find(suitableChunks.begin(), suitableChunks.end(), it.first) == suitableChunks.end()) + toRemove.push_back(it.first); } - sectionsMutex.unlock(); -} -void RendererWorld::RenderEntities(RenderState& renderState) -{ - renderState.SetActiveShader(entityShader->Program); - glCheckError(); + for (auto& it : toRemove) { + EventAgregator::PushEvent(EventType::DeleteSectionRender, DeleteSectionRenderData{ it }); + } + + playerChunk.y = std::floor(gs->g_PlayerY / 16.0); + std::sort(suitableChunks.begin(), suitableChunks.end(), [playerChunk](Vector lhs, Vector rhs) { + return (playerChunk - lhs).GetLength() < (playerChunk - rhs).GetLength(); + }); + for (auto& it : suitableChunks) { + EventAgregator::PushEvent(EventType::ChunkChanged, ChunkChangedData{ it }); + } } -RendererWorld::RendererWorld(GameState * ptr):gs(ptr) { - MaxRenderingDistance = 15; +RendererWorld::RendererWorld(std::shared_ptr<GameState> ptr):gs(ptr) { + MaxRenderingDistance = 2; + numOfWorkers = 4; PrepareRender(); @@ -187,8 +104,15 @@ RendererWorld::RendererWorld(GameState * ptr):gs(ptr) { listener.RegisterHandler(EventType::NewRenderDataAvailable,[this](EventData eventData) { renderDataMutex.lock(); - while (!renderData.empty()) { - auto &data = renderData.front(); + int i = 0; + while (!renderData.empty() && i<20) { + auto &data = renderData.front(); + isParsingMutex.lock(); + if (isParsing[data.sectionPos] != true) + LOG(WARNING) << "Generated not parsed data"; + isParsing[data.sectionPos] = false; + isParsingMutex.unlock(); + sectionsMutex.lock(); if (sections.find(data.sectionPos) != sections.end()) { if (sections.find(data.sectionPos)->second.GetHash() == data.hash) { @@ -204,20 +128,9 @@ RendererWorld::RendererWorld(GameState * ptr):gs(ptr) { sectionsMutex.unlock(); renderData.pop(); } + if (renderData.empty()) + std::queue<RendererSectionData>().swap(renderData); renderDataMutex.unlock(); - - /*sectionsMutex.lock(); - renderList.clear(); - for (auto& it : sections) { - renderList.push_back(it.first); - } - sectionsMutex.unlock(); - std::sort(renderList.begin(), renderList.end(), [&](Vector lhs, Vector rhs) { - VectorF playerPos(gs->g_PlayerX, gs->g_PlayerY, gs->g_PlayerZ); - VectorF left = VectorF(lhs.x, lhs.y, lhs.z) * 16 - playerPos; - VectorF right = VectorF(rhs.x, rhs.y, rhs.z) * 16 - playerPos; - return left.GetLength() > right.GetLength(); - });*/ }); listener.RegisterHandler(EventType::EntityChanged, [this](EventData eventData) { @@ -229,29 +142,42 @@ RendererWorld::RendererWorld(GameState * ptr):gs(ptr) { } }); - /*listener.RegisterHandler(EventType::ChunkChanged, [this](EventData eventData) { + listener.RegisterHandler(EventType::ChunkChanged, [this](EventData eventData) { auto vec = std::get<ChunkChangedData>(eventData).chunkPosition; Vector playerChunk(std::floor(gs->g_PlayerX / 16), 0, std::floor(gs->g_PlayerZ / 16)); double distanceToChunk = (Vector(vec.x, 0, vec.z) - playerChunk).GetLength(); - if (distanceToChunk > MaxRenderingDistance) { - //LOG(WARNING) << (Vector(vec.x, 0, vec.z) - playerChunk).GetLength(); - //LOG(WARNING) << distanceToChunk; + if (MaxRenderingDistance != 1000 && distanceToChunk > MaxRenderingDistance) { + return; + } + + isParsingMutex.lock(); + if (isParsing.find(vec) == isParsing.end()) + isParsing[vec] = false; + if (isParsing[vec] == true) { + //LOG(WARNING) << "Changed parsing block"; + isParsingMutex.unlock(); return; } + isParsing[vec] = true; + isParsingMutex.unlock(); EventAgregator::PushEvent(EventType::RendererWorkerTask, RendererWorkerTaskData{ currentWorker++,vec }); - if (currentWorker > numOfWorkers) + if (currentWorker >= numOfWorkers) currentWorker = 0; - LOG(INFO) << "New task " << vec << " for " << currentWorker; }); - for (int i = 0; i < numOfWorkers; i++) - workers[i] = std::thread(&RendererWorld::WorkerFunction, this, i);*/ + listener.RegisterHandler(EventType::UpdateSectionsRender, [this](EventData eventData) { + UpdateAllSections(VectorF(gs->g_PlayerX,gs->g_PlayerY,gs->g_PlayerZ)); + }); - resourceLoader = std::thread(&RendererWorld::LoadedSectionController, this); + listener.RegisterHandler(EventType::PlayerPosChanged, [this](EventData eventData) { + auto pos = std::get<PlayerPosChangedData>(eventData).newPos; + UpdateAllSections(pos); + }); - //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); + for (int i = 0; i < numOfWorkers; i++) + workers.push_back(std::thread(&RendererWorld::WorkerFunction, this, i)); } RendererWorld::~RendererWorld() { @@ -263,9 +189,8 @@ RendererWorld::~RendererWorld() { sectionsMutex.unlock(); LOG(INFO) << "Total faces to render: "<<faces; isRunning = false; - /*for (int i = 0; i < numOfWorkers; i++) - workers[i].join();*/ - resourceLoader.join(); + for (int i = 0; i < numOfWorkers; i++) + workers[i].join(); delete blockShader; delete entityShader; } @@ -284,36 +209,6 @@ void RendererWorld::Render(RenderState & renderState) { glUniform2f(windowSizeLoc, renderState.WindowWidth, renderState.WindowHeight); glCheckError(); - /*for (auto& pos : renderList) { - std::vector<Vector> sectionCorners = { - Vector(0, 0, 0), - Vector(0, 0, 16), - Vector(0, 16, 0), - Vector(0, 16, 16), - Vector(16, 0, 0), - Vector(16, 0, 16), - Vector(16, 16, 0), - Vector(16, 16, 16), - }; - bool isBreak = true; - glm::mat4 vp = projection * view; - for (auto &it : sectionCorners) { - glm::vec3 point(pos.x * 16 + it.x, pos.y * 16 + it.y, pos.z * 16 + it.z); - glm::vec4 p = vp * glm::vec4(point, 1); - glm::vec3 res = glm::vec3(p) / p.w; - if (res.x < 1 && res.x > -1 && res.y < 1 && res.y > -1 && res.z > 0) { - isBreak = false; - break; - } - } - double lengthToSection = (VectorF(gs->g_PlayerX, gs->g_PlayerY, gs->g_PlayerZ) - VectorF(pos.x * 16, pos.y * 16, pos.z * 16)).GetLength(); - - if (isBreak && lengthToSection > 30.0f) { - continue; - } - - sections.find(pos)->second.Render(renderState); - }*/ sectionsMutex.lock(); for (auto& section : sections) { sectionsMutex.unlock(); @@ -386,7 +281,13 @@ bool RendererWorld::IsNeedResourcesPrepare() { return false; } -void RendererWorld::Update() { - while (listener.IsEventsQueueIsNotEmpty()) +void RendererWorld::Update(double timeToUpdate) { + auto timeSincePreviousUpdate = std::chrono::steady_clock::now(); + int i = 0; + while (listener.IsEventsQueueIsNotEmpty() && i++ < 50) listener.HandleEvent(); + if (std::chrono::steady_clock::now() - timeSincePreviousUpdate > std::chrono::seconds(5)) { + EventAgregator::PushEvent(EventType::UpdateSectionsRender, UpdateSectionsRenderData{}); + timeSincePreviousUpdate = std::chrono::steady_clock::now(); + } } diff --git a/src/RendererWorld.hpp b/src/RendererWorld.hpp index 54c1826..e7bcdf0 100644 --- a/src/RendererWorld.hpp +++ b/src/RendererWorld.hpp @@ -9,27 +9,26 @@ class RendererWorld: public Renderer { //General - GameState *gs; + std::shared_ptr<GameState> gs; EventListener listener; - /*static const size_t numOfWorkers = 4; + size_t numOfWorkers; size_t currentWorker = 0; - std::thread workers[numOfWorkers]; - void WorkerFunction(size_t WorkerId);*/ - std::thread resourceLoader; - void LoadedSectionController(); + std::vector<std::thread> workers; + void WorkerFunction(size_t WorkerId); bool isRunning = true; + std::mutex isParsingMutex; + std::map<Vector, bool> isParsing; //Blocks std::vector<Vector> renderList; std::mutex sectionsMutex; std::map<Vector, RendererSection> sections; Shader *blockShader; - void RenderBlocks(RenderState& renderState); + void UpdateAllSections(VectorF playerPos); //Entities Shader *entityShader; std::vector<RendererEntity> entities; - void RenderEntities(RenderState& renderState); public: - RendererWorld(GameState* ptr); + RendererWorld(std::shared_ptr<GameState> ptr); ~RendererWorld(); void Render(RenderState& renderState) override; @@ -39,7 +38,7 @@ public: double MaxRenderingDistance; - void Update(); + void Update(double timeToUpdate); std::mutex renderDataMutex; std::queue<RendererSectionData> renderData; diff --git a/src/Texture.cpp b/src/Texture.cpp index 5d183c3..5c7ebf7 100644 --- a/src/Texture.cpp +++ b/src/Texture.cpp @@ -8,7 +8,8 @@ Texture::Texture(std::string filename, GLenum textureWrapping, GLenum textureFil glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, textureWrapping); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, textureWrapping); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, textureFiltering); + //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); //Image load @@ -27,7 +28,7 @@ Texture::Texture(std::string filename, GLenum textureWrapping, GLenum textureFil //Creating texture glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.getSize().x, image.getSize().y, 0, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid *) image.getPixelsPtr()); - glGenerateMipmap(GL_TEXTURE_2D); + //glGenerateMipmap(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, 0); } diff --git a/src/ThreadGame.cpp b/src/ThreadGame.cpp index d3750ea..ef7a95e 100644 --- a/src/ThreadGame.cpp +++ b/src/ThreadGame.cpp @@ -19,13 +19,14 @@ void ThreadGame::Execute() { listener.RegisterHandler(EventType::ConnectionSuccessfull, [this](EventData eventData) { auto data = std::get<ConnectionSuccessfullData>(eventData); - gs = new GameState(data.ptr); + gs = std::make_shared<GameState>(data.ptr); + gs->gs = gs; }); listener.RegisterHandler(EventType::Disconnected, [this](EventData eventData) { std::this_thread::sleep_for(std::chrono::milliseconds(500)); - delete gs; - gs = nullptr; + gs->gs.reset(); + gs.reset(); }); listener.RegisterHandler(EventType::KeyPressed, [this](EventData eventData) { @@ -100,5 +101,5 @@ void ThreadGame::Execute() { } timer.Update(); } - delete gs; + gs.reset(); } diff --git a/src/ThreadGame.hpp b/src/ThreadGame.hpp index 9894235..41676d6 100644 --- a/src/ThreadGame.hpp +++ b/src/ThreadGame.hpp @@ -5,7 +5,7 @@ #include "Event.hpp" class ThreadGame: Thread { - GameState *gs = nullptr; + std::shared_ptr<GameState> gs; bool isRunning = true; bool isMoving[5] = { 0,0,0,0,0 }; public: diff --git a/src/ThreadNetwork.cpp b/src/ThreadNetwork.cpp index f734c0c..5a8a61a 100644 --- a/src/ThreadNetwork.cpp +++ b/src/ThreadNetwork.cpp @@ -25,7 +25,7 @@ void ThreadNetwork::Execute() { LOG(INFO) << "Connecting to server"; EventAgregator::PushEvent(EventType::Connecting, ConnectingData{}); try { - nc = new NetworkClient(data.address, data.port, "HelloOne"); + nc = std::make_shared<NetworkClient>(data.address, data.port, "HelloOne"); } catch (std::exception &e) { LOG(WARNING) << "Connection failed"; EventAgregator::PushEvent(EventType::ConnectionFailed, ConnectionFailedData{e.what()}); @@ -39,8 +39,7 @@ void ThreadNetwork::Execute() { auto data = std::get<DisconnectData>(eventData); EventAgregator::PushEvent(EventType::Disconnected, DisconnectedData{ data.reason }); LOG(INFO) << "Disconnected: " << data.reason; - delete nc; - nc = nullptr; + nc.reset(); }); listener.RegisterHandler(EventType::NetworkClientException, [this](EventData eventData) { @@ -54,5 +53,5 @@ void ThreadNetwork::Execute() { timer.Update(); } - delete nc; + nc.reset(); }
\ No newline at end of file diff --git a/src/ThreadNetwork.hpp b/src/ThreadNetwork.hpp index 60df0d0..73f9ab8 100644 --- a/src/ThreadNetwork.hpp +++ b/src/ThreadNetwork.hpp @@ -5,7 +5,7 @@ #include "Event.hpp" class ThreadNetwork : Thread { - NetworkClient *nc = nullptr; + std::shared_ptr<NetworkClient> nc; bool isRunning = true; public: ThreadNetwork(); diff --git a/src/Utility.cpp b/src/Utility.cpp index 655c26f..30561c4 100644 --- a/src/Utility.cpp +++ b/src/Utility.cpp @@ -81,3 +81,9 @@ double LoopExecutionTimeController::GetRealDeltaS() { return std::chrono::duration<double,std::ratio<1,1>>(previousUpdate - previousPreviousUpdate).count(); } + +double LoopExecutionTimeController::RemainTimeMs() +{ + auto remain = delayLength - GetDelta(); + return remain.count(); +} diff --git a/src/Utility.hpp b/src/Utility.hpp index c7ec358..d31a1a3 100644 --- a/src/Utility.hpp +++ b/src/Utility.hpp @@ -57,4 +57,6 @@ public: double GetDeltaS(); double GetRealDeltaS(); + + double RemainTimeMs(); }; diff --git a/src/World.cpp b/src/World.cpp index 44816e7..740c276 100644 --- a/src/World.cpp +++ b/src/World.cpp @@ -2,38 +2,23 @@ #include "Event.hpp" void World::ParseChunkData(std::shared_ptr<PacketChunkData> packet) { - StreamBuffer chunkData(packet->Data.data(), packet->Data.size()); + StreamBuffer chunkData(packet->Data.data(), packet->Data.size()); std::bitset<16> bitmask(packet->PrimaryBitMask); for (int i = 0; i < 16; i++) { if (bitmask[i]) { Vector chunkPosition = Vector(packet->ChunkX, i, packet->ChunkZ); - PackedSection section = ParseSection(&chunkData, chunkPosition); - auto it = sections.find(chunkPosition); - if (it == sections.end()) { - sections.insert(std::make_pair(chunkPosition, section)); + PackedSection packedSection = ParseSection(&chunkData, chunkPosition); + Section section(packedSection); + auto it = cachedSections.find(chunkPosition); + if (it == cachedSections.end()) { + cachedSections.insert(std::make_pair(chunkPosition, section)); } else { using std::swap; swap(it->second, section); } EventAgregator::PushEvent(EventType::ChunkChanged, ChunkChangedData{ chunkPosition }); - - /*parseMutex.lock(); - toParse.push(section); - parseMutex.unlock();*/ - /*section.Parse(); - sectionMutexes[chunkPosition].lock(); - auto it = sections.find(chunkPosition); - if (it == sections.end()) { - sections.insert(std::make_pair(chunkPosition, section)); - } else { - using std::swap; - swap(it->second, section); - } - sectionMutexes[chunkPosition].unlock(); - - EventAgregator::PushEvent(EventType::ChunkChanged, ChunkChangedData{ chunkPosition });*/ - } + } } } @@ -53,38 +38,7 @@ PackedSection World::ParseSection(StreamInput *data, Vector position) { return PackedSection(position, dataArray.data(), dataArray.size(), blockLight.data(), (skyLight.size() > 0 ? skyLight.data() : nullptr), bitsPerBlock, palette); } -/* -void World::ParserFunc() -{ - LoopExecutionTimeController timer(std::chrono::milliseconds(32)); - while (isRunning) { - parseMutex.lock(); - while (toParse.size() > 0) { - Section section = toParse.front(); - toParse.pop(); - parseMutex.unlock(); - - section.Parse(); - sectionMutexes[section.GetPosition()].lock(); - auto it = sections.find(section.GetPosition()); - if (it == sections.end()) { - sections.insert(std::make_pair(section.GetPosition(), section)); - } - else { - using std::swap; - swap(it->second, section); - } - sectionMutexes[section.GetPosition()].unlock(); - EventAgregator::PushEvent(EventType::ChunkChanged, ChunkChangedData{ section.GetPosition() }); - - parseMutex.lock(); - } - parseMutex.unlock(); - timer.Update(); - } -} -*/ World::~World() { } @@ -112,6 +66,8 @@ World::World() { bool World::isPlayerCollides(double X, double Y, double Z) { Vector PlayerChunk(floor(X / 16.0), floor(Y / 16.0), floor(Z / 16.0)); + if (cachedSections.find(PlayerChunk) == cachedSections.end() || cachedSections.find(PlayerChunk - Vector(0,1,0)) == cachedSections.end()) + return true; std::vector<Vector> closestSectionsCoordinates = { Vector(PlayerChunk.x, PlayerChunk.y, PlayerChunk.z), Vector(PlayerChunk.x + 1, PlayerChunk.y, PlayerChunk.z), |