00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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
00041 const float BufferedInput::interval60FPS = FPSController::interval60FPS;
00042
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
00126
00127 }
00128 }
00129
00130
00131 void BufferedInput::devicePolling(){
00132
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 }
00225