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

BufferedInput.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  * バッファ入力実装
00022  * @author Junpee
00023  */
00024 
00025 #include "LampBasic.h"
00026 #include "Input/System/BufferedInput.h"
00027 #include "Core/Thread/SynchronizedBlock.h"
00028 #include "Input/System/LampInput.h"
00029 #include "Input/Keyboard/Keyboard.h"
00030 #include "Input/Keyboard/KeyboardDevice.h"
00031 #include "Input/Mouse/Mouse.h"
00032 #include "Input/Mouse/MouseDevice.h"
00033 #include "Input/Joystick/Joystick.h"
00034 #include "Input/Joystick/JoystickDevice.h"
00035 #include "Core/InputOutput/BinaryWriter.h"
00036 #include "Core/InputOutput/BinaryReader.h"
00037 
00038 namespace Lamp{
00039 
00040 // 60FPS
00041 const float BufferedInput::interval60FPS = FPSController::interval60FPS;
00042 // 30FPS
00043 const float BufferedInput::interval30FPS = FPSController::interval30FPS;
00044 
00045 //------------------------------------------------------------------------------
00046 // コンストラクタ
00047 BufferedInput::BufferedInput() : logWriter_(NULL), logReader_(NULL),
00048     keyboard_(NULL), keyboardDevice_(NULL), mouse_(NULL), mouseDevice_(NULL){
00049     // プライオリティをあげる
00050     setPriority(priorityTimeCritical);
00051 }
00052 //------------------------------------------------------------------------------
00053 // デストラクタ
00054 BufferedInput::~BufferedInput(){
00055 }
00056 //------------------------------------------------------------------------------
00057 // 目標時間間隔の設定
00058 void BufferedInput::setTargetInterval(float targetInterval){
00059     SynchronizedBlock synchronizedBlock(this);
00060     fpsController_.setTargetInterval(targetInterval);
00061 }
00062 //------------------------------------------------------------------------------
00063 // 目標時間間隔の取得
00064 float BufferedInput::getTargetInterval(){
00065     SynchronizedBlock synchronizedBlock(this);
00066     return fpsController_.getTargetInterval();
00067 }
00068 //------------------------------------------------------------------------------
00069 // 入力があるか
00070 bool BufferedInput::hasMoreInput(){
00071     Assert((keyboard_ != NULL) && (keyboardDevice_ != NULL));
00072     Assert((mouse_ != NULL) && (mouseDevice_ != NULL));
00073     SynchronizedBlock synchronizedBlock(this);
00074     return (keyboardStates_.getCount() != 0);
00075 }
00076 //------------------------------------------------------------------------------
00077 // 次の入力
00078 void BufferedInput::nextInput(){
00079     Assert((keyboard_ != NULL) && (keyboardDevice_ != NULL));
00080     Assert((mouse_ != NULL) && (mouseDevice_ != NULL));
00081     Assert(hasMoreInput());
00082     SynchronizedBlock synchronizedBlock(this);
00083     keyboard_->setNextState(keyboardStates_.popFront());
00084     mouse_->setNextState(mouseStates_.popFront());
00085     int joystickCount = joysticks_.getCount();
00086     for(int i = 0; i < joystickCount; i++){
00087         joysticks_[i]->setNextState(joystickStates_.popFront());
00088     }
00089 }
00090 //------------------------------------------------------------------------------
00091 // 入力数の取得
00092 int BufferedInput::getInputCount(){
00093     Assert((keyboard_ != NULL) && (keyboardDevice_ != NULL));
00094     Assert((mouse_ != NULL) && (mouseDevice_ != NULL));
00095     SynchronizedBlock synchronizedBlock(this);
00096     return keyboardStates_.getCount();
00097 }
00098 //------------------------------------------------------------------------------
00099 // 実行
00100 void BufferedInput::run(Thread* thread){
00101     Assert((keyboard_ != NULL) && (keyboardDevice_ != NULL));
00102     Assert((mouse_ != NULL) && (mouseDevice_ != NULL));
00103     fpsController_.sleep();
00104     // 停止リクエストが来るまで動作しつづける
00105     while(!isStopRequested()){
00106         // デバイスポーリングには排他制御をかけない
00107         if(logReader_ == NULL){ devicePolling(); }
00108         {
00109             SynchronizedBlock synchronizedBlock(this);
00110             if(logReader_ == NULL){
00111                 // デバイスアップデート
00112                 deviceUpdate();
00113             }else{
00114                 // ログアップデート
00115                 logUpdate();
00116             }
00117             // ログの書き出し
00118             writeLog();
00119             // ログ終端チェック
00120             if((logReader_ != NULL) && (logReader_->isEnd())){
00121                 LampInput::stopLog();
00122             }
00123         }
00124         float interval = fpsController_.sleep();
00125 //      Assert(interval > fpsController_.getTargetInterval() * 0.5f);
00126 //      Assert(interval < fpsController_.getTargetInterval() * 2.f);
00127     }
00128 }
00129 //------------------------------------------------------------------------------
00130 // デバイスポーリング
00131 void BufferedInput::devicePolling(){
00132     // 60FPSで1分間入力が取得されなかったら注意しておく
00133     Assert(keyboardStates_.getCount() < 60 * 60);
00134     // キーボードのポーリング
00135     keyboardDevice_->polling();
00136     // マウスのポーリング
00137     mouseDevice_->polling();
00138     // ジョイスティックのポーリング
00139     int joystickDeviceCount = joystickDevices_.getCount();
00140     for(int i = 0; i < joystickDeviceCount; i++){
00141         joystickDevices_[i]->polling();
00142     }
00143 }
00144 //------------------------------------------------------------------------------
00145 // デバイスアップデート
00146 void BufferedInput::deviceUpdate(){
00147     // キーボードのアップデート
00148     keyboardStates_.pushBack(keyboardDevice_->getKeyboardState());
00149     // マウスのアップデート
00150     mouseStates_.pushBack(mouseDevice_->getMouseState());
00151     // ジョイスティックのアップデート
00152     int joystickDeviceCount = joystickDevices_.getCount();
00153     for(int i = 0; i < joystickDeviceCount; i++){
00154         joystickStates_.pushBack(joystickDevices_[i]->getJoystickState());
00155     }
00156 }
00157 //------------------------------------------------------------------------------
00158 // ログアップデート
00159 void BufferedInput::logUpdate(){
00160     // キーボードのログ読み込み
00161     KeyboardState keyboardState;
00162     keyboardState.readBinary(logReader_);
00163     keyboardStates_.pushBack(keyboardState);
00164 
00165     // マウスのログ読み込み
00166     MouseState mouseState;
00167     mouseState.readBinary(logReader_);
00168     mouseStates_.pushBack(mouseState);
00169 
00170     // ジョイスティックのログ読み込み
00171     int joystickCount = joystickDevices_.getCount();
00172     for(int i = 0; i < joystickCount; i++){
00173         JoystickState joystickState;
00174         joystickState.readBinary(logReader_);
00175         joystickStates_.pushBack(joystickState);
00176     }
00177 }
00178 //------------------------------------------------------------------------------
00179 // ログの書き出し
00180 void BufferedInput::writeLog(){
00181     if(logWriter_ == NULL){ return; }
00182     keyboard_->getState().writeBinary(logWriter_);
00183     mouse_->getState().writeBinary(logWriter_);
00184     int joystickCount = joysticks_.getCount();
00185     for(int i = 0; i < joystickCount; i++){
00186         joysticks_[i]->getState().writeBinary(logWriter_);
00187     }
00188 }
00189 //------------------------------------------------------------------------------
00190 // ログ取得
00191 //------------------------------------------------------------------------------
00192 // ログ取得の開始
00193 void BufferedInput::startLogging(BinaryWriter* binaryWriter){
00194     Assert(logWriter_ == NULL);
00195     SynchronizedBlock synchronizedBlock(this);
00196     logWriter_ = binaryWriter;
00197     logWriter_->writeFloat(fpsController_.getTargetInterval());
00198 }
00199 //------------------------------------------------------------------------------
00200 // ログ取得の終了
00201 void BufferedInput::endLogging(){
00202     Assert(logWriter_ != NULL);
00203     SynchronizedBlock synchronizedBlock(this);
00204     logWriter_ = NULL;
00205 }
00206 //------------------------------------------------------------------------------
00207 // ログ再生
00208 //------------------------------------------------------------------------------
00209 // ログ再生の開始
00210 void BufferedInput::playLog(BinaryReader* binaryReader){
00211     Assert(logReader_ == NULL);
00212     SynchronizedBlock synchronizedBlock(this);
00213     logReader_ = binaryReader;
00214     fpsController_.setTargetInterval(logReader_->readFloat());
00215 }
00216 //------------------------------------------------------------------------------
00217 // ログ再生の停止
00218 void BufferedInput::stopLog(){
00219     Assert(logReader_ != NULL);
00220     SynchronizedBlock synchronizedBlock(this);
00221     logReader_ = NULL;
00222 }
00223 //------------------------------------------------------------------------------
00224 } // End of namespace Lamp
00225 //------------------------------------------------------------------------------

Generated on Wed Mar 16 10:29:28 2005 for Lamp by doxygen 1.3.2