7e325b1ee6983e6206d1ec3c16774ca341a7b172
[supertux.git] / src / object / gradient.cpp
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/gradient.hpp"
18 #include "supertux/object_factory.hpp"
19 #include "util/reader.hpp"
20
21 Gradient::Gradient() :
22   layer(LAYER_BACKGROUND0),
23   gradient_top(),
24   gradient_bottom()
25 {
26 }
27
28 Gradient::Gradient(const Reader& reader) :
29   layer(LAYER_BACKGROUND0),
30   gradient_top(),
31   gradient_bottom()
32 {
33   reader.get("layer", layer);
34   std::vector<float> bkgd_top_color, bkgd_bottom_color;
35   if(!reader.get("top_color", bkgd_top_color) ||
36      !reader.get("bottom_color", bkgd_bottom_color))
37     throw std::runtime_error("Must specify top_color and bottom_color in gradient");
38
39   gradient_top = Color(bkgd_top_color);
40   gradient_bottom = Color(bkgd_bottom_color);
41 }
42
43 Gradient::~Gradient()
44 {
45 }
46
47 void
48 Gradient::update(float)
49 {
50 }
51
52 void
53 Gradient::set_gradient(Color top, Color bottom)
54 {
55   gradient_top = top;
56   gradient_bottom = bottom;
57
58   if (gradient_top.red > 1.0 || gradient_top.green > 1.0
59       || gradient_top.blue > 1.0 || gradient_top.alpha > 1.0)
60     log_warning << "top gradient color has values above 1.0" << std::endl;
61   if (gradient_bottom.red > 1.0 || gradient_bottom.green > 1.0
62       || gradient_bottom.blue > 1.0 || gradient_bottom.alpha > 1.0)
63     log_warning << "bottom gradient color has values above 1.0" << std::endl;
64 }
65
66 void
67 Gradient::draw(DrawingContext& context)
68 {
69   context.push_transform();
70   context.set_translation(Vector(0, 0));
71   context.draw_gradient(gradient_top, gradient_bottom, layer);
72   context.pop_transform();
73 }
74
75 /* EOF */