Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

# 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             m_pRenderApp->Release();
    00050         }
    00051         ILogger::Log("<-- Destroy RenderTarget '%s'", m_strName.c_str());
    00052     }
    00053
    00054     //-----------------------------------------------------------------------
    00055     void IRenderTarget::GetMetrics(uint& nWidth, uint& nHeight, uint& nColourDepth)
    00056     {
    00057         nWidth = m_nWidth;
    00058         nHeight = m_nHeight;
    00059         nColourDepth = m_nColourDepth;
    00060     }
    00061
    00062     //-----------------------------------------------------------------------
    00063     void IRenderTarget::Init(void)
    00064     {
    00065         ILogger::Log("--> Init RenderTarget '%s'", m_strName.c_str());
    00066         m_pRenderApp->Init();
    00067         m_pRenderApp->SetInit(true);
    00068     }
    00069
    00070     //-----------------------------------------------------------------------
    00071     void IRenderTarget::Update(void)
    00072     {
    00073         m_Stats.triangleCount = 0;
    00074
    00075         // Update statistics (always on top)
    00076         UpdateStats();
    00077
    00078         m_pRenderApp->Update();
    00079         m_pRenderApp->Render();
    00080     }
    00081
    00082     //-----------------------------------------------------------------------
    00083     void IRenderTarget::ResetStatistics(void)
    00084     {
    00085         m_Stats.avgFPS = 0.0;
    00086         m_Stats.bestFPS = 0.0;
    00087         m_Stats.lastFPS = 0.0;
    00088         m_Stats.worstFPS = 999.0;
    00089         m_Stats.triangleCount = 0;
    00090         m_Stats.bestFrameTime = 999999;
    00091         m_Stats.worstFrameTime = 0;
    00092
    00093         m_nLastTime = m_pTimer->GetMilliseconds();
    00094         m_nLastSecond = m_nLastTime;
    00095         m_nFrameCount = 0;
    00096     }
    00097
    00098     //-----------------------------------------------------------------------
    00099     void IRenderTarget::UpdateStats(void)
    00100     {
    00101         ++m_nFrameCount;
    00102         unsigned long thisTime = m_pTimer->GetMilliseconds();
    00103
    00104         // check frame time
    00105         unsigned long frameTime = thisTime - m_nLastTime ;
    00106         m_nLastTime = thisTime ;
    00107
    00108         m_Stats.bestFrameTime = std::min(m_Stats.bestFrameTime, frameTime);
    00109         m_Stats.worstFrameTime = std::max(m_Stats.worstFrameTime, frameTime);
    00110
    00111         // check if new second (update only once per second)
    00112         if (thisTime - m_nLastSecond > 1000)
    00113         {
    00114             // new second - not 100% precise
    00115             m_Stats.lastFPS = (float)m_nFrameCount / (float)(thisTime - m_nLastSecond) * 1000.0;
    00116
    00117             if (m_Stats.avgFPS == 0)
    00118                 m_Stats.avgFPS = m_Stats.lastFPS;
    00119             else
    00120                 m_Stats.avgFPS = (m_Stats.avgFPS + m_Stats.lastFPS) / 2; // not strictly correct, but good enough
    00121
    00122             m_Stats.bestFPS = std::max(m_Stats.bestFPS, m_Stats.lastFPS);
    00123             m_Stats.worstFPS = std::min(m_Stats.worstFPS, m_Stats.lastFPS);
    00124
    00125             m_nLastSecond = thisTime ;
    00126             m_nFrameCount  = 0;
    00127
    00128         }
    00129     }
    00130
    00131 } // namespace Keos