From a67fb47b466b3e671e78f48be301120a6fe1c327 Mon Sep 17 00:00:00 2001 From: "Grayson Riffe (Desktop)" Date: Tue, 2 Nov 2021 02:23:49 -0500 Subject: [PATCH] Cleaned up gamestate fading --- Game/src/MainState.cpp | 2 +- NothinFancy/src/Application.cpp | 10 +++---- NothinFancy/src/Renderer/Renderer.cpp | 40 +++++++++++++-------------- NothinFancy/src/include/Application.h | 2 +- NothinFancy/src/include/Renderer.h | 5 ++-- 5 files changed, 30 insertions(+), 29 deletions(-) diff --git a/Game/src/MainState.cpp b/Game/src/MainState.cpp index 8c318d3..b063661 100644 --- a/Game/src/MainState.cpp +++ b/Game/src/MainState.cpp @@ -97,7 +97,7 @@ void MainState::update(float deltaTime) { setGravity(grav); } if (app->isKeyPressed(NFI_T)) { - grav = 1.0f; + grav = 2.0f; setGravity(1.0f); } gravText.setText("Gravity Scale: " + std::to_string(grav)); diff --git a/NothinFancy/src/Application.cpp b/NothinFancy/src/Application.cpp index 81af94c..271613d 100644 --- a/NothinFancy/src/Application.cpp +++ b/NothinFancy/src/Application.cpp @@ -112,7 +112,7 @@ namespace nf { if (m_states.find(stateName) == m_states.end()) Error("State \"" + (std::string)stateName + (std::string)"\" doesn't exist!"); m_stateChange = true; - m_nextState = stateName; + m_nextState = m_states[stateName]; } Gamestate* Application::getCurrentState() { @@ -306,7 +306,7 @@ namespace nf { Gamestate* sIntro = new IntroGamestate; m_currentState = sIntro; m_currentState->run(this, false); - m_renderer->setFade(true, false, true); + m_renderer->setFade(true, false, false); std::chrono::steady_clock::time_point startOfCurrentFrame = std::chrono::steady_clock::now(); std::chrono::steady_clock::time_point startOfLastFrame = std::chrono::steady_clock::now(); while (m_running) { @@ -347,16 +347,16 @@ namespace nf { void Application::doStateChange() { if (!m_stateChangeStarted) { - m_renderer->setFade(false, true, false); + m_renderer->setFade(false, true); m_stateChangeStarted = true; } if (m_renderer->isFadeOutComplete()) { m_audio->stopAllSounds(); m_currentState->stop(); - m_currentState = m_states[m_nextState]; + m_currentState = m_nextState; m_currentState->run(this); - m_renderer->setFade(true, false, false); + m_renderer->setFade(true, false); m_stateChange = false; m_stateChangeStarted = false; } diff --git a/NothinFancy/src/Renderer/Renderer.cpp b/NothinFancy/src/Renderer/Renderer.cpp index d80acf6..d1b0697 100644 --- a/NothinFancy/src/Renderer/Renderer.cpp +++ b/NothinFancy/src/Renderer/Renderer.cpp @@ -28,7 +28,9 @@ namespace nf { m_cubemap(nullptr), m_fadeIn(false), m_fadeOut(false), - m_fadeNoText(false), + m_fadeInOpacity(1.0f), + m_fadeOutOpacity(0.0f), + m_fadeText(true), m_fadeOutComplete(false) { m_hdc = GetDC(m_app->getWindow()); @@ -120,10 +122,10 @@ namespace nf { m_loadingText.create("NFLoadingText", Vec2(0.025f, 0.044f), Vec3(0.7f)); } - void Renderer::setFade(bool in, bool out, bool noText) { + void Renderer::setFade(bool in, bool out, bool text) { m_fadeIn = in; m_fadeOut = out; - m_fadeNoText = noText; + m_fadeText = text; } bool Renderer::isFadeOutComplete() { @@ -239,42 +241,40 @@ namespace nf { //Fade over everything when states change if (m_fadeIn) { - static float opacity = 1.0f; - m_fadeShader->setUniform("opacity", opacity); + m_fadeShader->setUniform("opacity", m_fadeInOpacity); m_quadVAO->bind(); m_quadIB->bind(); glDrawElements(GL_TRIANGLES, m_quadIB->getCount(), GL_UNSIGNED_INT, nullptr); - if (!m_fadeNoText) { + if (m_fadeText) { m_textShader->setUniform("proj", proj); - m_loadingText.setOpacity(opacity); + m_loadingText.setOpacity(m_fadeInOpacity); m_loadingText.render(m_textShader, m_app->getConfig().width, m_app->getConfig().height); } - if (dT > 1.0f / 60.0f) - dT = 1.0f / 60.0f; - opacity -= 2.5f * dT; - if (opacity <= 0.0) { + if (m_fadeInOpacity <= 0.0) { m_fadeIn = false; - opacity = 1.0; + m_fadeInOpacity = 1.0; m_fadeOutComplete = false; } + else + m_fadeInOpacity -= 2.5f * dT; } else if (m_fadeOut) { - static float opacity = 0.0f; - m_fadeShader->setUniform("opacity", opacity); + m_fadeShader->setUniform("opacity", m_fadeOutOpacity); m_quadVAO->bind(); m_quadIB->bind(); glDrawElements(GL_TRIANGLES, m_quadIB->getCount(), GL_UNSIGNED_INT, nullptr); - if (!m_fadeNoText) { + if (m_fadeText) { m_textShader->setUniform("proj", proj); - m_loadingText.setOpacity(opacity); + m_loadingText.setOpacity(m_fadeOutOpacity); m_loadingText.render(m_textShader, m_app->getConfig().width, m_app->getConfig().height); } - opacity += 3.0f * dT; - if (opacity >= 1.0) { - m_fadeIn = false; - opacity = 0.0; + if (m_fadeOutOpacity >= 1.0) { + m_fadeOut = false; + m_fadeOutOpacity = 0.0; m_fadeOutComplete = true; } + else + m_fadeOutOpacity += 3.0f * dT; } glEnable(GL_DEPTH_TEST); diff --git a/NothinFancy/src/include/Application.h b/NothinFancy/src/include/Application.h index 0800e90..7f9a3bc 100644 --- a/NothinFancy/src/include/Application.h +++ b/NothinFancy/src/include/Application.h @@ -87,7 +87,7 @@ namespace nf { bool m_defaultStateAdded; Gamestate* m_currentState; bool m_stateChange; - std::string m_nextState; + Gamestate* m_nextState; bool m_stateChangeStarted; //Array of booleans that represent keyboard and mouse input minus the scrollwheel diff --git a/NothinFancy/src/include/Renderer.h b/NothinFancy/src/include/Renderer.h index e2ff833..08b383d 100644 --- a/NothinFancy/src/include/Renderer.h +++ b/NothinFancy/src/include/Renderer.h @@ -21,7 +21,7 @@ namespace nf { public: Renderer(Application* app); - void setFade(bool in, bool out, bool noText); + void setFade(bool in, bool out, bool text = true); bool isFadeOutComplete(); void render(Entity& in); @@ -68,7 +68,8 @@ namespace nf { Shader* m_pointShadowShader; bool m_fadeIn, m_fadeOut; - bool m_fadeNoText; + float m_fadeInOpacity, m_fadeOutOpacity; + bool m_fadeText; bool m_fadeOutComplete; Text m_loadingText;