MercuryScreen.cpp

Go to the documentation of this file.
00001 #include "global.h"
00002 #include "MercuryScreen.h"
00003 #include "MercuryModel.h"
00004 #include "MercuryScreenManager.h"
00005 #include "MercuryDisplay.h"
00006 #include "MercuryLog.h"
00007 #include "MercuryTheme.h"
00008 #include "MercuryTextureManager.h"
00009 #include "MercuryObjectFactory.h"
00010 
00011 #define     MC_DISPLAYRESIZED       1       //convention: MESSAGE CODE  
00012 
00013 std::map< MString, NewScreenFunction >  * ToMakeScreens = NULL;
00014 
00015 void RegisterMercuryScreen( const MString& sClassName, NewScreenFunction pfn )
00016 {
00017     if ( ToMakeScreens == NULL )
00018         ToMakeScreens = new std::map< MString, NewScreenFunction >;
00019     (*ToMakeScreens)[sClassName] = pfn;
00020 }
00021 
00022 MercuryScreen::MercuryScreen() : MercuryObject()
00023 {
00024     m_pBackground = NULL;
00025     m_pCamera = NULL;
00026 }
00027 
00028 MercuryScreen::MercuryScreen( const MercuryScreen & in ) : MercuryObject()
00029 {
00030     m_pBackground = NULL;
00031     m_pCamera = NULL;
00032 }
00033 
00034 MercuryScreen::MercuryScreen( const MString &name ) : MercuryObject( name )
00035 {
00036     m_pBackground = NULL;
00037     m_pCamera = NULL;
00038 }
00039 
00040 MercuryScreen::~MercuryScreen()
00041 {
00042     SAFE_DELETE(m_pBackground);
00043     SAFE_DELETE(m_pCamera);
00044 }
00045 
00046 void MercuryScreen::Init()
00047 {
00048     m_useLighting = THEME.GetMetricB( GetName(), "UseLighting", true );
00049     MercuryObject::Init();
00050     //No idea why this isn't always set.  I think it should always be set.
00051     SetDiffuse( MercuryColor(1,1,1,1 ) );
00052     CreateProjections();
00053     m_PerspObjects.Init();
00054     m_OrthoObjects.Init();
00055     m_PerspObjects.SetDrawOrder(0);
00056     m_OrthoObjects.SetDrawOrder(1);
00057 
00058     //XXX why can't I disable depth write
00059     m_OrthoObjects.GetGLState().Disable(MGLS_LIGHTING | MGLS_DEPTHTEST );//| MGLS_DEPTHWRITE);
00060     AddObject(&m_PerspObjects);
00061     AddObject(&m_OrthoObjects);
00062     RegisterMessage( MC_DISPLAYRESIZED, "DisplayResized" );
00063 }
00064 
00065 void MercuryScreen::CreateProjections()
00066 {
00067     DisplayDimensions d = DISPLAY->GetBasedDimensions();
00068     m_perspProjection = Perspective(45.0f,(float)(DISPLAY->GetWindow()->GetWidth())/(float)(DISPLAY->GetWindow()->GetHeight()),1.0f,1500.0f);
00069     m_orthoProjection = Ortho(0.0f, float(d.width), float(d.height), 0.0f, 0.0f, -1.0f);
00070 }
00071 
00072 void MercuryScreen::Message( int Message, PStack & data, const MString & name )
00073 {
00074     MercuryObject::Message(Message, data, name);
00075     switch ( Message ) 
00076     {
00077     case MC_DISPLAYRESIZED:
00078         CreateProjections();
00079         break;
00080     };
00081 }
00082 
00083 void MercuryScreen::Update(const float dTime)
00084 {
00085     ASSERT_M(m_initalized,"Object '" + GetName() + "'not initialized.");
00086 
00087     if ((m_state == UNLOADED) || (dTime == 0))
00088         return;
00089 
00090     if ( m_bMarkedForDestroy )
00091     {
00092         ((MercuryObject*)m_parentObject)->RemoveObject( this );
00093         return;
00094     }
00095 
00096     Tweening.Update( dTime );
00097     WorldStack.Push();
00098     Prerender();
00099 
00100     UpdatePerspectiveObjects(dTime);
00101     UpdateOrthoObjects(dTime);
00102 
00103     WorldStack.Pop();
00104 }
00105 
00106 void MercuryScreen::Render()
00107 {
00108     RenderPerspectiveObjects();
00109     RenderOrthoObjects();
00110 }
00111 
00112 void MercuryScreen::RenderPerspectiveObjects()
00113 {
00114     DISPLAY->SetProjection(m_perspProjection);
00115 
00116     if (m_pCamera != NULL)
00117         m_pCamera->Render();
00118 
00119     if (m_pBackground != NULL)
00120         m_pBackground->Render();
00121 
00122     if (m_useLighting)
00123         DISPLAY->EnableGLState(MGLS_LIGHTING);
00124     else
00125         DISPLAY->DisableGLState(MGLS_LIGHTING);
00126 
00127     m_PerspObjects.Render();
00128     DISPLAY->RenderTranslucentObject();
00129 }
00130 
00131 void MercuryScreen::RenderOrthoObjects()
00132 {
00133     DISPLAY->SetProjection(m_orthoProjection);
00134     DISPLAY->GetView().Identity();
00135 
00136     m_OrthoObjects.Render();
00137     DISPLAY->RenderTranslucentObject();
00138 }
00139 
00140 
00141 void MercuryScreen::UpdatePerspectiveObjects(float dTime)
00142 {
00143     DISPLAY->SetProjection(m_perspProjection); //needed for culling
00144 
00145     if (m_pCamera != NULL)
00146         m_pCamera->Update(dTime);
00147 
00148     if (m_pBackground != NULL)
00149         m_pBackground->Update(dTime);
00150 
00151 //  Draw(); //sets camera and background
00152 
00153     if (m_useLighting)
00154         DISPLAY->EnableGLState(MGLS_LIGHTING);
00155     else
00156         DISPLAY->DisableGLState(MGLS_LIGHTING);
00157 
00158     m_PerspObjects.Update(dTime);
00159 }
00160 
00161 void MercuryScreen::UpdateOrthoObjects(float dTime)
00162 {
00163     //needed for culling
00164     DISPLAY->SetProjection(m_orthoProjection);
00165     DISPLAY->GetView().Identity();
00166 
00167     m_OrthoObjects.Update(dTime);
00168 }
00169 
00170 //void MercuryScreen::AddObject(MercuryObject* object, bool bParentDelete = false);
00171 bool MercuryScreen::RemoveObject(MercuryObject* object, bool bAllowDelete)
00172 {
00173     MDequeIterator<ParentHoldPair> i;
00174 
00175     if ( m_PerspObjects.RemoveObject(object, bAllowDelete) )
00176         return true;
00177     if ( m_OrthoObjects.RemoveObject(object, bAllowDelete) )
00178         return true;
00179     return false;
00180 }
00181 
00182 void MercuryScreen::AddPerspObject(MercuryObject* object, bool bParentDelete)
00183 {
00184     m_PerspObjects.AddObject(object, bParentDelete);
00185 }
00186 
00187 void MercuryScreen::AddOrthoObject(MercuryObject* object, bool bParentDelete)
00188 {
00189     m_OrthoObjects.AddObject(object, bParentDelete);
00190 }
00191 
00192 MercuryObject * MercuryScreen::Spawn( const MString & sClass, const MString & sName, PROJECTIONTYPE projection )
00193 {
00194     MercuryObject * pObject = MercuryObjectFactory::GetInstance().GenerateObject( sClass, sName );
00195     if ( pObject == NULL )
00196         return false;
00197 
00198     pObject->Init();
00199     
00200     if (projection == PERSPECTIVE)
00201         AddPerspObject(pObject, true);
00202     else
00203         AddOrthoObject(pObject, true);
00204 
00205     return pObject;
00206 }
00207 
00208 /* 
00209  * Copyright (c) 2005-2006, Joshua Allen
00210  * All rights reserved.
00211  *
00212  * Redistribution and use in source and binary forms, with or
00213  * without modification, are permitted provided that the following
00214  * conditions are met:
00215  *  -   Redistributions of source code must retain the above
00216  *      copyright notice, this list of conditions and the following disclaimer.
00217  *  -   Redistributions in binary form must reproduce the above copyright
00218  *      notice, this list of conditions and the following disclaimer in
00219  *      the documentation and/or other materials provided with the distribution.
00220  *  -   Neither the name of the Mercury Engine nor the names of its
00221  *      contributors may be used to endorse or promote products derived from
00222  *      this software without specific prior written permission.
00223  *
00224  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00225  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00226  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00227  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
00228  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00229  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00230  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00231  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00232  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00233  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00234  */

Hosted by SourceForge.net Logo