-
Notifications
You must be signed in to change notification settings - Fork 1
/
ModuleFadeToBlack.cpp
152 lines (129 loc) · 3.26 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <math.h>
#include "Globals.h"
#include "Application.h"
#include "ModuleFadeToBlack.h"
#include "ModuleRender.h"
#include "SDL/include/SDL_render.h"
#include "SDL/include/SDL_timer.h"
ModuleFadeToBlack::ModuleFadeToBlack()
{
screen = {0, 0, SCREEN_WIDTH * SCREEN_SIZE, SCREEN_HEIGHT * SCREEN_SIZE};
}
ModuleFadeToBlack::~ModuleFadeToBlack()
{}
// Load assets
bool ModuleFadeToBlack::Start()
{
LOG("Preparing Fade Screen");
SDL_SetRenderDrawBlendMode(App->render->renderer, SDL_BLENDMODE_BLEND);
return true;
}
// Update: draw background
update_status ModuleFadeToBlack::Update()
{
if(current_step == fade_step::none)
return UPDATE_CONTINUE;
Uint32 now = SDL_GetTicks() - start_time;
float normalized = MIN(1.0f, (float)now / (float)total_time);
switch(current_step)
{
case fade_step::fade_to_black:
{
if(now >= total_time)
{
total_time += total_time;
start_time = SDL_GetTicks();
current_step = fade_step::fade_from_black;
if (switch_scene) {
module_off->Disable();
module_on->Enable();
}
else {
isBlack = true;
}
}
} break;
case fade_step::fade_only_black:
{
if (now >= total_time)
{
isBlack = true;
}
} break;
case fade_step::fade_from_black:
{
isBlack = false;
normalized = 1.0f - normalized;
if (now >= total_time) {
current_step = fade_step::none;
switch_scene = false;
isfadding = false;
}
} break;
}
// Finally render the black square with alpha on the screen
SDL_SetRenderDrawColor(App->render->renderer, 0, 0, 0, (Uint8)(normalized * 255.0f));
SDL_RenderFillRect(App->render->renderer, &screen);
return UPDATE_CONTINUE;
}
// Fade to black. At mid point deactivate one module, then activate the other
bool ModuleFadeToBlack::FadeToBlack(Module* module_off, Module* module_on, float time)
{
bool ret = false;
if(current_step == fade_step::none)
{
current_step = fade_step::fade_to_black;
start_time = SDL_GetTicks();
total_time = (Uint32)(time * 0.5f * 1000.0f);
ret = true;
this->module_off = module_off;
this->module_on = module_on;
switch_scene = true;
}
else if (isfadding){
/* If the background is fading to the second texture of the background
* it will continue the fade to black and load the level, if it's from the
* black it will return to black and load the level
*/
if (current_step == fade_step::fade_from_black) {
current_step = fade_step::fade_to_black;
start_time = (SDL_GetTicks() - start_time) + (SDL_GetTicks() - total_time);
}
isfadding = false;
ret = true;
this->module_off = module_off;
this->module_on = module_on;
switch_scene = true;
}
return ret;
}
bool ModuleFadeToBlack::FadeToBlack(float time)
{
bool ret = false;
if (current_step == fade_step::none)
{
current_step = fade_step::fade_to_black;
start_time = SDL_GetTicks();
total_time = (Uint32)(time * 0.5f * 1000.0f);
ret = true;
isfadding = true;
}
return ret;
}
bool ModuleFadeToBlack::FadeonlyBlack(float time)
{
bool ret = false;
if (current_step == fade_step::none && !isBlack)
{
current_step = fade_step::fade_only_black;
start_time = SDL_GetTicks();
total_time = (Uint32)(time * 1000.0f);
ret = true;
isfadding = true;
}
return ret;
}
void ModuleFadeToBlack::resetStep()
{
current_step = fade_step::none;
}