1 /*
2  * Copyright (C) 2006-2019 Christopho, Solarus - http://www.solarus-games.org
3  *
4  * Solarus 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  * Solarus 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 along
15  * with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 #ifndef SOLARUS_DRAWABLE_H
18 #define SOLARUS_DRAWABLE_H
19 
20 #include "solarus/core/Common.h"
21 #include "solarus/core/Point.h"
22 #include "solarus/core/Size.h"
23 #include "solarus/graphics/BlendMode.h"
24 #include "solarus/graphics/SurfacePtr.h"
25 #include "solarus/graphics/ShaderPtr.h"
26 #include "solarus/lua/ExportableToLua.h"
27 #include "solarus/lua/ScopedLuaRef.h"
28 #include "solarus/graphics/DrawProxies.h"
29 #include <memory>
30 
31 namespace Solarus {
32 
33 class Movement;
34 class Transition;
35 class Rectangle;
36 
37 /**
38  * \brief An object that can be drawn on a surface with additional effects.
39  *
40  * Possible additional effects are movements and transitions.
41  * Lua callbacks may be associated to the end of these effects.
42  */
43 class Drawable: public ExportableToLua {
44 
45   public:
46 
47     virtual ~Drawable();
48 
49     virtual Point get_origin() const;
50 
51     /**
52      * \brief Returns the size of this drawable.
53      * \return The size of this drawable.
54      */
55     virtual Size get_size() const = 0;
56 
57     // dynamic effects
58     void start_movement(const std::shared_ptr<Movement>& movement);
59     void stop_movement();
60     const std::shared_ptr<Movement>& get_movement();
61     const Point& get_xy() const;
62     void set_xy(const Point& xy);
63 
64     void start_transition(
65         std::unique_ptr<Transition> transition,
66         const ScopedLuaRef& callback_ref
67     );
68     void stop_transition();
69     Transition* get_transition();
70 
71     // drawing with effects
72     void draw(const SurfacePtr& dst_surface) const;
73     void draw(const SurfacePtr& dst_surface, int x, int y) const;
74     void draw(const SurfacePtr& dst_surface, const Point& dst_position) const;
75     void draw(const SurfacePtr &dst_surface, const Point &dst_position, const DrawProxy& proxy) const;
76     void draw_region(const Rectangle& region, const SurfacePtr& dst_surface) const;
77     void draw_region(const Rectangle& region,
78         const SurfacePtr& dst_surface, const Point& dst_position) const;
79     void draw_region(const Rectangle& region,
80                      const SurfacePtr& dst_surface, const Point& dst_position, const DrawProxy& proxy) const;
81 
82     void draw_with_transition(const Rectangle& region, const SurfacePtr& dst_surface, const Point& dst_position, const Transition &trans) const;
83 
84     void set_shader(const ShaderPtr& shader);
85     const ShaderPtr& get_shader() const;
86 
87     /**
88      * \brief Draws this object with effect information passed
89      *
90      * Redefine this function to draw your object onto the destination
91      * surface.
92      *
93      * This version may ignore the passed infos.region parameter and draw
94      * at full size, this allow sprites to optimize drawing when no cropping
95      * is needed, as it is often the case
96      *
97      * \param dst_surface The destination surface.
98      * \param infos draw informations bundle
99      */
100     virtual void raw_draw(
101         Surface& dst_surface,
102         const DrawInfos& infos
103     ) const = 0;
104 
105     /**
106      * @brief Draw
107      *
108      * @param dst_surface
109      * @param infos draw information bundle
110      */
111     virtual void raw_draw_region(
112         Surface& dst_surface,
113         const DrawInfos& infos
114     ) const = 0;
115 
116     virtual void update();
117     bool is_suspended() const;
118     virtual void set_suspended(bool suspended);
119 
120     BlendMode get_blend_mode() const;
121     void set_blend_mode(BlendMode blend_mode);
122 
123     const Color& get_color_modulation() const;
124     void set_color_modulation(const Color& color);
125 
126     uint8_t get_opacity() const;
127     void set_opacity(uint8_t opacity);
128 
129     double get_rotation() const;
130     void set_rotation(double rotation);
131 
132     const Scale& get_scale() const;
133     void set_scale(const Scale& scale);
134 
135     const Point& get_transformation_origin() const;
136     void set_transformation_origin(const Point& origin);
137 
138     Point get_full_origin() const;
139 
140     virtual Rectangle get_region() const = 0;
141   protected:
142     Drawable();
143   private:
144     const DrawProxy& terminal() const;
145 
146     Point xy;                     /**< Current position of this object
147                                    * (result of movements). */
148     std::shared_ptr<Movement>
149         movement;                 /**< A movement applied or nullptr. */
150     std::unique_ptr<Transition>
151         transition;               /**< A transition applied or nullptr. */
152     ScopedLuaRef transition_callback_ref;
153                                   /**< Lua registry ref of a function to call
154                                    * when the transition finishes */
155     bool suspended;               /**< Whether this object is suspended. */
156     BlendMode blend_mode;         /**< How to draw this object on a surface. */
157     ShaderPtr shader;             /**< Optional shader used to draw the object */
158     uint8_t opacity = 255;        /**< Opacity of this drawable object */
159     double rotation = 0;          /**< Rotation of the object around transform_origin*/
160     Scale scale;                  /**< Scale of the object around transform_origin*/
161     Point transformation_origin;       /**< pivot for the transformations (rot,scale) of the object*/
162     Color color_mod = Color::white;
163 };
164 
165 }
166 
167 #endif
168 
169