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

# MouseDevice.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/Mouse/MouseDevice.h"
    00027
    00028 namespace Lamp{
    00029
    00030 //------------------------------------------------------------------------------
    00031 // コンストラクタ
    00032 MouseDevice::MouseDevice() : InputDevice(false){
    00033     zResolution_ = 120;// とりあえず一般的な120にしておく
    00034 }
    00035 //------------------------------------------------------------------------------
    00036 // デストラクタ
    00037 MouseDevice::~MouseDevice(){
    00038 }
    00039 //------------------------------------------------------------------------------
    00040 // 初期化
    00041 bool MouseDevice::initialize(DirectInputDevice* inputDevice, HWND windowHandle){
    00042     if(!InputDevice::initialize(inputDevice, windowHandle)){ return false; }
    00043     // データフォーマットの指定
    00044     if(DirectXFailed(inputDevice->SetDataFormat(&c_dfDIMouse2))){
    00045         ErrorOut("MouseDevice::initialize() "
    00046             "マウスデバイスのデータフォーマット指定に失敗しました。");
    00047         return false;
    00048     }
    00049     // Z軸があれば解像度を調べる
    00050     if((getAxisCount() >= 3) && isAttached()){
    00051         DIPROPDWORD property;
    00052         property.diph.dwSize = sizeof(DIPROPDWORD);
    00053         property.diph.dwHeaderSize = sizeof(DIPROPHEADER);
    00054         property.diph.dwHow = DIPH_BYOFFSET;
    00055         property.diph.dwObj = DIMOFS_Z;
    00056         HRESULT result =
    00057             inputDevice->GetProperty(DIPROP_GRANULARITY, &property.diph);
    00058         if(DirectXFailed(result)){
    00059             ErrorOut("MouseDevice::initialize() Z解像度の取得に失敗しました。");
    00060         }else{
    00061             zResolution_ = property.dwData;
    00062         }
    00063     }
    00064     // 優先度の指定
    00065     if(!setCooperativeLevel(isExclusive(), isForeground())){ return false; }
    00066     return true;
    00067 }
    00068 //------------------------------------------------------------------------------
    00069 // ポーリング
    00070 bool MouseDevice::polling(){
    00071     InputDevice::polling();
    00072     // マウスデバイスステート取得
    00073     DIMOUSESTATE2 state;
    00074     HRESULT result = inputDevice_->GetDeviceState(
    00075         sizeof(DIMOUSESTATE2), &state);
    00076     if(DirectXSucceeded(result)){
    00077         mouseState_.setXAxis(state.lX);
    00078         mouseState_.setYAxis(state.lY);
    00079         mouseState_.setZAxis(state.lZ);
    00080         for(int i = 0; i < maxButtonCount; i++){
    00081             mouseState_.setButtonPressed(i,
    00082                 ((state.rgbButtons[i] & 0x80) != 0));
    00083         }
    00084         return true;
    00085     }else{
    00086         // エラーなら入力無し
    00087         mouseState_.clear();
    00088         // 深刻なエラーでないかチェック
    00089         if((result != DIERR_INPUTLOST) && (result != DIERR_NOTACQUIRED)){
    00090             ErrorOut("GetDeviceState()に失敗しました。");
    00091         }else{
    00092             // デバイス再取得
    00093             acquire();
    00094         }
    00095     }
    00096     return false;
    00097 }
    00098 //------------------------------------------------------------------------------
    00099 } // End of namespace Lamp
    00100 //------------------------------------------------------------------------------