/
Sprite.h
205 lines (169 loc) · 5.81 KB
/
Sprite.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
* GDevelop Core
* Copyright 2008-2014 Florian Rival (Florian.Rival@gmail.com). All rights reserved.
* This project is released under the MIT License.
*/
#ifndef SPRITE_H
#define SPRITE_H
#include <string>
#include <boost/shared_ptr.hpp>
#include <SFML/Graphics/Sprite.hpp>
#include "GDCore/BuiltinExtensions/SpriteExtension/Point.h"
#include "GDCore/BuiltinExtensions/SpriteExtension/Polygon2d.h"
class SFMLTextureWrapper;
#undef LoadImage //prevent windows.h to be polluting everything
namespace gd
{
/**
* \brief Represents a sprite to be included in a Direction and to display on the screen
*
* A sprite contains a SFML sprite to be displayed, some points, and can also have its own image ( rather than a image from ImageManager ).
*
* \see Direction
* \see SpriteObject
* \ingroup SpriteObjectExtension
*/
class GD_CORE_API Sprite
{
public:
Sprite();
virtual ~Sprite();
/**
* Change the name of the Sprite's image
*/
inline void SetImageName(const std::string & image_) { image = image_; }
/**
* Get the name of the Sprite's image
*/
inline const std::string & GetImageName() const { return image; }
/**
* Get a reference to the name of the image
*/
inline std::string & GetImageName() { return image; }
/**
* Get the collision mask ( custom or automatically generated owing to IsCollisionMaskAutomatic() )
*
* \warning If the image has not been loaded ( using LoadImage ) and the collision mask is set as automatic,
* the returned mask won't be correct.
*/
std::vector<Polygon2d> GetCollisionMask() const;
/**
* Set the custom collision mask.
* Call then SetCollisionMaskAutomatic() to use it.
*/
void SetCustomCollisionMask(const std::vector<Polygon2d> & collisionMask);
/**
* Return true if the collision mask is a bounding box, false if a custom collision mask is used.
*/
inline bool IsCollisionMaskAutomatic() const { return automaticCollisionMask; }
/**
* Un/Set use of the custom collision mask.
*/
inline void SetCollisionMaskAutomatic(bool enabled) { automaticCollisionMask = enabled; };
/**
* Return all points without origin and center
*/
inline std::vector < Point > & GetAllNonDefaultPoints() { return points; }
/**
* Return all points without origin and center
*/
inline const std::vector < Point > & GetAllNonDefaultPoints() const { return points; }
/**
* Add a point
*/
void AddPoint( const Point & point );
/**
* Delete point with the specified name
*/
void DelPoint( const std::string & name );
/**
* Return point with name passed in argument
*/
const Point & GetPoint( const std::string & name) const;
/**
* Return point with name passed in argument
*/
Point & GetPoint(const std::string & name);
/**
* Return true if the point exists.
*/
bool HasPoint( const std::string & name ) const;
/**
* Return Origin point.
*/
inline const Point & GetOrigin() const { return origine; }
/**
* Return Origin point.
*/
inline Point & GetOrigin() { return origine; }
/**
* Return Centre point.
*
* \warning If the image has not been loaded ( using LoadImage ) and the center point is set as automatic,
* the returned point won't be correct.
*/
inline const Point & GetCenter() const { return centre; }
/**
* Return Centre point.
*
* \warning If the image has not been loaded ( using LoadImage ) and the center point is set as automatic,
* the returned point won't be correct.
*/
inline Point & GetCenter() { automaticCentre = false; return centre; }
/**
* Return true if the center point is automatically computed
*/
inline bool IsDefaultCenterPoint() const { return automaticCentre; }
/**
* Un/Set automatic centre
*/
bool SetDefaultCenterPoint(bool enabled);
#if !defined(EMSCRIPTEN)
/** \name Sprite runtime management
* Functions used by the C++ game engine.
*/
///@{
/**
* Get the SFML Sprite associated with the Sprite
*/
inline const sf::Sprite & GetSFMLSprite() const { return sfmlSprite; }
/**
* Get the SFML Sprite associated with the Sprite
*/
inline sf::Sprite & GetSFMLSprite() { return sfmlSprite; }
/**
* Set a new image to the Sprite
*/
void LoadImage(boost::shared_ptr<SFMLTextureWrapper> image);
/**
* Get SFML Image used by the sprite
*/
boost::shared_ptr<SFMLTextureWrapper> GetSFMLTexture() { return sfmlImage; };
/**
* Get SFML Image used by the sprite
*/
const boost::shared_ptr<SFMLTextureWrapper> GetSFMLTexture() const { return sfmlImage; };
/**
* Make the sprite, if it uses an image from ImageManager,
* copy this image and own it inside.
*/
void MakeSpriteOwnsItsImage();
///@}
#endif
private:
#if !defined(EMSCRIPTEN)
sf::Sprite sfmlSprite; ///< Displayed SFML sprite
boost::shared_ptr<SFMLTextureWrapper> sfmlImage; ///< Pointer to the image displayed by the sprite.
bool hasItsOwnImage; ///< True if sfmlImage is only owned by this Sprite.
#endif
std::string image; ///< Name of the image to be loaded in Image Manager.
bool automaticCollisionMask; ///< True to use the custom collision mask. Otherwise, a basic bounding box is returned by GetCollisionMask()
std::vector<Polygon2d> customCollisionMask; ///< Custom collision mask
std::vector < Point > points; ///< List of the points used by the sprite
Point origine; ///< Origin point
Point centre; ///< Center point
bool automaticCentre; ///< True to let the sprite compute its center
static Point badPoint; ///< Returned when no other valid Point object is available.
};
}
#endif // SPRITE_H