-
Notifications
You must be signed in to change notification settings - Fork 0
/
TransitionFade.cpp
136 lines (119 loc) · 2.79 KB
/
TransitionFade.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
/** @file TransitionFade.cpp */
#include "TransitionFade.h"
#include "Surface.h"
#include "System.h"
/**
* @brief Creates a fade-in or fade-out transition effect.
* @param direction direction of the transition effect (in or out)
*/
TransitionFade::TransitionFade(Transition::Direction direction): Transition(direction), finished(false), alpha(-1),
dst_surface(NULL)
{
if (direction == OUT)
{
alpha_start = 256;
alpha_limit = 0;
alpha_increment = -8;
}
else
{
alpha_start = 0;
alpha_limit = 256;
alpha_increment = 8;
}
set_delay(20);
}
/**
* @brief Destructor.
*/
TransitionFade::~TransitionFade()
{
}
/**
* @brief Sets the delay between two frames.
*
* The default delay is 20 ms.
*
* @param delay the new delay in milliseconds
*/
void TransitionFade::set_delay(uint32_t delay)
{
this->delay = delay;
}
/**
* @brief Starts this transition effect.
*/
void TransitionFade::start()
{
alpha = alpha_start;
next_frame_date = System::now();
}
/**
* @brief Returns whether the transition effect is started and not finished yet.
* @return true if the transition effect is started
*/
bool TransitionFade::is_started() const
{
return alpha != -1 && !is_finished();
}
/**
* @brief Returns whether the transition effect is finished.
* @return true if the transition effect is finished
*/
bool TransitionFade::is_finished() const
{
return finished;
}
/**
* @brief Notifies the transition effect that it was just suspended
* or resumed.
* @param suspended true if suspended, false if resumed.
*/
void TransitionFade::notify_suspended(bool suspended)
{
if (!suspended)
{
next_frame_date += System::now() - get_when_suspended();
}
}
/**
* @brief Updates this transition effect.
*
* This function should be called repeatedly while the transition exists.
*/
void TransitionFade::update()
{
if (!is_started() || is_suspended())
{
return;
}
uint32_t now = System::now();
// update the transition effect if needed
while (now >= next_frame_date && !finished)
{
alpha += alpha_increment;
next_frame_date += delay; // 20 ms between two frame updates (default)
if (dst_surface != NULL)
{
// make sure the final opacity is applied to the surface
int alpha_impl = std::min(alpha, 255);
dst_surface->set_opacity(alpha_impl);
}
finished = (alpha == alpha_limit);
}
}
/**
* @brief Draws the transition effect on a surface.
* @param dst_surface the destination surface
*/
void TransitionFade::draw(Surface& dst_surface)
{
if(!(this->dst_surface == NULL || this->dst_surface == &dst_surface))
{
std::cerr << "Unexpected surface for transition\n";
}
// draw the transition effect on the surface
int alpha_impl = std::min(alpha, 255);
dst_surface.set_opacity(alpha_impl);
this->dst_surface = &dst_surface;
}