00001 #ifndef MERCURYOBJECT_H
00002 #define MERCURYOBJECT_H
00003
00004 #include "MercuryObjectCommands.h"
00005 #include "MercuryMessages.h"
00006 #include "MercuryDisplayTypes.h"
00007 #include "MercuryMaterial.h"
00008 #include "MercuryObjectBase.h"
00009 #include "MercuryUtil.h"
00010 #include "MercuryGLState.h"
00011
00012 class MercuryPoly;
00013
00014 enum State
00015 {
00016 UNLOADED = 0,
00017 LOADED
00018 };
00019
00020 enum LightState
00021 {
00022 LS_IGNORE = 0,
00023 LS_ENABLE,
00024 LS_DISABLE
00025 };
00026
00028 class MercuryObject : public MercuryObjectBase, public MercuryMessageHandler
00029 {
00030 public:
00032 struct ParentHoldPair
00033 {
00034 ParentHoldPair(MercuryObject* o = NULL) : pObject(o) {};
00035 inline bool operator<(const ParentHoldPair& r) const { return *pObject < *(r.pObject); }
00036 inline bool operator>=(const ParentHoldPair& r) const { return *pObject >= *(r.pObject); }
00037
00038 MercuryObject * pObject;
00039 bool bAutoDelete;
00040 };
00041
00042 enum HALIGN
00043 {
00044 LEFT = 0,
00045 CENTER,
00046 RIGHT
00047 };
00048 enum VALIGN
00049 {
00050 TOP = 0,
00051 VCENTER,
00052 BOTTOM
00053 };
00054 MercuryObject();
00055 MercuryObject( const MString & name );
00056 virtual ~MercuryObject();
00057 virtual void Init();
00058
00059 void CalculateRealColor();
00060 void SetName(const MString& name);
00061
00062
00063 virtual void SortDrawOrder();
00064 virtual void AddObject(MercuryObject* object, bool bParentDelete = false);
00065 virtual bool RemoveObject(MercuryObject* object, bool bAllowDelete = true );
00066 virtual bool ReplaceObject(MercuryObject* object, MercuryObject* newobject);
00067
00068 inline void Destroy() { m_bMarkedForDestroy = true; }
00069 inline void SetMaterial(MercuryMaterial* material) { ASSERT(material); m_pMaterial = material; CalculateRealColor();}
00070 inline void SetDiffuse( const MercuryColor & c ) { m_modColor.m_diffuse = c; CalculateRealColor(); }
00071 inline void SetDiffusef( float r, float g, float b, float a ) { m_modColor.m_diffuse = MercuryColor(r,g,b,a); }
00072 inline void SetEmissive( const MercuryColor & c ) { m_modColor.m_emissive = c; CalculateRealColor(); }
00073 inline void SetEmissivef( float r, float g, float b, float a ) { m_modColor.m_emissive = MercuryColor(r,g,b,a); }
00074 inline void SetSpecular( const MercuryColor & c ) { m_modColor.m_specular = c; CalculateRealColor(); }
00075 inline void SetSpecularf( float r, float g, float b, float a ) { m_modColor.m_specular = MercuryColor(r,g,b,a); }
00076 inline void SetAmbient( const MercuryColor & c ) { m_modColor.m_ambient = c; CalculateRealColor(); }
00077 inline void SetAmbientf( float r, float g, float b, float a ) { m_modColor.m_ambient = MercuryColor(r,g,b,a); }
00078 inline const MString& GetName() const { return m_name; }
00079 inline const MercuryMaterial* GetMaterial() const { return m_pMaterial; }
00080 inline const MercuryMaterial& GetFinalMaterial() const { return m_finalMaterial; }
00081 inline const MercuryColor& GetDiffuse( ) { return m_modColor.m_diffuse; }
00082 inline const MercuryColor& GetEmissive( ) { return m_modColor.m_emissive; }
00083 inline const MercuryColor& GetSpecular( ) { return m_modColor.m_specular; }
00084 inline const MercuryColor& GetAmbient( ) { return m_modColor.m_ambient; }
00085 void GetDiffusef( float & r, float & g, float & b, float & a );
00086 virtual MercuryObject* Spawn( const MString & sClass, const MString & sName, PROJECTIONTYPE projection = PERSPECTIVE );
00087 unsigned int GetNumChildren() const { return m_objects.size(); }
00088
00089
00090
00091
00092
00093 virtual void SetHAlignment(HALIGN alignment) { m_halignment = alignment; }
00094 inline HALIGN GetHAlignment() const { return m_halignment; }
00095 virtual void SetVAlignment(VALIGN alignment) { m_valignment = alignment; }
00096 inline VALIGN GetVAlignment() const { return m_valignment; }
00097
00098
00099
00100
00102
00106 virtual void Prerender();
00107 virtual void Render();
00108 virtual void CustomRender();
00109 void ComputeCull();
00110
00111 inline bool IsCulled() const { return m_culled; }
00112
00114 virtual void Update( const float dTime );
00116 virtual void SetDrawOrder(int order);
00117
00119 MercuryTweenState Tweening;
00120 MDeque<ParentHoldPair> m_objects;
00121
00123 virtual void Draw() {};
00124 CLASS_RTTI( MercuryObject, MercuryMessageHandler );
00125
00128 virtual void Clonize() { m_bClone = true; Tweening.AttachToObject( this ); }
00129
00130 inline bool IsInitalized() const { return m_initalized; }
00131
00132 inline float GetScaledRadius() const { return m_fVisRadius*GetScale().GetBiggestElement(); }
00133 inline State GetState() const { return m_state; }
00134 inline MercuryGLState& GetGLState() { return m_glState; }
00135
00136 void ComputeInheritedGLState();
00137 inline MercuryGLState GetInheritedGLState() { return m_inheretedGLState; }
00138
00139 protected:
00140 bool m_bClone;
00141 bool m_bMarkedForDestroy;
00142 unsigned int m_polys;
00143 MString m_name;
00144
00145 int m_width, m_height;
00146
00147 bool m_drawable;
00148 bool m_culled;
00149
00150 MercuryMaterial m_finalMaterial;
00151 MercuryMaterial* m_pMaterial;
00152
00153
00154
00155
00156 State m_state;
00157 MercuryGLState m_glState;
00158 bool m_initalized;
00159 private:
00160
00161 MercuryMaterial m_modColor;
00162 MercuryMaterial m_realColor;
00163
00164
00165 HALIGN m_halignment;
00166 VALIGN m_valignment;
00167
00168 MercuryGLState m_inheretedGLState;
00169 };
00170
00171 template<typename T>
00172 void TSortDrawOrder(MDeque<T>& Xdeque)
00173 {
00181 MDequeIterator< T > child = Xdeque.begin();
00182 MDequeIterator< T > tmp;
00183
00184 if ( Xdeque.empty() )
00185 return;
00186
00187 while ( true )
00188 {
00189
00190 child = Xdeque.begin();
00191 for (++child; child != Xdeque.end(); ++child)
00192 {
00193 tmp = child;
00194 --tmp;
00195 if ( (*child) < (*tmp) )
00196 break;
00197 }
00198
00199 if ( child == Xdeque.end() )
00200 return;
00201
00202 for ( ; child != Xdeque.begin(); --child )
00203 {
00204 tmp = child;
00205 --tmp;
00206
00207
00208 if ( (*child) >= (*tmp) )
00209 break;
00210
00211 Xdeque.swap( child, tmp );
00212 child = tmp;
00213 }
00214 }
00215 }
00216
00217
00218 #define FOREACH_Object( x, obj ) for( MDequeIterator<MercuryObject::ParentHoldPair> obj = x.begin(); obj != x.end(); ++obj)
00219 #define FOREACH_CONST_Object( x, obj ) for( MDequeIterator<MercuryObject::ParentHoldPair> obj = x.begin(); obj != x.end(); ++obj)
00220
00222
00224 class MercuryObjectRegister
00225 {
00226 public:
00227 MercuryObjectRegister();
00228 void Register( MercuryObject * obj, const MString & name );
00229 void Unregister( const MString & name );
00230 MercuryObject * GetObjectFromName( const MString & name );
00231 static void SendCompiledMessage( const MString &message, void * data, PStack & info );
00232 static int GetCountOfObject( const MString & name );
00233 void ShutDown();
00234 private:
00235 const int NOTFOUND;
00236 static MHash< MercuryObject * > m_mObjects;
00237 static MHash< int > m_mObjectCounts;
00238 };
00239
00240 extern MercuryObjectRegister OBJECTREGISTER;
00241
00242 long HGEXPORT GetObjectByName( const char * sName );
00243
00244 #endif
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272