diff options
Diffstat (limited to '')
-rw-r--r-- | CMakeLists.txt | 17 | ||||
-rw-r--r-- | shaders/simple.fs | 4 | ||||
-rw-r--r-- | src/core/Core.cpp | 359 | ||||
-rw-r--r-- | src/core/Core.hpp | 59 | ||||
-rw-r--r-- | src/gamestate/GameState.cpp | 127 | ||||
-rw-r--r-- | src/gamestate/GameState.hpp | 28 | ||||
-rw-r--r-- | src/graphics/AssetManager.cpp | 3 | ||||
-rw-r--r-- | src/graphics/Display.cpp | 168 | ||||
-rw-r--r-- | src/graphics/Shader.hpp | 1 | ||||
-rw-r--r-- | src/gui/Gui.cpp | 1 | ||||
-rw-r--r-- | src/gui/Gui.hpp | 5 | ||||
-rw-r--r-- | src/gui/Widget.cpp | 1 | ||||
-rw-r--r-- | src/gui/Widget.hpp | 8 | ||||
-rw-r--r-- | src/main.cpp | 7 | ||||
-rw-r--r-- | src/network/Network.cpp | 11 | ||||
-rw-r--r-- | src/network/Network.hpp | 1 |
16 files changed, 695 insertions, 105 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 01c3521..baeabf1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,12 +13,17 @@ if (CMAKE_COMPILER_IS_GNUCXX) set(PLATFORM_LIBRARIES "pthread") endif () -set(SOURCE_FILES src/main.cpp src/packet/Field.hpp src/utility/utility.cpp src/packet/Packet.hpp src/packet/FieldParser.hpp src/network/Network.hpp src/network/Network.cpp src/network/NetworkClient.cpp - src/network/NetworkClient.hpp src/packet/PacketBuilder.cpp src/packet/PacketBuilder.hpp src/packet/Packet.cpp src/packet/FieldParser.cpp src/packet/Field.cpp - src/packet/PacketParser.cpp src/packet/PacketParser.hpp src/gamestate/Game.cpp - src/gamestate/Game.hpp src/world/World.cpp src/world/World.hpp src/world/Block.cpp src/world/Block.hpp src/world/Section.cpp src/world/Section.hpp src/nbt/Nbt.hpp src/graphics/AssetManager.cpp - src/graphics/AssetManager.hpp src/graphics/Display.cpp src/graphics/Display.hpp src/graphics/Camera3D.hpp src/graphics/Camera3D.cpp - src/graphics/Shader.hpp src/graphics/Shader.cpp src/graphics/Texture.hpp src/graphics/Texture.cpp src/core/Core.cpp src/core/Core.hpp src/gamestate/GameState.cpp src/gamestate/GameState.hpp src/utility/Vector.hpp) +set(SOURCE_FILES src/main.cpp src/packet/Field.hpp src/utility/utility.cpp src/packet/Packet.hpp + src/packet/FieldParser.hpp src/network/Network.hpp src/network/Network.cpp src/network/NetworkClient.cpp + src/network/NetworkClient.hpp src/packet/PacketBuilder.cpp src/packet/PacketBuilder.hpp src/packet/Packet.cpp + src/packet/FieldParser.cpp src/packet/Field.cpp src/packet/PacketParser.cpp src/packet/PacketParser.hpp + src/gamestate/Game.cpp src/gamestate/Game.hpp src/world/World.cpp src/world/World.hpp src/world/Block.cpp + src/world/Block.hpp src/world/Section.cpp src/world/Section.hpp src/nbt/Nbt.hpp src/graphics/AssetManager.cpp + src/graphics/AssetManager.hpp src/graphics/Display.cpp src/graphics/Display.hpp src/graphics/Camera3D.hpp + src/graphics/Camera3D.cpp src/graphics/Shader.hpp src/graphics/Shader.cpp src/graphics/Texture.hpp + src/graphics/Texture.cpp src/core/Core.cpp src/core/Core.hpp src/gamestate/GameState.cpp + src/gamestate/GameState.hpp src/utility/Vector.hpp src/gui/Gui.cpp src/gui/Gui.hpp src/gui/Widget.cpp + src/gui/Widget.hpp) add_executable(AltCraft ${SOURCE_FILES}) target_link_libraries(AltCraft ${PLATFORM_LIBRARIES}) diff --git a/shaders/simple.fs b/shaders/simple.fs index f933eb8..cc7f812 100644 --- a/shaders/simple.fs +++ b/shaders/simple.fs @@ -9,7 +9,7 @@ uniform float time; void main() { - //color = texture(blockTexture,TexCoord); - color = vec4(TexCoord.x,TexCoord.y,0.0,1.0); + color = texture(blockTexture,TexCoord); + //color = vec4(TexCoord.x,TexCoord.y,0.0,1.0); } diff --git a/src/core/Core.cpp b/src/core/Core.cpp index a286359..54a16a4 100644 --- a/src/core/Core.cpp +++ b/src/core/Core.cpp @@ -1,13 +1,362 @@ #include "Core.hpp" +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, +}; + Core::Core() { - LOG(INFO)<<"Core initializing..."; + 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); + LOG(INFO) << "Core is initialized"; +} + +Core::~Core() { + LOG(INFO) << "Core stopping..."; + delete shader; + delete client; + delete gameState; + LOG(INFO) << "Core is stopped"; +} + +void Core::Exec() { + isRunning = true; + while (isRunning) { + static sf::Clock clock, clock1; + deltaTime = clock.getElapsedTime().asSeconds(); + absTime = clock1.getElapsedTime().asSeconds(); + clock.restart(); + + static bool alreadyDone = false; + if (gameState->g_IsGameStarted && !alreadyDone) { + alreadyDone = true; + UpdateChunksToRender(); + } + + std::ostringstream toWindow; + glm::highp_vec3 camPos(camera.Position); + toWindow << std::setprecision(2) << std::fixed; + toWindow << "Pos: " << camPos.x << ", " << camPos.y << ", " << camPos.z << "; "; + toWindow << "FPS: " << (1.0f / deltaTime) << " "; + window->setTitle(toWindow.str()); + + HandleEvents(); + if (isMouseCaptured) + HandleMouseCapture(); + + RenderFrame(); + } } -void Core::MainLoop() { +void Core::RenderFrame() { + glClearColor(0.2f, 0.3f, 0.3f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + switch (currentState) { + case MainMenu: + //RenderGui(MenuScreen); + break; + case Loading: + //RenderGui(LoadingScreen); + break; + case Playing: + RenderWorld(gameState->world); + //RenderGui(HUD); + break; + case PauseMenu: + RenderWorld(gameState->world); + //RenderGui(PauseGui); + break; + } + window->display(); } -Core::~Core() { - LOG(INFO)<<"Core stopping..."; -}
\ No newline at end of file +void Core::InitSfml(unsigned int WinWidth, unsigned int WinHeight, std::string WinTitle) { + LOG(INFO) << "Creating window: " << WinWidth << "x" << WinHeight << " \"" << WinTitle << "\""; + 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->setPosition(sf::Vector2i(sf::VideoMode::getDesktopMode().width / 2 - window->getSize().x / 2, + sf::VideoMode::getDesktopMode().height / 2 - window->getSize().y / 2)); + + SetMouseCapture(false); +} + +void Core::InitGlew() { + LOG(INFO) << "Initializing GLEW"; + glewExperimental = GL_TRUE; + GLenum glewStatus = glewInit(); + if (glewStatus != GLEW_OK) { + LOG(FATAL) << "Failed to initialize GLEW: " << glewGetErrorString(glewStatus); + } + glViewport(0, 0, width(), height()); + glEnable(GL_DEPTH_TEST); + glEnable(GL_CULL_FACE); + glCullFace(GL_BACK); + glFrontFace(GL_CCW); +} + +unsigned int Core::width() { + return window->getSize().x; +} + +unsigned int Core::height() { + return window->getSize().y; +} + +void Core::HandleEvents() { + sf::Event event; + 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: + SetMouseCapture(!isMouseCaptured); + break; + default: + break; + } + case sf::Event::MouseWheelScrolled: + //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); +} + +void Core::HandleMouseCapture() { + 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); + mouseXDelta = (mousePos - center).x, mouseYDelta = (center - mousePos).y; + camera.ProcessMouseMovement(mouseXDelta, mouseYDelta); +} + +void Core::RenderGui(Gui &Target) { + +} + +void Core::RenderWorld(World &Target) { + 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 = gameState->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::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); + glUniform1i(glGetUniformLocation(shader->Program, "blockTexture"), 0); + + glDrawArrays(GL_TRIANGLES, 0, 36); + } + } + } + } + glBindVertexArray(0); +} + +void Core::SetMouseCapture(bool IsCaptured) { + window->setMouseCursorVisible(!isMouseCaptured); + sf::Mouse::setPosition(sf::Vector2i(window->getSize().x / 2, window->getSize().y / 2), *window); + isMouseCaptured = IsCaptured; + window->setMouseCursorVisible(!IsCaptured); +} + +void Core::PrepareToWorldRendering() { + + 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 = new Shader("./shaders/simple.vs", "./shaders/simple.fs"); + shader->Use(); +} + +void Core::UpdateChunksToRender() { + camera.Position = glm::vec3(gameState->g_PlayerX, gameState->g_PlayerY, gameState->g_PlayerZ); + toRender.clear(); + 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) { + Vector chunkPosition = it.first; + Vector delta = chunkPosition - playerChunk; + if (delta.GetDistance() > ChunkDistance) + continue; + toRender.push_back(chunkPosition); + } + LOG(INFO) << "Chunks to render: " << toRender.size(); +} + +void Core::UpdateGameState() { + while (gameState && client){ + gameState->Update(); + } +} diff --git a/src/core/Core.hpp b/src/core/Core.hpp index 5348e4e..a877613 100644 --- a/src/core/Core.hpp +++ b/src/core/Core.hpp @@ -1,16 +1,69 @@ #pragma once +#include <easylogging++.h> +#include <SFML/Window.hpp> +#include <GL/glew.h> +#include <iomanip> +#include <glm/gtc/type_ptr.hpp> #include "../gamestate/GameState.hpp" #include "../network/NetworkClient.hpp" -#include <easylogging++.h> - +#include "../gui/Gui.hpp" +#include "../graphics/Camera3D.hpp" +#include "../graphics/Shader.hpp" +#include "../graphics/AssetManager.hpp" class Core { GameState *gameState; NetworkClient *client; + sf::Window *window; + bool isMouseCaptured = false, isRunning = true; + enum { + MainMenu, + Loading, + Playing, + PauseMenu, + } currentState = Playing; + float mouseXDelta, mouseYDelta; + float deltaTime; + float absTime; + + void RenderWorld(World &Target); + + void RenderGui(Gui &Target); + + void HandleMouseCapture(); + + void HandleEvents(); + + void InitSfml(unsigned int WinWidth, unsigned int WinHeight, std::string WinTitle); + + void InitGlew(); + + void SetMouseCapture(bool IsCaptured); + + void PrepareToWorldRendering(); + + void RenderFrame(); + + unsigned int width(); + + unsigned int height(); + + void UpdateChunksToRender(); + + void UpdateGameState(); + + std::thread gameStateLoopThread; + + Camera3D camera; + Shader *shader; + GLuint VBO, VAO, VBO2; + std::vector<Vector> toRender; public: Core(); + ~Core(); - void MainLoop(); + + void Exec(); }; diff --git a/src/gamestate/GameState.cpp b/src/gamestate/GameState.cpp index 7bcc2d5..aaeecb1 100644 --- a/src/gamestate/GameState.cpp +++ b/src/gamestate/GameState.cpp @@ -1 +1,128 @@ #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; + } + PacketParser::Parse(*response, Login); + g_PlayerUuid = response->GetField(0).GetString(); + g_PlayerName = response->GetField(1).GetString(); + delete response; + m_networkState = ConnectionState::Play; + LOG(INFO) << g_PlayerName << "'s UUID is " << g_PlayerUuid; +} + +void GameState::Update() { + Packet &packet = *nc->GetPacket(); + if (&packet == nullptr) + return; + + nlohmann::json json; + + PacketParser::Parse(packet, m_networkState); + + 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(); + LOG(INFO) << "Gamemode is " << g_Gamemode << ", Difficulty is " << (int) g_Difficulty + << ", Level Type is " << g_LevelType; + 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(); + LOG(INFO) << "Spawn position is " << g_SpawnPosition.GetX() << "," << g_SpawnPosition.GetY() << "," + << g_SpawnPosition.GetZ(); + 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(); + LOG(INFO) << "FOV modifier is " << g_PlayerFovModifier; + 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; + nc->AddPacketToQueue(PacketBuilder::CPlay0x03(0)); + nc->AddPacketToQueue(PacketBuilder::CPlay0x00(packet.GetField(6).GetVarInt())); + LOG(INFO) << "Game is started!"; + LOG(INFO) << "PlayerPos is " << g_PlayerX << ", " << g_PlayerY << ", " << g_PlayerZ << "\tAngle: " + << g_PlayerYaw + << "," << g_PlayerPitch; + 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: + world.ParseChunkData(packet); + break; + case 0x07: + LOG(INFO) << "Statistics: "; + for (int i = 0; i < packet.GetField(0).GetVarInt(); i++) { + LOG(INFO) << "\t" << packet.GetField(1).GetArray()[0].GetString() << ": " + << packet.GetField(1).GetArray()[1].GetVarInt(); + } + break; + default: + 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) { + nc->AddPacketToQueue( + PacketBuilder::CPlay0x0D(g_PlayerX, g_PlayerY, g_PlayerZ, g_PlayerYaw, g_PlayerPitch, true)); + timeOfPreviousSendedPpalPacket = clock.now(); + } + } + + delete &packet; +} diff --git a/src/gamestate/GameState.hpp b/src/gamestate/GameState.hpp index e9c433d..3d7dc80 100644 --- a/src/gamestate/GameState.hpp +++ b/src/gamestate/GameState.hpp @@ -1,8 +1,36 @@ #pragma once #include "../world/World.hpp" +#include "../network/NetworkClient.hpp" class GameState { + NetworkClient *nc; public: + GameState(NetworkClient *NetClient); World world; + void Update(); + + std::string g_PlayerUuid; + std::string g_PlayerName; + ConnectionState m_networkState; + bool g_IsGameStarted; + int g_PlayerEid; + int g_Gamemode; + int g_Dimension; + byte g_Difficulty; + 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; + float g_PlayerFlyingSpeed; + float g_PlayerFovModifier; + float g_PlayerPitch; + float g_PlayerYaw; + double g_PlayerX; + double g_PlayerY; + double g_PlayerZ; }; diff --git a/src/graphics/AssetManager.cpp b/src/graphics/AssetManager.cpp index f2f756d..93462c3 100644 --- a/src/graphics/AssetManager.cpp +++ b/src/graphics/AssetManager.cpp @@ -89,6 +89,9 @@ std::string AssetManager::GetAssetNameByBlockId(unsigned short id) { case 3: textureName="dirt"; break; + case 4: + textureName="cobblestone"; + break; case 16: textureName="coal_ore"; break; diff --git a/src/graphics/Display.cpp b/src/graphics/Display.cpp index 80bf524..1a44fbc 100644 --- a/src/graphics/Display.cpp +++ b/src/graphics/Display.cpp @@ -49,79 +49,105 @@ GLuint indices[] = { 0, 1, 2, 0, 2, 3 };*/ -GLfloat uv_coords[] = { +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, - 1.0f, 1.0f, - 1.0f, 1.0f, 0.0f, 1.0f, - 0.0f, 0.0f, + 1.0f, 0.0f, + 1.0f, 1.0f, - 0.0f, 0.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, - 0.0f, 0.0f, + //X+ + 0.0f, 0.0f, 1.0f, 0.0f, - 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, - 0.0f, 0.0f, 1.0f, 0.0f, + 1.0f, 1.0f, - 1.0f, 0.0f, + //X- + 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, - 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, - - 0.0f, 1.0f, 1.0f, 1.0f, - 1.0f, 0.0f, - 1.0f, 0.0f, - 0.0f, 0.0f, - 0.0f, 1.0f, - 0.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, }; -GLfloat vertices[] = { - -1.0f, -1.0f, -1.0f, - -1.0f, 1.0f, -1.0f, - 1.0f, -1.0f, -1.0f, - 1.0f, 1.0f, -1.0f, - - -1.0f, -1.0f, 1.0f, - -1.0f, 1.0f, 1.0f, - 1.0f, -1.0f, 1.0f, - 1.0f, 1.0f, 1.0f, -}; -GLuint indices[] = { - 5, 4, 6, //Z+ edge - 5, 6, 7, - - 0, 1, 2, //Z- edge - 2, 1, 3, - - 0, 4, 1, //X+ edge - 1, 4, 5, - 6, 2, 3, //X- edge - 6, 3, 7, - - 3, 1, 5, //Y+ edge - 3, 5, 7, - - 0, 2, 4, //Y- edge - 4, 2, 6, -}; Display::Display(unsigned int winWidth, unsigned int winHeight, const char *winTitle, World *worldPtr) : world( worldPtr) { @@ -133,8 +159,12 @@ Display::Display(unsigned int winWidth, unsigned int winHeight, const char *winT 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) { @@ -153,7 +183,7 @@ bool Display::IsClosed() { } void Display::SetPlayerPos(double playerX, double playerY, double playerZ) { - //camera.Position = glm::vec3(playerX, playerY, 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)); @@ -175,9 +205,8 @@ void Display::SetPlayerPos(double playerX, double playerY, double playerZ) { void Display::MainLoop() { Shader shader("./shaders/simple.vs", "./shaders/simple.fs"); - GLuint VBO, VAO, EBO, VBO2; + GLuint VBO, VAO, VBO2; glGenBuffers(1, &VBO); - glGenBuffers(1, &EBO); glGenBuffers(1, &VBO2); glGenVertexArrays(1, &VAO); @@ -192,9 +221,6 @@ void Display::MainLoop() { glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), 0); glEnableVertexAttribArray(0); - - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); } glBindVertexArray(0); @@ -219,7 +245,6 @@ void Display::MainLoop() { while (window->pollEvent(event)) { switch (event.type) { case sf::Event::Closed: - window->close(); isRunning = false; break; case sf::Event::Resized: @@ -255,13 +280,6 @@ void Display::MainLoop() { << ", " << cameraPosition.z << "; "; toWindow << "FPS: " << (1.0f / deltaTime) << " "; window->setTitle(toWindow.str()); - 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); - } if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) camera.ProcessKeyboard(Camera_Movement::FORWARD, deltaTime); if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) @@ -270,7 +288,13 @@ void Display::MainLoop() { 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); @@ -289,26 +313,8 @@ void Display::MainLoop() { glUniform1f(timeLoc, absTime); glBindVertexArray(VAO); - camera.MovementSpeed = 5.0f; - if (camera.Position.y > 50) - camera.Position = glm::vec3(0, 0, 3.0f); - - glm::mat4 model; - glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model)); - glUniform1i(blockLoc, 1); - - std::string textureName = AssetManager::GetAssetNameByBlockId(1); - Texture &texture1 = *(AssetManager::GetAsset(textureName).data.texture); - - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, texture1.texture); - glUniform1i(glGetUniformLocation(shader.Program, "blockTexture"), 0); - glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0); - //glDrawArrays(GL_TRIANGLES, 0, 36); - - - /*for (auto §ionPos:toRender) { + 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++) { @@ -336,7 +342,7 @@ void Display::MainLoop() { } } } - }*/ + } glBindVertexArray(0); //End of render code diff --git a/src/graphics/Shader.hpp b/src/graphics/Shader.hpp index 8178d2a..90db5f7 100644 --- a/src/graphics/Shader.hpp +++ b/src/graphics/Shader.hpp @@ -1,3 +1,4 @@ +#pragma once #include <string> #include <fstream> #include <sstream> diff --git a/src/gui/Gui.cpp b/src/gui/Gui.cpp new file mode 100644 index 0000000..bf600cb --- /dev/null +++ b/src/gui/Gui.cpp @@ -0,0 +1 @@ +#include "Gui.hpp" diff --git a/src/gui/Gui.hpp b/src/gui/Gui.hpp new file mode 100644 index 0000000..1ea4e11 --- /dev/null +++ b/src/gui/Gui.hpp @@ -0,0 +1,5 @@ +#pragma once + +class Gui { + +}; diff --git a/src/gui/Widget.cpp b/src/gui/Widget.cpp new file mode 100644 index 0000000..a88a2bb --- /dev/null +++ b/src/gui/Widget.cpp @@ -0,0 +1 @@ +#include "Widget.hpp" diff --git a/src/gui/Widget.hpp b/src/gui/Widget.hpp new file mode 100644 index 0000000..c4d5dc1 --- /dev/null +++ b/src/gui/Widget.hpp @@ -0,0 +1,8 @@ +#pragma once + +class Widget { + unsigned int x,y,w,h; +public: + Widget(Widget *parent); + ~Widget(); +}; diff --git a/src/main.cpp b/src/main.cpp index 2644038..cb25acf 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,6 +4,9 @@ INITIALIZE_EASYLOGGINGPP int main(){ - Game game; - game.Exec(); + el::Configurations loggerConfiguration; + loggerConfiguration.set(el::Level::Info,el::ConfigurationType::Format,"%level: %msg"); + el::Loggers::reconfigureAllLoggers(loggerConfiguration); + Core core; + core.Exec(); }
\ No newline at end of file diff --git a/src/network/Network.cpp b/src/network/Network.cpp index a3023ff..03ee6c6 100644 --- a/src/network/Network.cpp +++ b/src/network/Network.cpp @@ -3,24 +3,23 @@ #include "../packet/PacketBuilder.hpp" Network::Network(std::string address, unsigned short port) : m_address(address), m_port(port) { - std::cout << "Connecting to server " << m_address << ":" << m_port << std::endl; + LOG(INFO) << "Connecting to server " << m_address << ":" << m_port; sf::Socket::Status status = m_socket.connect(sf::IpAddress(m_address), m_port); m_socket.setBlocking(true); if (status != sf::Socket::Done) { if (status == sf::Socket::Error) { - std::cerr << "Can't connect to remote server" << std::endl; - throw 14; + LOG(ERROR) << "Can't connect to remote server"; } else { - std::cerr << "Connection failed with unknown reason" << std::endl; + LOG(ERROR) << "Connection failed with unknown reason"; throw 13; } } - std::cout << "Connected." << std::endl; + LOG(INFO) << "Connected to server"; } Network::~Network() { - std::cout << "Disconnecting..." << std::endl; m_socket.disconnect(); + LOG(INFO) << "Disconnected"; } void Network::SendHandshake(std::string username) { diff --git a/src/network/Network.hpp b/src/network/Network.hpp index ea9042c..7d8cfeb 100644 --- a/src/network/Network.hpp +++ b/src/network/Network.hpp @@ -1,6 +1,7 @@ #pragma once #include <string> +#include <easylogging++.h> #include <SFML/Network.hpp> #include "../packet/Packet.hpp" |