1 | // SuperTux |
2 | // Copyright (C) 2006 Matthias Braun <matze@braunis.de> |
3 | // |
4 | // This program is free software: you can redistribute it and/or modify |
5 | // it under the terms of the GNU General Public License as published by |
6 | // the Free Software Foundation, either version 3 of the License, or |
7 | // (at your option) any later version. |
8 | // |
9 | // This program is distributed in the hope that it will be useful, |
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | // GNU General Public License for more details. |
13 | // |
14 | // You should have received a copy of the GNU General Public License |
15 | // along with this program. If not, see <http://www.gnu.org/licenses/>. |
16 | |
17 | #include "supertux/fadetoblack.hpp" |
18 | |
19 | #include "supertux/globals.hpp" |
20 | #include "video/drawing_context.hpp" |
21 | |
22 | FadeToBlack::FadeToBlack(Direction direction, float fade_time, Color color) : |
23 | m_direction(direction), |
24 | m_fade_time(fade_time), |
25 | m_color(color), |
26 | m_accum_time(0) |
27 | { |
28 | } |
29 | |
30 | void |
31 | FadeToBlack::update(float dt_sec) |
32 | { |
33 | m_accum_time += dt_sec; |
34 | if (m_accum_time > m_fade_time) |
35 | m_accum_time = m_fade_time; |
36 | } |
37 | |
38 | void |
39 | FadeToBlack::draw(DrawingContext& context) |
40 | { |
41 | Color col = m_color; |
42 | col.alpha = m_accum_time / m_fade_time; |
43 | if (m_direction != FADEOUT) |
44 | col.alpha = 1.0f - col.alpha; |
45 | |
46 | // The colours are mixed directly in sRGB space, so change alpha for a more |
47 | // linear fading (it may only work correctly with black) |
48 | col.alpha = Color::remove_gamma(col.alpha); |
49 | |
50 | context.color().draw_filled_rect(Rectf(0, 0, |
51 | static_cast<float>(context.get_width()), |
52 | static_cast<float>(context.get_height())), |
53 | col, LAYER_GUI + 1); |
54 | } |
55 | |
56 | bool |
57 | FadeToBlack::done() const |
58 | { |
59 | return m_accum_time >= m_fade_time; |
60 | } |
61 | |
62 | /* EOF */ |
63 | |