Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members

# FPSController.cpp

Go to the documentation of this file.

    00001 //------------------------------------------------------------------------------
    00002 // Lamp : Open source game middleware
    00003 // Copyright (C) 2004  Junpei Ohtani ( Email : junpee@users.sourceforge.jp )
    00004 //
    00005 // This library is free software; you can redistribute it and/or
    00006 // modify it under the terms of the GNU Lesser General Public
    00007 // License as published by the Free Software Foundation; either
    00008 // version 2.1 of the License, or (at your option) any later version.
    00009 //
    00010 // This library is distributed in the hope that it will be useful,
    00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
    00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00013 // Lesser General Public License for more details.
    00014 //
    00015 // You should have received a copy of the GNU Lesser General Public
    00016 // License along with this library; if not, write to the Free Software
    00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    00018 //------------------------------------------------------------------------------
    00019
    00020 /** @file
    00021  * FPSコントローラ実装
    00022  * @author Junpee
    00023  */
    00024
    00025 #include "LampBasic.h"
    00026 #include "Core/Utility/FPSController.h"
    00027 #include "Core/Thread/Thread.h"
    00028 #include "Core/Thread/SynchronizedBlock.h"
    00029
    00030 namespace Lamp{
    00031
    00032 /// 60FPS
    00033 const float FPSController::interval60FPS = 16.6666666f;
    00034 /// 30FPS
    00035 const float FPSController::interval30FPS = 33.3333333f;
    00036
    00037 //------------------------------------------------------------------------------
    00038 // コンストラクタ
    00039 FPSController::FPSController(float targetInterval){
    00040     setTargetInterval(targetInterval);
    00041     tick_ = Timer::getTick();
    00042 }
    00043 //------------------------------------------------------------------------------
    00044 // デストラクタ
    00045 FPSController::~FPSController(){
    00046     Assert(backgroundThreads_.getCount() == 0);
    00047 }
    00048 //------------------------------------------------------------------------------
    00049 // FPSを維持するためにカレントスレッドをsleepする
    00050 float FPSController::sleep(){
    00051     // sleep時間の算出
    00052     intervalTime_ = Timer::getInterval(tick_);
    00053     tick_ = Timer::getTick();
    00054     sleepTime_ += (targetInterval_ - intervalTime_);
    00055     if(sleepTime_ < 0.f){ sleepTime_ = 0.f; }
    00056     else if(sleepTime_ > targetInterval_){ sleepTime_ = targetInterval_; }
    00057     // インターバルスレッドの再開
    00058     bool hasBackgroundTask = false;
    00059     {
    00060         SynchronizedBlock synchronizedBlock(criticalSection_);
    00061         if(backgroundThreads_.getCount() > 0){
    00062             hasBackgroundTask = true;
    00063             backgroundThreads_[0]->resume();
    00064         }
    00065     }
    00066     // バックグラウンドタスクがある場合は最低1msはsleepする
    00067     u_int correctedSleepTime = (u_int)sleepTime_;
    00068     if(hasBackgroundTask && (correctedSleepTime == 0)){
    00069         correctedSleepTime = 1;
    00070     }
    00071     Thread::sleep(correctedSleepTime);
    00072     // インターバルスレッドの停止
    00073     {
    00074         SynchronizedBlock synchronizedBlock(criticalSection_);
    00075         if(hasBackgroundTask){
    00076             Thread* backgroundThread = backgroundThreads_[0];
    00077             if(backgroundThread->isFinished()){
    00078                 backgroundThreads_.remove(0);
    00079             }else{
    00080                 backgroundThread->suspend();
    00081             }
    00082         }
    00083     }
    00084     return intervalTime_;
    00085 }
    00086 //------------------------------------------------------------------------------
    00087 // バックグラウンドスレッドの登録
    00088 void FPSController::registerBackgroundThread(Thread* backgroundThread){
    00089     SynchronizedBlock synchronizedBlock(criticalSection_);
    00090     Assert(!backgroundThread->isFinished());
    00091     backgroundThreads_.pushBack(backgroundThread);
    00092 }
    00093 //------------------------------------------------------------------------------
    00094 // バックグラウンドスレッド数の取得
    00095 int FPSController::getBackgroundThreadCount(){
    00096     SynchronizedBlock synchronizedBlock(criticalSection_);
    00097     return backgroundThreads_.getCount();
    00098 }
    00099 //------------------------------------------------------------------------------
    00100 // バックグラウンドスレッド数の取得
    00101 Thread* FPSController::getBackgroundThread(int index){
    00102     SynchronizedBlock synchronizedBlock(criticalSection_);
    00103     return backgroundThreads_[index];
    00104 }
    00105 //------------------------------------------------------------------------------
    00106 // 文字列への変換
    00107 String FPSController::toString() const{
    00108     String result;
    00109     result.format("interval %5.2f  sleep %5.2f  processing %5.2f%%",
    00110         getIntervalTime(), getSleepTime(),
    00111         getProcessingTime() / getIntervalTime() * 100.f);
    00112     return result;
    00113 }
    00114 //------------------------------------------------------------------------------
    00115 } // End of namespace Lamp
    00116 //------------------------------------------------------------------------------