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

JoystickDevice.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/Joystick/JoystickDevice.h"
00027 
00028 namespace Lamp{
00029 
00030 //------------------------------------------------------------------------------
00031 // コンストラクタ
00032 JoystickDevice::JoystickDevice() : InputDevice(true), povCount_(0),
00033     sliderCount_(0), hasXAxis_(false), hasYAxis_(false), hasZAxis_(false),
00034     hasXRotation_(false), hasYRotation_(false), hasZRotation_(false){
00035 }
00036 //------------------------------------------------------------------------------
00037 // デストラクタ
00038 JoystickDevice::~JoystickDevice(){
00039 }
00040 //------------------------------------------------------------------------------
00041 // 初期化
00042 bool JoystickDevice::initialize(DirectInputDevice* inputDevice, HWND windowHandle){
00043     if(!InputDevice::initialize(inputDevice, windowHandle)){ return false; }
00044     // データフォーマットの指定
00045     if(DirectXFailed(inputDevice->SetDataFormat(&c_dfDIJoystick2))){
00046         ErrorOut("JoystickDevice::initialize() "
00047             "ジョイスティックデバイスのデータフォーマット指定に失敗しました。");
00048         return false;
00049     }
00050     // デバイスオブジェクトの列挙
00051     if(DirectXFailed(inputDevice->EnumObjects(
00052         joystickObjectEnumeration, this, DIDFT_ALL))){
00053         ErrorOut("JoystickDevice::initialize() "
00054             "ジョイスティックデバイスオブジェクトの列挙に失敗しました。");
00055         return false;
00056     }
00057     // 優先度の指定
00058     if(!setCooperativeLevel(isExclusive(), isForeground())){ return false; }
00059     return true;
00060 }
00061 //------------------------------------------------------------------------------
00062 // ジョイスティックオブジェクトの列挙コールバック
00063 int __stdcall JoystickDevice::joystickObjectEnumeration(
00064     const DIDEVICEOBJECTINSTANCE* instance, void* userData){
00065     JoystickDevice* joystickDevice = (JoystickDevice* )userData;
00066     if(!joystickDevice->checkJoystickObject(instance)){ return DIENUM_STOP; }
00067     return DIENUM_CONTINUE;
00068 }
00069 //------------------------------------------------------------------------------
00070 // ジョイスティックオブジェクトのチェック
00071 bool JoystickDevice::checkJoystickObject(
00072     const DIDEVICEOBJECTINSTANCE* instance){
00073     // 軸の範囲を設定する
00074     if((instance->dwType & DIDFT_AXIS) != 0){
00075         DIPROPRANGE range;
00076         range.diph.dwSize = sizeof(DIPROPRANGE);
00077         range.diph.dwHeaderSize = sizeof(DIPROPHEADER);
00078         range.diph.dwHow = DIPH_BYID;
00079         range.diph.dwObj = instance->dwType;
00080         range.lMin = minAxisValue;
00081         range.lMax = maxAxisValue;
00082         if(DirectXFailed(inputDevice_->SetProperty(DIPROP_RANGE, &range.diph))){
00083             ErrorOut("JoystickDevice::initialize() "
00084                 "ジョイスティックの軸範囲設定に失敗しました。");
00085             return false;
00086         }
00087     }
00088     // スライダ以外の軸にデッドゾーンと飽和ゾーン設定
00089     if(((instance->dwType & DIDFT_AXIS) != 0) &&
00090         (instance->guidType != GUID_Slider)){
00091         DIPROPDWORD property;
00092         property.diph.dwSize = sizeof(DIPROPDWORD);
00093         property.diph.dwHeaderSize = sizeof(DIPROPHEADER);
00094         property.diph.dwHow = DIPH_BYID;
00095         property.diph.dwObj = instance->dwType;
00096         property.dwData = deadZone_;
00097         if(DirectXFailed(inputDevice_->SetProperty(
00098             DIPROP_DEADZONE, &property.diph))){
00099             ErrorOut("JoystickDevice::initialize() "
00100                 "ジョイスティックのデッドゾーン設定に失敗しました。");
00101             return false;
00102         }
00103         property.dwData = saturationZone_;
00104         if(DirectXFailed(inputDevice_->SetProperty(
00105             DIPROP_SATURATION, &property.diph))){
00106             ErrorOut("JoystickDevice::initialize() "
00107                 "ジョイスティックの飽和ゾーン設定に失敗しました。");
00108             return false;
00109         }
00110     }
00111     // X軸を持っているか
00112     if(instance->guidType == GUID_XAxis){ hasXAxis_ = true; }
00113     // Y軸を持っているか
00114     if(instance->guidType == GUID_YAxis){ hasYAxis_ = true; }
00115     // Z軸を持っているか
00116     if(instance->guidType == GUID_ZAxis){ hasZAxis_ = true; }
00117     // X回転を持っているか
00118     if(instance->guidType == GUID_RxAxis){ hasXRotation_ = true; }
00119     // Y回転を持っているか
00120     if(instance->guidType == GUID_RyAxis){ hasYRotation_ = true; }
00121     // Z回転を持っているか
00122     if(instance->guidType == GUID_RzAxis){ hasZRotation_ = true; }
00123     // 視点コントローラか
00124     if(instance->guidType == GUID_POV){ povCount_++; }
00125     // スライダか
00126     if(instance->guidType == GUID_Slider){ sliderCount_++; }
00127     return true;
00128 }
00129 //------------------------------------------------------------------------------
00130 // ポーリング
00131 bool JoystickDevice::polling(){
00132     InputDevice::polling();
00133     // ジョイスティックデバイスステート取得
00134     DIJOYSTATE2 state;
00135     HRESULT result = inputDevice_->GetDeviceState(
00136         sizeof(DIJOYSTATE2), &state);
00137     if(DirectXSucceeded(result)){
00138         // とりあえずDIJOYSTATEの範囲でステート取得
00139         joystickState_.setXAxis(state.lX);
00140         joystickState_.setYAxis(state.lY);
00141         joystickState_.setZAxis(state.lZ);
00142         joystickState_.setXRotation(state.lRx);
00143         joystickState_.setYRotation(state.lRy);
00144         joystickState_.setZRotation(state.lRz);
00145         for(int i = 0; i < maxPOVCount; i++){
00146             joystickState_.setPOV(i, state.rgdwPOV[i]);
00147         }
00148         for(int i = 0; i < maxSliderCount; i++){
00149             joystickState_.setSlider(i, state.rglSlider[i]);
00150         }
00151         for(int i = 0; i < maxButtonCount; i++){
00152             joystickState_.setButtonPressed(i,
00153                 ((state.rgbButtons[i] & 0x80) != 0));
00154         }
00155         return true;
00156     }else{
00157         // エラーなら入力無し
00158         joystickState_.clear();
00159         // 深刻なエラーでないかチェック
00160         if((result != DIERR_INPUTLOST) && (result != DIERR_NOTACQUIRED)){
00161             ErrorOut("GetDeviceState()に失敗しました。");
00162         }else{
00163             // デバイス再取得
00164             acquire();
00165         }
00166     }
00167     return false;
00168 }
00169 //------------------------------------------------------------------------------
00170 // 文字列への変換
00171 String JoystickDevice::toString() const{
00172     String result = getInputDeviceString();
00173     String joystickString;
00174     joystickString.format("Axis ( %d %d %d ) Rotation ( %d %d %d )  "
00175         "POV %d  Slider %d\n",
00176         hasXAxis(), hasYAxis(), hasZAxis(),
00177         hasXRotation(), hasYRotation(), hasZRotation(),
00178         getPOVCount(), getSliderCount());
00179     result += joystickString;
00180     result += joystickState_.toString();
00181     return result;
00182 }
00183 //------------------------------------------------------------------------------
00184 } // End of namespace Lamp
00185 //------------------------------------------------------------------------------

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