diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/core/AssetManager.cpp | 32 | ||||
-rw-r--r-- | src/core/Core.cpp | 47 | ||||
-rw-r--r-- | src/core/Core.hpp | 3 | ||||
-rw-r--r-- | src/gamestate/Game.cpp | 173 | ||||
-rw-r--r-- | src/gamestate/Game.hpp | 58 | ||||
-rw-r--r-- | src/gamestate/GameState.cpp | 27 | ||||
-rw-r--r-- | src/gamestate/GameState.hpp | 4 | ||||
-rw-r--r-- | src/graphics/AssetManager_old.cpp | 146 | ||||
-rw-r--r-- | src/graphics/AssetManager_old.hpp | 53 | ||||
-rw-r--r-- | src/graphics/Display.cpp | 353 | ||||
-rw-r--r-- | src/graphics/Display.hpp | 33 | ||||
-rw-r--r-- | src/main.cpp | 17 | ||||
-rw-r--r-- | src/network/Network.cpp | 39 | ||||
-rw-r--r-- | src/network/NetworkClient.cpp | 7 | ||||
-rw-r--r-- | src/network/NetworkClient.hpp | 2 | ||||
-rw-r--r-- | src/world/Block.cpp | 4 | ||||
-rw-r--r-- | src/world/Block.hpp | 4 | ||||
-rw-r--r-- | src/world/Section.cpp | 2 | ||||
-rw-r--r-- | src/world/World.cpp | 20 | ||||
-rw-r--r-- | src/world/World.hpp | 18 |
20 files changed, 111 insertions, 931 deletions
diff --git a/src/core/AssetManager.cpp b/src/core/AssetManager.cpp index 9913c18..003f2f0 100644 --- a/src/core/AssetManager.cpp +++ b/src/core/AssetManager.cpp @@ -1,5 +1,4 @@ -#include <easylogging++.h> -#include <nlohmann/json.hpp> + #include "AssetManager.hpp" namespace fs = std::experimental::filesystem; @@ -24,7 +23,7 @@ void AssetManager::LoadIds() { int id = it["type"].get<int>(); int state = it["meta"].get<int>(); std::string blockName = it["text_type"].get<std::string>(); - assetIds[blockName] = Block(id, state, 0); + assetIds[blockName] = Block(id, 0, state); } LOG(INFO) << "Loaded " << assetIds.size() << " ids"; } @@ -43,14 +42,13 @@ void AssetManager::LoadTextureResources() { auto frame = it["frame"]; TextureCoord coord; coord.x = frame["x"].get<int>(); - coord.y = frame["y"].get<int>(); + coord.y = frame["y"].get<int>();; coord.w = frame["w"].get<int>(); coord.h = frame["h"].get<int>(); std::string assetName = it["filename"].get<std::string>(); - assetName.insert(0,"minecraft/textures/"); - assetName.erase(assetName.length()-4); - LOG(ERROR)<<assetName; - assetTextures[assetName]=coord; + assetName.insert(0, "minecraft/textures/"); + assetName.erase(assetName.length() - 4); + assetTextures[assetName] = coord; } textureAtlas = new Texture(filename); @@ -58,11 +56,27 @@ void AssetManager::LoadTextureResources() { } TextureCoord AssetManager::GetTextureByAssetName(std::string AssetName) { - return TextureCoord{0, 0, 0, 0}; + return assetTextures[AssetName]; } std::string AssetManager::GetTextureAssetNameByBlockId(unsigned short BlockId, unsigned char BlockSide) { + //Block sides: 0 - bottom, 1 - top, 2 - north, 3 - south, 4 - west, 5 - east + std::map<Block, std::string> lookupTable = { + {Block(0), "minecraft/textures/blocks/air"}, + {Block(1, 0), "minecraft/textures/blocks/stone"}, + {Block(1, 1), "minecraft/textures/blocks/stone_granite"}, + + {Block(2, 0, 0), "minecraft/textures/blocks/dirt"}, + {Block(2, 0, 1), "minecraft/textures/blocks/grass_top"}, + {Block(2, 0, 2), "minecraft/textures/blocks/grass_side"}, + {Block(2, 0, 3), "minecraft/textures/blocks/grass_side"}, + {Block(2, 0, 4), "minecraft/textures/blocks/grass_side"}, + {Block(2, 0, 5), "minecraft/textures/blocks/grass_side"}, + {Block(3), "minecraft/textures/blocks/dirt"}, + {Block(4), "minecraft/textures/blocks/cobblestone"}, + }; + return lookupTable[Block(BlockId, BlockSide)]; } const GLuint AssetManager::GetTextureAtlas() { diff --git a/src/core/Core.cpp b/src/core/Core.cpp index 7814c32..1481e36 100644 --- a/src/core/Core.cpp +++ b/src/core/Core.cpp @@ -103,20 +103,21 @@ Core::Core() { LOG(INFO) << "Core initializing..."; InitSfml(1280, 720, "AltCraft"); InitGlew(); - PrepareToWorldRendering(); client = new NetworkClient("127.0.0.1", 25565, "HelloOne"); gameState = new GameState(client); - std::thread loop = std::thread(&Core::UpdateGameState,this); - std::swap(loop,gameStateLoopThread); + std::thread loop = std::thread(&Core::UpdateGameState, this); + std::swap(loop, gameStateLoopThread); assetManager = new AssetManager; + PrepareToWorldRendering(); LOG(INFO) << "Core is initialized"; } Core::~Core() { LOG(INFO) << "Core stopping..."; + gameStateLoopThread.join(); delete shader; - delete client; delete gameState; + delete client; LOG(INFO) << "Core is stopped"; } @@ -181,7 +182,7 @@ void Core::InitSfml(unsigned int WinWidth, unsigned int WinHeight, std::string W contextSetting.attributeFlags = contextSetting.Core; contextSetting.depthBits = 24; window = new sf::Window(sf::VideoMode(WinWidth, WinHeight), WinTitle, sf::Style::Default, contextSetting); - window->setVerticalSyncEnabled(true); + //window->setVerticalSyncEnabled(true); window->setPosition(sf::Vector2i(sf::VideoMode::getDesktopMode().width / 2 - window->getSize().x / 2, sf::VideoMode::getDesktopMode().height / 2 - window->getSize().y / 2)); @@ -228,6 +229,12 @@ void Core::HandleEvents() { case sf::Keyboard::T: SetMouseCapture(!isMouseCaptured); break; + case sf::Keyboard::Z: + camera.MovementSpeed /= 2; + break; + case sf::Keyboard::X: + camera.MovementSpeed *= 2; + break; default: break; } @@ -269,7 +276,7 @@ void Core::RenderWorld(World &Target) { GLint viewLoc = glGetUniformLocation(shader->Program, "view"); GLint blockLoc = glGetUniformLocation(shader->Program, "block"); GLint timeLoc = glGetUniformLocation(shader->Program, "time"); - glm::mat4 projection = glm::perspective(camera.Zoom, (float) width() / (float) height(), 0.1f, 1000.0f); + glm::mat4 projection = glm::perspective(camera.Zoom, (float) width() / (float) height(), 0.0001f, 1000.0f); glm::mat4 view = camera.GetViewMatrix(); glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection)); glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view)); @@ -282,20 +289,19 @@ void Core::RenderWorld(World &Target) { for (int y = 0; y < 16; y++) { for (int z = 0; z < 16; z++) { for (int x = 0; x < 16; x++) { + Block block = section.GetBlock(Vector(x, y, z)); + if (block.id==0) + continue; + glm::mat4 model; - model = glm::translate(model, - glm::vec3(sectionPos.GetX() * 16, sectionPos.GetY() * 16, - sectionPos.GetZ() * 16)); + model = glm::translate(model, glm::vec3(sectionPos.GetX() * 16, sectionPos.GetY() * 16, + sectionPos.GetZ() * 16)); model = glm::translate(model, glm::vec3(x, y, z)); - Block block = section.GetBlock(Vector(x, y, z)); + glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model)); glUniform1i(blockLoc, block.id); - glActiveTexture(GL_TEXTURE0); - //glBindTexture(GL_TEXTURE_2D, texture1.texture); - glUniform1i(glGetUniformLocation(shader->Program, "blockTexture"), 0); - glDrawArrays(GL_TRIANGLES, 0, 36); } } @@ -330,14 +336,19 @@ void Core::PrepareToWorldRendering() { } glBindVertexArray(0); - shader = new Shader("./shaders/simple.vs", "./shaders/simple.fs"); + shader = new Shader("./shaders/block.vs", "./shaders/block.fs"); shader->Use(); + + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, assetManager->GetTextureAtlas()); + glUniform1i(glGetUniformLocation(shader->Program, "textureAtlas"), 0); + } void Core::UpdateChunksToRender() { camera.Position = glm::vec3(gameState->g_PlayerX, gameState->g_PlayerY, gameState->g_PlayerZ); toRender.clear(); - const float ChunkDistance = 1; + const float ChunkDistance = 1.3; Vector playerChunk = Vector(floor(gameState->g_PlayerX / 16.0f), floor(gameState->g_PlayerY / 16.0f), floor(gameState->g_PlayerZ / 16.0f)); for (auto &it:gameState->world.m_sections) { @@ -351,7 +362,9 @@ void Core::UpdateChunksToRender() { } void Core::UpdateGameState() { - while (gameState && client){ + LOG(INFO) << "GameState thread is started"; + while (isRunning) { gameState->Update(); } + LOG(INFO) << "GameState thread is stopped"; } diff --git a/src/core/Core.hpp b/src/core/Core.hpp index 1c2bbc5..8bf74da 100644 --- a/src/core/Core.hpp +++ b/src/core/Core.hpp @@ -17,7 +17,8 @@ class Core { NetworkClient *client; sf::Window *window; AssetManager *assetManager; - bool isMouseCaptured = false, isRunning = true; + bool isMouseCaptured = false; + bool isRunning = true; enum { MainMenu, Loading, diff --git a/src/gamestate/Game.cpp b/src/gamestate/Game.cpp deleted file mode 100644 index 15f83e6..0000000 --- a/src/gamestate/Game.cpp +++ /dev/null @@ -1,173 +0,0 @@ -#include "Game.hpp" -#include "../packet/PacketParser.hpp" -#include "../packet/PacketBuilder.hpp" -#include <nlohmann/json.hpp> - -Game::Game() { - m_display = new Display(1280, 720, "AltCraft", &m_world); - m_nc = new NetworkClient("127.0.0.1", 25565, "HelloOne"); - Packet &response = *m_nc->GetPacket(); - if (response.GetId() != 0x02) { - std::cout << response.GetId() << std::endl; - throw 127; - } - PacketParser::Parse(response, Login); - g_PlayerUuid = response.GetField(0).GetString(); - g_PlayerName = response.GetField(1).GetString(); - delete &response; - m_networkState = ConnectionState::Play; - std::cout << g_PlayerName << "'s UUID is " << g_PlayerUuid << std::endl; -} - -Game::~Game() { - std::cout << "Stopping game thread..." << std::endl; - m_exit = true; - m_gameThread.join(); - std::cout << "Stopping graphics..." << std::endl; - delete m_display; - std::cout << "Stopping network..." << std::endl; - delete m_nc; -} - -void Game::MainLoop() { - while (!m_exit) { - - ParsePackets(); - if (m_display->IsClosed()) - m_exit = true; - } -} - -void Game::ParsePackets() { - Packet *packetPtr = m_nc->GetPacket(); - if (!packetPtr) { - using namespace std::chrono_literals; - std::this_thread::sleep_for(16ms); - return; - } - Packet packet = *packetPtr; - delete packetPtr; - PacketParser::Parse(packet); - nlohmann::json json; - //std::cout<<"Parsing packet: "<<std::hex<<packet.GetId()<<std::dec<<std::endl; - fflush(stdout); - - switch (packet.GetId()) { - case 0x23: - g_PlayerEid = packet.GetField(0).GetInt(); - g_Gamemode = (packet.GetField(1).GetUByte() & 0b11111011); - g_Dimension = packet.GetField(2).GetInt(); - g_Difficulty = packet.GetField(3).GetUByte(); - g_MaxPlayers = packet.GetField(4).GetUByte(); - g_LevelType = packet.GetField(5).GetString(); - g_ReducedDebugInfo = packet.GetField(6).GetBool(); - std::cout << "Gamemode is " << (int) g_Gamemode << ", Difficulty is " << (int) g_Difficulty - << ", Level Type is " << g_LevelType << std::endl; - break; - case 0x0D: - g_Difficulty = packet.GetField(0).GetUByte(); - std::cout << "Difficulty now is " << (int) g_Difficulty << std::endl; - break; - case 0x43: - g_SpawnPosition = packet.GetField(0).GetPosition(); - std::cout << "Spawn position is " << g_SpawnPosition.GetX() << "," << g_SpawnPosition.GetY() << "," - << g_SpawnPosition.GetZ() << std::endl; - break; - case 0x2B: - g_PlayerInvulnerable = (packet.GetField(0).GetByte() & 0x01) != 0; - g_PlayerFlying = (packet.GetField(0).GetByte() & 0x02) != 0; - g_PlayerAllowFlying = (packet.GetField(0).GetByte() & 0x04) != 0; - g_PlayerCreativeMode = (packet.GetField(0).GetByte() & 0x08) != 0; - g_PlayerFlyingSpeed = packet.GetField(1).GetFloat(); - g_PlayerFovModifier = packet.GetField(2).GetFloat(); - std::cout << "Fov modifier is " << g_PlayerFovModifier << std::endl; - break; - case 0x2E: - if ((packet.GetField(5).GetByte() & 0x10) != 0) { - g_PlayerPitch += packet.GetField(4).GetFloat(); - } else { - g_PlayerPitch = packet.GetField(4).GetFloat(); - }; - - if ((packet.GetField(5).GetByte() & 0x08) != 0) { - g_PlayerYaw += packet.GetField(3).GetFloat(); - } else { - g_PlayerYaw = packet.GetField(3).GetFloat(); - } - - if ((packet.GetField(5).GetByte() & 0x01) != 0) { - g_PlayerX += packet.GetField(0).GetDouble(); - } else { - g_PlayerX = packet.GetField(0).GetDouble(); - } - - if ((packet.GetField(5).GetByte() & 0x02) != 0) { - g_PlayerY += packet.GetField(1).GetDouble(); - } else { - g_PlayerY = packet.GetField(1).GetDouble(); - } - - if ((packet.GetField(5).GetByte() & 0x04) != 0) { - g_PlayerZ += packet.GetField(2).GetDouble(); - } else { - g_PlayerZ = packet.GetField(2).GetDouble(); - } - - g_IsGameStarted = true; - m_nc->AddPacketToQueue(PacketBuilder::CPlay0x03(0)); - m_nc->AddPacketToQueue(PacketBuilder::CPlay0x00(packet.GetField(6).GetVarInt())); - std::cout << "Game is started! " << std::endl; - std::cout << "PlayerPos is " << g_PlayerX << ", " << g_PlayerY << ", " << g_PlayerZ << "\tAngle: " - << g_PlayerYaw - << "," << g_PlayerPitch << std::endl; - m_display->SetPlayerPos(g_PlayerX, g_PlayerY, g_PlayerZ); - gameStartWaiter.notify_all(); - break; - case 0x1A: - json = nlohmann::json::parse(packet.GetField(0).GetString()); - std::cout << "Disconnect reason: " << json["text"].get<std::string>() << std::endl; - throw 119; - break; - case 0x20: - m_world.ParseChunkData(packet); - { - std::vector<Vector>vec; - for (auto &it:m_world.m_sections) { - for (auto& it2:vec){ - if (it2==it.first) - std::cout<<it.first<<std::endl; - } - vec.push_back(it.first); - } - } - break; - case 0x07: - std::cout << "Statistics:" << std::endl; - //int items = packet.GetField(0).GetVarInt(); - for (int i = 0; i < packet.GetField(0).GetVarInt(); i++) { - std::cout << "\t" << packet.GetField(1).GetArray()[0].GetString() << ": " - << packet.GetField(1).GetArray()[1].GetVarInt() << std::endl; - } - break; - default: - //std::cout << std::hex << packet.GetId() << std::dec << std::endl; - break; - } - if (g_IsGameStarted) { - std::chrono::steady_clock clock; - static auto timeOfPreviousSendedPpalPacket(clock.now()); - std::chrono::duration<double, std::milli> delta = clock.now() - timeOfPreviousSendedPpalPacket; - if (delta.count() >= 50) { - m_nc->AddPacketToQueue( - PacketBuilder::CPlay0x0D(g_PlayerX, g_PlayerY, g_PlayerZ, g_PlayerYaw, g_PlayerPitch, true)); - timeOfPreviousSendedPpalPacket = clock.now(); - /*std::cout << "PlayerPos is " << g_PlayerX << "," << g_PlayerY << "," << g_PlayerZ << " " << g_PlayerYaw - << "," << g_PlayerPitch << std::endl;*/ - } - } -} - -void Game::Exec() { - m_gameThread = std::thread(&Game::MainLoop, this); - m_display->MainLoop(); -} diff --git a/src/gamestate/Game.hpp b/src/gamestate/Game.hpp deleted file mode 100644 index adf9059..0000000 --- a/src/gamestate/Game.hpp +++ /dev/null @@ -1,58 +0,0 @@ -#pragma once - -#include "../utility/Vector.hpp" -#include "../network/NetworkClient.hpp" -#include "../world/World.hpp" -#include "../graphics/Display.hpp" - -class Game { -public: - Game(); - - ~Game(); - - void Exec(); - -private: - //utility variables - NetworkClient *m_nc; - std::thread m_ncThread; - bool m_exit = false; - ConnectionState m_networkState = ConnectionState::Handshaking; - Display *m_display; - std::thread m_gameThread; - - //utility methods - void ParsePackets(); - void MainLoop(); - - //GameState update - condVars - std::condition_variable gameStartWaiter; - - //game state variables - World m_world; - - std::string g_PlayerUuid; - std::string g_PlayerName; - int g_PlayerEid; - byte g_Gamemode; - byte g_Difficulty; - int g_Dimension; - byte g_MaxPlayers; - std::string g_LevelType; - bool g_ReducedDebugInfo; - Vector g_SpawnPosition; - bool g_PlayerInvulnerable; - bool g_PlayerFlying; - bool g_PlayerAllowFlying; - bool g_PlayerCreativeMode; - int g_PlayerFlyingSpeed; - int g_PlayerFovModifier; - bool g_IsGameStarted = false; - float g_PlayerPitch; - float g_PlayerYaw; - double g_PlayerX; - double g_PlayerY; - double g_PlayerZ; -}; - diff --git a/src/gamestate/GameState.cpp b/src/gamestate/GameState.cpp index 7b6734f..5c5b9c4 100644 --- a/src/gamestate/GameState.cpp +++ b/src/gamestate/GameState.cpp @@ -1,13 +1,10 @@ #include "GameState.hpp" -#include <nlohmann/json.hpp> -#include "../packet/PacketParser.hpp" -#include "../packet/PacketBuilder.hpp" GameState::GameState(NetworkClient *Net) : nc(Net) { Packet *response = nc->GetPacket(); if (response->GetId() != 0x02) { - std::cout << response->GetId() << std::endl; - throw 127; + LOG(ERROR) << "Response id is " << response->GetId(); + throw std::runtime_error("Response id is not 0x02"); } PacketParser::Parse(*response, Login); g_PlayerUuid = response->GetField(0).GetString(); @@ -18,14 +15,20 @@ GameState::GameState(NetworkClient *Net) : nc(Net) { } void GameState::Update() { - Packet &packet = *nc->GetPacket(); - if (&packet == nullptr) - return; + Packet *packetPtr; + try { + packetPtr = nc->GetPacket(); + if (packetPtr == nullptr) + return; + PacketParser::Parse(*packetPtr, m_networkState); + } catch (std::exception &e) { + LOG(ERROR) << "Catched exception during packet pulling: " << e.what(); + return; + } + Packet &packet = *packetPtr; nlohmann::json json; - PacketParser::Parse(packet, m_networkState); - switch (packet.GetId()) { case 0x23: g_PlayerEid = packet.GetField(0).GetInt(); @@ -97,7 +100,7 @@ void GameState::Update() { break; case 0x1A: json = nlohmann::json::parse(packet.GetField(0).GetString()); - std::cout << "Disconnect reason: " << json["text"].get<std::string>() << std::endl; + LOG(INFO) << "Disconnect reason: " << json["text"].get<std::string>(); throw 119; break; case 0x20: @@ -124,5 +127,5 @@ void GameState::Update() { } } - delete &packet; + delete packetPtr; } diff --git a/src/gamestate/GameState.hpp b/src/gamestate/GameState.hpp index 3d7dc80..c9ca44a 100644 --- a/src/gamestate/GameState.hpp +++ b/src/gamestate/GameState.hpp @@ -2,12 +2,16 @@ #include "../world/World.hpp" #include "../network/NetworkClient.hpp" +#include "../packet/PacketParser.hpp" +#include "../packet/PacketBuilder.hpp" class GameState { NetworkClient *nc; public: GameState(NetworkClient *NetClient); + World world; + void Update(); std::string g_PlayerUuid; diff --git a/src/graphics/AssetManager_old.cpp b/src/graphics/AssetManager_old.cpp deleted file mode 100644 index ef856ca..0000000 --- a/src/graphics/AssetManager_old.cpp +++ /dev/null @@ -1,146 +0,0 @@ -#include "AssetManager_old.hpp" - -const std::string pathToAssets = "./assets/"; -const std::string pathToObjects = pathToAssets + "objects/"; -const std::string pathToIndexFile = pathToAssets + "indexes/1.11.json"; -const std::string pathToAssetsMc = "./assets/"; -const std::map<Asset::AssetType, std::string> assetTypeFileExtensions{ - std::make_pair(Asset::AssetType::Texture, ".png"), - std::make_pair(Asset::AssetType::Lang, ".lang"), - std::make_pair(Asset::AssetType::Sound, ".ogg"), -}; - -AssetManager_old::AssetManager_old() { - return; - std::ifstream indexFile(pathToIndexFile); - if (!indexFile) { - std::cerr << "Can't open file " << pathToIndexFile << std::endl; - } - nlohmann::json json = nlohmann::json::parse(indexFile)["objects"]; - for (auto it = json.begin(); it != json.end(); ++it) { - size_t fileNameExtensionPos = -1; - std::string name = it.key(); - Asset::AssetType type = Asset::Unknown; - for (auto &it:assetTypeFileExtensions) { - if ((fileNameExtensionPos = name.find(it.second)) != std::string::npos) { - type = it.first; - name = name.substr(0, fileNameExtensionPos); - break; - } - } - std::string hash = it.value()["hash"].get<std::string>(); - size_t size = it.value()["size"].get<int>(); - Asset asset{name, hash, Asset::AssetData(), size, type}; - this->assets[name] = asset; - } -} - -AssetManager_old::~AssetManager_old() { - -} - -Asset &AssetManager_old::GetAsset(std::string AssetName) { - if (instance().assets.find(AssetName) == instance().assets.end() || !instance().assets[AssetName].isParsed()) - LoadAsset(AssetName); - return instance().assets[AssetName]; -} - -void AssetManager_old::LoadAsset(std::string AssetName) { - if (instance().assets.find(AssetName) != instance().assets.end() && instance().assets[AssetName].isParsed()) - return; - std::string AssetFileName = GetPathToAsset(AssetName); - Asset &asset = instance().assets[AssetName]; - - - if (asset.type == Asset::Texture) { - asset.data.texture = new Texture(AssetFileName,GL_CLAMP_TO_BORDER,GL_NEAREST); - //asset.data.texture.loadFromFile((asset.name + assetTypeFileExtensions.at(asset.type))); - } -} - -std::string AssetManager_old::GetPathToAsset(std::string AssetName) { - if (instance().assets.find(AssetName) != instance().assets.end()){ - auto it = instance().assets.find(AssetName); - return pathToObjects + std::string(instance().assets[AssetName].hash.c_str(), 2) + "/" + - instance().assets[AssetName].hash; - } - - instance().assets[AssetName].hash=""; - instance().assets[AssetName].type=Asset::AssetType::Texture; - instance().assets[AssetName].name=AssetName; - instance().assets[AssetName].size=0; - return pathToAssetsMc + "" + instance().assets[AssetName].name + - assetTypeFileExtensions.at(instance().assets[AssetName].type); -} - -std::string AssetManager_old::GetAssetNameByBlockId(unsigned short id) { - std::string assetBase = "minecraft/textures/blocks/"; - std::string textureName; - switch (id){ - case 0: - textureName="air"; - break; - case 1: - textureName="stone"; - break; - case 2: - textureName="grass"; - break; - case 3: - textureName="dirt"; - break; - case 4: - textureName="cobblestone"; - break; - case 16: - textureName="coal_ore"; - break; - case 17: - textureName="log_oak"; - break; - case 31: - textureName="air"; - break; - default: - //std::cout<<id<<std::endl; - textureName="beacon"; - break; - } - return assetBase+textureName; -} - -bool Asset::isParsed() { - switch (type) { - case Unknown: - return false; - break; - case Texture: - return this->data.texture != nullptr; - break; - case Sound: - return false; - break; - case Model: - return false; - break; - case Lang: - return false; - break; - } -} - -Asset::~Asset() { - switch (type) { - case Unknown: - break; - case Texture: - delete this->data.texture; - break; - case Sound: - break; - case Model: - break; - case Lang: - break; - } -} diff --git a/src/graphics/AssetManager_old.hpp b/src/graphics/AssetManager_old.hpp deleted file mode 100644 index 23a11a7..0000000 --- a/src/graphics/AssetManager_old.hpp +++ /dev/null @@ -1,53 +0,0 @@ -#pragma once - -#include <fstream> -#include <string> -#include <map> -#include <experimental/filesystem> -#include <nlohmann/json.hpp> -#include "Texture.hpp" - -struct Asset { - std::string name = ""; - std::string hash = ""; - union AssetData{ - Texture *texture; - } data; - size_t size = 0; - enum AssetType { - Unknown, - Texture, - Sound, - Model, - Lang, - } type = Unknown; - bool isParsed(); - ~Asset(); -}; - -class AssetManager_old { - AssetManager_old(); - - ~AssetManager_old(); - - AssetManager_old(const AssetManager_old &); - - AssetManager_old &operator=(const AssetManager_old &); - - std::map<std::string, Asset> assets; - - static AssetManager_old &instance() { - static AssetManager_old assetManager; - return assetManager; - } - - static std::string GetPathToAsset(std::string AssetName); -public: - - static Asset &GetAsset(std::string AssetName); - - static void LoadAsset(std::string AssetName); - - static std::string GetAssetNameByBlockId(unsigned short id); -}; - diff --git a/src/graphics/Display.cpp b/src/graphics/Display.cpp deleted file mode 100644 index 63498fa..0000000 --- a/src/graphics/Display.cpp +++ /dev/null @@ -1,353 +0,0 @@ -#include <iomanip> -#include "Display.hpp" -#include "AssetManager_old.hpp" - -/*GLfloat vertices[] = { - -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, - 0.5f, -0.5f, -0.5f, 1.0f, 0.0f, - 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, - 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, - -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, - -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, - - -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, - 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, - 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, - 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, - -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, - -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, - - -0.5f, 0.5f, 0.5f, 1.0f, 0.0f, - -0.5f, 0.5f, -0.5f, 1.0f, 1.0f, - -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, - -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, - -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, - -0.5f, 0.5f, 0.5f, 1.0f, 0.0f, - - 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, - 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, - 0.5f, -0.5f, -0.5f, 0.0f, 1.0f, - 0.5f, -0.5f, -0.5f, 0.0f, 1.0f, - 0.5f, -0.5f, 0.5f, 0.0f, 0.0f, - 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, - - -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, - 0.5f, -0.5f, -0.5f, 1.0f, 1.0f, - 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, - 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, - -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, - -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, - - -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, - 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, - 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, - 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, - -0.5f, 0.5f, 0.5f, 0.0f, 0.0f, - -0.5f, 0.5f, -0.5f, 0.0f, 1.0f -}; -GLuint indices[] = { - 0, 1, 2, - 0, 2, 3 -};*/ -const GLfloat vertices[] = { - //Z+ edge - -0.5f, 0.5f, 0.5f, - -0.5f, -0.5f, 0.5f, - 0.5f, -0.5f, 0.5f, - -0.5f, 0.5f, 0.5f, - 0.5f, -0.5f, 0.5f, - 0.5f, 0.5f, 0.5f, - - //Z- edge - -0.5f, -0.5f, -0.5f, - -0.5f, 0.5f, -0.5f, - 0.5f, -0.5f, -0.5f, - 0.5f, -0.5f, -0.5f, - -0.5f, 0.5f, -0.5f, - 0.5f, 0.5f, -0.5f, - - //X+ edge - -0.5f, -0.5f, -0.5f, - -0.5f, -0.5f, 0.5f, - -0.5f, 0.5f, -0.5f, - -0.5f, 0.5f, -0.5f, - -0.5f, -0.5f, 0.5f, - -0.5f, 0.5f, 0.5f, - - //X- edge - 0.5f, -0.5f, 0.5f, - 0.5f, 0.5f, -0.5f, - 0.5f, 0.5f, 0.5f, - 0.5f, -0.5f, 0.5f, - 0.5f, -0.5f, -0.5f, - 0.5f, 0.5f, -0.5f, - - //Y+ edge - 0.5f, 0.5f, -0.5f, - -0.5f, 0.5f, 0.5f, - 0.5f, 0.5f, 0.5f, - 0.5f, 0.5f, -0.5f, - -0.5f, 0.5f, -0.5f, - -0.5f, 0.5f, 0.5f, - - //Y- edge - -0.5f, -0.5f, 0.5f, - 0.5f, -0.5f, -0.5f, - 0.5f, -0.5f, 0.5f, - -0.5f, -0.5f, -0.5f, - 0.5f, -0.5f, -0.5f, - -0.5f, -0.5f, 0.5f, -}; -const GLfloat uv_coords[] = { - //Z+ - 0.0f, 1.0f, - 0.0f, 0.0f, - 1.0f, 0.0f, - 0.0f, 1.0f, - 1.0f, 0.0f, - 1.0f, 1.0f, - - //Z- - 1.0f, 0.0f, - 1.0f, 1.0f, - 0.0f, 0.0f, - 0.0f, 0.0f, - 1.0f, 1.0f, - 0.0f, 1.0f, - - //X+ - 0.0f, 0.0f, - 1.0f, 0.0f, - 0.0f, 1.0f, - 0.0f, 1.0f, - 1.0f, 0.0f, - 1.0f, 1.0f, - - //X- - 0.0f, 0.0f, - 1.0f, 1.0f, - 0.0f, 1.0f, - 0.0f, 0.0f, - 1.0f, 0.0f, - 1.0f, 1.0f, - - //Y+ - 0.0f, 0.0f, - 1.0f, 1.0f, - 0.0f, 1.0f, - 0.0f, 0.0f, - 1.0f, 0.0f, - 1.0f, 1.0f, - - //Y- - 1.0f, 0.0f, - 0.0f, 1.0f, - 0.0f, 0.0f, - 1.0f, 1.0f, - 0.0f, 1.0f, - 1.0f, 0.0f, -}; - - -Display::Display(unsigned int winWidth, unsigned int winHeight, const char *winTitle, World *worldPtr) : world( - worldPtr) { - sf::ContextSettings contextSetting; - contextSetting.majorVersion = 3; - contextSetting.minorVersion = 3; - contextSetting.attributeFlags = contextSetting.Core; - contextSetting.depthBits = 24; - window = new sf::Window(sf::VideoMode(winWidth, winHeight), winTitle, sf::Style::Default, contextSetting); - window->setVerticalSyncEnabled(true); - window->setMouseCursorVisible(false); - window->setPosition(sf::Vector2i(sf::VideoMode::getDesktopMode().width / 2 - window->getSize().x/2, - sf::VideoMode::getDesktopMode().height / 2 - window->getSize().y/2)); - - sf::Mouse::setPosition(sf::Vector2i(window->getSize().x / 2, window->getSize().y / 2), *window); - - - //Glew - glewExperimental = GL_TRUE; - if (glewInit() != GLEW_OK) { - std::cout << "Failed to initialize GLEW" << std::endl; - throw 3; - } - glViewport(0, 0, width(), height()); - glEnable(GL_DEPTH_TEST); - glEnable(GL_CULL_FACE); - glCullFace(GL_BACK); - glFrontFace(GL_CCW); -} - -bool Display::IsClosed() { - return !window->isOpen(); -} - -void Display::SetPlayerPos(double playerX, double playerY, double playerZ) { - camera.Position = glm::vec3(playerX, playerY, playerZ); - toRender.clear(); - const float ChunkDistance = 1; - Vector playerChunk = Vector(floor(playerX / 16.0f), floor(playerY / 16.0f), floor(playerZ / 16.0f)); - for (auto &it:world->m_sections) { - Vector chunkPosition = it.first; - Vector delta = chunkPosition - playerChunk; - //std::cout << delta.GetDistance() << std::endl; - if (delta.GetDistance() > ChunkDistance) - continue; - /*std::cout << "Rendering " << delta.GetDistance() << " Detailed: " << delta.GetX() << " " << delta.GetZ() << " " - << delta.GetY() << std::endl << - "\t" << chunkPosition.GetX() << " " << chunkPosition.GetZ() << " " - << chunkPosition.GetY() << std::endl;*/ - toRender.push_back(chunkPosition); - } - std::cout << "Chunks to render: " << toRender.size() << std::endl; -} - -void Display::MainLoop() { - Shader shader("./shaders/simple.vs", "./shaders/simple.fs"); - - GLuint VBO, VAO, VBO2; - glGenBuffers(1, &VBO); - glGenBuffers(1, &VBO2); - glGenVertexArrays(1, &VAO); - - glBindVertexArray(VAO); - { - glBindBuffer(GL_ARRAY_BUFFER, VBO2); - glBufferData(GL_ARRAY_BUFFER, sizeof(uv_coords), uv_coords, GL_STATIC_DRAW); - glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), 0); - glEnableVertexAttribArray(2); - - glBindBuffer(GL_ARRAY_BUFFER, VBO); - glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), 0); - glEnableVertexAttribArray(0); - } - glBindVertexArray(0); - - shader.Use(); - - bool captureMouse = true; - - //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); - - bool isRunning = true; - while (isRunning) { - static sf::Clock clock, clock1; - float deltaTime = clock.getElapsedTime().asSeconds(); - float absTime = clock1.getElapsedTime().asSeconds(); - clock.restart(); - sf::Event event; - /*static sf::Clock clock2; - if (clock2.getElapsedTime().asSeconds() > 5.0f) { - clock2.restart(); - SetPlayerPos(camera.Position.x, camera.Position.y, camera.Position.z); - }*/ - while (window->pollEvent(event)) { - switch (event.type) { - case sf::Event::Closed: - isRunning = false; - break; - case sf::Event::Resized: - glViewport(0, 0, width(), height()); - break; - case sf::Event::KeyPressed: - switch (event.key.code) { - case sf::Keyboard::Escape: - isRunning = false; - break; - case sf::Keyboard::T: - captureMouse = !captureMouse; - window->setMouseCursorVisible(!captureMouse); - sf::Mouse::setPosition(sf::Vector2i(window->getSize().x / 2, window->getSize().y / 2), - *window); - break; - case sf::Keyboard::R: - shader.Reload(); - break; - default: - break; - } - case sf::Event::MouseWheelScrolled: - camera.ProcessMouseScroll(event.mouseWheelScroll.delta); - break; - default: - break; - } - } - std::ostringstream toWindow; - glm::highp_vec3 cameraPosition(camera.Position); - toWindow << std::setprecision(2) << std::fixed << "Pos: " << cameraPosition.x << ", " << cameraPosition.y - << ", " << cameraPosition.z << "; "; - toWindow << "FPS: " << (1.0f / deltaTime) << " "; - window->setTitle(toWindow.str()); - if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) - camera.ProcessKeyboard(Camera_Movement::FORWARD, deltaTime); - if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) - camera.ProcessKeyboard(Camera_Movement::BACKWARD, deltaTime); - if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) - camera.ProcessKeyboard(Camera_Movement::LEFT, deltaTime); - if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) - camera.ProcessKeyboard(Camera_Movement::RIGHT, deltaTime); - if (captureMouse) { - sf::Vector2i mousePos = sf::Mouse::getPosition(*window); - sf::Vector2i center = sf::Vector2i(window->getSize().x / 2, window->getSize().y / 2); - sf::Mouse::setPosition(center, *window); - int deltaX = (mousePos - center).x, deltaY = (center - mousePos).y; - camera.ProcessMouseMovement(deltaX, deltaY); - } - - //Render code - glClearColor(0.2f, 0.3f, 0.3f, 1.0f); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - shader.Use(); - - GLint modelLoc = glGetUniformLocation(shader.Program, "model"); - GLint projectionLoc = glGetUniformLocation(shader.Program, "projection"); - GLint viewLoc = glGetUniformLocation(shader.Program, "view"); - GLint blockLoc = glGetUniformLocation(shader.Program, "block"); - GLint timeLoc = glGetUniformLocation(shader.Program, "time"); - glm::mat4 projection = glm::perspective(camera.Zoom, (float) width() / (float) height(), 0.1f, 1000.0f); - glm::mat4 view = camera.GetViewMatrix(); - glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection)); - glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view)); - glUniform1f(timeLoc, absTime); - - glBindVertexArray(VAO); - - for (auto §ionPos:toRender) { - Section §ion = world->m_sections.find(sectionPos)->second; - for (int y = 0; y < 16; y++) { - for (int z = 0; z < 16; z++) { - for (int x = 0; x < 16; x++) { - glm::mat4 model; - model = glm::translate(model, - glm::vec3(sectionPos.GetX() * 16, sectionPos.GetY() * 16, - sectionPos.GetZ() * 16)); - model = glm::translate(model, glm::vec3(x, y, z)); - - Block block = section.GetBlock(Vector(x, y, z)); - glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model)); - glUniform1i(blockLoc, block.id); - - std::string textureName = AssetManager_old::GetAssetNameByBlockId(block.id); - if (textureName.find("air") != std::string::npos) - continue; - Texture &texture1 = *(AssetManager_old::GetAsset(textureName).data.texture); - - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, texture1.texture); - glUniform1i(glGetUniformLocation(shader.Program, "blockTexture"), 0); - - glDrawArrays(GL_TRIANGLES, 0, 36); - } - } - } - } - glBindVertexArray(0); - - //End of render code - - window->display(); - } - -} diff --git a/src/graphics/Display.hpp b/src/graphics/Display.hpp deleted file mode 100644 index 7458bcd..0000000 --- a/src/graphics/Display.hpp +++ /dev/null @@ -1,33 +0,0 @@ -#pragma once - -#include <SFML/Window.hpp> -#include "../world/World.hpp" -#include <glm/glm.hpp> -#include <glm/gtc/matrix_transform.hpp> -#include <glm/gtc/type_ptr.hpp> -#include "Shader.hpp" -#include "Texture.hpp" -#include "Camera3D.hpp" - -class Display { - sf::Window *window; - World* world; - std::vector<Vector> toRender; - Camera3D camera; -public: - Display(unsigned int winWidth, unsigned int winHeight, const char winTitle[9], World *worldPtr); - - bool IsClosed(); - - void SetPlayerPos(double playerX, double playerY, double playerZ); - - void MainLoop(); - - unsigned int width() { - return window->getSize().x; - } - - unsigned int height() { - return window->getSize().y; - } -};
\ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 53f90eb..de2c9d8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,15 +1,24 @@ #include "core/Core.hpp" -#define ELPP_THREAD_SAFE -#define ELPP_FEATURE_CRASH_LOG -#define ELPP_DISABLE_LOGS +const char *getTimeSinceProgramStart(void) { + static auto initialTime = std::chrono::steady_clock().now(); + auto now = std::chrono::steady_clock().now(); + std::chrono::duration<double> seconds = now-initialTime; + static char buffer[30]; + sprintf(buffer, "%.2f", seconds.count()); + return buffer; +} + INITIALIZE_EASYLOGGINGPP int main() { el::Configurations loggerConfiguration; - std::string format = "[%level]{%fbase}: %msg"; + el::Helpers::installCustomFormatSpecifier( + el::CustomFormatSpecifier("%startTime", std::bind(getTimeSinceProgramStart))); + std::string format = "[%startTime][%level][%fbase]: %msg"; loggerConfiguration.set(el::Level::Info, el::ConfigurationType::Format, format); loggerConfiguration.set(el::Level::Error, el::ConfigurationType::Format, format); + loggerConfiguration.set(el::Level::Fatal, el::ConfigurationType::Format, format); el::Loggers::reconfigureAllLoggers(loggerConfiguration); el::Loggers::addFlag(el::LoggingFlag::ColoredTerminalOutput); LOG(INFO) << "Logger is configured"; diff --git a/src/network/Network.cpp b/src/network/Network.cpp index ac84494..b3b1e4b 100644 --- a/src/network/Network.cpp +++ b/src/network/Network.cpp @@ -57,8 +57,8 @@ Packet Network::ReceivePacket() { } Field fLen = FieldParser::Parse(VarInt, bufLen); size_t packetLen = fLen.GetVarInt() + fLen.GetLength(); - if (packetLen > 1024 * 1024 * 30) - std::cout << "OMG! SIZEOF PACKET IS " << packetLen << std::endl; + if (packetLen > 1024 * 1024 * 15) + LOG(WARNING)<<"OMG SIZEOF PACKAGE IS "<<packetLen; if (packetLen < rec) { return Packet(bufLen); } @@ -68,39 +68,12 @@ Packet Network::ReceivePacket() { while (m_socket.receive(bufPack + dataLen, packetLen - dataLen, rec) == sf::Socket::Done && dataLen < packetLen) { dataLen += rec; } - if (dataLen < packetLen) - throw 93; - else { + if (dataLen < packetLen) { + LOG(ERROR) << "Received data is "<<dataLen<<" but "<<packetLen<<" is promoted"; + throw std::runtime_error(""); + } else { Packet p(bufPack); delete[] bufPack; return p; } - - /*if (m_socket.receive(bufPack + rec, packetLen - rec, rec) != sf::Socket::Done) { - delete[] bufPack; - throw 93; - } - rec++; - //Check for losted data - int losted = 0; - for (int i = packetLen - 2; i > 0; i--) - if (bufPack[i] == 'N') - losted++; - if (losted > 100) { - if (m_socket.receive(bufPack + rec, packetLen - rec, rec) != sf::Socket::Done) { - throw 93; - } - std::cout << "Keep receiving!" << std::endl; - } - //One more time - losted = 0; - for (int i = packetLen - 2; i > 0; i--) - if (bufPack[i] == 'N') - losted++; - if (losted > 100) { - std::cout << "\x1b[31m" << "Losted " << losted << " bytes of " << packetLen << "\x1b[0m" << std::endl; - delete[] bufPack; - throw 93; - }*/ - throw 94; } diff --git a/src/network/NetworkClient.cpp b/src/network/NetworkClient.cpp index cbe705b..9916b10 100644 --- a/src/network/NetworkClient.cpp +++ b/src/network/NetworkClient.cpp @@ -1,7 +1,4 @@ #include "NetworkClient.hpp" -#include "../packet/PacketParser.hpp" -#include "../packet/PacketBuilder.hpp" -#include <nlohmann/json.hpp> ServerInfo NetworkClient::ServerPing(std::string address, unsigned short port) { ServerInfo info; @@ -62,8 +59,10 @@ NetworkClient::NetworkClient(std::string address, unsigned short port, std::stri } NetworkClient::~NetworkClient() { + LOG(INFO)<<"NC stopping..."; isContinue=false; m_networkThread.join(); + LOG(INFO)<<"NC is stopped"; } Packet * NetworkClient::GetPacket() { @@ -101,7 +100,7 @@ void NetworkClient::MainLoop() { Update(); } } catch (int e){ - std::cerr<<"NetworkClient exception: "<<e<<std::endl; + LOG(ERROR)<<"Catched exception in NC: "<<e; } } diff --git a/src/network/NetworkClient.hpp b/src/network/NetworkClient.hpp index a41b5f4..9f68e37 100644 --- a/src/network/NetworkClient.hpp +++ b/src/network/NetworkClient.hpp @@ -4,6 +4,8 @@ #include <thread> #include <mutex> #include "Network.hpp" +#include "../packet/PacketParser.hpp" +#include "../packet/PacketBuilder.hpp" struct ServerInfo{ std::string version; diff --git a/src/world/Block.cpp b/src/world/Block.cpp index 3cf09db..54b7e5e 100644 --- a/src/world/Block.cpp +++ b/src/world/Block.cpp @@ -2,9 +2,7 @@ Block::~Block() {} -Block::Block(unsigned short idAndState, unsigned char light) : id(idAndState >> 4), state(idAndState & 0x0F) {} - -Block::Block(unsigned short id, unsigned char state, unsigned char light) : id(id), state(state) {} +Block::Block(unsigned short id, unsigned short state, unsigned char light) : id(id), state(state) {} Block::Block() : id(0), state(0) {} diff --git a/src/world/Block.hpp b/src/world/Block.hpp index ae952c9..1a53868 100644 --- a/src/world/Block.hpp +++ b/src/world/Block.hpp @@ -3,9 +3,7 @@ struct Block { Block(); - Block(unsigned short idAndState, unsigned char light); - - Block(unsigned short id, unsigned char state, unsigned char light); + Block(unsigned short id, unsigned short state = 0, unsigned char light = 0); ~Block(); diff --git a/src/world/Section.cpp b/src/world/Section.cpp index ac34fba..5c42ea5 100644 --- a/src/world/Section.cpp +++ b/src/world/Section.cpp @@ -77,7 +77,7 @@ void Section::Parse() { } for (int i = 0; i < 4096; i++) { unsigned short blockId = m_palette.size() > 0 ? m_palette[blocks[i]] : blocks[i]; - Block block(blockId, light[i]); + Block block(blockId, 0, light[i]); m_blocks.push_back(block); } if ((light.size() + blocks.size()) / 2 != 4096) { diff --git a/src/world/World.cpp b/src/world/World.cpp index af76fd5..d13d01d 100644 --- a/src/world/World.cpp +++ b/src/world/World.cpp @@ -84,27 +84,9 @@ Section World::ParseSection(byte *data, size_t &dataLen) { } World::~World() { - isContinue = false; - m_parseSectionWaiter.notify_all(); - m_sectionParseThread.join(); -} - -void World::SectionParsingThread() { - while (isContinue) { - std::unique_lock<std::mutex> sectionParseLocker(m_parseSectionMutex); - m_parseSectionWaiter.wait(sectionParseLocker); - while (m_sectionToParse.size() == 0 && isContinue) { - m_parseSectionWaiter.wait(sectionParseLocker); - } - while (m_sectionToParse.size() > 0) { - auto it = m_sectionToParse.front(); - m_sectionToParse.pop(); - it->second.Parse(); - } - } } World::World() { - m_sectionParseThread = std::thread(&World::SectionParsingThread, this); + } diff --git a/src/world/World.hpp b/src/world/World.hpp index 7b7ea60..cef9eea 100644 --- a/src/world/World.hpp +++ b/src/world/World.hpp @@ -11,22 +11,22 @@ class World { //utility vars - World(const World& other); - World&operator=(const World &other); - bool isContinue=true; - std::mutex m_parseSectionMutex; - std::condition_variable m_parseSectionWaiter; - std::thread m_sectionParseThread; - std::queue<std::map<Vector,Section>::iterator> m_sectionToParse; - //utility methods - void SectionParsingThread(); + World(const World &other); + + World &operator=(const World &other); + //game vars int m_dimension = 0; + //game methods Section ParseSection(byte *data, size_t &dataLen); + public: World(); + ~World(); + void ParseChunkData(Packet packet); + std::map<Vector, Section> m_sections; };
\ No newline at end of file |