fade out console
[supertux.git] / src / object / gradient.cpp
1 //  $Id: background.cpp 3144 2006-03-31 10:31:03Z matzebraun $
2 //
3 //  SuperTux -  A Jump'n Run
4 //  Copyright (C) 2004 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 #include <config.h>
20
21 #include <stdexcept>
22 #include "gradient.hpp"
23 #include "camera.hpp"
24 #include "video/drawing_context.hpp"
25 #include "lisp/lisp.hpp"
26 #include "lisp/writer.hpp"
27 #include "object_factory.hpp"
28 #include "resources.hpp"
29 #include "main.hpp"
30 #include "log.hpp"
31
32 Gradient::Gradient()
33   : layer(LAYER_BACKGROUND0)
34 {
35 }
36
37 Gradient::Gradient(const lisp::Lisp& reader)
38   : layer(LAYER_BACKGROUND0)
39 {
40   reader.get("layer", layer);
41   std::vector<float> bkgd_top_color, bkgd_bottom_color;
42   if(!reader.get_vector("top_color", bkgd_top_color) ||
43      !reader.get_vector("bottom_color", bkgd_bottom_color))
44     throw std::runtime_error("Must specify top_color and bottom_color in gradient");
45
46   gradient_top = Color(bkgd_top_color);
47   gradient_bottom = Color(bkgd_bottom_color);
48 }
49
50 Gradient::~Gradient()
51 {
52 }
53
54 void
55 Gradient::write(lisp::Writer& writer)
56 {
57   writer.start_list("gradient");
58
59   std::vector<float> bkgd_top_color, bkgd_bottom_color;
60   bkgd_top_color.push_back(gradient_top.red);
61   bkgd_top_color.push_back(gradient_top.green);
62   bkgd_top_color.push_back(gradient_top.blue);
63   bkgd_bottom_color.push_back(gradient_bottom.red);
64   bkgd_bottom_color.push_back(gradient_bottom.green);
65   bkgd_bottom_color.push_back(gradient_bottom.blue);
66   writer.write_float_vector("top_color", bkgd_top_color);
67   writer.write_float_vector("bottom_color", bkgd_bottom_color);
68
69   writer.write_int("layer", layer);
70   
71   writer.end_list("gradient");
72 }
73
74 void
75 Gradient::update(float)
76 {
77 }
78
79 void
80 Gradient::set_gradient(Color top, Color bottom)
81 {
82   gradient_top = top;
83   gradient_bottom = bottom;
84   
85   if (gradient_top.red > 1.0 || gradient_top.green > 1.0
86    || gradient_top.blue > 1.0 || gradient_top.alpha > 1.0)
87     log_warning << "top gradient color has values above 1.0" << std::endl;
88   if (gradient_bottom.red > 1.0 || gradient_bottom.green > 1.0
89    || gradient_bottom.blue > 1.0 || gradient_bottom.alpha > 1.0)
90     log_warning << "bottom gradient color has values above 1.0" << std::endl;
91 }
92
93 void
94 Gradient::draw(DrawingContext& context)
95 {
96   context.push_transform();
97   context.set_translation(Vector(0, 0));
98   context.draw_gradient(gradient_top, gradient_bottom, layer);
99   context.pop_transform();
100 }
101
102 IMPLEMENT_FACTORY(Gradient, "gradient");