diff options
author | LaG1924 <12997935+LaG1924@users.noreply.github.com> | 2017-05-26 16:11:17 +0200 |
---|---|---|
committer | LaG1924 <12997935+LaG1924@users.noreply.github.com> | 2017-05-26 16:11:17 +0200 |
commit | 8ac4fe6d5ba5091923c7fdf1fa88724d827706fa (patch) | |
tree | a4ce203f671f9b8311d09dd36f1f80ae65799a56 /src | |
parent | 2017-05-21 (diff) | |
download | AltCraft-8ac4fe6d5ba5091923c7fdf1fa88724d827706fa.tar AltCraft-8ac4fe6d5ba5091923c7fdf1fa88724d827706fa.tar.gz AltCraft-8ac4fe6d5ba5091923c7fdf1fa88724d827706fa.tar.bz2 AltCraft-8ac4fe6d5ba5091923c7fdf1fa88724d827706fa.tar.lz AltCraft-8ac4fe6d5ba5091923c7fdf1fa88724d827706fa.tar.xz AltCraft-8ac4fe6d5ba5091923c7fdf1fa88724d827706fa.tar.zst AltCraft-8ac4fe6d5ba5091923c7fdf1fa88724d827706fa.zip |
Diffstat (limited to '')
-rw-r--r-- | src/core/AssetManager.cpp | 70 | ||||
-rw-r--r-- | src/core/AssetManager.hpp | 33 | ||||
-rw-r--r-- | src/core/Core.cpp | 9 | ||||
-rw-r--r-- | src/core/Core.hpp | 3 | ||||
-rw-r--r-- | src/gamestate/GameState.cpp | 2 | ||||
-rw-r--r-- | src/graphics/AssetManager_old.cpp (renamed from src/graphics/AssetManager.cpp) | 16 | ||||
-rw-r--r-- | src/graphics/AssetManager_old.hpp (renamed from src/graphics/AssetManager.hpp) | 15 | ||||
-rw-r--r-- | src/graphics/Display.cpp | 6 | ||||
-rw-r--r-- | src/graphics/Shader.cpp | 11 | ||||
-rw-r--r-- | src/graphics/Shader.hpp | 1 | ||||
-rw-r--r-- | src/graphics/Texture.cpp | 10 | ||||
-rw-r--r-- | src/main.cpp | 15 | ||||
-rw-r--r-- | src/nbt/Nbt.hpp | 1 | ||||
-rw-r--r-- | src/network/Network.cpp | 1 | ||||
-rw-r--r-- | src/world/Block.cpp | 17 | ||||
-rw-r--r-- | src/world/Block.hpp | 10 | ||||
-rw-r--r-- | src/world/Section.cpp | 1 | ||||
-rw-r--r-- | src/world/World.cpp | 17 |
18 files changed, 174 insertions, 64 deletions
diff --git a/src/core/AssetManager.cpp b/src/core/AssetManager.cpp new file mode 100644 index 0000000..9913c18 --- /dev/null +++ b/src/core/AssetManager.cpp @@ -0,0 +1,70 @@ +#include <easylogging++.h> +#include <nlohmann/json.hpp> +#include "AssetManager.hpp" + +namespace fs = std::experimental::filesystem; + +const fs::path pathToAssets = "./assets/"; +const fs::path pathToAssetsList = "./items.json"; +const fs::path pathToTextureIndex = "./textures.json"; + +AssetManager::AssetManager() { + for (auto &it:fs::recursive_directory_iterator(pathToAssets)) { + + } + LoadIds(); + LoadTextureResources(); +} + +void AssetManager::LoadIds() { + std::ifstream in(pathToAssetsList); + nlohmann::json index; + in >> index; + for (auto &it:index) { + 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); + } + LOG(INFO) << "Loaded " << assetIds.size() << " ids"; +} + +AssetManager::~AssetManager() { + delete textureAtlas; +} + +//TODO: This function must be replaced with runtime texture atlas generating +void AssetManager::LoadTextureResources() { + std::ifstream in(pathToTextureIndex); + nlohmann::json index; + in >> index; + std::string filename = index["meta"]["image"].get<std::string>(); + for (auto &it:index["frames"]) { + auto frame = it["frame"]; + TextureCoord coord; + coord.x = frame["x"].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; + } + + textureAtlas = new Texture(filename); + LOG(INFO) << "Texture atlas id is " << textureAtlas->texture; +} + +TextureCoord AssetManager::GetTextureByAssetName(std::string AssetName) { + return TextureCoord{0, 0, 0, 0}; +} + +std::string AssetManager::GetTextureAssetNameByBlockId(unsigned short BlockId, unsigned char BlockSide) { + +} + +const GLuint AssetManager::GetTextureAtlas() { + return textureAtlas->texture; +} diff --git a/src/core/AssetManager.hpp b/src/core/AssetManager.hpp new file mode 100644 index 0000000..23b2ba6 --- /dev/null +++ b/src/core/AssetManager.hpp @@ -0,0 +1,33 @@ +#pragma once + +#include <experimental/filesystem> +#include <map> +#include <GL/glew.h> +#include <glm/vec4.hpp> +#include <nlohmann/json.hpp> +#include "../world/Block.hpp" +#include "../graphics/Texture.hpp" + +struct TextureCoord{ + unsigned int x,y,w,h; +}; + +class AssetManager { + Texture *textureAtlas; + std::map<std::string,Block> assetIds; + std::map<std::string,TextureCoord> assetTextures; +public: + AssetManager(); + + ~AssetManager(); + + void LoadTextureResources(); + + TextureCoord GetTextureByAssetName(std::string AssetName); + + std::string GetTextureAssetNameByBlockId(unsigned short BlockId, unsigned char BlockSide = 0); + + const GLuint GetTextureAtlas(); + + void LoadIds(); +}; diff --git a/src/core/Core.cpp b/src/core/Core.cpp index 54a16a4..7814c32 100644 --- a/src/core/Core.cpp +++ b/src/core/Core.cpp @@ -108,6 +108,7 @@ Core::Core() { gameState = new GameState(client); std::thread loop = std::thread(&Core::UpdateGameState,this); std::swap(loop,gameStateLoopThread); + assetManager = new AssetManager; LOG(INFO) << "Core is initialized"; } @@ -291,13 +292,8 @@ void Core::RenderWorld(World &Target) { glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model)); glUniform1i(blockLoc, block.id); - std::string textureName = AssetManager::GetAssetNameByBlockId(block.id); - if (textureName.find("air") != std::string::npos) - continue; - Texture &texture1 = *(AssetManager::GetAsset(textureName).data.texture); - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, texture1.texture); + //glBindTexture(GL_TEXTURE_2D, texture1.texture); glUniform1i(glGetUniformLocation(shader->Program, "blockTexture"), 0); glDrawArrays(GL_TRIANGLES, 0, 36); @@ -316,7 +312,6 @@ void Core::SetMouseCapture(bool IsCaptured) { } void Core::PrepareToWorldRendering() { - glGenBuffers(1, &VBO); glGenBuffers(1, &VBO2); glGenVertexArrays(1, &VAO); diff --git a/src/core/Core.hpp b/src/core/Core.hpp index a877613..1c2bbc5 100644 --- a/src/core/Core.hpp +++ b/src/core/Core.hpp @@ -10,12 +10,13 @@ #include "../gui/Gui.hpp" #include "../graphics/Camera3D.hpp" #include "../graphics/Shader.hpp" -#include "../graphics/AssetManager.hpp" +#include "AssetManager.hpp" class Core { GameState *gameState; NetworkClient *client; sf::Window *window; + AssetManager *assetManager; bool isMouseCaptured = false, isRunning = true; enum { MainMenu, diff --git a/src/gamestate/GameState.cpp b/src/gamestate/GameState.cpp index aaeecb1..7b6734f 100644 --- a/src/gamestate/GameState.cpp +++ b/src/gamestate/GameState.cpp @@ -40,7 +40,7 @@ void GameState::Update() { break; case 0x0D: g_Difficulty = packet.GetField(0).GetUByte(); - std::cout << "Difficulty now is " << (int) g_Difficulty << std::endl; + LOG(INFO) << "Difficulty now is " << (int) g_Difficulty; break; case 0x43: g_SpawnPosition = packet.GetField(0).GetPosition(); diff --git a/src/graphics/AssetManager.cpp b/src/graphics/AssetManager_old.cpp index 93462c3..ef856ca 100644 --- a/src/graphics/AssetManager.cpp +++ b/src/graphics/AssetManager_old.cpp @@ -1,16 +1,16 @@ -#include "AssetManager.hpp" +#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 = "./assetsMc/"; +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::AssetManager() { +AssetManager_old::AssetManager_old() { return; std::ifstream indexFile(pathToIndexFile); if (!indexFile) { @@ -35,17 +35,17 @@ AssetManager::AssetManager() { } } -AssetManager::~AssetManager() { +AssetManager_old::~AssetManager_old() { } -Asset &AssetManager::GetAsset(std::string AssetName) { +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::LoadAsset(std::string 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); @@ -58,7 +58,7 @@ void AssetManager::LoadAsset(std::string AssetName) { } } -std::string AssetManager::GetPathToAsset(std::string AssetName) { +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) + "/" + @@ -73,7 +73,7 @@ std::string AssetManager::GetPathToAsset(std::string AssetName) { assetTypeFileExtensions.at(instance().assets[AssetName].type); } -std::string AssetManager::GetAssetNameByBlockId(unsigned short id) { +std::string AssetManager_old::GetAssetNameByBlockId(unsigned short id) { std::string assetBase = "minecraft/textures/blocks/"; std::string textureName; switch (id){ diff --git a/src/graphics/AssetManager.hpp b/src/graphics/AssetManager_old.hpp index e723398..23a11a7 100644 --- a/src/graphics/AssetManager.hpp +++ b/src/graphics/AssetManager_old.hpp @@ -3,6 +3,7 @@ #include <fstream> #include <string> #include <map> +#include <experimental/filesystem> #include <nlohmann/json.hpp> #include "Texture.hpp" @@ -24,19 +25,19 @@ struct Asset { ~Asset(); }; -class AssetManager { - AssetManager(); +class AssetManager_old { + AssetManager_old(); - ~AssetManager(); + ~AssetManager_old(); - AssetManager(const AssetManager &); + AssetManager_old(const AssetManager_old &); - AssetManager &operator=(const AssetManager &); + AssetManager_old &operator=(const AssetManager_old &); std::map<std::string, Asset> assets; - static AssetManager &instance() { - static AssetManager assetManager; + static AssetManager_old &instance() { + static AssetManager_old assetManager; return assetManager; } diff --git a/src/graphics/Display.cpp b/src/graphics/Display.cpp index 1a44fbc..63498fa 100644 --- a/src/graphics/Display.cpp +++ b/src/graphics/Display.cpp @@ -1,6 +1,6 @@ #include <iomanip> #include "Display.hpp" -#include "AssetManager.hpp" +#include "AssetManager_old.hpp" /*GLfloat vertices[] = { -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, @@ -329,10 +329,10 @@ void Display::MainLoop() { glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model)); glUniform1i(blockLoc, block.id); - std::string textureName = AssetManager::GetAssetNameByBlockId(block.id); + std::string textureName = AssetManager_old::GetAssetNameByBlockId(block.id); if (textureName.find("air") != std::string::npos) continue; - Texture &texture1 = *(AssetManager::GetAsset(textureName).data.texture); + Texture &texture1 = *(AssetManager_old::GetAsset(textureName).data.texture); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, texture1.texture); diff --git a/src/graphics/Shader.cpp b/src/graphics/Shader.cpp index c84e169..9bb08ba 100644 --- a/src/graphics/Shader.cpp +++ b/src/graphics/Shader.cpp @@ -1,3 +1,4 @@ +#include <easylogging++.h> #include "Shader.hpp" Shader::Shader(const GLchar *vertexPath, const GLchar *fragmentPath) { @@ -27,7 +28,7 @@ Shader::Shader(const GLchar *vertexPath, const GLchar *fragmentPath) { fragmentCode = fShaderStream.str(); } catch (std::ifstream::failure e) { - std::cout << "ERROR::SHADER::FILE_NOT_SUCCESFULLY_READ" << std::endl; + LOG(ERROR) << "ERROR::SHADER::FILE_NOT_SUCCESFULLY_READ"; } const GLchar *vShaderCode = vertexCode.c_str(); const GLchar *fShaderCode = fragmentCode.c_str(); @@ -46,7 +47,7 @@ Shader::Shader(const GLchar *vertexPath, const GLchar *fragmentPath) { glGetShaderiv(vertex, GL_COMPILE_STATUS, &success); if (!success) { glGetShaderInfoLog(vertex, 512, NULL, infoLog); - std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl; + LOG(ERROR) << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog; }; // Вершинный шейдер @@ -57,7 +58,7 @@ Shader::Shader(const GLchar *vertexPath, const GLchar *fragmentPath) { glGetShaderiv(fragment, GL_COMPILE_STATUS, &success); if (!success) { glGetShaderInfoLog(fragment, 512, NULL, infoLog); - std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl; + LOG(ERROR) << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog; }; // Шейдерная программа @@ -69,7 +70,7 @@ Shader::Shader(const GLchar *vertexPath, const GLchar *fragmentPath) { glGetProgramiv(this->Program, GL_LINK_STATUS, &success); if (!success) { glGetProgramInfoLog(this->Program, 512, NULL, infoLog); - std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl; + LOG(ERROR) << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog; } // Удаляем шейдеры, поскольку они уже в программу и нам больше не нужны. @@ -86,5 +87,5 @@ void Shader::Reload() { const GLchar *fragmentPath = fragment; this->~Shader(); new(this) Shader(vertexPath, fragmentPath); - std::cout<<"Shader is realoded!"<<std::endl; + LOG(INFO) << "Shader is realoded!"; } diff --git a/src/graphics/Shader.hpp b/src/graphics/Shader.hpp index 90db5f7..66d687c 100644 --- a/src/graphics/Shader.hpp +++ b/src/graphics/Shader.hpp @@ -2,7 +2,6 @@ #include <string> #include <fstream> #include <sstream> -#include <iostream> #include <GL/glew.h> diff --git a/src/graphics/Texture.cpp b/src/graphics/Texture.cpp index 0104530..bd5c53f 100644 --- a/src/graphics/Texture.cpp +++ b/src/graphics/Texture.cpp @@ -1,5 +1,5 @@ -#include <iostream> #include <SFML/Graphics.hpp> +#include <easylogging++.h> #include "Texture.hpp" Texture::Texture(std::string filename, GLenum textureWrapping, GLenum textureFiltering) { @@ -10,17 +10,17 @@ 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, textureFiltering); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); //Image load sf::Image image; if (!image.loadFromFile(filename)) { - std::cout << "Can't open image " << filename << std::endl; + LOG(ERROR) << "Can't open image " << filename; throw 201; } - if (image.getPixelsPtr()==nullptr){ - std::cout<<"Image data is corrupted!"<<std::endl; + if (image.getPixelsPtr() == nullptr) { + LOG(ERROR) << "Image data is corrupted!"; throw 202; } image.flipVertically(); diff --git a/src/main.cpp b/src/main.cpp index cb25acf..53f90eb 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,12 +1,21 @@ #include "core/Core.hpp" -#include "gamestate/Game.hpp" +#define ELPP_THREAD_SAFE +#define ELPP_FEATURE_CRASH_LOG +#define ELPP_DISABLE_LOGS INITIALIZE_EASYLOGGINGPP -int main(){ +int main() { el::Configurations loggerConfiguration; - loggerConfiguration.set(el::Level::Info,el::ConfigurationType::Format,"%level: %msg"); + std::string format = "[%level]{%fbase}: %msg"; + loggerConfiguration.set(el::Level::Info, el::ConfigurationType::Format, format); + loggerConfiguration.set(el::Level::Error, el::ConfigurationType::Format, format); el::Loggers::reconfigureAllLoggers(loggerConfiguration); + el::Loggers::addFlag(el::LoggingFlag::ColoredTerminalOutput); + LOG(INFO) << "Logger is configured"; + Core core; core.Exec(); + + return 0; }
\ No newline at end of file diff --git a/src/nbt/Nbt.hpp b/src/nbt/Nbt.hpp index 70dcd6a..28e5345 100644 --- a/src/nbt/Nbt.hpp +++ b/src/nbt/Nbt.hpp @@ -2,7 +2,6 @@ #include <cstddef> #include <vector> -#include <iostream> #include <zlib.h> #include <fstream> #include "../utility/utility.h" diff --git a/src/network/Network.cpp b/src/network/Network.cpp index 03ee6c6..ac84494 100644 --- a/src/network/Network.cpp +++ b/src/network/Network.cpp @@ -1,4 +1,3 @@ -#include <iostream> #include "Network.hpp" #include "../packet/PacketBuilder.hpp" diff --git a/src/world/Block.cpp b/src/world/Block.cpp index 64e5330..3cf09db 100644 --- a/src/world/Block.cpp +++ b/src/world/Block.cpp @@ -2,9 +2,18 @@ Block::~Block() {} -Block::Block(unsigned short idAndState, unsigned char light) : id(idAndState >> 4), state(idAndState & 0x0F), - light(light) {} +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), light(light) {} +Block::Block(unsigned short id, unsigned char state, unsigned char light) : id(id), state(state) {} -Block::Block() : id(0), state(0), light(0) {} +Block::Block() : id(0), state(0) {} + +bool operator<(const Block &lhs, const Block &rhs) { + if (lhs.id < rhs.id) + return true; + if (lhs.id == rhs.id) { + if (lhs.state != rhs.state) + return lhs.state < rhs.state; + } + return false; +} diff --git a/src/world/Block.hpp b/src/world/Block.hpp index 7c780c1..ae952c9 100644 --- a/src/world/Block.hpp +++ b/src/world/Block.hpp @@ -1,15 +1,17 @@ #pragma once struct Block { + Block(); + Block(unsigned short idAndState, unsigned char light); Block(unsigned short id, unsigned char state, unsigned char light); - Block(); - ~Block(); unsigned short id:13; unsigned char state:4; - unsigned char light:4; -};
\ No newline at end of file + //unsigned char light:4; +}; + +bool operator<(const Block &lhs, const Block &rhs);
\ No newline at end of file diff --git a/src/world/Section.cpp b/src/world/Section.cpp index f53c987..ac34fba 100644 --- a/src/world/Section.cpp +++ b/src/world/Section.cpp @@ -1,4 +1,3 @@ -#include <iostream> #include "Section.hpp" Section::Section(byte *dataBlocks, size_t dataBlocksLength, byte *dataLight, byte *dataSky, byte bitsPerBlock, diff --git a/src/world/World.cpp b/src/world/World.cpp index adbb3e1..af76fd5 100644 --- a/src/world/World.cpp +++ b/src/world/World.cpp @@ -1,5 +1,5 @@ -#include <iostream> #include <bitset> +#include <easylogging++.h> #include "World.hpp" void World::ParseChunkData(Packet packet) { @@ -22,22 +22,17 @@ void World::ParseChunkData(Packet packet) { if (bitmask[i]) { size_t len = 0; Vector chunkPosition = Vector(chunkX, i, chunkZ); - if (!m_sections.insert(std::make_pair(chunkPosition,ParseSection(content,len))).second) - std::cout<<"Chunk not created: "<<chunkPosition<<std::endl; + if (!m_sections.insert(std::make_pair(chunkPosition, ParseSection(content, len))).second) + LOG(ERROR) << "Chunk not created: " << chunkPosition; auto sectionIter = m_sections.find(chunkPosition); - if (sectionIter==m_sections.end()) - std::cout<<"Created chunk not found: "<<chunkPosition<<std::endl; + if (sectionIter == m_sections.end()) + LOG(ERROR)<< "Created chunk not found: " << chunkPosition; else sectionIter->second.Parse(); - /*m_sections[chunkPosition] = ParseSection(content, len); - m_sections[chunkPosition].Parse();*/ - /*m_sectionToParse.push(m_sections.find(Vector(chunkX, i, chunkZ))); - m_parseSectionWaiter.notify_one();*/ content += len; } } delete[] contentOrigPtr; - //std::cout<<m_sections.size()<<std::endl; } Section World::ParseSection(byte *data, size_t &dataLen) { @@ -105,8 +100,6 @@ void World::SectionParsingThread() { auto it = m_sectionToParse.front(); m_sectionToParse.pop(); it->second.Parse(); - /*std::cout << "Parsed chunk" << it->first.GetX() << "x" << it->first.GetY() << "x" << it->first.GetZ() - << std::endl;*/ } } } |