00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef LUGRE_SPRITELIST_H
00025 #define LUGRE_SPRITELIST_H
00026
00027
00028 #include <list>
00029 #include <OgrePrerequisites.h>
00030 #include <OgreVector2.h>
00031 #include <OgreVector3.h>
00032 #include <OgreQuaternion.h>
00033 #include "lugre_smartptr.h"
00034 #include "lugre_robrenderable.h"
00035 #include <OgreRenderQueueListener.h>
00036 #include <OgreRectangle.h>
00037 #include <vector>
00038 #include <Ogre.h>
00039
00040 using Ogre::Vector2;
00041 using Ogre::Vector3;
00042 using Ogre::Quaternion;
00043 using Ogre::Real;
00044
00045 class lua_State;
00046
00047
00048
00049 namespace Lugre {
00050
00051
00052 class cRenderManager2D;
00053
00055 class cRenderGroup2D : public Lugre::cSmartPointable { public :
00056 cRenderGroup2D();
00057 virtual ~cRenderGroup2D();
00058 typedef std::list<cRenderGroup2D*> tChildList;
00059 typedef tChildList::iterator tChildListItor;
00060
00061
00062 inline cRenderGroup2D* GetParent () { return mpParent; }
00063 void SetParent (cRenderGroup2D* pNewParent) {
00064 if (pNewParent == mpParent) return;
00065 if (mpParent && GetAddBoundsToParent()) mpParent->MarkRelBoundsAsDirty();
00066 _RemoveFromParent_NoClipUpdate();
00067 if (pNewParent) {
00068 _AddToParent(pNewParent,pNewParent->_GetFrontPos());
00069 if (GetAddBoundsToParent()) pNewParent->MarkRelBoundsAsDirty();
00070 }
00071 UpdateClip();
00072 }
00073 void BringToFront () { if (mpParent) _AddToParent(mpParent,mpParent->_GetFrontPos()); UpdateClip(); }
00074 void SendToBack () { if (mpParent) _AddToParent(mpParent,mpParent->_GetBackPos() ); UpdateClip(); }
00075 void InsertBefore (cRenderGroup2D& pOther) { if (mpParent && pOther.mpParent == mpParent) _AddToParent(mpParent, pOther._GetPosInParent()); UpdateClip(); }
00076 void InsertAfter (cRenderGroup2D& pOther) { if (mpParent && pOther.mpParent == mpParent) _AddToParent(mpParent,++pOther._GetPosInParent()); UpdateClip(); }
00077
00078
00080 inline int GetChildListRevision () { return miChildListRevision; }
00081 inline tChildListItor ChildListBegin () { return mlChilds.begin(); }
00082 inline tChildListItor ChildListEnd () { return mlChilds.end(); }
00083
00084
00085
00086 inline Ogre::Vector3 GetDerivedPos () { return mpParent ? (mvPos + mpParent->GetDerivedPos()) : mvPos; }
00087 inline Ogre::Vector3 GetPos () { return mvPos; }
00088 inline void SetPos (const Ogre::Vector3& vPos) { mvPos = vPos; UpdateClip(); MarkParentRelBoundsAsDirty(); }
00089
00090
00091 void SetClip (const Ogre::Rectangle& rClip) { mbClipActive = true; mrClip = rClip; UpdateClip(); }
00092 void ClearClip () { mbClipActive = false; UpdateClip(); }
00093 const bool GetEffectiveClipActive () { return mbTmpClipActive; }
00094 const Ogre::Rectangle& GetEffectiveClipAbs () { return mrTmpClip; }
00095 const Ogre::Rectangle& GetEffectiveClipRel () { return mrTmpClipRel; }
00096
00097
00098 inline void SetVisible (const bool bVisible) { mbVisible = bVisible; }
00099 inline bool GetVisible () { return mbVisible; }
00100 virtual void Render (cRenderManager2D& pRenderManager2D,const Ogre::Vector3& vPos);
00101 virtual void UpdateClip ();
00102
00103
00104 inline Ogre::Rectangle& GetRelBounds () { if (mbRelBoundsDirty) UpdateRelBounds(); return mrRelBounds; }
00105 inline void MarkParentRelBoundsAsDirty () { if (mpParent && GetAddBoundsToParent()) mpParent->MarkRelBoundsAsDirty(); }
00106 inline void MarkRelBoundsAsDirty () { mbRelBoundsDirty = true; MarkParentRelBoundsAsDirty(); }
00107 inline bool GetAddBoundsToParent () { return mbAddBoundsToParent; }
00108 inline void SetAddBoundsToParent (const bool bVal) { mbAddBoundsToParent = bVal; if (mpParent) mpParent->MarkRelBoundsAsDirty(); }
00109 void CalcAbsBounds (Ogre::Rectangle& r);
00110 virtual void UpdateRelBounds ();
00111 inline void SetForcedMinSize (int w,int h) { miForcedMinW = w; miForcedMinH = h; MarkRelBoundsAsDirty(); }
00112
00113
00114 void _BoundsAddRect (float l,float t,float r,float b) {
00115 if (mbRelBoundsEmpty) {
00116 mbRelBoundsEmpty = false;
00117 mrRelBounds.left = l;
00118 mrRelBounds.top = t;
00119 mrRelBounds.right = r;
00120 mrRelBounds.bottom = b;
00121 } else {
00122 mrRelBounds.left = mymin(l,mrRelBounds.left );
00123 mrRelBounds.top = mymin(t,mrRelBounds.top );
00124 mrRelBounds.right = mymax(r,mrRelBounds.right );
00125 mrRelBounds.bottom = mymax(b,mrRelBounds.bottom);
00126 }
00127 }
00128 void _BoundsAddRectWithOffset (const Ogre::Rectangle& rRect,const Ogre::Vector3& vPos) { _BoundsAddRect( rRect.left +vPos.x,
00129 rRect.top +vPos.y,
00130 rRect.right +vPos.x,
00131 rRect.bottom+vPos.y); }
00132
00133
00134 protected:
00135
00136
00137 tChildList mlChilds;
00138 cRenderGroup2D* mpParent;
00139 tChildListItor mpSelfItor;
00140
00141
00142 Ogre::Vector3 mvPos;
00143 bool mbVisible;
00144 int miChildListRevision;
00145 int miForcedMinW;
00146 int miForcedMinH;
00147
00148
00149 bool mbClipActive;
00150 Ogre::Rectangle mrClip;
00151 bool mbTmpClipActive;
00152 Ogre::Rectangle mrTmpClip;
00153 Ogre::Rectangle mrTmpClipRel;
00154 Ogre::Rectangle mrRelBounds;
00155 bool mbRelBoundsDirty;
00156 bool mbRelBoundsEmpty;
00157 bool mbAddBoundsToParent;
00158
00159
00160
00161 inline bool _ParentClipActive () { return mpParent && mpParent->mbTmpClipActive; }
00162 inline tChildListItor _GetPosInParent () { return mpSelfItor; }
00163 inline tChildListItor _GetFrontPos () { return mlChilds.end(); }
00164 inline tChildListItor _GetBackPos () { return mlChilds.begin(); }
00165 inline void _RemoveFromParent_NoClipUpdate () {
00166 if (!mpParent) return;
00167 mpParent->mlChilds.erase(mpSelfItor);
00168 mpParent->miChildListRevision++;
00169 mpParent = 0;
00170 }
00171 bool _IsChildOf (cRenderGroup2D* pOther) {
00172 if (mpParent == pOther) return true;
00173 return mpParent ? mpParent->_IsChildOf(pOther) : false;
00174 }
00175 inline void _AddToParent (cRenderGroup2D* pParent,tChildListItor pPos) {
00176 if (!pParent) return;
00177 if (pParent->_IsChildOf(this)) return;
00178 if (mpParent) _RemoveFromParent_NoClipUpdate();
00179 if (pParent) {
00180 mpSelfItor = pParent->mlChilds.insert(pPos,this);
00181 pParent->miChildListRevision++;
00182 mpParent = pParent;
00183 }
00184 }
00185
00186 };
00187
00188
00191 class cSpriteList : public cRenderGroup2D { public :
00192 int iMaxInitializedSprite;
00193 cSpriteList (const bool bVertexBufferDynamic=false,const bool bVertexCol=false);
00194 virtual ~cSpriteList ();
00195
00196 struct cSprite {
00197 Ogre::Vector3 p;
00198 float w;
00199 float h;
00200 Ogre::Vector2 mvTexCoord0;
00201 Ogre::Vector2 mvTexCoordX;
00202 Ogre::Vector2 mvTexCoordY;
00203 Ogre::ColourValue mvCol;
00204
00205 cSprite() : w(0), h(0) {}
00206
00211 inline void Set (const float x,const float y,const float _w,const float _h,const Ogre::Vector2& uv_0,const Ogre::Vector2& uv_x,const Ogre::Vector2& uv_y,const float z=0.0,const Ogre::ColourValue& vCol=Ogre::ColourValue::White) {
00212 p.x = x;
00213 p.y = y;
00214 p.z = z;
00215 w = _w;
00216 h = _h;
00217 mvTexCoord0 = uv_0;
00218 mvTexCoordX = uv_x;
00219 mvTexCoordY = uv_y;
00220 mvCol = vCol;
00221 }
00222
00224 inline void Set (const float x,const float y,const float _w,const float _h,const Ogre::Vector2& uv_0,const float uv_w,const float uv_h,const float z=0.0,const Ogre::ColourValue& vCol=Ogre::ColourValue::White) {
00225 Set(x,y,_w,_h,uv_0,Ogre::Vector2(uv_w,0),Ogre::Vector2(0,uv_h),z,vCol);
00226 }
00227
00228 inline void SetCol (const Ogre::ColourValue& col) { mvCol = col; }
00229
00230 void WriteGeometry (cRobRenderOp& pGeometry,const bool bVertexCol);
00231 bool WriteGeometryClipped (cRobRenderOp& pGeometry,const bool bVertexCol,const Ogre::Rectangle& rClip);
00232 };
00233
00234
00235 inline cSprite& GetSprite (const int iIndex) { return mlSprites[iIndex]; }
00236 inline void ResizeList (const int iNewListSize) { return mlSprites.resize(iNewListSize); }
00237 inline int GetListSize () { return mlSprites.size(); }
00238
00239
00240 void UpdateGeometry ();
00241 void UpdateGeometryClipped (const Ogre::Rectangle& rClip);
00242 inline void MarkGeometryAsDirty () { mbGeometryDirty = true; MarkRelBoundsAsDirty(); }
00243
00244
00245 void ClearTexTransform () { if (mpTexTransformMatrix) delete mpTexTransformMatrix; mpTexTransformMatrix = 0; }
00246 void SetTexTransform (const Vector3 &position, const Vector3 &scale, const Quaternion &orientation) {
00247 if (!mpTexTransformMatrix) mpTexTransformMatrix = new Ogre::Matrix4();
00248 mpTexTransformMatrix->makeTransform(position,scale,orientation);
00249 }
00250 void SetTexTransform (const Ogre::Matrix4& pMat) {
00251 if (!mpTexTransformMatrix) mpTexTransformMatrix = new Ogre::Matrix4();
00252 *mpTexTransformMatrix = pMat;
00253 }
00254
00255
00256 virtual void UpdateRelBounds ();
00257 virtual void UpdateClip ();
00258 virtual void Render (cRenderManager2D& pRenderManager2D,const Ogre::Vector3& vPos);
00259 void SetMaterial (Ogre::MaterialPtr pMat);
00260 void SetMaterial (const char* szMatName);
00261 inline Ogre::Pass* GetMatPass () { return mpPass; }
00262
00263 inline bool GetUsesVertexCol () { return mbVertexCol; }
00264
00265
00266 static void LuaRegister (lua_State *L);
00267
00268 private:
00269 Ogre::MaterialPtr mpMat;
00270 Ogre::Pass* mpPass;
00271 bool mbGeometryClipped;
00272 bool mbGeometryDirty;
00273 bool mbVertexBufferDynamic;
00274 bool mbVertexCol;
00275 std::vector<cSprite> mlSprites;
00276 Ogre::RenderOperation mRenderOp;
00277 cRobRenderOp mRobRenderOp;
00278 Ogre::Matrix4* mpTexTransformMatrix;
00279 };
00280
00281
00284 class cRobRenderable2D : public cRenderGroup2D { public :
00285 cRobRenderable2D ();
00286 virtual ~cRobRenderable2D ();
00287
00288
00289
00290 void ClearTexTransform () { if (mpTexTransformMatrix) delete mpTexTransformMatrix; mpTexTransformMatrix = 0; }
00291 void SetTexTransform (const Vector3 &position, const Vector3 &scale, const Quaternion &orientation) {
00292 if (!mpTexTransformMatrix) mpTexTransformMatrix = new Ogre::Matrix4();
00293 mpTexTransformMatrix->makeTransform(position,scale,orientation);
00294 }
00295 void SetTexTransform (const Ogre::Matrix4& pMat) {
00296 if (!mpTexTransformMatrix) mpTexTransformMatrix = new Ogre::Matrix4();
00297 *mpTexTransformMatrix = pMat;
00298 }
00299
00300
00301 virtual void UpdateRelBounds ();
00302 virtual void Render (cRenderManager2D& pRenderManager2D,const Ogre::Vector3& vPos);
00303 void SetMaterial (Ogre::MaterialPtr pMat);
00304 void SetMaterial (const char* szMatName);
00305 inline Ogre::Pass* GetMatPass () { return mpPass; }
00306 inline cRobRenderOp* GetRobRenderOp () { return &mRobRenderOp; }
00307
00308
00309 static void LuaRegister (lua_State *L);
00310
00311 private:
00312 Ogre::MaterialPtr mpMat;
00313 Ogre::Pass* mpPass;
00314 Ogre::RenderOperation mRenderOp;
00315 cRobRenderOp mRobRenderOp;
00316 Ogre::Matrix4* mpTexTransformMatrix;
00317 };
00318
00320 class cRenderManager2D : public Ogre::RenderQueueListener, public cRenderGroup2D { public:
00321
00322 cRenderManager2D(Ogre::SceneManager* pSceneMan=0,Ogre::uint8 iQueueGroupID=0);
00323 virtual ~cRenderManager2D();
00324
00325 virtual void renderQueueStarted (Ogre::uint8 queueGroupId, const Ogre::String &invocation, bool &skipThisInvocation);
00326 virtual void renderQueueEnded (Ogre::uint8 queueGroupId, const Ogre::String &invocation, bool &repeatThisInvocation);
00327 void SetRenderState (Ogre::RenderSystem& pRenderSys);
00328
00329 inline Ogre::SceneManager* GetSceneMan () { return mpSceneMan; }
00330 inline Ogre::RenderSystem* GetRenderSystem () { return mpRenderSys; }
00331
00332 void SetRenderEvenIfOverlaysDisabled (bool render);
00333
00334 private:
00335 Ogre::RenderSystem* mpRenderSys;
00336 Ogre::SceneManager* mpSceneMan;
00337 Ogre::uint8 miQueueGroupID;
00338
00339 bool mbRenderEvenIfOverlaysDisabled;
00340 };
00341
00342
00343
00344 };
00345
00346 #endif