diff options
author | Elisey Puzko <puzko.e02@gmail.com> | 2018-02-18 12:18:48 +0100 |
---|---|---|
committer | Elisey Puzko <puzko.e02@gmail.com> | 2018-02-18 12:18:48 +0100 |
commit | a33df10e04b26bcc81e1910d2b3bacb0b0e0dcb4 (patch) | |
tree | 3228933773c255b69aa670fcca765dcec18cbec6 /src | |
parent | Small changes to conform Google C++ styleguide (diff) | |
download | AltCraft-a33df10e04b26bcc81e1910d2b3bacb0b0e0dcb4.tar AltCraft-a33df10e04b26bcc81e1910d2b3bacb0b0e0dcb4.tar.gz AltCraft-a33df10e04b26bcc81e1910d2b3bacb0b0e0dcb4.tar.bz2 AltCraft-a33df10e04b26bcc81e1910d2b3bacb0b0e0dcb4.tar.lz AltCraft-a33df10e04b26bcc81e1910d2b3bacb0b0e0dcb4.tar.xz AltCraft-a33df10e04b26bcc81e1910d2b3bacb0b0e0dcb4.tar.zst AltCraft-a33df10e04b26bcc81e1910d2b3bacb0b0e0dcb4.zip |
Diffstat (limited to 'src')
-rw-r--r-- | src/Render.cpp | 233 | ||||
-rw-r--r-- | src/RendererWorld.cpp | 14 | ||||
-rw-r--r-- | src/World.cpp | 62 |
3 files changed, 194 insertions, 115 deletions
diff --git a/src/Render.cpp b/src/Render.cpp index 534782e..d126326 100644 --- a/src/Render.cpp +++ b/src/Render.cpp @@ -13,7 +13,9 @@ #include "GameState.hpp" #include "RendererWorld.hpp" -Render::Render(unsigned int windowWidth, unsigned int windowHeight, std::string windowTitle) : timer(std::chrono::milliseconds(16)) { +Render::Render(unsigned int windowWidth, unsigned int windowHeight, + std::string windowTitle) + : timer(std::chrono::milliseconds(16)) { InitEvents(); InitSdl(windowWidth, windowHeight, windowTitle); @@ -44,7 +46,10 @@ void Render::InitSdl(unsigned int WinWidth, unsigned int WinHeight, std::string SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); - window = SDL_CreateWindow(WinTitle.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WinWidth, WinHeight, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE); + window = SDL_CreateWindow( + WinTitle.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, + WinWidth, WinHeight, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE); + if (!window) throw std::runtime_error("Window creation failed: " + std::string(SDL_GetError())); @@ -141,94 +146,121 @@ void Render::HandleEvents() { ImGui_ImplSdlGL3_ProcessEvent(&event); switch (event.type) { - case SDL_QUIT: - LOG(INFO) << "Received close event by window closing"; - PUSH_EVENT("Exit",0); - break; - case SDL_WINDOWEVENT: { - switch (event.window.event) { - case SDL_WINDOWEVENT_RESIZED: { - int width, height; - SDL_GL_GetDrawableSize(window, &width, &height); - glViewport(0, 0, width, height); - renderState.WindowWidth = width; - renderState.WindowHeight = height; - break; - } - case SDL_WINDOWEVENT_FOCUS_GAINED: - HasFocus = true; - break; - case SDL_WINDOWEVENT_FOCUS_LOST: - HasFocus = false; - if (GlobalState::GetState() == State::Inventory || GlobalState::GetState() == State::Playing || GlobalState::GetState() == State::Chat) - GlobalState::SetState(State::Paused); + case SDL_QUIT: { + LOG(INFO) << "Received close event by window closing"; + PUSH_EVENT("Exit",0); break; } - break; - } - case SDL_KEYDOWN: - switch (event.key.keysym.scancode) { - case SDL_SCANCODE_ESCAPE: - switch (GlobalState::GetState()) { - case State::Playing: - GlobalState::SetState(State::Paused); - break; - case State::Inventory: - GlobalState::SetState(State::Paused); - break; - case State::Paused: - GlobalState::SetState(State::Playing); - break; - case State::MainMenu: - LOG(INFO) << "Received close event by esc"; - PUSH_EVENT("Exit",0); - break; - } - break; - case SDL_SCANCODE_E: - switch (GlobalState::GetState()) { - case State::Playing: - GlobalState::SetState(State::Inventory); - break; - case State::Inventory: - GlobalState::SetState(State::Playing); - break; + + case SDL_WINDOWEVENT: { + switch (event.window.event) { + case SDL_WINDOWEVENT_RESIZED: { + int width, height; + SDL_GL_GetDrawableSize(window, &width, &height); + glViewport(0, 0, width, height); + renderState.WindowWidth = width; + renderState.WindowHeight = height; + break; + } + + case SDL_WINDOWEVENT_FOCUS_GAINED: + HasFocus = true; + break; + + case SDL_WINDOWEVENT_FOCUS_LOST: { + HasFocus = false; + auto state = GlobalState::GetState(); + if (state == State::Inventory || + state == State::Playing || + state == State::Chat) { + GlobalState::SetState(State::Paused); + } + break; + } + } break; - case SDL_SCANCODE_T: - if (!ImGui::GetIO().WantCaptureKeyboard) - switch (GlobalState::GetState()) { - case State::Playing: - GlobalState::SetState(State::Chat); - SetMouseCapture(false); + } + + case SDL_KEYDOWN: { + switch (event.key.keysym.scancode) { + case SDL_SCANCODE_ESCAPE: { + auto state = GlobalState::GetState(); + if (state == State::Playing || + state == State::Inventory) { + GlobalState::SetState(State::Paused); + } else if (state == State::Paused || + state == State::Chat) { + GlobalState::SetState(State::Playing); + } else if (state == State::MainMenu) { + LOG(INFO) << "Received close event by esc"; + PUSH_EVENT("Exit", 0); + } + break; - case State::Chat: - GlobalState::SetState(State::Playing); - SetMouseCapture(true); + } + + case SDL_SCANCODE_E: { + auto state = GlobalState::GetState(); + if (state == State::Playing) { + GlobalState::SetState(State::Inventory); + } else if (state == State::Inventory) { + GlobalState::SetState(State::Playing); + } + break; } + + case SDL_SCANCODE_T: { + if (!ImGui::GetIO().WantCaptureKeyboard) { + auto state = GlobalState::GetState(); + if (state == State::Playing) { + GlobalState::SetState(State::Chat); + SetMouseCapture(false); + } else if (state == State::Chat) { + GlobalState::SetState(State::Playing); + SetMouseCapture(true); + } + } + + break; + } + + default: + break; + } + break; } - break; - case SDL_MOUSEMOTION: - if (isMouseCaptured) { - double deltaX = event.motion.xrel; - double deltaY = event.motion.yrel; - deltaX *= sensetivity; - deltaY *= sensetivity * -1; - PUSH_EVENT("MouseMove", std::make_tuple(deltaX, deltaY)); - } + + case SDL_MOUSEMOTION: { + if (isMouseCaptured) { + double deltaX = event.motion.xrel; + double deltaY = event.motion.yrel; + deltaX *= sensetivity; + deltaY *= sensetivity * -1; + PUSH_EVENT("MouseMove", std::make_tuple(deltaX, deltaY)); + } + break; - case SDL_MOUSEBUTTONDOWN: + } + + case SDL_MOUSEBUTTONDOWN: { if (event.button.button == SDL_BUTTON_LEFT && !ImGui::GetIO().WantCaptureMouse) - PUSH_EVENT("LmbPressed",0); + PUSH_EVENT("LmbPressed", 0); + break; - case SDL_MOUSEBUTTONUP: + } + + case SDL_MOUSEBUTTONUP: { if (event.button.button == SDL_BUTTON_LEFT && !ImGui::GetIO().WantCaptureMouse) - PUSH_EVENT("LmbReleased",0); + PUSH_EVENT("LmbReleased", 0); + + break; + } + + default: break; - default: - break; } } } @@ -272,7 +304,12 @@ void Render::RenderGui() { auto& io = ImGui::GetIO(); io.MousePos = ImVec2(-FLT_MAX, -FLT_MAX); } - const ImGuiWindowFlags windowFlags = ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoSavedSettings; + + const ImGuiWindowFlags windowFlags = ImGuiWindowFlags_NoTitleBar | + ImGuiWindowFlags_NoResize | + ImGuiWindowFlags_NoMove | + ImGuiWindowFlags_AlwaysAutoResize| + ImGuiWindowFlags_NoSavedSettings; //ImGui::ShowTestWindow(); @@ -285,13 +322,38 @@ void Render::RenderGui() { float gameTime = DebugInfo::gameThreadTime / 100.0f; if (world) { ImGui::Text("TPS: %.1f (%.2fms)", 1000.0f / gameTime, gameTime); - ImGui::Text("Sections loaded: %d", (int)DebugInfo::totalSections); - ImGui::Text("SectionsRenderer: %d (%d)", (int)DebugInfo::renderSections, (int)DebugInfo::readyRenderer); - ImGui::Text("Culled sections: %d", (int)DebugInfo::renderSections - world->culledSections); - ImGui::Text("Player pos: %.1f %.1f %.1f OnGround=%d", world->GameStatePtr()->player->pos.x, world->GameStatePtr()->player->pos.y, world->GameStatePtr()->player->pos.z, world->GameStatePtr()->player->onGround); - ImGui::Text("Player vel: %.1f %.1f %.1f", world->GameStatePtr()->player->vel.x, world->GameStatePtr()->player->vel.y, world->GameStatePtr()->player->vel.z); - ImGui::Text("Player health: %.1f/%.1f", world->GameStatePtr()->g_PlayerHealth, 20.0f); - ImGui::Text("Selected block: %d %d %d : %.1f",world->GameStatePtr()->selectedBlock.x,world->GameStatePtr()->selectedBlock.y,world->GameStatePtr()->selectedBlock.z,world->GameStatePtr()->distanceToSelectedBlock); + ImGui::Text("Sections loaded: %d", (int) DebugInfo::totalSections); + ImGui::Text( + "SectionsRenderer: %d (%d)", + (int) DebugInfo::renderSections,(int) DebugInfo::readyRenderer); + + ImGui::Text( + "Culled sections: %d", + (int) DebugInfo::renderSections - world->culledSections); + + ImGui::Text( + "Player pos: %.1f %.1f %.1f OnGround=%d", + world->GameStatePtr()->player->pos.x, + world->GameStatePtr()->player->pos.y, + world->GameStatePtr()->player->pos.z, + world->GameStatePtr()->player->onGround); + + ImGui::Text( + "Player vel: %.1f %.1f %.1f", + world->GameStatePtr()->player->vel.x, + world->GameStatePtr()->player->vel.y, + world->GameStatePtr()->player->vel.z); + + ImGui::Text( + "Player health: %.1f/%.1f", + world->GameStatePtr()->g_PlayerHealth, 20.0f); + + ImGui::Text( + "Selected block: %d %d %d : %.1f", + world->GameStatePtr()->selectedBlock.x, + world->GameStatePtr()->selectedBlock.y, + world->GameStatePtr()->selectedBlock.z, + world->GameStatePtr()->distanceToSelectedBlock); } ImGui::End(); @@ -304,7 +366,8 @@ void Render::RenderGui() { static int port = 25565; static char buffName[512] = "HelloOne"; if (ImGui::Button("Connect")) { - PUSH_EVENT("ConnectToServer", std::make_tuple(std::string(buff), (unsigned short)port, std::string(buffName))); + PUSH_EVENT("ConnectToServer", std::make_tuple(std::string(buff), + (unsigned short) port, std::string(buffName))); } ImGui::InputText("Username", buffName, 512); ImGui::InputText("Address", buff, 512); diff --git a/src/RendererWorld.cpp b/src/RendererWorld.cpp index 8390de7..5065f9d 100644 --- a/src/RendererWorld.cpp +++ b/src/RendererWorld.cpp @@ -60,8 +60,7 @@ void RendererWorld::WorkerFunction(size_t workerId) { } } -void RendererWorld::UpdateAllSections(VectorF playerPos) -{ +void RendererWorld::UpdateAllSections(VectorF playerPos) { Vector playerChunk(std::floor(gs->player->pos.x / 16), 0, std::floor(gs->player->pos.z / 16)); std::vector<Vector> suitableChunks; @@ -227,7 +226,10 @@ RendererWorld::~RendererWorld() { void RendererWorld::Render(RenderState & renderState) { //Common GLint projectionLoc, viewLoc, modelLoc, pvLoc, windowSizeLoc, colorLoc; - glm::mat4 projection = glm::perspective(45.0f, (float)renderState.WindowWidth / (float)renderState.WindowHeight, 0.1f, 10000000.0f); + glm::mat4 projection = glm::perspective( + 45.0f, (float) renderState.WindowWidth / (float) renderState.WindowHeight, + 0.1f, 10000000.0f + ); glm::mat4 view = gs->GetViewMatrix(); glm::mat4 projView = projection * view; @@ -328,7 +330,11 @@ void RendererWorld::Render(RenderState & renderState) { } } - double lengthToSection = (gs->player->pos - VectorF(section.first.x*16,section.first.y*16,section.first.z*16)).GetLength(); + double lengthToSection = (gs->player->pos - + VectorF(section.first.x*16, + section.first.y*16, + section.first.z*16) + ).GetLength(); if (!isVisible && lengthToSection > 30.0f) { sectionsMutex.lock(); diff --git a/src/World.cpp b/src/World.cpp index 4678964..f593bce 100644 --- a/src/World.cpp +++ b/src/World.cpp @@ -21,13 +21,13 @@ void World::ParseChunkData(std::shared_ptr<PacketChunkData> packet) { if (!sections.insert(std::make_pair(chunkPosition, std::make_unique<Section>(section))).second) { LOG(ERROR) << "New chunk not created " << chunkPosition << " potential memory leak"; } + UpdateSectionsList(); + } else { + std::swap(*sections.at(chunkPosition).get(), section); } - else { - using std::swap; - swap(*sections.at(chunkPosition).get(), section); - } - PUSH_EVENT("ChunkChanged", chunkPosition); + + PUSH_EVENT("ChunkChanged", chunkPosition); } } } @@ -53,7 +53,9 @@ Section World::ParseSection(StreamInput *data, Vector position) { std::vector<long long> blockArray(blockData, blockData + dataArray.size() / sizeof(long long)); - return Section(position, bitsPerBlock, std::move(palette), std::move(blockArray), std::move(blockLight), std::move(skyLight)); + return Section( + position, bitsPerBlock, std::move(palette),std::move(blockArray), + std::move(blockLight), std::move(skyLight)); } World::~World() { @@ -64,7 +66,8 @@ World::World() { bool World::isPlayerCollides(double X, double Y, double Z) { Vector PlayerChunk(floor(X / 16.0), floor(Y / 16.0), floor(Z / 16.0)); - if (sections.find(PlayerChunk) == sections.end() || sections.find(PlayerChunk - Vector(0, 1, 0)) == sections.end()) + if (sections.find(PlayerChunk) == sections.end() || + sections.find(PlayerChunk - Vector(0, 1, 0)) == sections.end()) return false; std::vector<Vector> closestSectionsCoordinates = { @@ -154,8 +157,7 @@ RaycastResult World::Raycast(glm::vec3 position, glm::vec3 direction) { return result; } -void World::UpdatePhysics(float delta) -{ +void World::UpdatePhysics(float delta) { struct CollisionResult { bool isCollide; //Vector block; @@ -164,7 +166,6 @@ void World::UpdatePhysics(float delta) }; auto testCollision = [this](double width, double height, VectorF pos)->CollisionResult { - int blockXBegin = pos.x - width - 1.0; int blockXEnd = pos.x + width + 0.5; int blockYBegin = pos.y - 0.5; @@ -211,6 +212,7 @@ void World::UpdatePhysics(float delta) it.pos = newPos; } } + { //Horizontal velocity VectorF newPos = it.pos + VectorF(it.vel.x, 0, it.vel.z) * delta; auto coll = testCollision(it.width, it.height, newPos); @@ -231,8 +233,7 @@ void World::UpdatePhysics(float delta) DebugInfo::totalSections = sections.size(); } -Entity & World::GetEntity(unsigned int EntityId) -{ +Entity& World::GetEntity(unsigned int EntityId){ entitiesMutex.lock(); for (auto& it : entities) { if (it.entityId == EntityId) { @@ -241,12 +242,12 @@ Entity & World::GetEntity(unsigned int EntityId) } } entitiesMutex.unlock(); + static Entity fallback; return fallback; } -std::vector<unsigned int> World::GetEntitiesList() -{ +std::vector<unsigned int> World::GetEntitiesList() { entitiesMutex.lock(); std::vector<unsigned int> ret; for (auto& it : entities) { @@ -256,8 +257,7 @@ std::vector<unsigned int> World::GetEntitiesList() return ret; } -void World::AddEntity(Entity entity) -{ +void World::AddEntity(Entity entity) { entitiesMutex.lock(); for (auto& it : entities) { if (it.entityId == entity.entityId) { @@ -270,8 +270,7 @@ void World::AddEntity(Entity entity) entitiesMutex.unlock(); } -void World::DeleteEntity(unsigned int EntityId) -{ +void World::DeleteEntity(unsigned int EntityId) { entitiesMutex.lock(); auto it = entities.begin(); for (; it != entities.end(); ++it) { @@ -285,10 +284,16 @@ void World::DeleteEntity(unsigned int EntityId) } void World::ParseChunkData(std::shared_ptr<PacketBlockChange> packet) { - SetBlockId(packet->Position, BlockId{(unsigned short) (packet->BlockId >> 4),(unsigned char) (packet->BlockId & 0xF) }); - - Vector sectionPos(std::floor(packet->Position.x / 16.0), std::floor(packet->Position.y / 16.0), std::floor(packet->Position.z / 16.0)); - PUSH_EVENT("ChunkChanged", sectionPos); + SetBlockId(packet->Position, + BlockId { + (unsigned short) (packet->BlockId >> 4), + (unsigned char) (packet->BlockId & 0xF) + }); + + Vector sectionPos(std::floor(packet->Position.x / 16.0), + std::floor(packet->Position.y / 16.0), + std::floor(packet->Position.z / 16.0)); + PUSH_EVENT("ChunkChanged", sectionPos); } void World::ParseChunkData(std::shared_ptr<PacketMultiBlockChange> packet) { @@ -306,7 +311,7 @@ void World::ParseChunkData(std::shared_ptr<PacketMultiBlockChange> packet) { } for (auto& sectionPos : changedSections) - PUSH_EVENT("ChunkChanged", sectionPos); + PUSH_EVENT("ChunkChanged", sectionPos); } void World::ParseChunkData(std::shared_ptr<PacketUnloadChunk> packet) { @@ -316,7 +321,7 @@ void World::ParseChunkData(std::shared_ptr<PacketUnloadChunk> packet) { toRemove.push_back(it); } for (auto& it : toRemove) { - PUSH_EVENT("ChunkDeleted", it->first); + PUSH_EVENT("ChunkDeleted", it->first); sections.erase(it); } UpdateSectionsList(); @@ -332,14 +337,19 @@ void World::UpdateSectionsList() { } BlockId World::GetBlockId(Vector pos) { - Vector sectionPos(std::floor(pos.x / 16.0), std::floor(pos.y / 16.0), std::floor(pos.z / 16.0)); + Vector sectionPos(std::floor(pos.x / 16.0), + std::floor(pos.y / 16.0), + std::floor(pos.z / 16.0)); Section* section = GetSectionPtr(sectionPos); return !section ? BlockId{0, 0} : section->GetBlockId(pos - (sectionPos * 16)); } void World::SetBlockId(Vector pos, BlockId block) { - Vector sectionPos(std::floor(pos.x / 16.0), std::floor(pos.y / 16.0), std::floor(pos.z / 16.0)); + Vector sectionPos(std::floor(pos.x / 16.0), + std::floor(pos.y / 16.0), + std::floor(pos.z / 16.0)); + Section* section = GetSectionPtr(sectionPos); section->SetBlockId(pos - (sectionPos * 16), block); PUSH_EVENT("ChunkChanged",sectionPos); |