-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModuleFadeToBlack.cpp
94 lines (69 loc) · 1.96 KB
/
ModuleFadeToBlack.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include "ModuleFadeToBlack.h"
#include "Application.h"
#include "ModuleWindow.h"
#include "Globals.h"
#include "ModuleRender.h"
#include "SDL/include/SDL_render.h"
ModuleFadeToBlack::ModuleFadeToBlack(Application* app, bool start_enabled) : Module(app, start_enabled)
{
}
ModuleFadeToBlack::~ModuleFadeToBlack()
{
}
bool ModuleFadeToBlack::Start()
{
LOG("Preparing Fade Screen");
screenRect = { 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT };
// Enable blending mode for transparency
SDL_SetRenderDrawBlendMode(App->renderer->renderer, SDL_BLENDMODE_BLEND);
return true;
}
update_status ModuleFadeToBlack::Update()
{
// Exit this function if we are not performing a fade
if (currentStep == Fade_Step::NONE) return update_status::UPDATE_CONTINUE;
if (currentStep == Fade_Step::TO_BLACK)
{
++frameCount;
if (frameCount >= maxFadeFrames)
{
moduleToDisable->Disable();
moduleToEnable->Enable();
currentStep = Fade_Step::FROM_BLACK;
}
}
else
{
--frameCount;
if (frameCount <= 0)
{
currentStep = Fade_Step::NONE;
}
}
return update_status::UPDATE_CONTINUE;
}
update_status ModuleFadeToBlack::PostUpdate()
{
// Exit this function if we are not performing a fade
if (currentStep == Fade_Step::NONE) return update_status::UPDATE_CONTINUE;
float fadeRatio = (float)frameCount / (float)maxFadeFrames;
// Render the black square with alpha on the screen
SDL_SetRenderDrawColor(App->renderer->renderer, 0, 0, 0, (Uint8)(fadeRatio * 255.0f));
SDL_RenderFillRect(App->renderer->renderer, &screenRect);
return update_status::UPDATE_CONTINUE;
}
bool ModuleFadeToBlack::FadeToBlack(Module* moduleToDisable, Module* moduleToEnable, float frames)
{
bool ret = false;
// If we are already in a fade process, ignore this call
if (currentStep == Fade_Step::NONE)
{
currentStep = Fade_Step::TO_BLACK;
frameCount = 0;
maxFadeFrames = frames;
this->moduleToDisable = moduleToDisable;
this->moduleToEnable = moduleToEnable;
ret = true;
}
return ret;
}