MercuryObject.h

Go to the documentation of this file.
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, //If we are not loaded, we are probably expecting a callback
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,  //VCENTER to avoid confliction with HALIGN CENTER
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     //overloaded for MercuryScreen
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     /*  It does not currently make sense to align most objects because we do not know
00090         the width and the height of most objects. However, in the future we may have the
00091         ability to know the with and the height of objects, thus making it possible to
00092         align any object. */
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     /* Objects do not have to be removed when the parent is destroying
00099         because vector will do it for us */
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;   //renamed to keep the compiler from becomming confused
00152 
00153     /*  State is used to prevent rendering of objects who have not been loaded
00154         We don't want to use hide for this, because hide can be modified outside
00155         of this class. */
00156     State m_state;
00157     MercuryGLState m_glState;
00158     bool m_initalized;
00159 private:
00160 
00161     MercuryMaterial m_modColor; //Modifier color (from tweens and stuff)
00162     MercuryMaterial m_realColor; //Inherited and base combined.
00163 
00164     //stuff for alignment
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         //If we see a swapped pair, start swapping
00190         child = Xdeque.begin();
00191         for (++child; child != Xdeque.end(); ++child) //starts one after begin
00192         {
00193             tmp = child;
00194             --tmp; //One before child
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;//one before child
00206 
00207             //Check to see if we're in order
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  * Copyright (c) 2005-2006, Joshua Allen, Charles Lohr
00248  * All rights reserved.
00249  *
00250  * Redistribution and use in source and binary forms, with or
00251  * without modification, are permitted provided that the following
00252  * conditions are met:
00253  *  -   Redistributions of source code must retain the above
00254  *      copyright notice, this list of conditions and the following disclaimer.
00255  *  -   Redistributions in binary form must reproduce the above copyright
00256  *      notice, this list of conditions and the following disclaimer in
00257  *      the documentation and/or other materials provided with the distribution.
00258  *  -   Neither the name of the Mercury Engine nor the names of its
00259  *      contributors may be used to endorse or promote products derived from
00260  *      this software without specific prior written permission.
00261  *
00262  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00263  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00264  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00265  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
00266  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00267  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00268  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00269  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00270  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00271  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00272  */

Hosted by SourceForge.net Logo