-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathModuleFadeToBlack.cpp
80 lines (66 loc) · 1.67 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
#include <math.h>
#include "Globals.h"
#include "Application.h"
#include "ModuleFadeToBlack.h"
#include "ModuleRender.h"
#include "SDL.h"
ModuleFadeToBlack::ModuleFadeToBlack(bool start_enabled) : Module(start_enabled)
{}
ModuleFadeToBlack::~ModuleFadeToBlack()
{}
// Load assets
bool ModuleFadeToBlack::Start()
{
SDL_SetRenderDrawBlendMode(App->renderer->renderer, SDL_BLENDMODE_BLEND);
return true;
}
// Update: draw background
update_status ModuleFadeToBlack::Update()
{
if (start_time > 0)
{
Uint32 now = SDL_GetTicks() - start_time;
float normalized = (float)now / (float)total_time;
if (normalized > 1.0f)
normalized = 1.0f;
if (fading_in == false)
normalized = 1.0f - normalized;
SDL_SetRenderDrawColor(App->renderer->renderer, 0, 0, 0, (Uint8)(normalized * 255.0f));
SDL_RenderFillRect(App->renderer->renderer, NULL);
if (module_off == nullptr && module_on != nullptr)
{
module_on->Enable();
module_on = nullptr;
}
if (now >= total_time)
{
if (fading_in == true)
{
if (module_off != nullptr)
module_off->Disable();
module_on->Enable();
total_time += total_time;
start_time = SDL_GetTicks();
fading_in = false;
}
else
{
start_time = 0;
}
}
}
return UPDATE_CONTINUE;
}
// Fade to black. At mid point deactivate one module, then activate the other
void ModuleFadeToBlack::FadeToBlack(Module* module_on, Module* module_off, float time)
{
fading_in = (module_off != nullptr) ? true : false;
start_time = SDL_GetTicks();
total_time = (Uint32)(time * 0.5f * 1000.0f);
this->module_on = module_on;
this->module_off = module_off;
}
bool ModuleFadeToBlack::isFading() const
{
return start_time > 0;
}