From d93c2073896e63aca5a859fe7182ba24dbe84cd3 Mon Sep 17 00:00:00 2001 From: LaG1924 <12997935+LaG1924@users.noreply.github.com> Date: Sun, 28 May 2017 19:16:05 +0500 Subject: 2017-05-28 --- src/core/AssetManager.cpp | 32 +++++++++++++++++++++++--------- src/core/Core.cpp | 47 ++++++++++++++++++++++++++++++----------------- src/core/Core.hpp | 3 ++- 3 files changed, 55 insertions(+), 27 deletions(-) (limited to 'src/core') diff --git a/src/core/AssetManager.cpp b/src/core/AssetManager.cpp index 9913c18..003f2f0 100644 --- a/src/core/AssetManager.cpp +++ b/src/core/AssetManager.cpp @@ -1,5 +1,4 @@ -#include -#include + #include "AssetManager.hpp" namespace fs = std::experimental::filesystem; @@ -24,7 +23,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, state, 0); + assetIds[blockName] = Block(id, 0, state); } LOG(INFO) << "Loaded " << assetIds.size() << " ids"; } @@ -43,14 +42,13 @@ void AssetManager::LoadTextureResources() { auto frame = it["frame"]; TextureCoord coord; coord.x = frame["x"].get(); - coord.y = frame["y"].get(); + coord.y = frame["y"].get();; coord.w = frame["w"].get(); coord.h = frame["h"].get(); std::string assetName = it["filename"].get(); - assetName.insert(0,"minecraft/textures/"); - assetName.erase(assetName.length()-4); - LOG(ERROR)< lookupTable = { + {Block(0), "minecraft/textures/blocks/air"}, + {Block(1, 0), "minecraft/textures/blocks/stone"}, + {Block(1, 1), "minecraft/textures/blocks/stone_granite"}, + + {Block(2, 0, 0), "minecraft/textures/blocks/dirt"}, + {Block(2, 0, 1), "minecraft/textures/blocks/grass_top"}, + {Block(2, 0, 2), "minecraft/textures/blocks/grass_side"}, + {Block(2, 0, 3), "minecraft/textures/blocks/grass_side"}, + {Block(2, 0, 4), "minecraft/textures/blocks/grass_side"}, + {Block(2, 0, 5), "minecraft/textures/blocks/grass_side"}, + {Block(3), "minecraft/textures/blocks/dirt"}, + {Block(4), "minecraft/textures/blocks/cobblestone"}, + }; + return lookupTable[Block(BlockId, BlockSide)]; } const GLuint AssetManager::GetTextureAtlas() { diff --git a/src/core/Core.cpp b/src/core/Core.cpp index 7814c32..1481e36 100644 --- a/src/core/Core.cpp +++ b/src/core/Core.cpp @@ -103,20 +103,21 @@ Core::Core() { LOG(INFO) << "Core initializing..."; InitSfml(1280, 720, "AltCraft"); InitGlew(); - PrepareToWorldRendering(); client = new NetworkClient("127.0.0.1", 25565, "HelloOne"); gameState = new GameState(client); - std::thread loop = std::thread(&Core::UpdateGameState,this); - std::swap(loop,gameStateLoopThread); + std::thread loop = std::thread(&Core::UpdateGameState, this); + std::swap(loop, gameStateLoopThread); assetManager = new AssetManager; + PrepareToWorldRendering(); LOG(INFO) << "Core is initialized"; } Core::~Core() { LOG(INFO) << "Core stopping..."; + gameStateLoopThread.join(); delete shader; - delete client; delete gameState; + delete client; LOG(INFO) << "Core is stopped"; } @@ -181,7 +182,7 @@ void Core::InitSfml(unsigned int WinWidth, unsigned int WinHeight, std::string W contextSetting.attributeFlags = contextSetting.Core; contextSetting.depthBits = 24; window = new sf::Window(sf::VideoMode(WinWidth, WinHeight), WinTitle, sf::Style::Default, contextSetting); - window->setVerticalSyncEnabled(true); + //window->setVerticalSyncEnabled(true); window->setPosition(sf::Vector2i(sf::VideoMode::getDesktopMode().width / 2 - window->getSize().x / 2, sf::VideoMode::getDesktopMode().height / 2 - window->getSize().y / 2)); @@ -228,6 +229,12 @@ void Core::HandleEvents() { case sf::Keyboard::T: SetMouseCapture(!isMouseCaptured); break; + case sf::Keyboard::Z: + camera.MovementSpeed /= 2; + break; + case sf::Keyboard::X: + camera.MovementSpeed *= 2; + break; default: break; } @@ -269,7 +276,7 @@ void Core::RenderWorld(World &Target) { GLint viewLoc = glGetUniformLocation(shader->Program, "view"); GLint blockLoc = glGetUniformLocation(shader->Program, "block"); GLint timeLoc = glGetUniformLocation(shader->Program, "time"); - glm::mat4 projection = glm::perspective(camera.Zoom, (float) width() / (float) height(), 0.1f, 1000.0f); + glm::mat4 projection = glm::perspective(camera.Zoom, (float) width() / (float) height(), 0.0001f, 1000.0f); glm::mat4 view = camera.GetViewMatrix(); glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection)); glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view)); @@ -282,20 +289,19 @@ void Core::RenderWorld(World &Target) { for (int y = 0; y < 16; y++) { for (int z = 0; z < 16; z++) { for (int x = 0; x < 16; x++) { + Block block = section.GetBlock(Vector(x, y, z)); + if (block.id==0) + continue; + glm::mat4 model; - model = glm::translate(model, - glm::vec3(sectionPos.GetX() * 16, sectionPos.GetY() * 16, - sectionPos.GetZ() * 16)); + model = glm::translate(model, glm::vec3(sectionPos.GetX() * 16, sectionPos.GetY() * 16, + sectionPos.GetZ() * 16)); model = glm::translate(model, glm::vec3(x, y, z)); - Block block = section.GetBlock(Vector(x, y, z)); + glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model)); glUniform1i(blockLoc, block.id); - glActiveTexture(GL_TEXTURE0); - //glBindTexture(GL_TEXTURE_2D, texture1.texture); - glUniform1i(glGetUniformLocation(shader->Program, "blockTexture"), 0); - glDrawArrays(GL_TRIANGLES, 0, 36); } } @@ -330,14 +336,19 @@ void Core::PrepareToWorldRendering() { } glBindVertexArray(0); - shader = new Shader("./shaders/simple.vs", "./shaders/simple.fs"); + shader = new Shader("./shaders/block.vs", "./shaders/block.fs"); shader->Use(); + + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, assetManager->GetTextureAtlas()); + glUniform1i(glGetUniformLocation(shader->Program, "textureAtlas"), 0); + } void Core::UpdateChunksToRender() { camera.Position = glm::vec3(gameState->g_PlayerX, gameState->g_PlayerY, gameState->g_PlayerZ); toRender.clear(); - const float ChunkDistance = 1; + const float ChunkDistance = 1.3; Vector playerChunk = Vector(floor(gameState->g_PlayerX / 16.0f), floor(gameState->g_PlayerY / 16.0f), floor(gameState->g_PlayerZ / 16.0f)); for (auto &it:gameState->world.m_sections) { @@ -351,7 +362,9 @@ void Core::UpdateChunksToRender() { } void Core::UpdateGameState() { - while (gameState && client){ + LOG(INFO) << "GameState thread is started"; + while (isRunning) { gameState->Update(); } + LOG(INFO) << "GameState thread is stopped"; } diff --git a/src/core/Core.hpp b/src/core/Core.hpp index 1c2bbc5..8bf74da 100644 --- a/src/core/Core.hpp +++ b/src/core/Core.hpp @@ -17,7 +17,8 @@ class Core { NetworkClient *client; sf::Window *window; AssetManager *assetManager; - bool isMouseCaptured = false, isRunning = true; + bool isMouseCaptured = false; + bool isRunning = true; enum { MainMenu, Loading, -- cgit v1.2.3