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 "object/display_effect.hpp"
18
19#include "supertux/globals.hpp"
20#include "video/drawing_context.hpp"
21
22static const float BORDER_SIZE = 75;
23
24DisplayEffect::DisplayEffect(const std::string& name) :
25 GameObject(name),
26 ExposedObject<DisplayEffect, scripting::DisplayEffect>(this),
27 screen_fade(NO_FADE),
28 screen_fadetime(0),
29 screen_fading(0),
30 border_fade(NO_FADE),
31 border_fadetime(0),
32 border_fading(),
33 border_size(0),
34 black(false),
35 borders(false)
36{
37}
38
39DisplayEffect::~DisplayEffect()
40{
41}
42
43void
44DisplayEffect::update(float dt_sec)
45{
46 switch (screen_fade) {
47 case NO_FADE:
48 break;
49 case FADE_IN:
50 screen_fading -= dt_sec;
51 if (screen_fading < 0) {
52 screen_fade = NO_FADE;
53 }
54 break;
55 case FADE_OUT:
56 screen_fading -= dt_sec;
57 if (screen_fading < 0) {
58 screen_fade = NO_FADE;
59 black = true;
60 }
61 break;
62 default:
63 assert(false);
64 }
65
66 switch (border_fade) {
67 case NO_FADE:
68 break;
69 case FADE_IN:
70 border_fading -= dt_sec;
71 if (border_fading < 0) {
72 border_fade = NO_FADE;
73 }
74 border_size = (border_fadetime - border_fading)
75 / border_fadetime * BORDER_SIZE;
76 break;
77 case FADE_OUT:
78 border_fading -= dt_sec;
79 if (border_fading < 0) {
80 borders = false;
81 border_fade = NO_FADE;
82 }
83 border_size = border_fading / border_fadetime * BORDER_SIZE;
84 break;
85 default:
86 assert(false);
87 }
88}
89
90void
91DisplayEffect::draw(DrawingContext& context)
92{
93 context.push_transform();
94 context.set_translation(Vector(0, 0));
95
96 if (black || screen_fade != NO_FADE) {
97 float alpha;
98 if (black) {
99 alpha = 1.0f;
100 } else {
101 switch (screen_fade) {
102 case FADE_IN:
103 alpha = screen_fading / screen_fadetime;
104 break;
105 case FADE_OUT:
106 alpha = (screen_fadetime - screen_fading) / screen_fadetime;
107 break;
108 default:
109 alpha = 0.0f; // NOLINT
110 assert(false);
111 }
112
113 // Same as in fadetoblack.cpp
114 alpha = Color::remove_gamma(alpha);
115 }
116 context.color().draw_filled_rect(Rectf(0, 0,
117 static_cast<float>(context.get_width()),
118 static_cast<float>(context.get_height())),
119 Color(0, 0, 0, alpha), LAYER_GUI - 10);
120 }
121
122 if (borders) {
123 context.color().draw_filled_rect(Rectf(0, 0,
124 static_cast<float>(context.get_width()),
125 static_cast<float>(border_size)),
126 Color(0, 0, 0, 1.0f), LAYER_GUI-10);
127 context.color().draw_filled_rect(Rectf(Vector(0,
128 static_cast<float>(context.get_height()) - border_size),
129 Sizef(static_cast<float>(context.get_width()),
130 static_cast<float>(border_size))),
131 Color(0, 0, 0, 1.0f), LAYER_GUI-10);
132 }
133
134 context.pop_transform();
135}
136
137void
138DisplayEffect::fade_out(float fadetime)
139{
140 black = false;
141 screen_fadetime = fadetime;
142 screen_fading = fadetime;
143 screen_fade = FADE_OUT;
144}
145
146void
147DisplayEffect::fade_in(float fadetime)
148{
149 black = false;
150 screen_fadetime = fadetime;
151 screen_fading = fadetime;
152 screen_fade = FADE_IN;
153}
154
155void
156DisplayEffect::set_black(bool enabled)
157{
158 black = enabled;
159}
160
161bool
162DisplayEffect::is_black() const
163{
164 return black;
165}
166
167void
168DisplayEffect::sixteen_to_nine(float fadetime)
169{
170 if (fadetime == 0) {
171 borders = true;
172 border_size = BORDER_SIZE;
173 } else {
174 borders = true;
175 border_size = 0;
176 border_fade = FADE_IN;
177 border_fadetime = fadetime;
178 border_fading = border_fadetime;
179 }
180}
181
182void
183DisplayEffect::four_to_three(float fadetime)
184{
185 if (fadetime == 0) {
186 borders = false;
187 } else {
188 border_size = BORDER_SIZE;
189 border_fade = FADE_OUT;
190 border_fadetime = fadetime;
191 border_fading = border_fadetime;
192 }
193}
194
195/* EOF */
196