-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModuleFadeToBlack.cpp
More file actions
85 lines (71 loc) · 1.89 KB
/
ModuleFadeToBlack.cpp
File metadata and controls
85 lines (71 loc) · 1.89 KB
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
#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)
{
to_disable->Disable();
to_enable->Enable();
total_time += total_time;
start_time = SDL_GetTicks();
current_step = fade_step::fade_from_black;
}
} break;
case fade_step::fade_from_black:
{
normalized = 1.0f - normalized;
if(now >= total_time)
current_step = fade_step::none;
} 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);
to_enable = module_on;
to_disable = module_off;
ret = true;
}
return ret;
}
bool ModuleFadeToBlack::IsFading() const
{
return current_step != fade_step::none;
}