1 /*
2 Copyright (C) 2007, 2010 - Bit-Blot
3 
4 This file is part of Aquaria.
5 
6 Aquaria is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
10 
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 
15 See the GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20 */
21 #ifndef __render_object__
22 #define __render_object__
23 
24 #include "Base.h"
25 #include "Texture.h"
26 #include "Flags.h"
27 #include "ScriptObject.h"
28 
29 class Core;
30 class StateData;
31 
32 enum RenderObjectFlags
33 {
34 	RO_CLEAR			= 0x00,
35 	RO_RENDERBORDERS	= 0x01,
36 	RO_NEXT				= 0x02,
37 	RO_MOTIONBLUR		= 0x04
38 };
39 
40 enum AutoSize
41 {
42 	AUTO_VIRTUALWIDTH		= -101,
43 	AUTO_VIRTUALHEIGHT		= -102
44 };
45 
46 enum ParentManaged
47 {
48 	PM_NONE					= 0,
49 	PM_POINTER				= 1,
50 	PM_STATIC				= 2
51 };
52 
53 enum ChildOrder
54 {
55 	CHILD_BACK				= 0,
56 	CHILD_FRONT				= 1
57 };
58 
59 enum RenderBeforeParent
60 {
61 	RBP_NONE				= -1,
62 	RBP_OFF					= 0,
63 	RBP_ON					= 1
64 };
65 
66 struct MotionBlurFrame
67 {
68 	Vector position;
69 	float rotz;
70 };
71 
72 typedef std::vector<RectShape> CollideRects;
73 
74 class RenderObjectLayer;
75 
76 class RenderObject : public ScriptObject
77 {
78 public:
79 	friend class Core;
80 	RenderObject();
81 	virtual ~RenderObject();
82 	virtual void render();
83 
84 	static RenderObjectLayer *rlayer;
85 
setTexturePointer(CountedPtr<Texture> t)86 	void setTexturePointer(CountedPtr<Texture> t)
87 	{
88 		this->texture = t;
89 		onSetTexture();
90 	}
91 
92 	void setStateDataObject(StateData *state);
93 	bool setTexture(const std::string &name);
94 
95 	void toggleAlpha(float t = 0.2);
96 
97 	virtual void update(float dt);
isDead()98 	bool isDead() const {return _dead;}
isHidden()99 	bool isHidden() const {return _hidden || (parent && parent->isHidden());}
isStatic()100 	bool isStatic() const {return _static;}
101 
102 	// Set whether the object is hidden.  If hidden, no updates (except
103 	// lifetime checks) or render operations will be performed, and no
104 	// child objects will be updated or rendered.
setHidden(bool hidden)105 	void setHidden(bool hidden) {_hidden = hidden;}
106 
107 	// Set whether the object is static.  If static, the object's data
108 	// (including position, scale, rotation, color, etc.) are assumed
109 	// not to change over the lifetime of the object, to allow for
110 	// optimized rendering.
setStatic(bool staticFlag)111 	void setStatic(bool staticFlag) {_static = staticFlag;}
112 
setLife(float life)113 	void setLife(float life)
114 	{
115 		maxLife = this->life = life;
116 	}
setDecayRate(float decayRate)117 	void setDecayRate(float decayRate)
118 	{
119 		this->decayRate = decayRate;
120 	}
setBlendType(int bt)121 	void setBlendType (int bt)
122 	{
123 		blendType = bt;
124 	}
125 
126 	//enum DestroyType { RANDOM=0, REMOVE_STATE };
127 	virtual void destroy();
128 
129 	virtual void flipHorizontal();
130 	virtual void flipVertical();
131 
isfh()132 	bool isfh() const { return _fh; }
isfv()133 	bool isfv() const { return _fv; }
134 
135 	// recursive
136 	bool isfhr();
137 	bool isfvr();
138 
getIdx()139 	int getIdx() const { return idx; }
setIdx(int idx)140 	void setIdx(int idx) { this->idx = idx; }
141 	void moveToFront();
142 	void moveToBack();
143 
getCullRadiusSqr()144 	inline float getCullRadiusSqr() const
145 	{
146 		if (overrideCullRadiusSqr)
147 			return overrideCullRadiusSqr;
148 		if (width == 0 || height == 0)
149 			return 0;
150 		const float w = width*scale.x;
151 		const float h = height*scale.y;
152 		return w*w + h*h;
153 	}
154 
155 	int getTopLayer();
156 
157 	void setColorMult(const Vector &color, const float alpha);
158 	void clearColorMult();
159 
160 	void enableMotionBlur(int sz=10, int off=5);
161 	void disableMotionBlur();
162 
163 	void addChild(RenderObject *r, ParentManaged pm, RenderBeforeParent rbp = RBP_NONE, ChildOrder order = CHILD_BACK);
164 	void removeChild(RenderObject *r);
165 
166 	Vector getRealPosition();
167 	Vector getRealScale();
168 
169 	virtual float getSortDepth();
170 
171 	StateData *getStateData();
172 
173 	void setPositionSnapTo(InterpolatedVector *positionSnapTo);
174 
175 	// HACK: This is defined in RenderObject_inline.h because it needs
176 	// the class Core definition.  --achurch
177 	inline bool isOnScreen();
178 
179 	bool isCoordinateInRadius(const Vector &pos, float r);
180 
181 	void copyProperties(RenderObject *target);
182 
183 	const RenderObject &operator=(const RenderObject &r);
184 
185 	void toggleCull(bool value);
186 
187 	void safeKill();
188 
189 	void enqueueChildDeletion(RenderObject *r);
190 
191 	Vector getWorldPosition();
192 	Vector getWorldCollidePosition(const Vector &vec=Vector(0,0,0));
193 	Vector getInvRotPosition(const Vector &vec);
194 	bool isPieceFlippedHorizontal();
195 
196 	RenderObject *getTopParent();
197 
onAnimationKeyPassed(int key)198 	virtual void onAnimationKeyPassed(int key){}
199 
200 	Vector getAbsoluteRotation();
201 	float getWorldRotation();
202 	Vector getWorldPositionAndRotation(); // more efficient shortcut, returns rotation in vector z component
203 	Vector getNormal();
204 	Vector getForward();
205 	void setOverrideCullRadius(float ovr);
setRenderPass(int pass)206 	void setRenderPass(int pass) { renderPass = pass; }
getRenderPass()207 	int getRenderPass() { return renderPass; }
setOverrideRenderPass(int pass)208 	void setOverrideRenderPass(int pass) { overrideRenderPass = pass; }
getOverrideRenderPass()209 	int getOverrideRenderPass() { return overrideRenderPass; }
210 	enum { RENDER_ALL=314, OVERRIDE_NONE=315 };
211 
212 	// Defined in RenderObject_inline.h
213 	inline Vector getFollowCameraPosition() const;
214 
215 	void lookAt(const Vector &pos, float t, float minAngle, float maxAngle, float offset=0);
getParent()216 	inline RenderObject *getParent() const {return parent;}
217 	void applyBlendType();
218 	void fhTo(bool fh);
219 	void addDeathNotify(RenderObject *r);
220 	virtual void unloadDevice();
221 	virtual void reloadDevice();
222 
223 	Vector getCollisionMaskNormal(int index);
224 
225 	//-------------------------------- Methods above, fields below
226 
227 	static bool renderCollisionShape;
228 	static bool renderPaths;
229 	static int lastTextureApplied;
230 	static bool lastTextureRepeat;
231 
232 	float width, height;  // Only used by Quads, but stored here for getCullRadius()
233 	InterpolatedVector position, scale, color, alpha, rotation;
234 	InterpolatedVector offset, rotationOffset, internalOffset, beforeScaleOffset;
235 	InterpolatedVector velocity, gravity;
236 
237 	CountedPtr<Texture> texture;
238 
239 	//int mode;
240 
241 	bool fadeAlphaWithLife;
242 
243 	bool blendEnabled;
244 	enum BlendTypes { BLEND_DEFAULT = 0, BLEND_ADD, BLEND_SUB, BLEND_MULT };
245 	unsigned char blendType;
246 
247 	float life;
248 	//float lifeAlphaFadeMultiplier;
249 	float followCamera;
250 
251 	//bool useColor;
252 	bool renderBeforeParent;
253 	bool updateAfterParent;
254 
255 	//bool followXOnly;
256 	//bool renderOrigin;
257 
258 	//float updateMultiplier;
259 	//EventPtr deathEvent;
260 
261 	bool colorIsSaved;  // Used for both color and alpha
262 	Vector savedColor;  // Saved values from setColorMult()
263 	float savedAlpha;
264 
265 	bool shareAlphaWithChildren;
266 	bool shareColorWithChildren;
267 
268 	bool cull;
269 	float updateCull;
270 	int layer;
271 
272 	InterpolatedVector *positionSnapTo;
273 
274 	//DestroyType destroyType;
275 	typedef std::vector<RenderObject*> Children;
276 	Children children, childGarbage;
277 
278 	//Flags flags;
279 
280 #ifdef BBGE_BUILD_DIRECTX
281 	bool useDXTransform;
282 	//D3DXMATRIX matrix;
283 #endif
284 
285 	float collideRadius;
286 	Vector collidePosition;
287 	std::vector<Vector> collisionMask;
288 	std::vector<Vector> transformedCollisionMask;
289 
290 	CollideRects collisionRects;
291 	float collisionMaskRadius;
292 
293 	float alphaMod;
294 
295 	bool ignoreUpdate;
296 	bool useOldDT;
297 
298 protected:
onFH()299 	virtual void onFH(){}
onFV()300 	virtual void onFV(){}
onDestroy()301 	virtual void onDestroy(){}
onSetTexture()302 	virtual void onSetTexture(){}
onRender()303 	virtual void onRender(){}
304 	virtual void onUpdate(float dt);
305 	virtual void deathNotify(RenderObject *r);
onEndOfLife()306 	virtual void onEndOfLife() {}
307 
updateLife(float dt)308 	inline void updateLife(float dt)
309 	{
310 		if (decayRate > 0)
311 		{
312 			life -= decayRate*dt;
313 			if (life<=0)
314 			{
315 				safeKill();
316 			}
317 		}
318 		if (fadeAlphaWithLife && !alpha.isInterpolating())
319 		{
320 			//alpha = ((life*lifeAlphaFadeMultiplier)/maxLife);
321 			alpha = life/maxLife;
322 		}
323 	}
324 
325 	// Is this object or any of its children rendered in pass "pass"?
326 	bool hasRenderPass(const int pass);
327 
328 	inline void renderCall();
329 	void renderCollision();
330 
331 	bool repeatTexture;
332 	//ParentManaged pm;
333 	unsigned char pm;  // unsigned char to save space
334 	typedef std::list<RenderObject*> RenderObjectList;
335 	RenderObjectList deathNotifications;
336 	int overrideRenderPass;
337 	int renderPass;
338 	float overrideCullRadiusSqr;
339 	float motionBlurTransitionTimer;
340 	int motionBlurFrameOffsetCounter, motionBlurFrameOffset;
341 	std::vector<MotionBlurFrame>motionBlurPositions;
342 	bool motionBlur, motionBlurTransition;
343 
344 	bool _dead;
345 	bool _hidden;
346 	bool _static;
347 	bool _fv, _fh;
348 	//bool rotateFirst;
349 	int idx;
350 	RenderObject *parent;
351 	StateData *stateData;
352 	float decayRate;
353 	float maxLife;
354 };
355 
356 #endif
357