KeosRenderTarget.cpp

Go to the documentation of this file.
00001 /*
00002  * This source file is part of KEOS (Free 3D Engine)
00003  * For the latest info, see http://www.keosengine.org/
00004  * E-mails : thierry.vouriot@keosengine.org, yeri@keosengine.org
00005  *
00006  * This program is free software; you can redistribute it and/or modify it under
00007  * the terms of the GNU Lesser General Public License as published by the Free Software
00008  * Foundation; either version 2 of the License, or (at your option) any later
00009  * version.
00010  *
00011  * This program is distributed in the hope that it will be useful, but WITHOUT
00012  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00013  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Lesser General Public License along with
00016  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00017  * Place - Suite 330, Boston, MA 02111-1307, USA, or go to
00018  * http://www.gnu.org/copyleft/lesser.txt.
00019  *
00020  */
00021 #include "KeosRenderTarget.h"
00022 #include "KeosRoot.h"
00023 #include "KeosTimer.h"
00024 #include "KeosLogger.h"
00025 
00026 namespace Keos
00027 {
00028   //=======================================================================
00029   // IRenderSystem implementation
00030   //=======================================================================
00031 
00032   //-----------------------------------------------------------------------
00033   IRenderTarget::IRenderTarget()
00034   {
00035     m_pRenderApp = NULL;
00036 
00037     // Default to no stats display
00038     m_bActive = true;
00039     m_bAutoUpdate = true;
00040 
00041     m_pTimer = CRoot::Instance().GetTimer();
00042     ResetStatistics();
00043   }
00044 
00045   //-----------------------------------------------------------------------
00046   IRenderTarget::~IRenderTarget()
00047   {
00048     if (m_pRenderApp && m_pRenderApp->IsInit())
00049     {
00050       m_pRenderApp->Release();
00051     }
00052     ILogger::Log("<-- Destroy RenderTarget '%s'", m_strName.c_str());
00053   }
00054 
00055   //-----------------------------------------------------------------------
00056   void IRenderTarget::GetMetrics(uint& nWidth, uint& nHeight, uint& nColourDepth)
00057   {
00058     nWidth = m_nWidth;
00059     nHeight = m_nHeight;
00060     nColourDepth = m_nColourDepth;
00061   }
00062 
00063   //-----------------------------------------------------------------------
00064   void IRenderTarget::Init(void)
00065   {
00066     ILogger::Log("--> Init RenderTarget '%s'", m_strName.c_str());
00067     m_pRenderApp->Init();
00068     m_pRenderApp->SetInit(true);
00069   }
00070 
00071   //-----------------------------------------------------------------------
00072   void IRenderTarget::Update(void)
00073   {
00074     m_Stats.triangleCount = 0;
00075 
00076     // Update statistics (always on top)
00077     UpdateStats();
00078 
00079     m_pRenderApp->Update();
00080     m_pRenderApp->Render();
00081   }
00082 
00083   //-----------------------------------------------------------------------
00084   void IRenderTarget::ResetStatistics(void)
00085   {
00086     m_Stats.avgFPS = 0.0;
00087     m_Stats.bestFPS = 0.0;
00088     m_Stats.lastFPS = 0.0;
00089     m_Stats.worstFPS = 999.0;
00090     m_Stats.triangleCount = 0;
00091     m_Stats.bestFrameTime = 999999;
00092     m_Stats.worstFrameTime = 0;
00093     m_Stats.lastFrameTime = 0;
00094 
00095     m_nLastTime = m_pTimer->GetMilliseconds();
00096     m_nLastSecond = m_nLastTime;
00097     m_nFrameCount = 0;
00098   }
00099 
00100   //-----------------------------------------------------------------------
00101   void IRenderTarget::UpdateStats(void)
00102   {
00103     ++m_nFrameCount;
00104     unsigned long thisTime = m_pTimer->GetMilliseconds();
00105 
00106     // check frame time
00107     unsigned long frameTime = thisTime - m_nLastTime ;
00108     m_Stats.lastFrameTime = frameTime;
00109     m_nLastTime = thisTime ;
00110 
00111     m_Stats.bestFrameTime = std::min(m_Stats.bestFrameTime, frameTime);
00112     m_Stats.worstFrameTime = std::max(m_Stats.worstFrameTime, frameTime);
00113 
00114     // check if new second (update only once per second)
00115     if (thisTime - m_nLastSecond > 1000)
00116     {
00117       // new second - not 100% precise
00118       m_Stats.lastFPS = (float)m_nFrameCount / (float)(thisTime - m_nLastSecond) * 1000.0;
00119 
00120       if (m_Stats.avgFPS == 0)
00121         m_Stats.avgFPS = m_Stats.lastFPS;
00122       else
00123         m_Stats.avgFPS = (m_Stats.avgFPS + m_Stats.lastFPS) / 2; // not strictly correct, but good enough
00124 
00125       m_Stats.bestFPS = std::max(m_Stats.bestFPS, m_Stats.lastFPS);
00126       m_Stats.worstFPS = std::min(m_Stats.worstFPS, m_Stats.lastFPS);
00127 
00128       m_nLastSecond = thisTime ;
00129       m_nFrameCount  = 0;
00130 
00131     }
00132   }
00133 
00134 } // namespace Keos

Generated on Fri Mar 9 14:29:03 2007 for Keos by  doxygen 1.5.1-p1