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#ifndef HEADER_SUPERTUX_OBJECT_BACKGROUND_HPP
18#define HEADER_SUPERTUX_OBJECT_BACKGROUND_HPP
19
20#include "math/vector.hpp"
21#include "scripting/background.hpp"
22#include "squirrel/exposed_object.hpp"
23#include "supertux/game_object.hpp"
24#include "video/blend.hpp"
25#include "video/drawing_context.hpp"
26#include "video/surface_ptr.hpp"
27
28class ReaderMapping;
29
30class Background final : public GameObject,
31 public ExposedObject<Background, scripting::Background>
32{
33public:
34 Background();
35 Background(const ReaderMapping& reader);
36 virtual ~Background();
37
38 virtual void update(float dt_sec) override;
39 virtual void draw(DrawingContext& context) override;
40
41 virtual std::string get_class() const override { return "background"; }
42 virtual std::string get_display_name() const override { return _("Background"); }
43
44 virtual const std::string get_icon_path() const override {
45 return "images/engine/editor/background.png";
46 }
47
48 virtual ObjectSettings get_settings() override;
49 virtual void after_editor_set() override;
50
51 void set_image(const std::string& name);
52 void set_images(const std::string& name_top, const std::string& name_middle, const std::string& name_bottom);
53 void set_speed(float bgd_speed);
54
55 void draw_image(DrawingContext& context, const Vector& pos);
56
57 std::string get_image() const { return m_imagefile; }
58 float get_speed() const { return m_parallax_speed.x; }
59 int get_layer() const { return m_layer; }
60
61private:
62 enum Alignment {
63 NO_ALIGNMENT,
64 LEFT_ALIGNMENT,
65 RIGHT_ALIGNMENT,
66 TOP_ALIGNMENT,
67 BOTTOM_ALIGNMENT
68 };
69
70private:
71 /** Backgrounds with NO_ALIGNMENT are repeated over the whole
72 screen, backgrounds with left, right, top, bottom alignment are
73 only repeated in one direction and attached to the level edge. */
74 Alignment m_alignment;
75
76 /** If fill is set, the background will not repeat and is instead
77 stretched over the whole screen, alignment and top/bottom images
78 are ignored in that case. */
79 bool m_fill;
80
81 int m_layer;
82 std::string m_imagefile_top;
83 std::string m_imagefile;
84 std::string m_imagefile_bottom;
85 Vector m_pos; /**< coordinates of upper-left corner of image */
86 Vector m_parallax_speed;
87 Vector m_scroll_speed;
88 Vector m_scroll_offset;
89 SurfacePtr m_image_top; /**< image to draw above pos */
90 SurfacePtr m_image; /**< image to draw, anchored at pos */
91 SurfacePtr m_image_bottom; /**< image to draw below pos+screenheight */
92
93 bool m_has_pos_x;
94 bool m_has_pos_y;
95
96 Blend m_blend;
97 DrawingTarget m_target;
98
99private:
100 Background(const Background&) = delete;
101 Background& operator=(const Background&) = delete;
102};
103
104#endif
105
106/* EOF */
107