Game Develop C++ Platform: SpriteObject.h Source File

| Game Develop C++ Platform Platform for developing native games based on OpenGL and SFML with Game Develop

- Main Page
- Modules
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Groups Pages
SpriteObject.h
Go to the documentation of this file.
1
6 #ifndef SPRITEOBJECT_H
7 #define SPRITEOBJECT_H
8
9 #include "GDCpp/Object.h"
10 #include "GDCpp/RuntimeObject.h"
11 namespace gd { class InitialInstance; }
12 namespace gd { class Object; }
13 namespace gd { class Layout; }
14 namespace sf { class Sprite; }
15 class Animation;
16 class Sprite;
17 class RuntimeScene;
18 #if defined(GD_IDE_ONLY)
19 class wxBitmap;
20 class wxWindow;
21 namespace gd { class MainFrameWrapper; }
22 namespace gd { class PropgridPropertyDescriptor; }
23 #endif
24
29 class GD_API AnimationProxy
30 {
31 public:
32 AnimationProxy();
33 AnimationProxy(const Animation & animation);
34 virtual ~AnimationProxy();
35 AnimationProxy(const AnimationProxy & proxy);
36 AnimationProxy & operator=(const AnimationProxy & rhs);
37
38 Animation & Get() {return *animation; }
39 const Animation & Get() const {return *animation; }
40 Animation & GetNonConst() {return *animation; }
41
42 private:
43 Animation * animation;
44 };
45
56 class GD_API SpriteObject : public gd::Object
57 {
58 public :
59
60 SpriteObject(std::string name_);
61 virtual ~SpriteObject();
62 virtual gd::Object * Clone() const { return new SpriteObject(*this);}
63
64 #if defined(GD_IDE_ONLY)
65 virtual bool GenerateThumbnail(const gd::Project & project, wxBitmap & thumbnail) const;
66 virtual void ExposeResources(gd::ArbitraryResourceWorker & worker);
67
68 virtual void EditObject( wxWindow* parent, gd::Project & project, gd::MainFrameWrapper & mainFrameWrapper_ );
69 virtual std::map<std::string, gd::PropgridPropertyDescriptor> GetInitialInstanceProperties(const gd::InitialInstance & position, gd::Project & project, gd::Layout & scene);
70 virtual bool UpdateInitialInstanceProperty(gd::InitialInstance & position, const std::string & name, const std::string & value, gd::Project & project, gd::Layout & scene);
71 virtual void DrawInitialInstance(gd::InitialInstance & instance, sf::RenderTarget & renderTarget, gd::Project & project, gd::Layout & layout);
72 virtual sf::Vector2f GetInitialInstanceDefaultSize(gd::InitialInstance & instance, gd::Project & project, gd::Layout & layout) const;
73 virtual sf::Vector2f GetInitialInstanceOrigin(gd::InitialInstance & instance, gd::Project & project, gd::Layout & layout) const;
74 virtual void LoadResources(gd::Project & project, gd::Layout & layout);
75
76 virtual bool SupportShaders() { return true; }
77 #endif
78
82 inline const Animation & GetAnimation(unsigned int nb) const { if ( nb >= GetAnimationCount() ) return badAnimation; else return animations[nb].Get(); }
84 inline Animation & GetAnimation(unsigned int nb) { if ( nb >= GetAnimationCount() ) return badAnimation; else return animations[nb].Get(); }
85 unsigned int GetAnimationCount() const {return animations.size();};
86 void AddAnimation(const Animation & animation);
87 bool RemoveAnimation(unsigned int nb);
88 inline void RemoveAllAnimation() { animations.clear(); }
89 inline bool HasNoAnimations() const { return animations.empty(); }
90 inline const std::vector < AnimationProxy > & GetAllAnimations() const { return animations; }
92
93 private:
94
95 virtual void DoLoadFromXml(gd::Project & project, const TiXmlElement * elemScene);
96 #if defined(GD_IDE_ONLY)
97 virtual void DoSaveToXml(TiXmlElement * elemScene);
98 #endif
99
100 const Sprite * GetInitialInstanceSprite(gd::InitialInstance & instance, gd::Project & project, gd::Layout & layout, bool * shouldNotRotate = NULL) const;
101 mutable std::vector < AnimationProxy > animations;
102
103 //Null objects if need to return a bad object.
104 static Animation badAnimation;
105 };
106
107 class GD_API RuntimeSpriteObject : public RuntimeObject
108 {
109 public :
110
111 RuntimeSpriteObject(RuntimeScene & scene, const gd::Object & object);
112 virtual ~RuntimeSpriteObject() {};
113 virtual RuntimeObject * Clone() const { return new RuntimeSpriteObject(*this);}
114
115 virtual bool ExtraInitializationFromInitialInstance(const gd::InitialInstance & position);
116
117 virtual bool Draw(sf::RenderTarget & renderTarget);
118
119 #if defined(GD_IDE_ONLY)
120 virtual void GetPropertyForDebugger (unsigned int propertyNb, std::string & name, std::string & value) const;
121 virtual bool ChangeProperty(unsigned int propertyNb, std::string newValue);
122 virtual unsigned int GetNumberOfProperties() const;
123 #endif
124
125 virtual void UpdateTime(float timeElapsed);
126
127 virtual void OnPositionChanged() {needUpdateCurrentSprite = true;};
128
129 virtual float GetWidth() const;
130 virtual float GetHeight() const;
131 virtual void SetWidth(float newWidth);
132 virtual void SetHeight(float newHeight);
133 virtual void SetOriginalSize();
134
135 virtual float GetDrawableX() const;
136 virtual float GetDrawableY() const;
137
138 virtual float GetCenterX() const;
139 virtual float GetCenterY() const;
140
141 float GetPointX(const std::string & point) const;
142 float GetPointY(const std::string & point) const;
143
144 void UpdateCurrentSprite() const;
145 const sf::Sprite & GetCurrentSFMLSprite() const;
146 const Sprite & GetCurrentSprite() const;
147
151 void StopAnimation() { animationStopped = true; };
153 void PlayAnimation() { animationStopped = false; };
154 bool IsAnimationStopped() const { return animationStopped; }
155 bool AnimationEnded() const;
156 unsigned int GetAnimationCount() const;
157
158 inline unsigned int GetCurrentAnimation() const { return currentAnimation; }
159 bool SetCurrentAnimation(unsigned int nb);
161
162 bool CursorOnObject( RuntimeScene & scene, bool accurate );
163
164 virtual bool SetAngle(float newAngle);
165 virtual float GetAngle() const;
166
167 bool SetDirection(float nb);
168 inline unsigned int GetCurrentDirection() const { return currentDirection; }
169
173 float GetCurrentDirectionOrAngle() const;
174
175 bool SetSprite(unsigned int nb);
176 inline unsigned int GetSpriteNb() const { return currentSprite; }
177
178 void SetOpacity(float val);
179 inline float GetOpacity() const {return opacity;};
180
181 inline void SetBlendMode(unsigned int blendMode_) { blendMode = blendMode_; };
182 inline unsigned int GetBlendMode() const {return blendMode;};
183
184 inline void SetScaleX(float val) { if ( val > 0 ) scaleX = val; needUpdateCurrentSprite = true; };
185 inline float GetScaleX() const { return scaleX; };
186 inline void SetScaleY(float val) { if ( val > 0 ) scaleY = val; needUpdateCurrentSprite = true; };
187 inline float GetScaleY() const { return scaleY; };
188
189 void SetColor(unsigned int r,unsigned int v,unsigned int b);
190 unsigned int GetColorR() const;
191 unsigned int GetColorV() const;
192 unsigned int GetColorB() const;
193
194 virtual std::vector<Polygon2d> GetHitBoxes() const;
195
196 void FlipX(bool flip = true);
197 void FlipY(bool flip = true);
198 bool IsFlippedX() const { return isFlippedX; };
199 bool IsFlippedY() const { return isFlippedY; };
200
201 void TurnTowardPosition(float Xposition, float Yposition);
202 void TurnTowardObject( RuntimeObject * object );
203
207 void ChangeScale(const std::string & operatorStr, double newValue);
208
212 void SetColor(const std::string & colorStr);
213
214 void CopyImageOnImageOfCurrentSprite(RuntimeScene & scene, const std::string & imageName, float xPosition, float yPosition, bool useTransparency);
215 void MakeColorTransparent( const std::string & colorStr );
216
217 private:
218
219 //Animations, Directions et numéro de sprite actuel
220 unsigned int currentAnimation;
221 unsigned int currentDirection;
222 float currentAngle;
223 unsigned int currentSprite;
224 bool animationStopped;
225 float timeElapsedOnCurrentSprite;
226
227 mutable Sprite * ptrToCurrentSprite; //Ptr to the current sprite
228 mutable bool needUpdateCurrentSprite;
229
230 mutable std::vector < AnimationProxy > animations;
231 mutable unsigned int cacheAnimationsSize;
232 mutable bool cacheAnimationSizeNeedUpdate;
233
234 float opacity;
235 unsigned int blendMode;
236 bool isFlippedX;
237 bool isFlippedY;
238
239 float scaleX;
240 float scaleY;
241
242 //Color filter
243 unsigned int colorR;
244 unsigned int colorV;
245 unsigned int colorB;
246
247 //Null objects if need to return a bad object.
248 static Sprite * badSpriteDatas;
249 static Animation badAnimation;
250 };
251
252
253 GD_API void DestroySpriteObject(gd::Object * object);
254 GD_API gd::Object * CreateSpriteObject(std::string name);
255 GD_API void DestroyRuntimeSpriteObject(RuntimeObject * object);
256 GD_API RuntimeObject * CreateRuntimeSpriteObject(RuntimeScene & scene, const gd::Object & object);
257
258 #endif // SPRITEOBJECT_H
259