diff options
Diffstat (limited to '')
-rw-r--r-- | src/Game.cpp | 352 | ||||
-rw-r--r-- | src/Game.hpp | 36 | ||||
-rw-r--r-- | src/Render.cpp | 59 | ||||
-rw-r--r-- | src/RendererEntity.cpp | 1 | ||||
-rw-r--r-- | src/RendererWorld.cpp | 54 | ||||
-rw-r--r-- | src/RendererWorld.hpp | 9 | ||||
-rw-r--r-- | src/main.cpp | 2 |
7 files changed, 231 insertions, 282 deletions
diff --git a/src/Game.cpp b/src/Game.cpp index 46785b1..927cdc2 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -1,238 +1,200 @@ #include "Game.hpp" -#include "NetworkClient.hpp" -#include "GameState.hpp" +#include <memory> + #include "Render.hpp" -#include "DebugInfo.hpp" -#include "Event.hpp" +#include "GameState.hpp" +#include "NetworkClient.hpp" -//Global game variables +bool isRunning = true; +bool isMoving[5] = { 0,0,0,0,0 }; +State state; std::unique_ptr<NetworkClient> nc; std::unique_ptr<GameState> gs; -std::shared_ptr<GameState> gsReadOnly; std::unique_ptr<Render> render; -bool isRunning; -bool isPhysRunning; EventListener listener; -bool isMoving[5] = { 0,0,0,0,0 }; -std::thread threadPhys; -State state; -std::mutex gsCopyMutex; - -void PhysExec(); void InitEvents() { - /* - * Network Events - */ + /* + * Network Events + */ - listener.RegisterHandler("Exit", [](const Event&) { - isRunning = false; - }); - - listener.RegisterHandler("ConnectToServer", [](const Event& eventData) { + listener.RegisterHandler("ConnectToServer", [](const Event & eventData) { auto data = eventData.get <std::tuple<std::string, unsigned short, std::string>>(); //address,port,username - if (std::get<0>(data) == "" || std::get<1>(data) == 0) - LOG(FATAL) << "NOT VALID CONNECT-TO-SERVER EVENT"; - if (nc != nullptr) { - LOG(ERROR) << "Already connected"; - return; - } + if (std::get<0>(data) == "" || std::get<1>(data) == 0) + LOG(FATAL) << "NOT VALID CONNECT-TO-SERVER EVENT"; + if (nc != nullptr) { + LOG(ERROR) << "Already connected"; + return; + } LOG(INFO) << "Connecting to server at address " + std::get<0>(data) + ":" + std::to_string(std::get<1>(data)) + " as " + std::get<2>(data); PUSH_EVENT("Connecting",0); - gs = std::make_unique<GameState>(); - isPhysRunning = true; - threadPhys = std::thread(&PhysExec); - try { - nc = std::make_unique<NetworkClient>(std::get<0>(data), - std::get<1>(data), - std::get<2>(data)); - } catch (std::exception &e) { - LOG(WARNING) << "Connection failed"; + gs = std::make_unique<GameState>(); + try { + nc = std::make_unique<NetworkClient>(std::get<0>(data), + std::get<1>(data), + std::get<2>(data)); + } catch (std::exception &e) { + LOG(WARNING) << "Connection failed"; PUSH_EVENT("ConnectionFailed", std::string(e.what())); - isPhysRunning = false; - threadPhys.join(); - gs.reset(); - return; - } - LOG(INFO) << "Connected to server"; + gs.reset(); + return; + } + LOG(INFO) << "Connected to server"; PUSH_EVENT("ConnectionSuccessfull", 0); - }); + }); - listener.RegisterHandler("Disconnect", [](const Event& eventData) { + listener.RegisterHandler("Disconnect", [](const Event& eventData) { auto data = eventData.get<std::string>(); PUSH_EVENT("Disconnected", data); - LOG(INFO) << "Disconnected: " << data; - nc.reset(); - }); + LOG(INFO) << "Disconnected: " << data; + nc.reset(); + }); - listener.RegisterHandler("NetworkClientException", [](const Event& eventData) { + listener.RegisterHandler("NetworkClientException", [](const Event& eventData) { auto data = eventData.get < std::string>(); PUSH_EVENT("Disconnect", data); - }); + }); - /* - * GameState Events - */ + /* + * GameState Events + */ - listener.RegisterHandler("Exit", [](const Event&) { - isRunning = false; - }); + listener.RegisterHandler("Exit", [](const Event&) { + isRunning = false; + }); - listener.RegisterHandler("Disconnected", [](const Event&) { - if (!gs) - return; - isPhysRunning = false; - threadPhys.join(); - gs.reset(); - }); + listener.RegisterHandler("Disconnected", [](const Event&) { + if (!gs) + return; + gs.reset(); + }); - listener.RegisterHandler("SendChatMessage", [](const Event& eventData) { + listener.RegisterHandler("SendChatMessage", [](const Event& eventData) { auto message = eventData.get<std::string>(); - auto packet = std::static_pointer_cast<Packet>(std::make_shared<PacketChatMessageSB>(message)); - PUSH_EVENT("SendPacket",packet); - }); + auto packet = std::static_pointer_cast<Packet>(std::make_shared<PacketChatMessageSB>(message)); + PUSH_EVENT("SendPacket",packet); + }); + + /* + * Phys Events + */ + + listener.RegisterHandler("KeyPressed", [](const Event& eventData) { + if (!gs) + return; + switch (eventData.get<SDL_Scancode>()) { + case SDL_SCANCODE_W: + isMoving[GameState::FORWARD] = true; + break; + case SDL_SCANCODE_A: + isMoving[GameState::LEFT] = true; + break; + case SDL_SCANCODE_S: + isMoving[GameState::BACKWARD] = true; + break; + case SDL_SCANCODE_D: + isMoving[GameState::RIGHT] = true; + break; + case SDL_SCANCODE_SPACE: + isMoving[GameState::JUMP] = true; + break; + default: + break; + } + }); + + listener.RegisterHandler("KeyReleased", [](const Event& eventData) { + if (!gs) + return; + switch (eventData.get<SDL_Scancode>()) { + case SDL_SCANCODE_W: + isMoving[GameState::FORWARD] = false; + break; + case SDL_SCANCODE_A: + isMoving[GameState::LEFT] = false; + break; + case SDL_SCANCODE_S: + isMoving[GameState::BACKWARD] = false; + break; + case SDL_SCANCODE_D: + isMoving[GameState::RIGHT] = false; + break; + case SDL_SCANCODE_SPACE: + isMoving[GameState::JUMP] = false; + break; + default: + break; + } + }); + + listener.RegisterHandler("MouseMove", [](const Event& eventData) { + if (!gs) + return; + auto data = eventData.get<std::tuple<double,double>>(); + gs->HandleRotation(std::get<0>(data),std::get<1>(data)); + }); + + listener.RegisterHandler("ReceivedPacket", [](const Event& eventData) { + std::shared_ptr<Packet> packet = eventData.get<std::shared_ptr<Packet>>(); + gs->UpdatePacket(packet); + }); + + listener.RegisterHandler("LmbPressed",[](const Event& eventData) { + gs->StartDigging(); + }); + + listener.RegisterHandler("LmbReleased",[](const Event& eventData) { + gs->CancelDigging(); + }); + + listener.RegisterHandler("RmbPressed", [](const Event& eventData) { + gs->PlaceBlock(); + }); + + listener.RegisterHandler("SelectedBlockChanged", [](const Event& eventData) { + //TODO: + //gs->CancelDigging(); + }); } -void PhysExec() { - EventListener listener; - - listener.RegisterHandler("KeyPressed", [](const Event& eventData) { - if (!gs) - return; - switch (eventData.get<SDL_Scancode>()) { - case SDL_SCANCODE_W: - isMoving[GameState::FORWARD] = true; - break; - case SDL_SCANCODE_A: - isMoving[GameState::LEFT] = true; - break; - case SDL_SCANCODE_S: - isMoving[GameState::BACKWARD] = true; - break; - case SDL_SCANCODE_D: - isMoving[GameState::RIGHT] = true; - break; - case SDL_SCANCODE_SPACE: - isMoving[GameState::JUMP] = true; - break; - default: - break; - } - }); - - listener.RegisterHandler("KeyReleased", [](const Event& eventData) { - if (!gs) - return; - switch (eventData.get<SDL_Scancode>()) { - case SDL_SCANCODE_W: - isMoving[GameState::FORWARD] = false; - break; - case SDL_SCANCODE_A: - isMoving[GameState::LEFT] = false; - break; - case SDL_SCANCODE_S: - isMoving[GameState::BACKWARD] = false; - break; - case SDL_SCANCODE_D: - isMoving[GameState::RIGHT] = false; - break; - case SDL_SCANCODE_SPACE: - isMoving[GameState::JUMP] = false; - break; - default: - break; - } - }); - - listener.RegisterHandler("MouseMove", [](const Event& eventData) { - if (!gs) - return; - auto data = eventData.get<std::tuple<double,double>>(); - gs->HandleRotation(std::get<0>(data),std::get<1>(data)); - }); - - listener.RegisterHandler("ReceivedPacket", [](const Event& eventData) { - std::shared_ptr<Packet> packet = eventData.get<std::shared_ptr<Packet>>(); - gs->UpdatePacket(packet); - }); - - listener.RegisterHandler("LmbPressed",[](const Event& eventData) { - gs->StartDigging(); - }); - - listener.RegisterHandler("LmbReleased",[](const Event& eventData) { - gs->CancelDigging(); - }); - - listener.RegisterHandler("RmbPressed", [](const Event& eventData) { - gs->PlaceBlock(); - }); - - listener.RegisterHandler("SelectedBlockChanged", [](const Event& eventData) { - //TODO: - //gs->CancelDigging(); - }); - - LoopExecutionTimeController timer(std::chrono::milliseconds(8)); - - while (isPhysRunning) { - DebugInfo::gameThreadTime = timer.GetRealDeltaS() * 1000'00.0f; - - if (state == State::Playing) { - if (isMoving[GameState::FORWARD]) - gs->HandleMovement(GameState::FORWARD, timer.GetRealDeltaS()); - if (isMoving[GameState::BACKWARD]) - gs->HandleMovement(GameState::BACKWARD, timer.GetRealDeltaS()); - if (isMoving[GameState::LEFT]) - gs->HandleMovement(GameState::LEFT, timer.GetRealDeltaS()); - if (isMoving[GameState::RIGHT]) - gs->HandleMovement(GameState::RIGHT, timer.GetRealDeltaS()); - if (isMoving[GameState::JUMP]) - gs->HandleMovement(GameState::JUMP, timer.GetRealDeltaS()); - } - - gs->Update(timer.GetRealDeltaS()); +void RunGame() { + InitEvents(); - listener.HandleAllEvents(); + render = std::make_unique<Render>(900, 480, "AltCraft"); + + SetState(State::MainMenu); + LoopExecutionTimeController time(std::chrono::milliseconds(16)); - gsCopyMutex.lock(); - gsReadOnly = std::make_shared<GameState>(*gs.get()); - gsCopyMutex.unlock(); + while (isRunning) { + listener.HandleAllEvents(); + if (gs) + gs->Update(time.GetDeltaS()); + render->Update(); + time.Update(); + } - timer.Update(); - } + render.reset(); } -void GlobalState::Exec() { - render = std::make_unique<Render>(900, 480, "AltCraft"); - isRunning = true; - InitEvents(); - GlobalState::SetState(State::MainMenu); - while (isRunning) { - render->Update(); - listener.HandleAllEvents(); - } - PUSH_EVENT("Exit", 0); - isRunning = false; - render.reset(); +State GetState() { + return state; } -std::shared_ptr<GameState> GlobalState::GetGameState() { - std::lock_guard<std::mutex> guard(gsCopyMutex); - return gsReadOnly; +void SetState(State newState) { + if (newState != state) + PUSH_EVENT("StateUpdated", 0); + state = newState; } -Render *GlobalState::GetRender() { - return render.get(); +GameState *GetGameState() { + return gs.get(); } -State GlobalState::GetState() { - return state; +Render *GetRender() { + return render.get(); } -void GlobalState::SetState(const State &newState) { - if (newState != state) - PUSH_EVENT("StateUpdated", 0); - state = newState; +NetworkClient *GetNetworkClient() { + return nc.get(); } diff --git a/src/Game.hpp b/src/Game.hpp index bc7224f..633585f 100644 --- a/src/Game.hpp +++ b/src/Game.hpp @@ -1,25 +1,27 @@ #pragma once -#include <memory> - -class NetworkClient; class GameState; class Render; +class NetworkClient; enum class State { - InitialLoading, - MainMenu, - Loading, - Playing, - Paused, - Inventory, - Chat, + InitialLoading, + MainMenu, + Loading, + Playing, + Paused, + Inventory, + Chat, }; -struct GlobalState { - static std::shared_ptr<GameState> GetGameState(); - static Render *GetRender(); - static void Exec(); - static State GetState(); - static void SetState(const State &newState); -};
\ No newline at end of file +void RunGame(); + +State GetState(); + +void SetState(State newState); + +GameState* GetGameState(); + +Render* GetRender(); + +NetworkClient* GetNetworkClient();
\ No newline at end of file diff --git a/src/Render.cpp b/src/Render.cpp index a20ce21..2e69369 100644 --- a/src/Render.cpp +++ b/src/Render.cpp @@ -8,13 +8,13 @@ #include "AssetManager.hpp" #include "Event.hpp" #include "DebugInfo.hpp" -#include "Game.hpp" #include "World.hpp" #include "GameState.hpp" #include "RendererWorld.hpp" #include "Settings.hpp" #include "Framebuffer.hpp" #include "Plugin.hpp" +#include "Game.hpp" Render::Render(unsigned int windowWidth, unsigned int windowHeight, std::string windowTitle) @@ -228,11 +228,11 @@ void Render::HandleEvents() { case SDL_WINDOWEVENT_FOCUS_LOST: { HasFocus = false; - auto state = GlobalState::GetState(); + State state = GetState(); if (state == State::Inventory || state == State::Playing || state == State::Chat) { - GlobalState::SetState(State::Paused); + SetState(State::Paused); } break; } @@ -244,13 +244,13 @@ void Render::HandleEvents() { case SDL_KEYDOWN: { switch (event.key.keysym.scancode) { case SDL_SCANCODE_ESCAPE: { - auto state = GlobalState::GetState(); + auto state = GetState(); if (state == State::Playing) { - GlobalState::SetState(State::Paused); + SetState(State::Paused); } else if (state == State::Paused || state == State::Inventory || state == State::Chat) { - GlobalState::SetState(State::Playing); + SetState(State::Playing); } else if (state == State::MainMenu) { LOG(INFO) << "Received close event by esc"; PUSH_EVENT("Exit", 0); @@ -260,11 +260,11 @@ void Render::HandleEvents() { } case SDL_SCANCODE_E: { - auto state = GlobalState::GetState(); + auto state = GetState(); if (state == State::Playing) { - GlobalState::SetState(State::Inventory); + SetState(State::Inventory); } else if (state == State::Inventory) { - GlobalState::SetState(State::Playing); + SetState(State::Playing); } break; @@ -273,11 +273,11 @@ void Render::HandleEvents() { case SDL_SCANCODE_SLASH: case SDL_SCANCODE_T: { if (!ImGui::GetIO().WantCaptureKeyboard) { - auto state = GlobalState::GetState(); + auto state = GetState(); if (state == State::Playing) { - GlobalState::SetState(State::Chat); + SetState(State::Chat); } else if (state == State::Chat) { - GlobalState::SetState(State::Playing); + SetState(State::Playing); } } @@ -351,11 +351,8 @@ void Render::SetMouseCapture(bool IsCaptured) { } void Render::Update() { - if (world) - world->UpdateGameState(GlobalState::GetGameState()); - HandleEvents(); - if (HasFocus && GlobalState::GetState() == State::Playing) UpdateKeyboard(); + if (HasFocus && GetState() == State::Playing) UpdateKeyboard(); if (isMouseCaptured) HandleMouseCapture(); glCheckError(); @@ -388,9 +385,9 @@ void Render::RenderGui() { ImGui::Text("FPS: %.1f (%.3fms)", ImGui::GetIO().Framerate, 1000.0f / ImGui::GetIO().Framerate); float gameTime = DebugInfo::gameThreadTime / 100.0f; if (world) { - Entity *playerPtr = world->GameStatePtr()->GetPlayer(); - SelectionStatus selectionStatus = world->GameStatePtr()->GetSelectionStatus(); - const World *worldPtr = &world->GameStatePtr()->GetWorld(); + Entity *playerPtr = GetGameState()->GetPlayer(); + SelectionStatus selectionStatus = GetGameState()->GetSelectionStatus(); + const World *worldPtr = &GetGameState()->GetWorld(); ImGui::Text("TPS: %.1f (%.2fms)", 1000.0f / gameTime, gameTime); ImGui::Text("Sections loaded: %d", (int) DebugInfo::totalSections); @@ -431,7 +428,7 @@ void Render::RenderGui() { ImGui::Text( "Player health: %.1f/%.1f", - world->GameStatePtr()->GetPlayerStatus().health, 20.0f); + GetGameState()->GetPlayerStatus().health, 20.0f); ImGui::Text( "Selected block: %d %d %d : %.1f", @@ -456,7 +453,7 @@ void Render::RenderGui() { ImGui::End(); - switch (GlobalState::GetState()) { + switch (GetState()) { case State::MainMenu: { ImGui::SetNextWindowPosCenter(); ImGui::Begin("Menu", 0, windowFlags); @@ -513,7 +510,7 @@ void Render::RenderGui() { }; ImGui::SetNextWindowPosCenter(); ImGui::Begin("Inventory", 0, windowFlags); - const Window& inventory = world->GameStatePtr()->GetInventory(); + const Window& inventory = GetGameState()->GetInventory(); //Hand and drop slots if (renderSlot(inventory.handSlot, -1)) { @@ -600,7 +597,7 @@ void Render::RenderGui() { ImGui::SetNextWindowPosCenter(); ImGui::Begin("Pause Menu", 0, windowFlags); if (ImGui::Button("Continue")) { - GlobalState::SetState(State::Playing); + SetState(State::Playing); } ImGui::Separator(); @@ -629,7 +626,7 @@ void Render::RenderGui() { if (fieldSensetivity != sensetivity) sensetivity = fieldSensetivity; - world->GameStatePtr()->GetPlayer()->isFlying = fieldFlight; + GetGameState()->GetPlayer()->isFlying = fieldFlight; isWireframe = fieldWireframe; timer.SetDelayLength(std::chrono::duration<double, std::milli>(1.0 / fieldTargetFps * 1000.0)); @@ -675,7 +672,7 @@ void Render::InitEvents() { listener.RegisterHandler("PlayerConnected", [this](const Event&) { stateString = "Loading terrain..."; - world = std::make_unique<RendererWorld>(GlobalState::GetGameState()); + world = std::make_unique<RendererWorld>(); world->MaxRenderingDistance = fieldDistance; PUSH_EVENT("UpdateSectionsRender", 0); }); @@ -683,9 +680,9 @@ void Render::InitEvents() { listener.RegisterHandler("RemoveLoadingScreen", [this](const Event&) { stateString = "Playing"; renderWorld = true; - GlobalState::SetState(State::Playing); + SetState(State::Playing); glClearColor(0, 0, 0, 1.0f); - world->GameStatePtr()->GetPlayer()->isFlying = this->fieldFlight; + GetGameState()->GetPlayer()->isFlying = this->fieldFlight; PUSH_EVENT("SetMinLightLevel", fieldBrightness); }); @@ -693,7 +690,7 @@ void Render::InitEvents() { stateString = "Connection failed: " + eventData.get <std::string>(); renderWorld = false; world.reset(); - GlobalState::SetState(State::MainMenu); + SetState(State::MainMenu); glClearColor(0.8, 0.8, 0.8, 1.0f); }); @@ -701,13 +698,13 @@ void Render::InitEvents() { stateString = "Disconnected: " + eventData.get<std::string>(); renderWorld = false; world.reset(); - GlobalState::SetState(State::MainMenu); + SetState(State::MainMenu); glClearColor(0.8, 0.8, 0.8, 1.0f); }); listener.RegisterHandler("Connecting", [this](const Event&) { stateString = "Connecting to the server..."; - GlobalState::SetState(State::Loading); + SetState(State::Loading); }); listener.RegisterHandler("ChatMessageReceived", [this](const Event& eventData) { @@ -717,7 +714,7 @@ void Render::InitEvents() { }); listener.RegisterHandler("StateUpdated", [this](const Event& eventData) { - switch (GlobalState::GetState()) { + switch (GetState()) { case State::Playing: SetMouseCapture(true); PluginSystem::CallOnChangeState("Playing"); diff --git a/src/RendererEntity.cpp b/src/RendererEntity.cpp index ed1e854..fc5e1a2 100644 --- a/src/RendererEntity.cpp +++ b/src/RendererEntity.cpp @@ -7,7 +7,6 @@ #include "GameState.hpp" #include "Renderer.hpp" #include "AssetManager.hpp" -#include "Game.hpp" const GLfloat vertices[] = { -0.5f, 0.5f, 0.5f, diff --git a/src/RendererWorld.cpp b/src/RendererWorld.cpp index 7f7c850..bcb6f92 100644 --- a/src/RendererWorld.cpp +++ b/src/RendererWorld.cpp @@ -12,6 +12,7 @@ #include "GameState.hpp" #include "Section.hpp" #include "RendererSectionData.hpp" +#include "Game.hpp" void RendererWorld::WorkerFunction(size_t workerId) { EventListener tasksListener; @@ -52,13 +53,13 @@ void RendererWorld::ParseQueueUpdate() { vec.y -= 4500; } - parsing[id].data.section = gs->GetWorld().GetSection(vec); - parsing[id].data.north = gs->GetWorld().GetSection(vec + Vector(0, 0, 1)); - parsing[id].data.south = gs->GetWorld().GetSection(vec + Vector(0, 0, -1)); - parsing[id].data.west = gs->GetWorld().GetSection(vec + Vector(1, 0, 0)); - parsing[id].data.east = gs->GetWorld().GetSection(vec + Vector(-1, 0, 0)); - parsing[id].data.bottom = gs->GetWorld().GetSection(vec + Vector(0, -1, 0)); - parsing[id].data.top = gs->GetWorld().GetSection(vec + Vector(0, 1, 0)); + parsing[id].data.section = GetGameState()->GetWorld().GetSection(vec); + parsing[id].data.north = GetGameState()->GetWorld().GetSection(vec + Vector(0, 0, 1)); + parsing[id].data.south = GetGameState()->GetWorld().GetSection(vec + Vector(0, 0, -1)); + parsing[id].data.west = GetGameState()->GetWorld().GetSection(vec + Vector(1, 0, 0)); + parsing[id].data.east = GetGameState()->GetWorld().GetSection(vec + Vector(-1, 0, 0)); + parsing[id].data.bottom = GetGameState()->GetWorld().GetSection(vec + Vector(0, -1, 0)); + parsing[id].data.top = GetGameState()->GetWorld().GetSection(vec + Vector(0, 1, 0)); parsing[id].parsing = true; @@ -86,7 +87,7 @@ void RendererWorld::ParseQeueueRemoveUnnecessary() { if (std::find(elements.begin(), elements.end(), vec) != elements.end()) continue; - const Section& section = gs->GetWorld().GetSection(vec); + const Section& section = GetGameState()->GetWorld().GetSection(vec); bool skip = false; @@ -112,10 +113,10 @@ void RendererWorld::ParseQeueueRemoveUnnecessary() { } void RendererWorld::UpdateAllSections(VectorF playerPos) { - Vector playerChunk(std::floor(gs->GetPlayer()->pos.x / 16), 0, std::floor(gs->GetPlayer()->pos.z / 16)); + Vector playerChunk(std::floor(GetGameState()->GetPlayer()->pos.x / 16), 0, std::floor(GetGameState()->GetPlayer()->pos.z / 16)); std::vector<Vector> suitableChunks; - auto chunks = gs->GetWorld().GetSectionsList(); + auto chunks = GetGameState()->GetWorld().GetSectionsList(); for (auto& it : chunks) { double distance = (Vector(it.x, 0, it.z) - playerChunk).GetLength(); if (distance > MaxRenderingDistance) @@ -134,7 +135,7 @@ void RendererWorld::UpdateAllSections(VectorF playerPos) { PUSH_EVENT("DeleteSectionRender", it); } - playerChunk.y = std::floor(gs->GetPlayer()->pos.y / 16.0); + playerChunk.y = std::floor(GetGameState()->GetPlayer()->pos.y / 16.0); std::sort(suitableChunks.begin(), suitableChunks.end(), [playerChunk](Vector lhs, Vector rhs) { double leftLengthToPlayer = (playerChunk - lhs).GetLength(); double rightLengthToPlayer = (playerChunk - rhs).GetLength(); @@ -146,8 +147,7 @@ void RendererWorld::UpdateAllSections(VectorF playerPos) { } } -RendererWorld::RendererWorld(std::shared_ptr<GameState> ptr) { - gs = ptr; +RendererWorld::RendererWorld() { MaxRenderingDistance = 2; numOfWorkers = _max(1, (signed int) std::thread::hardware_concurrency() - 2); @@ -186,7 +186,7 @@ RendererWorld::RendererWorld(std::shared_ptr<GameState> ptr) { listener->RegisterHandler("EntityChanged", [this](const Event& eventData) { auto data = eventData.get<unsigned int>(); - for (unsigned int entityId : gs->GetWorld().GetEntitiesList()) { + for (unsigned int entityId : GetGameState()->GetWorld().GetEntitiesList()) { if (entityId == data) { entities.push_back(RendererEntity(entityId)); } @@ -198,7 +198,7 @@ RendererWorld::RendererWorld(std::shared_ptr<GameState> ptr) { if (vec == Vector()) return; - Vector playerChunk(std::floor(gs->GetPlayer()->pos.x / 16), 0, std::floor(gs->GetPlayer()->pos.z / 16)); + Vector playerChunk(std::floor(GetGameState()->GetPlayer()->pos.x / 16), 0, std::floor(GetGameState()->GetPlayer()->pos.z / 16)); double distanceToChunk = (Vector(vec.x, 0, vec.z) - playerChunk).GetLength(); if (MaxRenderingDistance != 1000 && distanceToChunk > MaxRenderingDistance) { @@ -215,7 +215,7 @@ RendererWorld::RendererWorld(std::shared_ptr<GameState> ptr) { if (vec == Vector()) return; - Vector playerChunk(std::floor(gs->GetPlayer()->pos.x / 16), 0, std::floor(gs->GetPlayer()->pos.z / 16)); + Vector playerChunk(std::floor(GetGameState()->GetPlayer()->pos.x / 16), 0, std::floor(GetGameState()->GetPlayer()->pos.z / 16)); double distanceToChunk = (Vector(vec.x, 0, vec.z) - playerChunk).GetLength(); if (MaxRenderingDistance != 1000 && distanceToChunk > MaxRenderingDistance) { @@ -230,7 +230,7 @@ RendererWorld::RendererWorld(std::shared_ptr<GameState> ptr) { }); listener->RegisterHandler("UpdateSectionsRender", [this](const Event&) { - UpdateAllSections(gs->GetPlayer()->pos); + UpdateAllSections(GetGameState()->GetPlayer()->pos); }); listener->RegisterHandler("PlayerPosChanged", [this](const Event& eventData) { @@ -276,7 +276,7 @@ void RendererWorld::Render(RenderState & renderState) { glm::radians(70.0f), (float) renderState.WindowWidth / (float) renderState.WindowHeight, 0.1f, 10000000.0f ); - glm::mat4 view = gs->GetViewMatrix(); + glm::mat4 view = GetGameState()->GetViewMatrix(); glm::mat4 projView = projection * view; //Render Entities @@ -289,11 +289,11 @@ void RendererWorld::Render(RenderState & renderState) { renderState.SetActiveVao(RendererEntity::GetVao()); for (auto& it : entities) { - it.Render(renderState, &gs->GetWorld()); + it.Render(renderState, &GetGameState()->GetWorld()); } //Render selected block - Vector selectedBlock = gs->GetSelectionStatus().selectedBlock; + Vector selectedBlock = GetGameState()->GetSelectionStatus().selectedBlock; if (selectedBlock != Vector()) { glLineWidth(2.0f); { @@ -311,7 +311,7 @@ void RendererWorld::Render(RenderState & renderState) { //Render raycast hit const bool renderHit = false; if (renderHit) { - VectorF hit = gs->GetSelectionStatus().raycastHit; + VectorF hit = GetGameState()->GetSelectionStatus().raycastHit; glLineWidth(2.0f); { glm::mat4 model; @@ -331,16 +331,16 @@ void RendererWorld::Render(RenderState & renderState) { glCheckError(); //Render sky - renderState.TimeOfDay = gs->GetTimeStatus().timeOfDay; + renderState.TimeOfDay = GetGameState()->GetTimeStatus().timeOfDay; Shader *skyShader = AssetManager::GetAsset<AssetShader>("/altcraft/shaders/sky")->shader.get(); skyShader->Activate(); skyShader->SetUniform("projection", projection); skyShader->SetUniform("view", view); glm::mat4 model = glm::mat4(1.0); - model = glm::translate(model, gs->GetPlayer()->pos.glm()); + model = glm::translate(model, GetGameState()->GetPlayer()->pos.glm()); const float scale = 1000000.0f; model = glm::scale(model, glm::vec3(scale, scale, scale)); - float shift = gs->GetTimeStatus().interpolatedTimeOfDay / 24000.0f; + float shift = GetGameState()->GetTimeStatus().interpolatedTimeOfDay / 24000.0f; if (shift < 0) shift *= -1.0f; model = glm::rotate(model, glm::radians(90.0f), glm::vec3(0, 1.0f, 0.0f)); @@ -357,7 +357,7 @@ void RendererWorld::Render(RenderState & renderState) { const float moonriseLength = moonriseMax - moonriseMin; float mixLevel = 0; - float dayTime = gs->GetTimeStatus().interpolatedTimeOfDay; + float dayTime = GetGameState()->GetTimeStatus().interpolatedTimeOfDay; if (dayTime < 0) dayTime *= -1; while (dayTime > 24000) @@ -451,7 +451,3 @@ void RendererWorld::Update(double timeToUpdate) { DebugInfo::readyRenderer = parseQueue.size(); DebugInfo::renderSections = sections.size(); } - -GameState* RendererWorld::GameStatePtr() { - return gs.get(); -} diff --git a/src/RendererWorld.hpp b/src/RendererWorld.hpp index 913e510..d031179 100644 --- a/src/RendererWorld.hpp +++ b/src/RendererWorld.hpp @@ -27,7 +27,6 @@ class RendererWorld { }; //General - std::shared_ptr<GameState> gs; std::unique_ptr<EventListener> listener; size_t numOfWorkers; size_t currentWorker = 0; @@ -51,7 +50,7 @@ class RendererWorld { Texture *skyTexture; RendererSky rendererSky; public: - RendererWorld(std::shared_ptr<GameState> ptr); + RendererWorld(); ~RendererWorld(); void Render(RenderState& renderState); @@ -61,11 +60,5 @@ public: void Update(double timeToUpdate); - GameState *GameStatePtr(); - int culledSections = 0; - - inline void UpdateGameState(std::shared_ptr<GameState> newPtr) { - gs = newPtr; - } };
\ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 100b7bc..2ed2b85 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -48,7 +48,7 @@ int main(int argc, char** argv) { return -1; } - GlobalState::Exec(); + RunGame(); return 0; }
\ No newline at end of file |