Skip to content

Commit 49f545f

Browse files
committed
Add fade effect to both States
The game now has a fade in/out effect when starting/leaving a State.
1 parent 3262dc5 commit 49f545f

File tree

4 files changed

+60
-3
lines changed

4 files changed

+60
-3
lines changed

PlayState.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ void PlayState::update(float deltaTime)
3232
if (witch.getDead())
3333
{
3434
countdownToReset -= deltaTime;
35+
if (countdownToReset <= 1.0f)
36+
{
37+
fadeIn = false;
38+
}
39+
3540
if (countdownToReset <= 0)
3641
m_GameStateManager.setGameState(std::make_unique<PlayState>(m_CanvasWidth, m_CanvasHeight, m_GameStateManager, m_MaxScore));
3742

@@ -95,6 +100,25 @@ void PlayState::render(float deltaTime)
95100
DrawText(TextFormat("Score: %i", score), 5, 5, 10, RAYWHITE);
96101
DrawText(TextFormat("High Score: %i", m_MaxScore), 5, 20, 1, RAYWHITE);
97102

103+
if (fadeColor.a > 0 && fadeIn)
104+
{
105+
if (fadeColor.a - fadeSpeed * deltaTime < 0)
106+
fadeColor.a = 0;
107+
else
108+
fadeColor.a -= fadeSpeed * deltaTime;
109+
110+
DrawRectangle(0, 0, m_CanvasWidth, m_CanvasHeight, fadeColor);
111+
}
112+
else if (fadeColor.a <= 255 && !fadeIn)
113+
{
114+
if (fadeColor.a + fadeSpeed * deltaTime > 255)
115+
fadeColor.a = 255;
116+
else
117+
fadeColor.a += fadeSpeed * deltaTime;
118+
119+
DrawRectangle(0, 0, m_CanvasWidth, m_CanvasHeight, fadeColor);
120+
}
121+
98122
//Uncomment to make current FPS visible for testing purposes
99123
//DrawText(TextFormat("%i FPS", GetFPS()), 5, m_CanvasHeight - 10, 10, GREEN);
100124
}

PlayState.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ class PlayState : public GameState
4444

4545
int score{ 0 };
4646
int& m_MaxScore;
47-
float countdownToReset{ 3.0f };
4847
Sound crashSound{ LoadSound("assets/sounds/crash.mp3") };
48+
float countdownToReset{ 3.0f };
49+
50+
Color fadeColor{ 0, 0, 0, 255 };
51+
float fadeSpeed{ 300.0f };
52+
bool fadeIn{ true };
4953
};

TitleScreenState.cpp

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,16 @@ void TitleScreenState::update(float deltaTime)
1414

1515
if (isMouseOverPlayButton && IsMouseButtonReleased(MOUSE_BUTTON_LEFT))
1616
{
17-
m_GameStateManager.setGameState(std::make_unique<PlayState>(m_CanvasWidth, m_CanvasHeight, m_GameStateManager, m_MaxScore));
17+
fadeIn = false;
1818
}
1919

2020
clouds.update(deltaTime);
21+
22+
if (!fadeIn)
23+
{
24+
if(fadeColor.a >= 255)
25+
m_GameStateManager.setGameState(std::make_unique<PlayState>(m_CanvasWidth, m_CanvasHeight, m_GameStateManager, m_MaxScore));
26+
}
2127
}
2228

2329
void TitleScreenState::render(float deltaTime)
@@ -71,7 +77,26 @@ void TitleScreenState::render(float deltaTime)
7177
else
7278
{
7379
DrawTexture(playButtonDefault, playButtonPosition.x, playButtonPosition.y, WHITE);
74-
}
80+
}
81+
82+
if (fadeColor.a > 0 && fadeIn)
83+
{
84+
if (fadeColor.a - fadeSpeed * deltaTime < 0)
85+
fadeColor.a = 0;
86+
else
87+
fadeColor.a -= fadeSpeed * deltaTime;
88+
89+
DrawRectangle(0, 0, m_CanvasWidth, m_CanvasHeight, fadeColor);
90+
}
91+
else if (fadeColor.a <= 255 && !fadeIn)
92+
{
93+
if (fadeColor.a + fadeSpeed * deltaTime > 255)
94+
fadeColor.a = 255;
95+
else
96+
fadeColor.a += fadeSpeed * deltaTime;
97+
98+
DrawRectangle(0, 0, m_CanvasWidth, m_CanvasHeight, fadeColor);
99+
}
75100
}
76101

77102
void TitleScreenState::unloadAssets()

TitleScreenState.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ class TitleScreenState : public GameState
3232
bool isMouseOverPlayButton{ false };
3333

3434
BackgroundLayer clouds { "assets/textures/titleScreenClouds.png", 2, m_CanvasWidth, m_CanvasHeight };
35+
36+
Color fadeColor{ 0, 0, 0, 255 };
37+
float fadeSpeed{ 300.0f };
38+
bool fadeIn{ true };
3539

3640
int witchFrameToDraw{ 0 };
3741
int witchFrameNumber{ 3 };

0 commit comments

Comments
 (0)