MainLoop.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 <engine/MainLoop.h>
00022 #include <common/Defines.h>
00023 #include <common/Logger.h>
00024 #ifndef S3D_SERVER
00025 #include <graph/OptionsDisplay.h>
00026 #include <GLEXT/GLState.h>
00027 #include <SDL/SDL.h>
00028 #endif
00029 
00030 MainLoop::MainLoop() : 
00031         exitLoop_(false), drawLogging_(false),
00032         drawTime_(0.0f), clearTime_(0.0f),
00033         totalTime_(0.0f)
00034 {
00035 
00036 }
00037 
00038 MainLoop::~MainLoop()
00039 {
00040 
00041 }
00042 
00043 void MainLoop::clear()
00044 {
00045         newMainLoops_.clear();
00046         mainLoops_.clear();
00047         exitLoop_ = false;
00048 }
00049 
00050 void MainLoop::addMainLoop(MainLoopI *MainLoop)
00051 {
00052         newMainLoops_.push_back(MainLoop);
00053 }
00054 
00055 void MainLoop::addNew()
00056 {
00057         while (!newMainLoops_.empty())
00058         {
00059                 mainLoops_.insert(newMainLoops_.front());
00060                 newMainLoops_.pop_front();
00061         }
00062 }
00063 
00064 void MainLoop::swapBuffers()
00065 {
00066 #ifndef S3D_SERVER
00067         SDL_GL_SwapBuffers();
00068 #endif
00069 }
00070 
00071 bool MainLoop::mainLoop()
00072 {
00073         float frameTime = fTimer_.getTimeDifference();
00074         totalTime_ += frameTime;
00075 
00076         while (frameTime > 0.0f)
00077         {
00078                 addNew();
00079 
00080                 float eventTime = frameTime;
00081                 simulate(eventTime);
00082                 frameTime -= eventTime;
00083         }
00084 
00085         return !exitLoop_;
00086 }
00087 
00088 void MainLoop::simulate(float frameTime)
00089 {
00090         std::set<MainLoopI *>::iterator itor;
00091         for (itor = mainLoops_.begin();
00092                 itor != mainLoops_.end();
00093                 itor++)
00094         {
00095                 MainLoopI *current = (*itor);
00096                 current->simulate(frameTime);
00097         }
00098 
00099 }
00100 
00101 void MainLoop::draw()
00102 {
00103 #ifndef S3D_SERVER
00104         dTimer_.getTimeDifference();
00105         static bool firstTime = true;
00106         if (firstTime)
00107         {
00108                 firstTime = false;
00109                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00110         }
00111 
00112         std::set<MainLoopI *>::iterator itor;
00113         for (itor = mainLoops_.begin();
00114                 itor != mainLoops_.end();
00115                 itor++)
00116         {
00117                 MainLoopI *current = (*itor);
00118                 current->draw();
00119         }
00120         drawTime_ += dTimer_.getTimeDifference();
00121 
00122         swapBuffers();
00123         {
00124                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00125                 GLboolean result;
00126                 glGetBooleanv(GL_BLEND, &result);
00127         }
00128         clearTime_ += dTimer_.getTimeDifference();
00129 
00130         if (drawLogging_ &&
00131                 totalTime_ > 10.0f)
00132         {
00133                 Logger::log(S3D::formatStringBuffer("Total %.2f = Draw %.2f + Clear %.2f",
00134                         totalTime_, drawTime_, clearTime_));
00135                 totalTime_ = 0.0f;
00136                 drawTime_ = 0.0f;
00137                 clearTime_ = 0.0f;
00138         }
00139 #endif
00140 }

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