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 * タイマヘッダ 00022 * @author Junpee 00023 */ 00024 00025 #ifndef Timer_H_ 00026 #define Timer_H_ 00027 00028 namespace Lamp{ 00029 00030 //------------------------------------------------------------------------------ 00031 /** 00032 * タイマ 00033 * 00034 * 時間間隔を計測します。<br> 00035 * タイマを使用するためには、まずシステムの初期化時に Timer::initialize()を 00036 * 呼んでください。その後時間を計測したい処理の前で Timer::getTick()を呼び、 00037 * 計測したい処理の後で Timer::getInterval() を呼んでください。 00038 * <pre> 00039 * サンプルコード 00040 * 00041 * // 初期化 00042 * Timer::initialize(); 00043 * // 計測開始時間の取得 00044 * Timer::Tick startTime = Timer::getTick(); 00045 * // 時間を計測する処理 00046 * targetMethod1(); 00047 * // 時間間隔の取得 00048 * float msTime = Timer::getInterval(startTime); 00049 * // デバッグ出力に表示 00050 * DebugOut("targetMethod1() using %fms\n", msTime); 00051 * </pre> 00052 */ 00053 //------------------------------------------------------------------------------ 00054 class Timer{ 00055 public: 00056 /** 00057 * Timer::getTick()を呼び出した時点での時間 00058 * 00059 * Timer::getInterval()により時間の間隔を取得できる。 00060 * @see Timer 00061 */ 00062 class Tick{ 00063 friend class Timer; 00064 public: 00065 /** 00066 * コンストラクタ 00067 */ 00068 Tick(){ tick = 0; } 00069 00070 /** 00071 * 値の取得 00072 * @return tickの下位32ビット値 00073 */ 00074 u_int getValue(){ return (u_int)(tick & 0xffffffff); } 00075 00076 private: 00077 // 時間 00078 long long tick; 00079 00080 }; 00081 00082 /** 00083 * タイマを初期化します 00084 * 00085 * システムの初期化時に一度呼び出してください。 00086 */ 00087 static void initialize(); 00088 00089 /** 00090 * 時間の取得 00091 * @return getTick()を呼んだ時間。 00092 * @see Tick 00093 */ 00094 static Tick getTick(); 00095 00096 /** 00097 * 時間間隔の取得 00098 * 00099 * 以前に取得した時間からの時間間隔をミリ秒単位で取得します。 00100 * @param previousTick 以前に取得した時間 00101 * @return ミリ秒単位での時間間隔 00102 */ 00103 static float getInterval(const Tick& previousTick); 00104 00105 /** 00106 * 時間間隔の取得 00107 * 00108 * 以前に取得した時間から以後に取得した時間の時間間隔をミリ秒単位で取得します。 00109 * @param previousTick 以前に取得した時間 00110 * @param afterTick 以後に取得した時間 00111 * @return ミリ秒単位での時間間隔 00112 */ 00113 static float getInterval(const Tick& previousTick, const Tick& afterTick); 00114 00115 private: 00116 // 1msあたりのクロック数 00117 static float tickPerMillisecond_; 00118 // 初期化済みフラグ 00119 static bool initialized_; 00120 }; 00121 00122 //------------------------------------------------------------------------------ 00123 } // End of namespace Lamp 00124 #endif // End of Timer_H_ 00125 //------------------------------------------------------------------------------