diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Event.hpp | 19 | ||||
-rw-r--r-- | src/GlobalState.cpp | 33 | ||||
-rw-r--r-- | src/GlobalState.hpp | 14 | ||||
-rw-r--r-- | src/Render.cpp | 110 | ||||
-rw-r--r-- | src/Render.hpp | 11 | ||||
-rw-r--r-- | src/RendererWorld.cpp | 100 |
6 files changed, 158 insertions, 129 deletions
diff --git a/src/Event.hpp b/src/Event.hpp index d2ddb15..021af0d 100644 --- a/src/Event.hpp +++ b/src/Event.hpp @@ -45,6 +45,7 @@ enum class EventType { ChunkDeleted, ChatMessageReceived, SendChatMessage, + StateUpdated, }; struct EchoData { @@ -176,14 +177,18 @@ struct SendChatMessageData { std::string message; }; +struct StateUpdatedData { + +}; + using EventData = std::variant<EchoData, ChunkChangedData, ConnectToServerData, ConnectionSuccessfullData, - DisconnectData, SendPacketData, ReceivePacketData, RequestNetworkClientData, RegisterNetworkClientData, - PlayerConnectedData, RemoveLoadingScreenData, ConnectionFailedData, ExitData, DisconnectedData, - ConnectingData, NetworkClientExceptionData, MouseMovedData, KeyPressedData, KeyReleasedData, - InitalizeSectionRenderData, CreateSectionRenderData, CreatedSectionRenderData, PlayerPosChangedData, - UpdateSectionsRenderData, DeleteSectionRenderData, EntityChangedData,NewRenderDataAvailableData, - BlockChangeData, RendererWorkerTaskData, ChunkDeletedData, ChatMessageReceivedData, - SendChatMessageData>; + DisconnectData, SendPacketData, ReceivePacketData, RequestNetworkClientData, RegisterNetworkClientData, + PlayerConnectedData, RemoveLoadingScreenData, ConnectionFailedData, ExitData, DisconnectedData, + ConnectingData, NetworkClientExceptionData, MouseMovedData, KeyPressedData, KeyReleasedData, + InitalizeSectionRenderData, CreateSectionRenderData, CreatedSectionRenderData, PlayerPosChangedData, + UpdateSectionsRenderData, DeleteSectionRenderData, EntityChangedData, NewRenderDataAvailableData, + BlockChangeData, RendererWorkerTaskData, ChunkDeletedData, ChatMessageReceivedData, + SendChatMessageData, StateUpdatedData>; struct Event { EventType type; diff --git a/src/GlobalState.cpp b/src/GlobalState.cpp index 78fba0e..23c7e51 100644 --- a/src/GlobalState.cpp +++ b/src/GlobalState.cpp @@ -16,6 +16,7 @@ bool isPhysRunning; EventListener listener; bool isMoving[5] = { 0,0,0,0,0 }; std::thread threadPhys; +State state; void PhysExec(); @@ -149,16 +150,18 @@ void PhysExec() { while (isPhysRunning) { DebugInfo::gameThreadTime = timer.GetRealDeltaS() * 1000'00.0f; - 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()); + 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()); @@ -215,4 +218,14 @@ GameState *GlobalState::GetGameState() { Render *GlobalState::GetRender() { return render.get(); +} + +State GlobalState::GetState() { + return state; +} + +void GlobalState::SetState(const State &newState) { + if (newState != state) + EventAgregator::PushEvent(EventType::StateUpdated, StateUpdatedData{}); + state = newState; }
\ No newline at end of file diff --git a/src/GlobalState.hpp b/src/GlobalState.hpp index 29ab7a0..c0f1769 100644 --- a/src/GlobalState.hpp +++ b/src/GlobalState.hpp @@ -7,8 +7,20 @@ class NetworkClient; class GameState; class Render; -struct GlobalState { +enum class State { + InitialLoading, + MainMenu, + Loading, + Playing, + Paused, + Inventory, + Chat, +}; + +struct GlobalState { static GameState *GetGameState(); static Render *GetRender(); static void Exec(); + static State GetState(); + static void SetState(const State &newState); };
\ No newline at end of file diff --git a/src/Render.cpp b/src/Render.cpp index 316efec..befc982 100644 --- a/src/Render.cpp +++ b/src/Render.cpp @@ -152,10 +152,9 @@ void Render::HandleEvents() { HasFocus = true; break; case SDL_WINDOWEVENT_FOCUS_LOST: - HasFocus = false; - SetMouseCapture(false); - if (state == GameState::Inventory || state == GameState::Playing || state == GameState::Chat) - state = GameState::Paused; + HasFocus = false; + if (GlobalState::GetState() == State::Inventory || GlobalState::GetState() == State::Playing || GlobalState::GetState() == State::Chat) + GlobalState::SetState(State::Paused); break; } break; @@ -163,48 +162,44 @@ void Render::HandleEvents() { case SDL_KEYDOWN: switch (event.key.keysym.scancode) { case SDL_SCANCODE_ESCAPE: - switch (state) { - case GameState::Playing: - state = GameState::Paused; - SetMouseCapture(false); + switch (GlobalState::GetState()) { + case State::Playing: + GlobalState::SetState(State::Paused); break; - case GameState::Inventory: - state = GameState::Playing; - SetMouseCapture(true); + case State::Inventory: + GlobalState::SetState(State::Paused); break; - case GameState::Paused: - state = GameState::Playing; - SetMouseCapture(true); + case State::Paused: + GlobalState::SetState(State::Playing); break; - case GameState::MainMenu: + case State::MainMenu: LOG(INFO) << "Received close event by esc"; isRunning = false; break; - } + } break; case SDL_SCANCODE_E: - switch (state) { - case GameState::Playing: - state = GameState::Inventory; - SetMouseCapture(false); + switch (GlobalState::GetState()) { + case State::Playing: + GlobalState::SetState(State::Inventory); break; - case GameState::Inventory: - state = GameState::Playing; - SetMouseCapture(true); + case State::Inventory: + GlobalState::SetState(State::Playing); break; } break; case SDL_SCANCODE_T: - switch (state) { - case GameState::Playing: - state = GameState::Chat; - SetMouseCapture(false); - break; - case GameState::Chat: - state = GameState::Playing; - SetMouseCapture(true); - break; - } + if (!ImGui::GetIO().WantCaptureKeyboard) + switch (GlobalState::GetState()) { + case State::Playing: + GlobalState::SetState(State::Chat); + SetMouseCapture(false); + break; + case State::Chat: + GlobalState::SetState(State::Playing); + SetMouseCapture(true); + break; + } break; } break; @@ -259,8 +254,7 @@ void Render::ExecuteRenderLoop() { listener.RegisterHandler(EventType::RemoveLoadingScreen, [this](EventData eventData) { stateString = "Playing"; renderWorld = true; - state = GameState::Playing; - SetMouseCapture(true); + GlobalState::SetState(State::Playing); glClearColor(0, 0, 0, 1.0f); }); @@ -268,7 +262,7 @@ void Render::ExecuteRenderLoop() { stateString = "Connection failed: " + std::get<ConnectionFailedData>(eventData).reason; renderWorld = false; world.reset(); - state = GameState::MainMenu; + GlobalState::SetState(State::MainMenu); glClearColor(0.8, 0.8, 0.8, 1.0f); }); @@ -276,14 +270,13 @@ void Render::ExecuteRenderLoop() { stateString = "Disconnected: " + std::get<DisconnectedData>(eventData).reason; renderWorld = false; world.reset(); - state = GameState::MainMenu; - SetMouseCapture(false); + GlobalState::SetState(State::MainMenu); glClearColor(0.8, 0.8, 0.8, 1.0f); }); listener.RegisterHandler(EventType::Connecting, [this](EventData eventData) { stateString = "Connecting to the server..."; - state = GameState::Loading; + GlobalState::SetState(State::Loading); }); listener.RegisterHandler(EventType::ChatMessageReceived, [this](EventData eventData) { @@ -292,11 +285,27 @@ void Render::ExecuteRenderLoop() { chatMessages.push_back(msg); }); - state = GameState::MainMenu; + listener.RegisterHandler(EventType::StateUpdated, [this](EventData eventData) { + switch (GlobalState::GetState()) { + case State::Playing: + SetMouseCapture(true); + break; + case State::InitialLoading: + case State::MainMenu: + case State::Loading: + case State::Paused: + case State::Inventory: + case State::Chat: + SetMouseCapture(false); + break; + } + }); + + GlobalState::SetState(State::MainMenu); while (isRunning) { HandleEvents(); - if (HasFocus && state == GameState::Playing) UpdateKeyboard(); + if (HasFocus && GlobalState::GetState() == State::Playing) UpdateKeyboard(); if (isMouseCaptured) HandleMouseCapture(); glCheckError(); @@ -318,7 +327,7 @@ void Render::RenderGui() { } const ImGuiWindowFlags windowFlags = ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoSavedSettings; - ImGui::ShowTestWindow(); + //ImGui::ShowTestWindow(); ImGui::SetNextWindowPos(ImVec2(10, 10)); ImGui::Begin("DebugInfo", 0, ImVec2(0, 0), 0.4f, windowFlags); @@ -339,8 +348,8 @@ void Render::RenderGui() { ImGui::End(); - switch (state) { - case GameState::MainMenu: { + switch (GlobalState::GetState()) { + case State::MainMenu: { ImGui::SetNextWindowPosCenter(); ImGui::Begin("Menu", 0, windowFlags); static char buff[512] = "127.0.0.1"; @@ -358,9 +367,9 @@ void Render::RenderGui() { ImGui::End(); break; } - case GameState::Loading: + case State::Loading: break; - case GameState::Chat: { + case State::Chat: { ImGui::SetNextWindowPosCenter(); ImGui::Begin("Chat", 0, windowFlags); for (const auto& msg : chatMessages) { @@ -376,7 +385,7 @@ void Render::RenderGui() { ImGui::End(); break; } - case GameState::Inventory: { + case State::Inventory: { auto renderSlot = [](const SlotData &slot, int i) -> bool { return ImGui::Button(((slot.BlockId == -1 ? " ##" : AssetManager::Instance().GetAssetNameByBlockId(BlockId{ (unsigned short)slot.BlockId,0 }) + " x" + std::to_string(slot.ItemCount) + "##") @@ -466,12 +475,11 @@ void Render::RenderGui() { break; } - case GameState::Paused: { + case State::Paused: { ImGui::SetNextWindowPosCenter(); ImGui::Begin("Pause Menu", 0, windowFlags); if (ImGui::Button("Continue")) { - state = GameState::Playing; - SetMouseCapture(true); + GlobalState::SetState(State::Playing); } ImGui::Separator(); static float distance = world->MaxRenderingDistance; @@ -507,7 +515,7 @@ void Render::RenderGui() { ImGui::End(); break; } - case GameState::InitialLoading: + case State::InitialLoading: break; } diff --git a/src/Render.hpp b/src/Render.hpp index f35d90c..8c4b4e8 100644 --- a/src/Render.hpp +++ b/src/Render.hpp @@ -23,16 +23,7 @@ class Render { float sensetivity = 0.1f; bool isWireframe = false; std::vector<std::string> chatMessages; - - enum GameState { - InitialLoading, - MainMenu, - Loading, - Playing, - Paused, - Inventory, - Chat, - } state = InitialLoading; + std::string stateString; void SetMouseCapture(bool IsCaptured); diff --git a/src/RendererWorld.cpp b/src/RendererWorld.cpp index 0438613..ea27b47 100644 --- a/src/RendererWorld.cpp +++ b/src/RendererWorld.cpp @@ -219,56 +219,6 @@ void RendererWorld::Render(RenderState & renderState) { glm::mat4 view = gs->GetViewMatrix(); glm::mat4 projView = projection * view; - //Render sky - renderState.TimeOfDay = gs->TimeOfDay; - renderState.SetActiveShader(skyShader->Program); - projectionLoc = glGetUniformLocation(skyShader->Program, "projection"); - viewLoc = glGetUniformLocation(skyShader->Program, "view"); - glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection)); - glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view)); - glm::mat4 model = glm::mat4(); - model = glm::translate(model, gs->player->pos.glm()); - const float scale = 1000000.0f; - model = glm::scale(model, glm::vec3(scale, scale, scale)); - float shift = gs->TimeOfDay / 24000.0f; - if (shift < 0) - shift *= -1.0f; - model = glm::rotate(model, glm::radians(90.0f), glm::vec3(0, 1.0f, 0.0f)); - model = glm::rotate(model, glm::radians(360.0f * shift), glm::vec3(-1.0f, 0.0f, 0.0f)); - modelLoc = glGetUniformLocation(skyShader->Program, "model"); - glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model)); - - glCheckError(); - - const int sunriseMin = 22000; - const int sunriseMax = 23500; - const int moonriseMin = 12000; - const int moonriseMax = 13500; - const float sunriseLength = sunriseMax - sunriseMin; - const float moonriseLength = moonriseMax - moonriseMin; - - float mixLevel = 0; - int dayTime = gs->TimeOfDay; - if (dayTime < 0) - dayTime *= -1; - while (dayTime > 24000) - dayTime -= 24000; - if (dayTime > 0 && dayTime < moonriseMin || dayTime > sunriseMax) //day - mixLevel = 1.0; - if (dayTime > moonriseMax && dayTime < sunriseMin) //night - mixLevel = 0.0; - if (dayTime >= sunriseMin && dayTime <= sunriseMax) //sunrise - mixLevel = (dayTime - sunriseMin) / sunriseLength; - if (dayTime >= moonriseMin && dayTime <= moonriseMax) { //moonrise - float timePassed = (dayTime - moonriseMin); - mixLevel = 1.0 - (timePassed / moonriseLength); - } - - glUniform1f(glGetUniformLocation(skyShader->Program, "DayTime"), mixLevel); - - rendererSky.Render(renderState); - glCheckError(); - //Render Entities glLineWidth(3.0); renderState.SetActiveShader(entityShader->Program); @@ -339,6 +289,56 @@ void RendererWorld::Render(RenderState & renderState) { this->culledSections = culledSections; sectionsMutex.unlock(); glCheckError(); + + //Render sky + renderState.TimeOfDay = gs->TimeOfDay; + renderState.SetActiveShader(skyShader->Program); + projectionLoc = glGetUniformLocation(skyShader->Program, "projection"); + viewLoc = glGetUniformLocation(skyShader->Program, "view"); + glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection)); + glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view)); + glm::mat4 model = glm::mat4(); + model = glm::translate(model, gs->player->pos.glm()); + const float scale = 1000000.0f; + model = glm::scale(model, glm::vec3(scale, scale, scale)); + float shift = gs->TimeOfDay / 24000.0f; + if (shift < 0) + shift *= -1.0f; + model = glm::rotate(model, glm::radians(90.0f), glm::vec3(0, 1.0f, 0.0f)); + model = glm::rotate(model, glm::radians(360.0f * shift), glm::vec3(-1.0f, 0.0f, 0.0f)); + modelLoc = glGetUniformLocation(skyShader->Program, "model"); + glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model)); + + glCheckError(); + + const int sunriseMin = 22000; + const int sunriseMax = 23500; + const int moonriseMin = 12000; + const int moonriseMax = 13500; + const float sunriseLength = sunriseMax - sunriseMin; + const float moonriseLength = moonriseMax - moonriseMin; + + float mixLevel = 0; + int dayTime = gs->TimeOfDay; + if (dayTime < 0) + dayTime *= -1; + while (dayTime > 24000) + dayTime -= 24000; + if (dayTime > 0 && dayTime < moonriseMin || dayTime > sunriseMax) //day + mixLevel = 1.0; + if (dayTime > moonriseMax && dayTime < sunriseMin) //night + mixLevel = 0.0; + if (dayTime >= sunriseMin && dayTime <= sunriseMax) //sunrise + mixLevel = (dayTime - sunriseMin) / sunriseLength; + if (dayTime >= moonriseMin && dayTime <= moonriseMax) { //moonrise + float timePassed = (dayTime - moonriseMin); + mixLevel = 1.0 - (timePassed / moonriseLength); + } + + glUniform1f(glGetUniformLocation(skyShader->Program, "DayTime"), mixLevel); + + rendererSky.Render(renderState); + glCheckError(); } void RendererWorld::PrepareRender() { |