Cleaned up gamestate fading

This commit is contained in:
Grayson Riffe (Desktop) 2021-11-02 02:23:49 -05:00
parent 3e403681a3
commit a67fb47b46
5 changed files with 30 additions and 29 deletions

View File

@ -97,7 +97,7 @@ void MainState::update(float deltaTime) {
setGravity(grav); setGravity(grav);
} }
if (app->isKeyPressed(NFI_T)) { if (app->isKeyPressed(NFI_T)) {
grav = 1.0f; grav = 2.0f;
setGravity(1.0f); setGravity(1.0f);
} }
gravText.setText("Gravity Scale: " + std::to_string(grav)); gravText.setText("Gravity Scale: " + std::to_string(grav));

View File

@ -112,7 +112,7 @@ namespace nf {
if (m_states.find(stateName) == m_states.end()) if (m_states.find(stateName) == m_states.end())
Error("State \"" + (std::string)stateName + (std::string)"\" doesn't exist!"); Error("State \"" + (std::string)stateName + (std::string)"\" doesn't exist!");
m_stateChange = true; m_stateChange = true;
m_nextState = stateName; m_nextState = m_states[stateName];
} }
Gamestate* Application::getCurrentState() { Gamestate* Application::getCurrentState() {
@ -306,7 +306,7 @@ namespace nf {
Gamestate* sIntro = new IntroGamestate; Gamestate* sIntro = new IntroGamestate;
m_currentState = sIntro; m_currentState = sIntro;
m_currentState->run(this, false); 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 startOfCurrentFrame = std::chrono::steady_clock::now();
std::chrono::steady_clock::time_point startOfLastFrame = std::chrono::steady_clock::now(); std::chrono::steady_clock::time_point startOfLastFrame = std::chrono::steady_clock::now();
while (m_running) { while (m_running) {
@ -347,16 +347,16 @@ namespace nf {
void Application::doStateChange() { void Application::doStateChange() {
if (!m_stateChangeStarted) { if (!m_stateChangeStarted) {
m_renderer->setFade(false, true, false); m_renderer->setFade(false, true);
m_stateChangeStarted = true; m_stateChangeStarted = true;
} }
if (m_renderer->isFadeOutComplete()) { if (m_renderer->isFadeOutComplete()) {
m_audio->stopAllSounds(); m_audio->stopAllSounds();
m_currentState->stop(); m_currentState->stop();
m_currentState = m_states[m_nextState]; m_currentState = m_nextState;
m_currentState->run(this); m_currentState->run(this);
m_renderer->setFade(true, false, false); m_renderer->setFade(true, false);
m_stateChange = false; m_stateChange = false;
m_stateChangeStarted = false; m_stateChangeStarted = false;
} }

View File

@ -28,7 +28,9 @@ namespace nf {
m_cubemap(nullptr), m_cubemap(nullptr),
m_fadeIn(false), m_fadeIn(false),
m_fadeOut(false), m_fadeOut(false),
m_fadeNoText(false), m_fadeInOpacity(1.0f),
m_fadeOutOpacity(0.0f),
m_fadeText(true),
m_fadeOutComplete(false) m_fadeOutComplete(false)
{ {
m_hdc = GetDC(m_app->getWindow()); m_hdc = GetDC(m_app->getWindow());
@ -120,10 +122,10 @@ namespace nf {
m_loadingText.create("NFLoadingText", Vec2(0.025f, 0.044f), Vec3(0.7f)); 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_fadeIn = in;
m_fadeOut = out; m_fadeOut = out;
m_fadeNoText = noText; m_fadeText = text;
} }
bool Renderer::isFadeOutComplete() { bool Renderer::isFadeOutComplete() {
@ -239,42 +241,40 @@ namespace nf {
//Fade over everything when states change //Fade over everything when states change
if (m_fadeIn) { if (m_fadeIn) {
static float opacity = 1.0f; m_fadeShader->setUniform("opacity", m_fadeInOpacity);
m_fadeShader->setUniform("opacity", opacity);
m_quadVAO->bind(); m_quadVAO->bind();
m_quadIB->bind(); m_quadIB->bind();
glDrawElements(GL_TRIANGLES, m_quadIB->getCount(), GL_UNSIGNED_INT, nullptr); glDrawElements(GL_TRIANGLES, m_quadIB->getCount(), GL_UNSIGNED_INT, nullptr);
if (!m_fadeNoText) { if (m_fadeText) {
m_textShader->setUniform("proj", proj); 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); m_loadingText.render(m_textShader, m_app->getConfig().width, m_app->getConfig().height);
} }
if (dT > 1.0f / 60.0f) if (m_fadeInOpacity <= 0.0) {
dT = 1.0f / 60.0f;
opacity -= 2.5f * dT;
if (opacity <= 0.0) {
m_fadeIn = false; m_fadeIn = false;
opacity = 1.0; m_fadeInOpacity = 1.0;
m_fadeOutComplete = false; m_fadeOutComplete = false;
} }
else
m_fadeInOpacity -= 2.5f * dT;
} }
else if (m_fadeOut) { else if (m_fadeOut) {
static float opacity = 0.0f; m_fadeShader->setUniform("opacity", m_fadeOutOpacity);
m_fadeShader->setUniform("opacity", opacity);
m_quadVAO->bind(); m_quadVAO->bind();
m_quadIB->bind(); m_quadIB->bind();
glDrawElements(GL_TRIANGLES, m_quadIB->getCount(), GL_UNSIGNED_INT, nullptr); glDrawElements(GL_TRIANGLES, m_quadIB->getCount(), GL_UNSIGNED_INT, nullptr);
if (!m_fadeNoText) { if (m_fadeText) {
m_textShader->setUniform("proj", proj); 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); m_loadingText.render(m_textShader, m_app->getConfig().width, m_app->getConfig().height);
} }
opacity += 3.0f * dT; if (m_fadeOutOpacity >= 1.0) {
if (opacity >= 1.0) { m_fadeOut = false;
m_fadeIn = false; m_fadeOutOpacity = 0.0;
opacity = 0.0;
m_fadeOutComplete = true; m_fadeOutComplete = true;
} }
else
m_fadeOutOpacity += 3.0f * dT;
} }
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);

View File

@ -87,7 +87,7 @@ namespace nf {
bool m_defaultStateAdded; bool m_defaultStateAdded;
Gamestate* m_currentState; Gamestate* m_currentState;
bool m_stateChange; bool m_stateChange;
std::string m_nextState; Gamestate* m_nextState;
bool m_stateChangeStarted; bool m_stateChangeStarted;
//Array of booleans that represent keyboard and mouse input minus the scrollwheel //Array of booleans that represent keyboard and mouse input minus the scrollwheel

View File

@ -21,7 +21,7 @@ namespace nf {
public: public:
Renderer(Application* app); Renderer(Application* app);
void setFade(bool in, bool out, bool noText); void setFade(bool in, bool out, bool text = true);
bool isFadeOutComplete(); bool isFadeOutComplete();
void render(Entity& in); void render(Entity& in);
@ -68,7 +68,8 @@ namespace nf {
Shader* m_pointShadowShader; Shader* m_pointShadowShader;
bool m_fadeIn, m_fadeOut; bool m_fadeIn, m_fadeOut;
bool m_fadeNoText; float m_fadeInOpacity, m_fadeOutOpacity;
bool m_fadeText;
bool m_fadeOutComplete; bool m_fadeOutComplete;
Text m_loadingText; Text m_loadingText;