From 8ac4fe6d5ba5091923c7fdf1fa88724d827706fa Mon Sep 17 00:00:00 2001 From: LaG1924 <12997935+LaG1924@users.noreply.github.com> Date: Fri, 26 May 2017 19:11:17 +0500 Subject: 2017-05-26 --- src/graphics/AssetManager.cpp | 146 -------------------------------------- src/graphics/AssetManager.hpp | 52 -------------- src/graphics/AssetManager_old.cpp | 146 ++++++++++++++++++++++++++++++++++++++ src/graphics/AssetManager_old.hpp | 53 ++++++++++++++ src/graphics/Display.cpp | 6 +- src/graphics/Shader.cpp | 11 +-- src/graphics/Shader.hpp | 1 - src/graphics/Texture.cpp | 10 +-- 8 files changed, 213 insertions(+), 212 deletions(-) delete mode 100644 src/graphics/AssetManager.cpp delete mode 100644 src/graphics/AssetManager.hpp create mode 100644 src/graphics/AssetManager_old.cpp create mode 100644 src/graphics/AssetManager_old.hpp (limited to 'src/graphics') diff --git a/src/graphics/AssetManager.cpp b/src/graphics/AssetManager.cpp deleted file mode 100644 index 93462c3..0000000 --- a/src/graphics/AssetManager.cpp +++ /dev/null @@ -1,146 +0,0 @@ -#include "AssetManager.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::map assetTypeFileExtensions{ - std::make_pair(Asset::AssetType::Texture, ".png"), - std::make_pair(Asset::AssetType::Lang, ".lang"), - std::make_pair(Asset::AssetType::Sound, ".ogg"), -}; - -AssetManager::AssetManager() { - 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(); - size_t size = it.value()["size"].get(); - Asset asset{name, hash, Asset::AssetData(), size, type}; - this->assets[name] = asset; - } -} - -AssetManager::~AssetManager() { - -} - -Asset &AssetManager::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) { - 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::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::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<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.hpp b/src/graphics/AssetManager.hpp deleted file mode 100644 index e723398..0000000 --- a/src/graphics/AssetManager.hpp +++ /dev/null @@ -1,52 +0,0 @@ -#pragma once - -#include -#include -#include -#include -#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 { - AssetManager(); - - ~AssetManager(); - - AssetManager(const AssetManager &); - - AssetManager &operator=(const AssetManager &); - - std::map assets; - - static AssetManager &instance() { - static AssetManager 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/AssetManager_old.cpp b/src/graphics/AssetManager_old.cpp new file mode 100644 index 0000000..ef856ca --- /dev/null +++ b/src/graphics/AssetManager_old.cpp @@ -0,0 +1,146 @@ +#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 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(); + size_t size = it.value()["size"].get(); + 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<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 new file mode 100644 index 0000000..23a11a7 --- /dev/null +++ b/src/graphics/AssetManager_old.hpp @@ -0,0 +1,53 @@ +#pragma once + +#include +#include +#include +#include +#include +#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 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 index 1a44fbc..63498fa 100644 --- a/src/graphics/Display.cpp +++ b/src/graphics/Display.cpp @@ -1,6 +1,6 @@ #include #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 #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!"< #include #include -#include #include 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 #include +#include #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!"<