diff options
author | LaG1924 <12997935+LaG1924@users.noreply.github.com> | 2017-05-28 16:16:05 +0200 |
---|---|---|
committer | LaG1924 <12997935+LaG1924@users.noreply.github.com> | 2017-05-28 16:16:05 +0200 |
commit | d93c2073896e63aca5a859fe7182ba24dbe84cd3 (patch) | |
tree | 6144aaf7e01854fe245011ca4f4735974366febb /src/core | |
parent | 2017-05-26 (diff) | |
download | AltCraft-d93c2073896e63aca5a859fe7182ba24dbe84cd3.tar AltCraft-d93c2073896e63aca5a859fe7182ba24dbe84cd3.tar.gz AltCraft-d93c2073896e63aca5a859fe7182ba24dbe84cd3.tar.bz2 AltCraft-d93c2073896e63aca5a859fe7182ba24dbe84cd3.tar.lz AltCraft-d93c2073896e63aca5a859fe7182ba24dbe84cd3.tar.xz AltCraft-d93c2073896e63aca5a859fe7182ba24dbe84cd3.tar.zst AltCraft-d93c2073896e63aca5a859fe7182ba24dbe84cd3.zip |
Diffstat (limited to '')
-rw-r--r-- | src/core/AssetManager.cpp | 32 | ||||
-rw-r--r-- | src/core/Core.cpp | 47 | ||||
-rw-r--r-- | src/core/Core.hpp | 3 |
3 files changed, 55 insertions, 27 deletions
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 <easylogging++.h> -#include <nlohmann/json.hpp> + #include "AssetManager.hpp" namespace fs = std::experimental::filesystem; @@ -24,7 +23,7 @@ void AssetManager::LoadIds() { int id = it["type"].get<int>(); int state = it["meta"].get<int>(); std::string blockName = it["text_type"].get<std::string>(); - assetIds[blockName] = Block(id, state, 0); + 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<int>(); - coord.y = frame["y"].get<int>(); + coord.y = frame["y"].get<int>();; coord.w = frame["w"].get<int>(); coord.h = frame["h"].get<int>(); std::string assetName = it["filename"].get<std::string>(); - assetName.insert(0,"minecraft/textures/"); - assetName.erase(assetName.length()-4); - LOG(ERROR)<<assetName; - assetTextures[assetName]=coord; + assetName.insert(0, "minecraft/textures/"); + assetName.erase(assetName.length() - 4); + assetTextures[assetName] = coord; } textureAtlas = new Texture(filename); @@ -58,11 +56,27 @@ void AssetManager::LoadTextureResources() { } TextureCoord AssetManager::GetTextureByAssetName(std::string AssetName) { - return TextureCoord{0, 0, 0, 0}; + return assetTextures[AssetName]; } 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<Block, std::string> 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, |