fix cr/lfs and remove trailing whitespaces...
[supertux.git] / src / object / gradient.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20 #include <config.h>
21
22 #include <stdexcept>
23 #include "gradient.hpp"
24 #include "camera.hpp"
25 #include "video/drawing_context.hpp"
26 #include "lisp/lisp.hpp"
27 #include "lisp/writer.hpp"
28 #include "object_factory.hpp"
29 #include "resources.hpp"
30 #include "main.hpp"
31 #include "log.hpp"
32
33 Gradient::Gradient()
34   : layer(LAYER_BACKGROUND0)
35 {
36 }
37
38 Gradient::Gradient(const lisp::Lisp& reader)
39   : layer(LAYER_BACKGROUND0)
40 {
41   reader.get("layer", layer);
42   std::vector<float> bkgd_top_color, bkgd_bottom_color;
43   if(!reader.get_vector("top_color", bkgd_top_color) ||
44      !reader.get_vector("bottom_color", bkgd_bottom_color))
45     throw std::runtime_error("Must specify top_color and bottom_color in gradient");
46
47   gradient_top = Color(bkgd_top_color);
48   gradient_bottom = Color(bkgd_bottom_color);
49 }
50
51 Gradient::~Gradient()
52 {
53 }
54
55 void
56 Gradient::write(lisp::Writer& writer)
57 {
58   writer.start_list("gradient");
59
60   std::vector<float> bkgd_top_color, bkgd_bottom_color;
61   bkgd_top_color.push_back(gradient_top.red);
62   bkgd_top_color.push_back(gradient_top.green);
63   bkgd_top_color.push_back(gradient_top.blue);
64   bkgd_bottom_color.push_back(gradient_bottom.red);
65   bkgd_bottom_color.push_back(gradient_bottom.green);
66   bkgd_bottom_color.push_back(gradient_bottom.blue);
67   writer.write_float_vector("top_color", bkgd_top_color);
68   writer.write_float_vector("bottom_color", bkgd_bottom_color);
69
70   writer.write_int("layer", layer);
71
72   writer.end_list("gradient");
73 }
74
75 void
76 Gradient::update(float)
77 {
78 }
79
80 void
81 Gradient::set_gradient(Color top, Color bottom)
82 {
83   gradient_top = top;
84   gradient_bottom = bottom;
85
86   if (gradient_top.red > 1.0 || gradient_top.green > 1.0
87    || gradient_top.blue > 1.0 || gradient_top.alpha > 1.0)
88     log_warning << "top gradient color has values above 1.0" << std::endl;
89   if (gradient_bottom.red > 1.0 || gradient_bottom.green > 1.0
90    || gradient_bottom.blue > 1.0 || gradient_bottom.alpha > 1.0)
91     log_warning << "bottom gradient color has values above 1.0" << std::endl;
92 }
93
94 void
95 Gradient::draw(DrawingContext& context)
96 {
97   context.push_transform();
98   context.set_translation(Vector(0, 0));
99   context.draw_gradient(gradient_top, gradient_bottom, layer);
100   context.pop_transform();
101 }
102
103 IMPLEMENT_FACTORY(Gradient, "gradient");