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 #include "solarus/entities/SelfScrollingTilePattern.h"
18 #include "solarus/entities/Tileset.h"
19 #include "solarus/graphics/Surface.h"
20
21 namespace Solarus {
22
23 /**
24 * \brief Creates a tile pattern with self scrolling.
25 * \param ground Kind of ground of the tile pattern.
26 * \param xy Coordinates of the tile pattern in the tileset.
27 * \param size Size of the tile pattern in the tileset.
28 */
SelfScrollingTilePattern(Ground ground,const Point & xy,const Size & size)29 SelfScrollingTilePattern::SelfScrollingTilePattern(Ground ground, const Point& xy, const Size& size):
30 SimpleTilePattern(ground, xy, size) {
31
32 }
33
34 /**
35 * \brief Draws the tile image on a surface.
36 * \param dst_surface the surface to draw
37 * \param dst_position position where tile pattern should be drawn on dst_surface
38 * \param tileset the tileset of this tile
39 * \param viewport coordinates of the top-left corner of dst_surface relative
40 * to the map (may be used for scrolling tiles)
41 */
draw(const SurfacePtr & dst_surface,const Point & dst_position,const Tileset & tileset,const Point &) const42 void SelfScrollingTilePattern::draw(
43 const SurfacePtr& dst_surface,
44 const Point& dst_position,
45 const Tileset& tileset,
46 const Point& /* viewport */) const {
47
48 Rectangle src = position_in_tileset;
49 Point dst = dst_position;
50
51 // draw the tile with an offset that depends on its position modulo its size
52 Point offset;
53
54 if (dst.x >= 0) {
55 offset.x = dst.x % src.get_width();
56 }
57 else { // the modulo operation does not like negative numbers
58 offset.x = src.get_width() - (-dst.x % src.get_width());
59 }
60
61 if (dst.y >= 0) {
62 offset.y = dst.y % src.get_height();
63 }
64 else {
65 offset.y = src.get_height() - (-dst.y % src.get_height());
66 }
67
68 // apply a scrolling ratio
69 offset /= 2;
70
71 // draw the pattern in four steps
72 const SurfacePtr& tileset_image = tileset.get_tiles_image();
73
74 src.add_x(offset.x);
75 src.add_width(-offset.x);
76 src.add_y(offset.y);
77 src.add_height(-offset.y);
78 tileset_image->draw_region(src, dst_surface, dst);
79
80 src = position_in_tileset;
81 dst = dst_position;
82 src.add_y(offset.y);
83 src.add_height(-offset.y);
84 dst.x += src.get_width() - offset.x;
85 src.set_width(offset.x);
86 tileset_image->draw_region(src, dst_surface, dst);
87
88 src = position_in_tileset;
89 dst = dst_position;
90 src.add_x(offset.x);
91 src.add_width(-offset.x);
92 dst.y += src.get_height() - offset.y;
93 src.set_height(offset.y);
94 tileset_image->draw_region(src, dst_surface, dst);
95
96 src = position_in_tileset;
97 dst = dst_position;
98 dst.x += src.get_width() - offset.x;
99 src.set_width(offset.x);
100 dst.y += src.get_height() - offset.y;
101 src.set_height(offset.y);
102 tileset_image->draw_region(src, dst_surface, dst);
103 }
104
105 /**
106 * \brief Returns whether this tile pattern is animated, i.e. not always displayed
107 * the same way.
108 *
109 * Non-animated tiles may be rendered faster by using intermediate surfaces
110 * that are drawn only once.
111 *
112 * \return true if this tile pattern is animated
113 */
is_animated() const114 bool SelfScrollingTilePattern::is_animated() const {
115 return true;
116 }
117
118 }
119
120