From f721ed0e3c6be33670fe330c029a2d4c3353f635 Mon Sep 17 00:00:00 2001
From: LaG1924 <12997935+LaG1924@users.noreply.github.com>
Date: Wed, 31 May 2017 19:17:09 +0500
Subject: 2017-05-31
---
CMakeLists.txt | 12 +-
cwd/shaders/block.fs | 95 ++-
cwd/shaders/block.vs | 8 +-
cwd/textures.tps | 1581 -----------------------------------------
depedencies/include/GL/glew.h | 2 +
src/core/AssetManager.cpp | 60 +-
src/core/AssetManager.hpp | 46 +-
src/core/Core.cpp | 255 ++++++-
src/core/Core.hpp | 5 +-
src/gamestate/GameState.hpp | 1 +
src/graphics/Shader.cpp | 3 +-
src/graphics/Shader.hpp | 2 +-
src/graphics/Texture.cpp | 2 -
src/graphics/Texture.hpp | 2 +
src/gui/Gui.hpp | 2 +
src/main.cpp | 4 +-
src/network/Network.cpp | 3 +-
src/network/Network.hpp | 1 +
src/network/NetworkClient.cpp | 1 +
src/network/NetworkClient.hpp | 1 +
src/packet/Field.cpp | 5 +-
src/packet/Field.hpp | 10 +-
src/packet/FieldParser.cpp | 43 +-
src/packet/FieldParser.hpp | 18 +-
src/packet/Packet.cpp | 19 +-
src/packet/Packet.hpp | 2 +-
src/packet/PacketParser.cpp | 24 +-
src/world/Block.cpp | 2 +-
src/world/Block.hpp | 2 +-
src/world/Section.cpp | 17 +-
src/world/Section.hpp | 1 +
src/world/World.cpp | 8 +-
src/world/World.hpp | 2 +
33 files changed, 499 insertions(+), 1740 deletions(-)
delete mode 100644 cwd/textures.tps
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 0d87b31..67974fb 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -26,13 +26,13 @@ add_executable(AltCraft ${SOURCE_FILES})
################
# CONFIGURATION
################
-set(LOGGER_DEFINITIONS "-DELPP_THREAD_SAFE -DELPP_FEATURE_CRASH_LOG")
+set(LOGGER_DEFINITIONS "-DELPP_FEATURE_CRASH_LOG -DELPP_THREAD_SAFE -DELPP_STL_LOGGING")
add_definitions(${LOGGER_DEFINITIONS})
#-DELPP_HANDLE_SIGABRT
#Set compiler's flags and setup platfrom-dependent libraries
if (CMAKE_COMPILER_IS_GNUCXX)
- set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic -w -Werror -Wall -Wextra")
+ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic -Werror")
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O0")
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3")
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address")
@@ -78,10 +78,4 @@ if (ZLIB_FOUND)
target_include_directories(AltCraft PUBLIC ${ZLIB_INCLUDE_DIR})
else ()
message(FATAL_ERROR "Zlib not found!")
-endif ()
-
-
-include(cotire)
-set_target_properties(AltCraft PROPERTIES
- COTIRE_PREFIX_HEADER_INCLUDE_PATH "${CMAKE_SOURCE_DIR}/depedencies")
-cotire(AltCraft)
\ No newline at end of file
+endif ()
\ No newline at end of file
diff --git a/cwd/shaders/block.fs b/cwd/shaders/block.fs
index 4df9b7b..dcc78cc 100644
--- a/cwd/shaders/block.fs
+++ b/cwd/shaders/block.fs
@@ -1,28 +1,95 @@
#version 330 core
-struct TextureCoordData {
- int blockId;
- int blockState;
- int blockSide;
- vec4 texture;
-};
-
in vec2 UvPosition;
-uniform sampler2D textureAtlas;
+uniform float time;
uniform int block;
+uniform sampler2D textureAtlas;
+
+// TextureIndex: [most significant bit]<-...<-side[3bit]<-id[13]<-state[4]
+layout(std140) uniform TextureIndexes { // binding point: 0
+ int totalTextures;
+ int indexes[2047];
+};
+// layout(std140) uniform TextureData { vec4 textureData[1024]; }; //binding point: 1
+// layout(std140) uniform TextureData2 { vec4 textureData2[1024]; }; //binddingpoint: 2
+vec4 GetTextureByBlockId(int BlockId);
+vec2 TransformTextureCoord(vec4 TextureAtlasCoords, vec2 UvCoords);
+
+void main() {
+vec4 bad = vec4(1,0,0,1);
+ vec4 color = vec4(0,1,0,1);
+ if (totalTextures!=6)
+ color = bad;
+ if (indexes[0]!=393248)
+ color = bad;
+ for (int i=1;i<2047;i++)
+ if (indexes[i]!=0)
+ color=bad;
+ gl_FragColor = color;
+ /*vec4 BlockTextureCoords = GetTextureByBlockId(block);
+ vec2 AtlasCoords = TransformTextureCoord(BlockTextureCoords, UvPosition);
+ gl_FragColor = texture(textureAtlas, AtlasCoords);*/
+}
vec4 GetTextureByBlockId(int BlockId) {
- return vec4(0,0,0,0);
+ if (indexes[0] == 0)
+ return vec4(0.05, 0.004, 0.007, 0.004);
+
+ if (totalTextures == 0)
+ return vec4(0, 0, 0.1, 0.1);
+ for (int i = 0; i < totalTextures; i++) {
+ int index = indexes[i + 1];
+ int side = (index & 0xE0000) >> 16;
+ int id = (index & 0xFF0) >> 4;
+ int state = index & 0xF;
+ if (id == BlockId) {
+ return vec4(i, 0, 1, 1);
+ }
+ }
+ /*
+ TNT texture:
+ X 0.0546875
+ Y ~0.00442477876106194690
+ W 0.0078125
+ H ~0.00442477876106194690
+ */
+ return vec4(0.0546875, 0.00442477876106194690, 0.0078125,
+ 0.00442477876106194690);
}
-vec4 TransformTextureCoord(vec4 TextureAtlasCoords){
- return vec4(0,0,0,0);
+vec2 TransformTextureCoord(vec4 TextureAtlasCoords, vec2 UvCoords) {
+ float x = TextureAtlasCoords.x;
+ float y = TextureAtlasCoords.y;
+ float w = TextureAtlasCoords.z;
+ float h = TextureAtlasCoords.w;
+ vec2 A = vec2(x, y);
+ vec2 B = vec2(x + w, y + h);
+
+ const bool isTextureFlippedVertically = true;
+ if (isTextureFlippedVertically) {
+ y = 1 - y;
+ A = vec2(x, y - h);
+ B = vec2(x + w, y);
+ }
+ return A + UvCoords * (B - A);
}
-void main()
+/*
+
+float near = 1.0;
+float far = 100.0;
+
+float LinearizeDepth(float depth)
{
- vec4 TextureCoords = GetTextureByBlockId(block);
- gl_FragColor = texture(textureAtlas,UvPosition);
+ float z = depth * 2.0 - 1.0; // Back to NDC
+ return (2.0 * near * far) / (far + near - z * (far - near));
}
+void main()
+{
+ float depth = LinearizeDepth(gl_FragCoord.z) / far; // divide by far for demonstration
+ gl_FragColor = vec4(vec3(depth), 1.0f);
+}*/
+
+
diff --git a/cwd/shaders/block.vs b/cwd/shaders/block.vs
index 7a36a5d..09d5b3f 100644
--- a/cwd/shaders/block.vs
+++ b/cwd/shaders/block.vs
@@ -4,12 +4,14 @@ layout (location = 2) in vec2 UvCoordinates;
out vec2 UvPosition;
+
+uniform mat4 view;
+uniform mat4 projection;
+uniform float time;
uniform mat4 model;
-uniform mat4 view;
-uniform mat4 projection;
void main()
{
gl_Position = projection * view * model * vec4(position, 1.0f);
UvPosition = vec2(UvCoordinates.x,UvCoordinates.y);
-}
\ No newline at end of file
+}
diff --git a/cwd/textures.tps b/cwd/textures.tps
deleted file mode 100644
index 914d320..0000000
--- a/cwd/textures.tps
+++ /dev/null
@@ -1,1581 +0,0 @@
-
-
-
- fileFormatVersion
- 4
- texturePackerVersion
- 4.4.0
- fileName
- /home/lag1924/dev/AltCraft/cwd/textures.tps
- autoSDSettings
-
-
- scale
- 1
- extension
-
- spriteFilter
-
- acceptFractionalValues
-
- maxTextureSize
-
- width
- -1
- height
- -1
-
-
-
- allowRotation
-
- shapeDebug
-
- dpi
- 72
- dataFormat
- json-array
- textureFileName
-
- flipPVR
-
- pvrCompressionQuality
- PVR_QUALITY_NORMAL
- atfCompressData
-
- mipMapMinSize
- 32768
- etc1CompressionQuality
- ETC1_QUALITY_LOW_PERCEPTUAL
- etc2CompressionQuality
- ETC2_QUALITY_LOW_PERCEPTUAL
- dxtCompressionMode
- DXT_PERCEPTUAL
- jxrColorFormat
- JXR_YUV444
- jxrTrimFlexBits
- 0
- jxrCompressionLevel
- 0
- ditherType
- NearestNeighbour
- backgroundColor
- 0
- libGdx
-
- filtering
-
- x
- Linear
- y
- Linear
-
-
- shapePadding
- 0
- jpgQuality
- 80
- pngOptimizationLevel
- 0
- webpQualityLevel
- 101
- textureSubPath
-
- atfFormats
-
- textureFormat
- png
- borderPadding
- 0
- maxTextureSize
-
- width
- 4096
- height
- 4096
-
- fixedTextureSize
-
- width
- -1
- height
- -1
-
- algorithmSettings
-
- algorithm
- Basic
- freeSizeMode
- Best
- sizeConstraints
- AnySize
- forceSquared
-
- maxRects
-
- heuristic
- Best
-
- basic
-
- sortBy
- Best
- order
- Ascending
-
- polygon
-
- alignToGrid
- 1
-
-
- andEngine
-
- minFilter
- Linear
- packageName
- Texture
- wrap
-
- s
- Clamp
- t
- Clamp
-
- magFilter
- MagLinear
-
- dataFileNames
-
- multiPack
-
- forceIdenticalLayout
-
- outputFormat
- RGBA8888
- alphaHandling
- ClearTransparentPixels
- contentProtection
-
- key
-
-
- autoAliasEnabled
-
- trimSpriteNames
-
- prependSmartFolderName
-
- autodetectAnimations
-
- globalSpriteSettings
-
- scale
- 1
- scaleMode
- Smooth
- extrude
- 0
- trimThreshold
- 1
- trimMargin
- 1
- trimMode
- None
- tracerTolerance
- 200
- heuristicMask
-
- defaultPivotPoint
- 0.5,0.5
- writePivotPoints
-
-
- individualSpriteSettings
-
- fileList
-
- assets/minecraft/textures
-
- ignoreFileList
-
- replaceList
-
- ignoredWarnings
-
- commonDivisorX
- 1
- commonDivisorY
- 1
- packNormalMaps
-
- autodetectNormalMaps
-
- normalMapFilter
-
- normalMapSuffix
-
- normalMapSheetFileName
-
- exporterProperties
-
-
-
diff --git a/depedencies/include/GL/glew.h b/depedencies/include/GL/glew.h
index 702265c..3c95c71 100644
--- a/depedencies/include/GL/glew.h
+++ b/depedencies/include/GL/glew.h
@@ -77,6 +77,8 @@
** MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
*/
+#define GLEW_STATIC
+
#ifndef __glew_h__
#define __glew_h__
#define __GLEW_H__
diff --git a/src/core/AssetManager.cpp b/src/core/AssetManager.cpp
index 003f2f0..eaa002f 100644
--- a/src/core/AssetManager.cpp
+++ b/src/core/AssetManager.cpp
@@ -8,9 +8,6 @@ const fs::path pathToAssetsList = "./items.json";
const fs::path pathToTextureIndex = "./textures.json";
AssetManager::AssetManager() {
- for (auto &it:fs::recursive_directory_iterator(pathToAssets)) {
-
- }
LoadIds();
LoadTextureResources();
}
@@ -23,7 +20,7 @@ void AssetManager::LoadIds() {
int id = it["type"].get();
int state = it["meta"].get();
std::string blockName = it["text_type"].get();
- assetIds[blockName] = Block(id, 0, state);
+ assetIds[blockName] = Block(id, state);
}
LOG(INFO) << "Loaded " << assetIds.size() << " ids";
}
@@ -38,13 +35,15 @@ void AssetManager::LoadTextureResources() {
nlohmann::json index;
in >> index;
std::string filename = index["meta"]["image"].get();
+ float textureWidth = index["meta"]["size"]["w"].get();
+ float textureHeight = index["meta"]["size"]["h"].get();
for (auto &it:index["frames"]) {
auto frame = it["frame"];
- TextureCoord coord;
- coord.x = frame["x"].get();
- coord.y = frame["y"].get();;
- coord.w = frame["w"].get();
- coord.h = frame["h"].get();
+ TextureCoordinates coord;
+ coord.x = frame["x"].get() / textureWidth;
+ coord.y = frame["y"].get() / textureHeight;
+ coord.w = frame["w"].get() / textureWidth;
+ coord.h = frame["h"].get() / textureHeight;
std::string assetName = it["filename"].get();
assetName.insert(0, "minecraft/textures/");
assetName.erase(assetName.length() - 4);
@@ -55,30 +54,37 @@ void AssetManager::LoadTextureResources() {
LOG(INFO) << "Texture atlas id is " << textureAtlas->texture;
}
-TextureCoord AssetManager::GetTextureByAssetName(std::string AssetName) {
- return assetTextures[AssetName];
+TextureCoordinates AssetManager::GetTextureByAssetName(std::string AssetName) {
+ if (assetTextures.find(AssetName) != assetTextures.end())
+ return assetTextures[AssetName];
+ else
+ return TextureCoordinates{-1, -1, -1, -1};
}
-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 lookupTable = {
- {Block(0), "minecraft/textures/blocks/air"},
- {Block(1, 0), "minecraft/textures/blocks/stone"},
- {Block(1, 1), "minecraft/textures/blocks/stone_granite"},
+std::string AssetManager::GetTextureAssetNameByBlockId(BlockTextureId block) {
+ //Block sides: 0 - bottom, 1 - top, 2 - north, 3 - south, 4 - west, 5 - east 6 - every side
+ std::map lookupTable = {
+ {BlockTextureId(0, 0), "minecraft/textures/blocks/air"},
+ {BlockTextureId(1, 0), "minecraft/textures/blocks/stone"},
+ {BlockTextureId(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"},
+ {BlockTextureId(2, 0, 0), "minecraft/textures/blocks/dirt"},
+ {BlockTextureId(2, 0, 1), "minecraft/textures/blocks/grass_top"},
+ {BlockTextureId(2, 0, 2), "minecraft/textures/blocks/grass_side"},
+ {BlockTextureId(2, 0, 3), "minecraft/textures/blocks/grass_side"},
+ {BlockTextureId(2, 0, 4), "minecraft/textures/blocks/grass_side"},
+ {BlockTextureId(2, 0, 5), "minecraft/textures/blocks/grass_side"},
- {Block(3), "minecraft/textures/blocks/dirt"},
- {Block(4), "minecraft/textures/blocks/cobblestone"},
+ {BlockTextureId(3, 0), "minecraft/textures/blocks/dirt"},
+ {BlockTextureId(4, 0), "minecraft/textures/blocks/cobblestone"},
};
- return lookupTable[Block(BlockId, BlockSide)];
+ return lookupTable[block];
}
-const GLuint AssetManager::GetTextureAtlas() {
+GLuint AssetManager::GetTextureAtlas() {
return textureAtlas->texture;
}
+
+TextureCoordinates AssetManager::GetTextureByBlock(BlockTextureId block) {
+ return this->GetTextureByAssetName(this->GetTextureAssetNameByBlockId(block));
+}
diff --git a/src/core/AssetManager.hpp b/src/core/AssetManager.hpp
index 23b2ba6..b378764 100644
--- a/src/core/AssetManager.hpp
+++ b/src/core/AssetManager.hpp
@@ -8,14 +8,44 @@
#include "../world/Block.hpp"
#include "../graphics/Texture.hpp"
-struct TextureCoord{
- unsigned int x,y,w,h;
+struct TextureCoordinates {
+ TextureCoordinates(float x = -1, float y = -1, float w = -1, float h = -1) : x(x), y(y), w(w), h(h) {}
+
+ bool operator==(const TextureCoordinates &rhs) const {
+ return x == rhs.x &&
+ y == rhs.y &&
+ w == rhs.w &&
+ h == rhs.h;
+ }
+
+ explicit operator bool() const {
+ return !(*this == TextureCoordinates(-1, -1, -1, -1));
+ }
+
+ float x, y, w, h;
+};
+
+struct BlockTextureId {
+ //Block sides: 0 - bottom, 1 - top, 2 - north, 3 - south, 4 - west, 5 - east 6 - every side
+ BlockTextureId(int id = 0, int state = 0, int side = 6) : id(id), state(state), side(side) {}
+
+ int id:9;
+ int state:4;
+ int side:3;
+
+ bool operator<(const BlockTextureId &rhs) const {
+ if (id < rhs.id)
+ return true;
+ if (rhs.id < id)
+ return false;
+ return state < rhs.state;
+ }
};
class AssetManager {
Texture *textureAtlas;
- std::map assetIds;
- std::map assetTextures;
+ std::map assetIds;
+ std::map assetTextures;
public:
AssetManager();
@@ -23,11 +53,13 @@ public:
void LoadTextureResources();
- TextureCoord GetTextureByAssetName(std::string AssetName);
+ TextureCoordinates GetTextureByAssetName(std::string AssetName);
- std::string GetTextureAssetNameByBlockId(unsigned short BlockId, unsigned char BlockSide = 0);
+ std::string GetTextureAssetNameByBlockId(BlockTextureId block);
- const GLuint GetTextureAtlas();
+ GLuint GetTextureAtlas();
void LoadIds();
+
+ TextureCoordinates GetTextureByBlock(BlockTextureId block);
};
diff --git a/src/core/Core.cpp b/src/core/Core.cpp
index 1481e36..ade043e 100644
--- a/src/core/Core.cpp
+++ b/src/core/Core.cpp
@@ -1,5 +1,39 @@
#include "Core.hpp"
+GLenum glCheckError_(const char *file, int line) {
+ GLenum errorCode;
+ while ((errorCode = glGetError()) != GL_NO_ERROR) {
+ std::string error;
+ switch (errorCode) {
+ case GL_INVALID_ENUM:
+ error = "INVALID_ENUM";
+ break;
+ case GL_INVALID_VALUE:
+ error = "INVALID_VALUE";
+ break;
+ case GL_INVALID_OPERATION:
+ error = "INVALID_OPERATION";
+ break;
+ case GL_STACK_OVERFLOW:
+ error = "STACK_OVERFLOW";
+ break;
+ case GL_STACK_UNDERFLOW:
+ error = "STACK_UNDERFLOW";
+ break;
+ case GL_OUT_OF_MEMORY:
+ error = "OUT_OF_MEMORY";
+ break;
+ case GL_INVALID_FRAMEBUFFER_OPERATION:
+ error = "INVALID_FRAMEBUFFER_OPERATION";
+ break;
+ }
+ LOG(ERROR) << "OpenGL error: " << error << " at " << file << ":" << line;
+ }
+ return errorCode;
+}
+
+#define glCheckError() glCheckError_(__FILE__, __LINE__)
+
const GLfloat vertices[] = {
//Z+ edge
-0.5f, 0.5f, 0.5f,
@@ -49,6 +83,56 @@ const GLfloat vertices[] = {
0.5f, -0.5f, -0.5f,
-0.5f, -0.5f, 0.5f,
};
+
+/*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,
@@ -102,7 +186,9 @@ const GLfloat uv_coords[] = {
Core::Core() {
LOG(INFO) << "Core initializing...";
InitSfml(1280, 720, "AltCraft");
+ glCheckError();
InitGlew();
+ glCheckError();
client = new NetworkClient("127.0.0.1", 25565, "HelloOne");
gameState = new GameState(client);
std::thread loop = std::thread(&Core::UpdateGameState, this);
@@ -110,6 +196,7 @@ Core::Core() {
assetManager = new AssetManager;
PrepareToWorldRendering();
LOG(INFO) << "Core is initialized";
+ glCheckError();
}
Core::~Core() {
@@ -118,10 +205,13 @@ Core::~Core() {
delete shader;
delete gameState;
delete client;
+ delete assetManager;
+ delete window;
LOG(INFO) << "Core is stopped";
}
void Core::Exec() {
+ LOG(INFO) << "Main loop is executing!";
isRunning = true;
while (isRunning) {
static sf::Clock clock, clock1;
@@ -145,8 +235,10 @@ void Core::Exec() {
HandleEvents();
if (isMouseCaptured)
HandleMouseCapture();
+ glCheckError();
RenderFrame();
+
}
}
@@ -162,11 +254,11 @@ void Core::RenderFrame() {
//RenderGui(LoadingScreen);
break;
case Playing:
- RenderWorld(gameState->world);
+ RenderWorld();
//RenderGui(HUD);
break;
case PauseMenu:
- RenderWorld(gameState->world);
+ RenderWorld();
//RenderGui(PauseGui);
break;
}
@@ -182,7 +274,8 @@ 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);
+ glCheckError();
+ 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));
@@ -193,6 +286,7 @@ void Core::InitGlew() {
LOG(INFO) << "Initializing GLEW";
glewExperimental = GL_TRUE;
GLenum glewStatus = glewInit();
+ glCheckError();
if (glewStatus != GLEW_OK) {
LOG(FATAL) << "Failed to initialize GLEW: " << glewGetErrorString(glewStatus);
}
@@ -201,6 +295,7 @@ void Core::InitGlew() {
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glFrontFace(GL_CCW);
+ glCheckError();
}
unsigned int Core::width() {
@@ -216,14 +311,18 @@ void Core::HandleEvents() {
while (window->pollEvent(event)) {
switch (event.type) {
case sf::Event::Closed:
+ LOG(INFO) << "Received close event by window closing";
isRunning = false;
break;
case sf::Event::Resized:
glViewport(0, 0, width(), height());
break;
case sf::Event::KeyPressed:
+ if (!window->hasFocus())
+ break;
switch (event.key.code) {
case sf::Keyboard::Escape:
+ LOG(INFO) << "Received close event by esc";
isRunning = false;
break;
case sf::Keyboard::T:
@@ -239,21 +338,24 @@ void Core::HandleEvents() {
break;
}
case sf::Event::MouseWheelScrolled:
- //camera.ProcessMouseScroll(event.mouseWheelScroll.delta);
+ if (!window->hasFocus())
+ break;
+ camera.ProcessMouseScroll(event.mouseWheelScroll.delta);
break;
default:
break;
}
}
-
- 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 (window->hasFocus()) {
+ 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);
+ }
}
void Core::HandleMouseCapture() {
@@ -265,23 +367,26 @@ void Core::HandleMouseCapture() {
}
void Core::RenderGui(Gui &Target) {
-
+ Target.WHY++;
}
-void Core::RenderWorld(World &Target) {
+void Core::RenderWorld() {
shader->Use();
+ glCheckError();
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.0001f, 1000.0f);
+ glm::mat4 projection = glm::perspective(camera.Zoom, (float) width() / (float) height(), 0.1f, 10000000.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);
+ glCheckError();
+
glBindVertexArray(VAO);
for (auto §ionPos:toRender) {
@@ -290,7 +395,7 @@ void Core::RenderWorld(World &Target) {
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)
+ if (block.id == 0)
continue;
glm::mat4 model;
@@ -298,7 +403,6 @@ void Core::RenderWorld(World &Target) {
sectionPos.GetZ() * 16));
model = glm::translate(model, glm::vec3(x, y, z));
-
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
glUniform1i(blockLoc, block.id);
@@ -308,6 +412,7 @@ void Core::RenderWorld(World &Target) {
}
}
glBindVertexArray(0);
+ glCheckError();
}
void Core::SetMouseCapture(bool IsCaptured) {
@@ -318,6 +423,7 @@ void Core::SetMouseCapture(bool IsCaptured) {
}
void Core::PrepareToWorldRendering() {
+ //Cube-rendering data
glGenBuffers(1, &VBO);
glGenBuffers(1, &VBO2);
glGenVertexArrays(1, &VAO);
@@ -336,19 +442,116 @@ void Core::PrepareToWorldRendering() {
}
glBindVertexArray(0);
+ glCheckError();
+
shader = new Shader("./shaders/block.vs", "./shaders/block.fs");
shader->Use();
+ LOG(INFO) << "Initializing texture atlas...";
+ //TextureAtlas texture
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, assetManager->GetTextureAtlas());
glUniform1i(glGetUniformLocation(shader->Program, "textureAtlas"), 0);
+ //TextureAtlas coordinates
+ std::vector textureCoordinates;
+ std::vector indexes;
+ GLint totalTextures;
+ for (int id = 0; id < 4096; id++) {
+ bool isReachedEnd = true;
+ for (int state = 0; state < 16; state++) {
+ if (!assetManager->GetTextureByBlock(BlockTextureId(id, state, 6)) ||
+ !assetManager->GetTextureByBlock(BlockTextureId(id, state, 0))) {
+ continue;
+ }
+ isReachedEnd = false;
+ int side = assetManager->GetTextureByBlock(BlockTextureId(id, state, 6)) ? 6 : 0;
+ do {
+ int index = (side << 16) | (id << 4) | state;
+ TextureCoordinates tc = assetManager->GetTextureByBlock(BlockTextureId(id, state, side));
+ textureCoordinates.push_back(glm::vec4(tc.x, tc.y, tc.w, tc.h));
+ indexes.push_back(index);
+ /*LOG(ERROR) << "Encoded (" << side << " " << id << " " << state << ") as " << index << " ("
+ << std::bitset<20>(index) << ")";*/
+ /*LOG(FATAL)<(index);
+ side = 0x7;
+ id = 0xFFF;
+ state = 0xF;
+ LOG(WARNING) << "side: " << side << " id: " << id << " state: " << state;
+ int i, si, st, index = 0;
+ si = side << 15;
+ i = id<<3;
+ st = state;
+ index = i | si | st;
+ LOG(FATAL) << std::bitset<18>(index) << " (" << index << "): " << std::bitset<18>(si) << " "
+ << std::bitset<18>(i) << " " << std::bitset<18>(st);*/
+ /*if (rand() == 73) //Almost impossible(Almost==1/32768)
+ {
+ int index = 393233;
+ LOG(WARNING) << std::bitset<20>(index) << "(" << index << ")";
+ int side = (index & 0xE0000) >> 16;
+ int id = (index & 0xFF0) >> 4;
+ int state = index & 0xF;
+ LOG(WARNING) << std::bitset<20>(side) << " " << std::bitset<20>(id) << " "
+ << std::bitset<20>(state);
+ LOG(FATAL) << side << " " << id << " " << state;
+ }*/
+ side++;
+ } while (side < 7);
+ }
+ if (isReachedEnd)
+ break;
+
+ }
+ totalTextures = indexes.size();
+ LOG(INFO) << "Created " << totalTextures << " texture indexes";
+ CHECK_EQ(indexes.size(), textureCoordinates.size()) << "Arrays of textureCoordinates and of indexes is not equals";
+ CHECK_LE(totalTextures, 2048) << "There is more texture indexes, than GLSL buffer allows";
+
+ for (auto& it:indexes){
+ LOG(WARNING)<Program, "TextureIndexes");
+ glUniformBlockBinding(shader->Program, ubo, 0);
+ glGenBuffers(1, &UBO);
+ glBindBuffer(GL_UNIFORM_BUFFER, UBO);
+ glBufferData(GL_UNIFORM_BUFFER, indexes.size() * sizeof(GLint), NULL, GL_STATIC_DRAW);
+ glBindBufferRange(GL_UNIFORM_BUFFER, 0, UBO, 0, indexes.size() * sizeof(GLint));
+ glBufferSubData(GL_UNIFORM_BUFFER, 0, indexes.size() * sizeof(GLint), &indexes[0]);
+ glCheckError();
+
+ LOG(WARNING)<<"Uploaded "<Program, "TextureData");
+ glUniformBlockBinding(shader->Program, ubo2, 1);
+ glGenBuffers(1, &UBO2);
+ glBindBuffer(GL_UNIFORM_BUFFER, UBO2);
+ glBufferData(GL_UNIFORM_BUFFER, sizeof(glm::vec4) * 1024, NULL, GL_STATIC_DRAW);
+ glBindBufferRange(GL_UNIFORM_BUFFER, 1, UBO2, 0, 1024 * sizeof(glm::vec4));
+ glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(glm::vec4) * textureCoordinates.size(), textureCoordinates.data());*/
+
+ /*
+ GLuint ubo3 = glGetUniformBlockIndex(shader->Program, "TextureData2");
+ glUniformBlockBinding(shader->Program, ubo3, 2);
+ glGenBuffers(1, &UBO3);
+ glBindBuffer(GL_UNIFORM_BUFFER, UBO3);
+ glBufferData(GL_UNIFORM_BUFFER, sizeof(glm::vec4) * 1024, NULL, GL_STATIC_DRAW);
+ glBindBufferRange(GL_UNIFORM_BUFFER, 2, UBO3, 0, 1024 * sizeof(glm::vec4));*/
+
+ glBindBuffer(GL_UNIFORM_BUFFER,0);
+ glCheckError();
}
void Core::UpdateChunksToRender() {
camera.Position = glm::vec3(gameState->g_PlayerX, gameState->g_PlayerY, gameState->g_PlayerZ);
toRender.clear();
- const float ChunkDistance = 1.3;
+ const float ChunkDistance = 1;
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) {
@@ -359,12 +562,26 @@ void Core::UpdateChunksToRender() {
toRender.push_back(chunkPosition);
}
LOG(INFO) << "Chunks to render: " << toRender.size();
+
+ /*std::map totalBlocks;
+ for (auto §ion:toRender)
+ for (int x = 0; x < 16; x++)
+ for (int y = 0; y < 16; y++)
+ for (int z = 0; z < 16; z++)
+ totalBlocks[gameState->world.m_sections.find(section)->second.GetBlock(Vector(x, y, z))]++;
+ for (auto &it:totalBlocks) {
+ LOG(WARNING) << it.first.id << ":" << (int) it.first.state << " = " << it.second << " ("
+ << std::bitset<13>(it.first.id) << ")";
+ }*/
}
void Core::UpdateGameState() {
+ el::Helpers::setThreadName("Game");
LOG(INFO) << "GameState thread is started";
while (isRunning) {
gameState->Update();
+ if (toRender.size() > 0)
+ break;
}
LOG(INFO) << "GameState thread is stopped";
}
diff --git a/src/core/Core.hpp b/src/core/Core.hpp
index 8bf74da..e5fe315 100644
--- a/src/core/Core.hpp
+++ b/src/core/Core.hpp
@@ -29,7 +29,7 @@ class Core {
float deltaTime;
float absTime;
- void RenderWorld(World &Target);
+ void RenderWorld();
void RenderGui(Gui &Target);
@@ -59,7 +59,8 @@ class Core {
Camera3D camera;
Shader *shader;
- GLuint VBO, VAO, VBO2;
+ //Cube verticies, Cube VAO, Cube UVs, TextureIndexes UBO, TextureData UBO, TextureData2 UBO
+ GLuint VBO, VAO, VBO2, UBO,UBO2,UBO3;
std::vector toRender;
public:
diff --git a/src/gamestate/GameState.hpp b/src/gamestate/GameState.hpp
index c9ca44a..8817f4c 100644
--- a/src/gamestate/GameState.hpp
+++ b/src/gamestate/GameState.hpp
@@ -1,5 +1,6 @@
#pragma once
+#include
#include "../world/World.hpp"
#include "../network/NetworkClient.hpp"
#include "../packet/PacketParser.hpp"
diff --git a/src/graphics/Shader.cpp b/src/graphics/Shader.cpp
index 9bb08ba..83eb93f 100644
--- a/src/graphics/Shader.cpp
+++ b/src/graphics/Shader.cpp
@@ -1,4 +1,3 @@
-#include
#include "Shader.hpp"
Shader::Shader(const GLchar *vertexPath, const GLchar *fragmentPath) {
@@ -70,7 +69,7 @@ Shader::Shader(const GLchar *vertexPath, const GLchar *fragmentPath) {
glGetProgramiv(this->Program, GL_LINK_STATUS, &success);
if (!success) {
glGetProgramInfoLog(this->Program, 512, NULL, infoLog);
- LOG(ERROR) << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog;
+ LOG(FATAL) << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog;
}
// Удаляем шейдеры, поскольку они уже в программу и нам больше не нужны.
diff --git a/src/graphics/Shader.hpp b/src/graphics/Shader.hpp
index 66d687c..a336b1a 100644
--- a/src/graphics/Shader.hpp
+++ b/src/graphics/Shader.hpp
@@ -2,7 +2,7 @@
#include
#include
#include
-
+#include
#include
class Shader
diff --git a/src/graphics/Texture.cpp b/src/graphics/Texture.cpp
index bd5c53f..5d183c3 100644
--- a/src/graphics/Texture.cpp
+++ b/src/graphics/Texture.cpp
@@ -1,5 +1,3 @@
-#include
-#include
#include "Texture.hpp"
Texture::Texture(std::string filename, GLenum textureWrapping, GLenum textureFiltering) {
diff --git a/src/graphics/Texture.hpp b/src/graphics/Texture.hpp
index 8e3f1af..277806a 100644
--- a/src/graphics/Texture.hpp
+++ b/src/graphics/Texture.hpp
@@ -1,5 +1,7 @@
#pragma once
+#include
+#include
#include
class Texture {
diff --git a/src/gui/Gui.hpp b/src/gui/Gui.hpp
index 1ea4e11..641b941 100644
--- a/src/gui/Gui.hpp
+++ b/src/gui/Gui.hpp
@@ -2,4 +2,6 @@
class Gui {
+public:
+ int WHY=0;
};
diff --git a/src/main.cpp b/src/main.cpp
index de2c9d8..8a7b974 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -15,10 +15,12 @@ int main() {
el::Configurations loggerConfiguration;
el::Helpers::installCustomFormatSpecifier(
el::CustomFormatSpecifier("%startTime", std::bind(getTimeSinceProgramStart)));
- std::string format = "[%startTime][%level][%fbase]: %msg";
+ std::string format = "[%startTime][%level][%thread][%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);
+ loggerConfiguration.set(el::Level::Warning, el::ConfigurationType::Format, format);
+ el::Helpers::setThreadName("Render");
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 b3b1e4b..7757be9 100644
--- a/src/network/Network.cpp
+++ b/src/network/Network.cpp
@@ -1,5 +1,4 @@
#include "Network.hpp"
-#include "../packet/PacketBuilder.hpp"
Network::Network(std::string address, unsigned short port) : m_address(address), m_port(port) {
LOG(INFO) << "Connecting to server " << m_address << ":" << m_port;
@@ -55,7 +54,7 @@ Packet Network::ReceivePacket() {
break;
}
}
- Field fLen = FieldParser::Parse(VarInt, bufLen);
+ Field fLen = FieldParser::Parse(VarIntType, bufLen);
size_t packetLen = fLen.GetVarInt() + fLen.GetLength();
if (packetLen > 1024 * 1024 * 15)
LOG(WARNING)<<"OMG SIZEOF PACKAGE IS "<
#include
#include "../packet/Packet.hpp"
+#include "../packet/PacketBuilder.hpp"
class Network {
diff --git a/src/network/NetworkClient.cpp b/src/network/NetworkClient.cpp
index 9916b10..fd957a5 100644
--- a/src/network/NetworkClient.cpp
+++ b/src/network/NetworkClient.cpp
@@ -95,6 +95,7 @@ void NetworkClient::Update() {
}
void NetworkClient::MainLoop() {
+ el::Helpers::setThreadName("Network");
try {
while (isContinue) {
Update();
diff --git a/src/network/NetworkClient.hpp b/src/network/NetworkClient.hpp
index 9f68e37..14745a5 100644
--- a/src/network/NetworkClient.hpp
+++ b/src/network/NetworkClient.hpp
@@ -3,6 +3,7 @@
#include
#include
#include
+#include
#include "Network.hpp"
#include "../packet/PacketParser.hpp"
#include "../packet/PacketBuilder.hpp"
diff --git a/src/packet/Field.cpp b/src/packet/Field.cpp
index 69402f1..9be2469 100644
--- a/src/packet/Field.cpp
+++ b/src/packet/Field.cpp
@@ -1,4 +1,3 @@
-#include
#include "Field.hpp"
Field::Field() {
@@ -59,7 +58,7 @@ int Field::GetVarInt() {
void Field::SetVarInt(int value) {
Clear();
- m_type = VarInt;
+ m_type = VarIntType;
m_data = new byte[5];
m_dataLength = VarIntWrite(value, m_data);
}
@@ -252,7 +251,7 @@ void Field::SetDouble(double value) {
size_t Field::GetFieldLength(FieldType type) {
switch (type) {
- case Unknown:
+ case UnknownType:
return 0;
case Boolean:
return 1;
diff --git a/src/packet/Field.hpp b/src/packet/Field.hpp
index 8be9c9b..c33cd1c 100644
--- a/src/packet/Field.hpp
+++ b/src/packet/Field.hpp
@@ -2,8 +2,10 @@
#include
#include
+#include
#include
#include
+
#include "../utility/utility.h"
#include "../utility/Vector.hpp"
@@ -11,7 +13,7 @@ typedef unsigned char byte;
typedef signed char sbyte;
enum FieldType {
- Unknown = 0,
+ UnknownType = 0,
Boolean, //Bool
Byte8_t, //int8_t
UnsignedByte, //uint8_t
@@ -28,7 +30,7 @@ enum FieldType {
String = 100, //std::string
Chat, //std::string
- VarInt, //int32_t
+ VarIntType, //int32_t
VarLong, //int64_t
ChunkSection, //byte*
EntityMetadata, //byte*
@@ -55,7 +57,7 @@ public:
void CopyToBuff(byte *ptr);
- void SetRaw(byte *ptr, size_t len = 0, FieldType type = Unknown);
+ void SetRaw(byte *ptr, size_t len = 0, FieldType type = UnknownType);
FieldType GetType();
@@ -114,6 +116,6 @@ public:
private:
size_t m_dataLength = 0;
byte *m_data = nullptr;
- FieldType m_type = Unknown;
+ FieldType m_type = UnknownType;
std::vector m_childs;
};
diff --git a/src/packet/FieldParser.cpp b/src/packet/FieldParser.cpp
index 500a973..295e78f 100644
--- a/src/packet/FieldParser.cpp
+++ b/src/packet/FieldParser.cpp
@@ -2,26 +2,26 @@
Field FieldParser::Parse(FieldType type, byte *data, size_t len) {
switch (type) {
- case VarInt:
+ case VarIntType:
return ParseVarInt(data, len);
case Boolean:
- return ParseBool(data, len);
+ return ParseBool(data);
case String:
- return ParseString(data, len);
+ return ParseString(data);
case Long:
- return ParseLong(data, len);
+ return ParseLong(data);
case Int:
- return ParseInt(data, len);
+ return ParseInt(data);
case UnsignedByte:
- return ParseUByte(data, len);
+ return ParseUByte(data);
case Byte8_t:
- return ParseByte(data, len);
+ return ParseByte(data);
case Float:
- return ParseFloat(data, len);
+ return ParseFloat(data);
case Position:
- return ParsePosition(data, len);
+ return ParsePosition(data);
case Double:
- return ParseDouble(data, len);
+ return ParseDouble(data);
case ByteArray:
return ParseByteArray(data, len);
default:
@@ -29,23 +29,23 @@ Field FieldParser::Parse(FieldType type, byte *data, size_t len) {
}
}
-Field FieldParser::ParseString(byte *data, size_t len) {
+Field FieldParser::ParseString(byte *data) {
Field fLen = ParseVarInt(data, 0);
Field f;
f.SetRaw(data, fLen.GetLength() + fLen.GetVarInt(), String);
return f;
}
-Field FieldParser::ParseBool(byte *data, size_t len) {
+Field FieldParser::ParseBool(byte *data) {
Field f;
- f.SetRaw(data,1,Boolean);
+ f.SetRaw(data, 1, Boolean);
return f;
}
Field FieldParser::ParseVarInt(byte *data, size_t len) {
if (len != 0) {
Field f;
- f.SetRaw(data, len, VarInt);
+ f.SetRaw(data, len, VarIntType);
return f;
}
int val = VarIntRead(data, len);
@@ -54,43 +54,43 @@ Field FieldParser::ParseVarInt(byte *data, size_t len) {
return f;
}
-Field FieldParser::ParseLong(byte *data, size_t len) {
+Field FieldParser::ParseLong(byte *data) {
Field f;
f.SetRaw(data, 8, Long);
return f;
}
-Field FieldParser::ParseInt(byte *data, size_t len) {
+Field FieldParser::ParseInt(byte *data) {
Field f;
f.SetRaw(data, 4, Int);
return f;
}
-Field FieldParser::ParseUByte(byte *data, size_t len) {
+Field FieldParser::ParseUByte(byte *data) {
Field f;
f.SetRaw(data, 1, UnsignedByte);
return f;
}
-Field FieldParser::ParseByte(byte *data, size_t len) {
+Field FieldParser::ParseByte(byte *data) {
Field f;
f.SetRaw(data, 1, Byte8_t);
return f;
}
-Field FieldParser::ParseFloat(byte *data, size_t len) {
+Field FieldParser::ParseFloat(byte *data) {
Field f;
f.SetRaw(data, 4, Float);
return f;
}
-Field FieldParser::ParsePosition(byte *data, size_t len) {
+Field FieldParser::ParsePosition(byte *data) {
Field f;
f.SetRaw(data, 8, Position);
return f;
}
-Field FieldParser::ParseDouble(byte *data, size_t len) {
+Field FieldParser::ParseDouble(byte *data) {
Field f;
f.SetRaw(data, 8, Double);
return f;
@@ -101,6 +101,5 @@ Field FieldParser::ParseByteArray(byte *data, size_t len) {
throw 119;
Field f;
f.SetRaw(data, len, Byte8_t);
- //f.SetRaw(data, len, ByteArray);
return f;
}
diff --git a/src/packet/FieldParser.hpp b/src/packet/FieldParser.hpp
index 274ab9e..f256b34 100644
--- a/src/packet/FieldParser.hpp
+++ b/src/packet/FieldParser.hpp
@@ -6,25 +6,25 @@ class FieldParser {
public:
static Field ParseVarInt(byte *data, size_t len);
- static Field ParseBool(byte *data, size_t len);
+ static Field ParseBool(byte *data);
- static Field ParseString(byte *data, size_t len);
+ static Field ParseString(byte *data);
static Field Parse(FieldType type, byte* data, size_t len=0);
- static Field ParseLong(byte *data, size_t len);
+ static Field ParseLong(byte *data);
- static Field ParseInt(byte *data, size_t len);
+ static Field ParseInt(byte *data);
- static Field ParseUByte(byte *data, size_t len);
+ static Field ParseUByte(byte *data);
- static Field ParseByte(byte *data, size_t len);
+ static Field ParseByte(byte *data);
- static Field ParseFloat(byte *data, size_t len);
+ static Field ParseFloat(byte *data);
- static Field ParsePosition(byte *data, size_t len);
+ static Field ParsePosition(byte *data);
- static Field ParseDouble(byte *data, size_t len);
+ static Field ParseDouble(byte *data);
static Field ParseByteArray(byte *data, size_t len);
};
\ No newline at end of file
diff --git a/src/packet/Packet.cpp b/src/packet/Packet.cpp
index 695e371..68cc3c3 100644
--- a/src/packet/Packet.cpp
+++ b/src/packet/Packet.cpp
@@ -23,9 +23,9 @@ void Packet::swap(Packet &other) {
void Packet::CopyToBuff(byte *ptr) {
m_fields[0].SetVarInt(GetLength() - m_fields[0].GetLength());
- for (int i = 0; i < m_fields.size(); i++) {
- m_fields[i].CopyToBuff(ptr);
- ptr += m_fields[i].GetLength();
+ for (auto &it:m_fields) {
+ it.CopyToBuff(ptr);
+ ptr += it.GetLength();
}
}
@@ -44,28 +44,29 @@ void Packet::ParseField(FieldType type, size_t len) {
}
Packet::Packet(byte *data) {
- Field fLen = FieldParser::Parse(VarInt, data);
+ Field fLen = FieldParser::Parse(VarIntType, data);
data += fLen.GetLength();
- Field fId = FieldParser::Parse(VarInt, data);
+ Field fId = FieldParser::Parse(VarIntType, data);
data += fId.GetLength();
m_dataLength = fLen.GetVarInt() - fId.GetLength();
m_data = new byte[m_dataLength];
- std::copy(data,data+m_dataLength,m_data);
+ std::copy(data, data + m_dataLength, m_data);
m_parsePtr = m_data;
m_fields.push_back(fLen);
m_fields.push_back(fId);
}
Field &Packet::GetField(int id) {
- if (id < -2 || id >= m_fields.size() - 2)
+ if (id < -2 || id >= (int) m_fields.size() - 2)
throw 111;
return m_fields[id + 2];
}
size_t Packet::GetLength() {
size_t len = 0;
- for (int i = 0; i < m_fields.size(); i++)
- len += m_fields[i].GetLength();
+ for (auto &it:m_fields) {
+ len += it.GetLength();
+ }
return len + m_dataLength;
}
diff --git a/src/packet/Packet.hpp b/src/packet/Packet.hpp
index 67e95e5..68a5d5e 100644
--- a/src/packet/Packet.hpp
+++ b/src/packet/Packet.hpp
@@ -30,7 +30,7 @@ enum PacketsClientBound{
ChatMessage,
MultiBlockChange,
ConfirmTransaction,
- CloseWindow,
+ CloseWindowEvent,
OpenWindow,
WindowItems,
WindowProperty,
diff --git a/src/packet/PacketParser.cpp b/src/packet/PacketParser.cpp
index 488c812..a609011 100644
--- a/src/packet/PacketParser.cpp
+++ b/src/packet/PacketParser.cpp
@@ -21,7 +21,8 @@ void PacketParser::Parse(Packet &packet, ConnectionState state, bool ClientBound
}
void PacketParser::ParseServerBound(Packet &packet, ConnectionState state) {
- throw 107;
+ if (packet.GetLength() != state)
+ throw 107;
}
void PacketParser::ParseLogin(Packet &packet) {
@@ -32,9 +33,8 @@ void PacketParser::ParseLogin(Packet &packet) {
case 0x02:
ParseLogin0x02(packet);
break;
- default:
- {
- int i = packet.GetId();
+ default: {
+
//throw 112;
}
}
@@ -94,7 +94,7 @@ void PacketParser::ParsePlay0x23(Packet &packet) {
}
void PacketParser::ParsePlay0x1F(Packet &packet) {
- packet.ParseField(VarInt);
+ packet.ParseField(VarIntType);
}
void PacketParser::ParsePlay0x0D(Packet &packet) {
@@ -118,7 +118,7 @@ void PacketParser::ParsePlay0x2E(Packet &packet) {
packet.ParseField(Float);
packet.ParseField(Float);
packet.ParseField(Byte8_t);
- packet.ParseField(VarInt);
+ packet.ParseField(VarIntType);
}
void PacketParser::ParsePlay0x1A(Packet &packet) {
@@ -129,19 +129,19 @@ void PacketParser::ParsePlay0x20(Packet &packet) {
packet.ParseField(Int);
packet.ParseField(Int);
packet.ParseField(Boolean);
- packet.ParseField(VarInt);
- packet.ParseField(VarInt);
+ packet.ParseField(VarIntType);
+ packet.ParseField(VarIntType);
packet.ParseField(ByteArray, packet.GetField(4).GetVarInt());
- packet.ParseField(VarInt);
+ packet.ParseField(VarIntType);
//packet.ParseField(NbtTag);
//packet.GetField(7).SetArray(packet.GetField(6).GetVarInt());
}
void PacketParser::ParsePlay0x07(Packet &packet) {
- packet.ParseField(VarInt);
+ packet.ParseField(VarIntType);
packet.AddField(Field());
- for (int i=0;i(m_dataBlocks);
- for (int i = 0; i < m_dataBlocksLen / 8; i++)
+ for (size_t i = 0; i < m_dataBlocksLen / 8; i++)
endswap(&longArray[i]);
std::vector blocks;
blocks.reserve(4096);
int bitPos = 0;
unsigned short t = 0;
- for (int i = 0; i < m_dataBlocksLen; i++) {
+ for (size_t i = 0; i < m_dataBlocksLen; i++) {
for (int j = 0; j < 8; j++) {
t |= (m_dataBlocks[i] & 0x01) ? 0x80 : 0x00;
t >>= 1;
@@ -77,7 +78,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, 0, light[i]);
+ Block block(blockId>>4, blockId>>4 & 0xF);
m_blocks.push_back(block);
}
if ((light.size() + blocks.size()) / 2 != 4096) {
@@ -90,7 +91,17 @@ void Section::Parse() {
m_dataLight = nullptr;
delete[] m_dataSkyLight;
m_dataSkyLight = nullptr;
+
parseWaiter.notify_all();
+ /*static std::map totalBlocks;
+ for (int x=0;x<16;x++)
+ for (int y=0;y<16;y++)
+ for (int z=0;z<16;z++)
+ totalBlocks[GetBlock(Vector(x,y,z))]++;
+ LOG(ERROR)<<"Logging chunk";
+ for (auto& it:totalBlocks){
+ LOG(WARNING)<
#include