FrameTimer.cpp

Go to the documentation of this file.
00001 ////////////////////////////////////////////////////////////////////////////////
00002 //    Scorched3D (c) 2000-2009
00003 //
00004 //    This file is part of Scorched3D.
00005 //
00006 //    Scorched3D is free software; you can redistribute it and/or modify
00007 //    it under the terms of the GNU General Public License as published by
00008 //    the Free Software Foundation; either version 2 of the License, or
00009 //    (at your option) any later version.
00010 //
00011 //    Scorched3D is distributed in the hope that it will be useful,
00012 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //    GNU General Public License for more details.
00015 //
00016 //    You should have received a copy of the GNU General Public License
00017 //    along with Scorched3D; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 ////////////////////////////////////////////////////////////////////////////////
00020 
00021 #include <stdio.h>
00022 #include <graph/FrameTimer.h>
00023 #include <client/ClientChannelManager.h>
00024 #include <graph/OptionsDisplay.h>
00025 #include <GLEXT/GLInfo.h>
00026 #include <GLEXT/GLTexture.h>
00027 #include <lang/LangResource.h>
00028 
00029 FrameTimer *FrameTimer::instance_ = 0;
00030 
00031 FrameTimer *FrameTimer::instance()
00032 {
00033         if (!instance_)
00034         {
00035                 instance_ = new FrameTimer;
00036         }
00037 
00038         return instance_;
00039 }
00040 
00041 FrameTimer::FrameTimer() : 
00042         GameStateI("FrameTimer"),
00043         totalTime_(0.0f), frameCount_(0), 
00044         lastStateCount_(0), lastTris_(0), lastTextureSets_(0),
00045         fps_(0.0f)
00046 {
00047 
00048 }
00049 
00050 FrameTimer::~FrameTimer()
00051 {
00052 
00053 }
00054 
00055 void FrameTimer::draw(const unsigned state)
00056 {
00057         frameCount_++;
00058 
00059         lastStateCount_ = GLState::getStateSwitches();
00060         GLState::resetStateSwitches();
00061 
00062         lastTris_ = GLInfo::getNoTriangles();
00063         GLInfo::resetNoTriangles();
00064         
00065         lastTextureSets_ = GLTexture::getTextureSets();
00066         GLTexture::resetTextureSets();
00067 }
00068 
00069 void FrameTimer::simulate(const unsigned state, float frameTime)
00070 {
00071         totalTime_ += frameTime;
00072         if (totalTime_ > 5.0f)
00073         {
00074                 float timeTaken = frameClock_.getTimeDifference();
00075                 fps_ = float(frameCount_) / timeTaken;
00076                 totalTime_ = 0.0f;
00077                 frameCount_ = 0;
00078 
00079                 if (OptionsDisplay::instance()->getFrameTimer())
00080                 {
00081                         ChannelText chText("info", 
00082                                 LANG_RESOURCE_1(
00083                                         "X_FRAMES_PER_SECOND", 
00084                                         "{0} frames per second.", 
00085                                         S3D::formatStringBuffer("%.2f", fps_)));
00086                         chText.setFlags(ChannelText::eNoLog | ChannelText::eNoSound);
00087                         ClientChannelManager::instance()->showText(chText);
00088                 }
00089         }
00090 }

Generated on Mon Feb 16 15:14:46 2009 for Scorched3D by  doxygen 1.5.3